2017-07-31 04:49:04 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const path = require('path');
|
2019-10-16 15:00:20 +00:00
|
|
|
const util = require('util');
|
2017-07-31 04:49:04 +00:00
|
|
|
const fs = require('fs');
|
2017-07-31 08:38:22 +00:00
|
|
|
|
2019-10-16 15:00:20 +00:00
|
|
|
const readFileAsync = util.promisify(fs.readFile);
|
|
|
|
const readdirAsync = util.promisify(fs.readdir);
|
|
|
|
const writeFileAsync = util.promisify(fs.writeFile);
|
2017-07-31 04:49:04 +00:00
|
|
|
|
|
|
|
const PROJECT_DIR = path.join(__dirname, '..', '..');
|
|
|
|
|
|
|
|
class Source {
|
|
|
|
/**
|
|
|
|
* @param {string} filePath
|
|
|
|
* @param {string} text
|
|
|
|
*/
|
|
|
|
constructor(filePath, text) {
|
|
|
|
this._filePath = filePath;
|
|
|
|
this._projectPath = path.relative(PROJECT_DIR, filePath);
|
|
|
|
this._name = path.basename(filePath);
|
|
|
|
this._text = text;
|
|
|
|
this._hasUpdatedText = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-12 10:38:24 +00:00
|
|
|
* @returns {string}
|
2017-07-31 04:49:04 +00:00
|
|
|
*/
|
|
|
|
filePath() {
|
|
|
|
return this._filePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-12 10:38:24 +00:00
|
|
|
* @returns {string}
|
2017-07-31 04:49:04 +00:00
|
|
|
*/
|
|
|
|
projectPath() {
|
|
|
|
return this._projectPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-12 10:38:24 +00:00
|
|
|
* @returns {string}
|
2017-07-31 04:49:04 +00:00
|
|
|
*/
|
|
|
|
name() {
|
|
|
|
return this._name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} text
|
2020-06-12 10:38:24 +00:00
|
|
|
* @returns {boolean}
|
2017-07-31 04:49:04 +00:00
|
|
|
*/
|
|
|
|
setText(text) {
|
2020-05-07 10:54:55 +00:00
|
|
|
if (text === this._text) return false;
|
2017-07-31 04:49:04 +00:00
|
|
|
this._hasUpdatedText = true;
|
|
|
|
this._text = text;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-12 10:38:24 +00:00
|
|
|
* @returns {string}
|
2017-07-31 04:49:04 +00:00
|
|
|
*/
|
|
|
|
text() {
|
|
|
|
return this._text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-12 10:38:24 +00:00
|
|
|
* @returns {boolean}
|
2017-07-31 04:49:04 +00:00
|
|
|
*/
|
|
|
|
hasUpdatedText() {
|
|
|
|
return this._hasUpdatedText;
|
|
|
|
}
|
|
|
|
|
2018-04-26 01:07:20 +00:00
|
|
|
async save() {
|
|
|
|
await writeFileAsync(this.filePath(), this.text());
|
2017-07-31 04:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} filePath
|
2020-06-12 10:38:24 +00:00
|
|
|
* @returns {!Promise<Source>}
|
2017-07-31 04:49:04 +00:00
|
|
|
*/
|
2018-04-26 01:07:20 +00:00
|
|
|
static async readFile(filePath) {
|
2017-07-31 04:49:04 +00:00
|
|
|
filePath = path.resolve(filePath);
|
2020-05-07 10:54:55 +00:00
|
|
|
const text = await readFileAsync(filePath, { encoding: 'utf8' });
|
2018-04-26 01:07:20 +00:00
|
|
|
return new Source(filePath, text);
|
2017-07-31 04:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} dirPath
|
|
|
|
* @param {string=} extension
|
2020-06-12 10:38:24 +00:00
|
|
|
* @returns {!Promise<!Array<!Source>>}
|
2017-07-31 04:49:04 +00:00
|
|
|
*/
|
2018-04-26 01:07:20 +00:00
|
|
|
static async readdir(dirPath, extension = '') {
|
2017-07-31 04:49:04 +00:00
|
|
|
const fileNames = await readdirAsync(dirPath);
|
2020-05-07 10:54:55 +00:00
|
|
|
const filePaths = fileNames
|
|
|
|
.filter((fileName) => fileName.endsWith(extension))
|
2020-05-12 15:30:13 +00:00
|
|
|
.map((fileName) => path.join(dirPath, fileName))
|
|
|
|
.filter((filePath) => {
|
|
|
|
const stats = fs.lstatSync(filePath);
|
|
|
|
return stats.isDirectory() === false;
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
return Promise.all(filePaths.map((filePath) => Source.readFile(filePath)));
|
2017-07-31 08:14:19 +00:00
|
|
|
}
|
2017-07-31 04:49:04 +00:00
|
|
|
}
|
2018-04-26 01:07:20 +00:00
|
|
|
module.exports = Source;
|