From eb130c6c3cd6fe0287e7b9dc4449d2d48564df84 Mon Sep 17 00:00:00 2001 From: "douglas.midgley" Date: Wed, 20 Apr 2022 22:46:41 +0200 Subject: [PATCH] #172: prefer path module usage --- docs/dist/documentation.md | 8 ++++---- lib/util/file.js | 16 ++++++++-------- lib/util/init.config.js | 37 ++++++++++++------------------------- 3 files changed, 24 insertions(+), 37 deletions(-) diff --git a/docs/dist/documentation.md b/docs/dist/documentation.md index c820ac147..dc3b0d90d 100644 --- a/docs/dist/documentation.md +++ b/docs/dist/documentation.md @@ -4734,7 +4734,7 @@ handles creation/update of one config file from the boilerplate at a time | Param | Type | Description | | --- | --- | --- | -| fileNameArr | Array.<string> | 0: path, 1: filename, 2: extension with dot | +| fileNameArr | Array.<string> | paths and/or filenames to relevant files | | relevantForcedUpdates | Array.<string> | if fileNameArr is in this list we require an override | | [boilerplateFileContent] | string | in case we cannot copy files 1:1 this can be used to pass in content | @@ -4963,7 +4963,7 @@ handles creation/update of one config file from the boilerplate at a time | Param | Type | Description | | --- | --- | --- | -| fileNameArr | Array.<string> | 0: path, 1: filename, 2: extension with dot | +| fileNameArr | Array.<string> | paths and/or filenames to relevant files | | relevantForcedUpdates | Array.<string> | if fileNameArr is in this list we require an override | | [boilerplateFileContent] | string | in case we cannot copy files 1:1 this can be used to pass in content | @@ -5192,7 +5192,7 @@ handles creation/update of one config file from the boilerplate at a time | Param | Type | Description | | --- | --- | --- | -| fileNameArr | Array.<string> | 0: path, 1: filename, 2: extension with dot | +| fileNameArr | Array.<string> | paths and/or filenames to relevant files | | relevantForcedUpdates | Array.<string> | if fileNameArr is in this list we require an override | | [boilerplateFileContent] | string | in case we cannot copy files 1:1 this can be used to pass in content | @@ -5421,7 +5421,7 @@ handles creation/update of one config file from the boilerplate at a time | Param | Type | Description | | --- | --- | --- | -| fileNameArr | Array.<string> | 0: path, 1: filename, 2: extension with dot | +| fileNameArr | Array.<string> | paths and/or filenames to relevant files | | relevantForcedUpdates | Array.<string> | if fileNameArr is in this list we require an override | | [boilerplateFileContent] | string | in case we cannot copy files 1:1 this can be used to pass in content | diff --git a/lib/util/file.js b/lib/util/file.js index 477554d9b..0ba969326 100644 --- a/lib/util/file.js +++ b/lib/util/file.js @@ -137,9 +137,7 @@ const File = { writeJSONToFile: async function (directory, filename, content) { directory = this.filterIllegalPathChars(this.normalizePath(directory)); filename = this.filterIllegalFilenames(filename); - if (!fs.existsSync(directory)) { - fs.mkdirpSync(directory); - } + fs.ensureDirSync(directory); try { await fs.writeJSON(path.join(directory, filename + '.json'), content, { spaces: 4 }); } catch (ex) { @@ -284,13 +282,15 @@ const File = { * @returns {Promise} Promise */ writeToFile: async function (directory, filename, filetype, content, encoding) { - directory = this.filterIllegalPathChars(this.normalizePath(directory)); - if (!fs.existsSync(directory)) { - fs.mkdirpSync(directory); - } + directory = this.filterIllegalPathChars( + this.normalizePath(path.relative(process.cwd(), directory)) + ); + await fs.ensureDir(directory); // filter characters that are illegal for file names in Windows filename = this.filterIllegalFilenames(filename); - const filePath = path.join(directory, filename + '.' + filetype); + const filePath = filetype + ? path.join(directory, filename + '.' + filetype) + : path.join(directory, filename); try { if (fs.existsSync(filePath)) { Util.logger.debug(`Overwriting: ${filePath}`); diff --git a/lib/util/init.config.js b/lib/util/init.config.js index 090ed4679..80b73a52e 100644 --- a/lib/util/init.config.js +++ b/lib/util/init.config.js @@ -162,13 +162,8 @@ const Init = { Util.logger.info('Checking configuration files (existing files will not be changed):'); const creationLog = []; - if (!File.existsSync('deploy/')) { - File.mkdirpSync('deploy/'); - } - - if (!File.existsSync('src/cloudPages')) { - File.mkdirpSync('src/cloudPages'); - } + await File.ensureDir('deploy/'); + await File.ensureDir('src/cloudPages'); const relevantForcedUpdates = this._getForcedUpdateList(versionBeforeUpgrade); @@ -184,11 +179,7 @@ const Init = { } else { const fileContent = File.readFileSync(gitignoreFileName, 'utf8'); creationLog.push( - await this._createIdeConfigFile( - ['.' + path.sep, '', '.gitignore'], - relevantForcedUpdates, - fileContent - ) + await this._createIdeConfigFile(['.gitignore'], relevantForcedUpdates, fileContent) ); } @@ -203,14 +194,9 @@ const Init = { // read all files in these directories if (!File.lstatSync(path.join(curDir, file)).isDirectory()) { // filter entries that are actually folders - const fileArr = file.split('.'); - const ext = '.' + fileArr.pop(); // awaiting the result here due to interactive optional overwrite creationLog.push( - await this._createIdeConfigFile( - [subdir + path.sep, fileArr.join('.'), ext], - relevantForcedUpdates - ) + await this._createIdeConfigFile([subdir, file], relevantForcedUpdates) ); } } @@ -281,22 +267,23 @@ const Init = { }, /** * handles creation/update of one config file from the boilerplate at a time - * @param {string[]} fileNameArr 0: path, 1: filename, 2: extension with dot + * @param {string[]} fileNameArr paths and/or filenames to relevant files * @param {string[]} relevantForcedUpdates if fileNameArr is in this list we require an override * @param {string} [boilerplateFileContent] in case we cannot copy files 1:1 this can be used to pass in content * @returns {Promise} install successful or error occured */ async _createIdeConfigFile(fileNameArr, relevantForcedUpdates, boilerplateFileContent) { let update = false; - const fileName = fileNameArr.join(''); + const fileName = path.resolve(...fileNameArr); const boilerplateFileName = path.resolve( __dirname, Util.boilerplateDirectory, 'files', - fileName + ...fileNameArr ); boilerplateFileContent = - boilerplateFileContent || File.readFileSync(boilerplateFileName, 'utf8'); + boilerplateFileContent || + (await File.readFile(boilerplateFileName, { encoding: 'utf8' })); if (File.existsSync(fileName)) { const existingFileContent = File.readFileSync(fileName, 'utf8'); @@ -331,9 +318,9 @@ const Init = { await File.rename(fileName, fileName + '.BAK'); } const saveStatus = await File.writeToFile( - fileNameArr[0], - fileNameArr[1], - fileNameArr[2].substr(1), + path.dirname(fileName), + path.basename(fileName, path.extname(fileName)), + path.extname(fileName).slice(1), boilerplateFileContent );