Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#172: prefer path module usage #276

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/dist/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -4734,7 +4734,7 @@ handles creation/update of one config file from the boilerplate at a time

| Param | Type | Description |
| --- | --- | --- |
| fileNameArr | <code>Array.&lt;string&gt;</code> | 0: path, 1: filename, 2: extension with dot |
| fileNameArr | <code>Array.&lt;string&gt;</code> | paths and/or filenames to relevant files |
| relevantForcedUpdates | <code>Array.&lt;string&gt;</code> | if fileNameArr is in this list we require an override |
| [boilerplateFileContent] | <code>string</code> | in case we cannot copy files 1:1 this can be used to pass in content |

Expand Down Expand Up @@ -4963,7 +4963,7 @@ handles creation/update of one config file from the boilerplate at a time

| Param | Type | Description |
| --- | --- | --- |
| fileNameArr | <code>Array.&lt;string&gt;</code> | 0: path, 1: filename, 2: extension with dot |
| fileNameArr | <code>Array.&lt;string&gt;</code> | paths and/or filenames to relevant files |
| relevantForcedUpdates | <code>Array.&lt;string&gt;</code> | if fileNameArr is in this list we require an override |
| [boilerplateFileContent] | <code>string</code> | in case we cannot copy files 1:1 this can be used to pass in content |

Expand Down Expand Up @@ -5192,7 +5192,7 @@ handles creation/update of one config file from the boilerplate at a time

| Param | Type | Description |
| --- | --- | --- |
| fileNameArr | <code>Array.&lt;string&gt;</code> | 0: path, 1: filename, 2: extension with dot |
| fileNameArr | <code>Array.&lt;string&gt;</code> | paths and/or filenames to relevant files |
| relevantForcedUpdates | <code>Array.&lt;string&gt;</code> | if fileNameArr is in this list we require an override |
| [boilerplateFileContent] | <code>string</code> | in case we cannot copy files 1:1 this can be used to pass in content |

Expand Down Expand Up @@ -5421,7 +5421,7 @@ handles creation/update of one config file from the boilerplate at a time

| Param | Type | Description |
| --- | --- | --- |
| fileNameArr | <code>Array.&lt;string&gt;</code> | 0: path, 1: filename, 2: extension with dot |
| fileNameArr | <code>Array.&lt;string&gt;</code> | paths and/or filenames to relevant files |
| relevantForcedUpdates | <code>Array.&lt;string&gt;</code> | if fileNameArr is in this list we require an override |
| [boilerplateFileContent] | <code>string</code> | in case we cannot copy files 1:1 this can be used to pass in content |

Expand Down
16 changes: 8 additions & 8 deletions lib/util/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -284,13 +282,15 @@ const File = {
* @returns {Promise<Boolean>} 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}`);
Expand Down
37 changes: 12 additions & 25 deletions lib/util/init.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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)
);
}

Expand All @@ -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)
);
}
}
Expand Down Expand Up @@ -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<Boolean>} 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');
Expand Down Expand Up @@ -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
);

Expand Down