ember-cli 7.4.0-beta.1 → 7.5.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/docs/build/data.json +1 -1
- package/package.json +8 -8
- package/packages/addon-blueprint/package.json +4 -4
- package/packages/app-blueprint/files/package.json +2 -2
- package/packages/app-blueprint/package.json +4 -4
- package/packages/blueprint-blueprint/files/blueprints/__name__/files/.gitkeep +0 -0
- package/packages/blueprint-blueprint/files/blueprints/__name__/index.js +16 -0
- package/packages/blueprint-blueprint/index.js +5 -0
- package/packages/blueprint-blueprint/package.json +13 -0
- package/packages/blueprint-model/index.js +1791 -0
- package/packages/blueprint-model/node_modules/.bin/markdown-it +21 -0
- package/packages/blueprint-model/package.json +31 -0
- package/packages/blueprint-model/utilities/clean-remove.js +31 -0
- package/packages/blueprint-model/utilities/directory-for-package-name.js +31 -0
- package/packages/blueprint-model/utilities/edit-file-diff.js +64 -0
- package/packages/blueprint-model/utilities/experiments.js +63 -0
- package/packages/blueprint-model/utilities/file-info.js +170 -0
- package/packages/blueprint-model/utilities/insert-into-file.js +125 -0
- package/packages/blueprint-model/utilities/is-addon.js +5 -0
- package/packages/blueprint-model/utilities/markdown-color.js +208 -0
- package/packages/blueprint-model/utilities/open-editor.js +44 -0
- package/packages/blueprint-model/utilities/prepend-emoji.js +12 -0
- package/packages/blueprint-model/utilities/print-command.js +102 -0
- package/packages/blueprint-model/utilities/process-template.js +10 -0
- package/packages/blueprint-model/utilities/sequence.js +42 -0
- package/packages/blueprint-model/utilities/walk-up-path.js +23 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
|
|
3
|
+
|
|
4
|
+
case `uname` in
|
|
5
|
+
*CYGWIN*|*MINGW*|*MSYS*)
|
|
6
|
+
if command -v cygpath > /dev/null 2>&1; then
|
|
7
|
+
basedir=`cygpath -w "$basedir"`
|
|
8
|
+
fi
|
|
9
|
+
;;
|
|
10
|
+
esac
|
|
11
|
+
|
|
12
|
+
if [ -z "$NODE_PATH" ]; then
|
|
13
|
+
export NODE_PATH="/home/runner/work/ember-cli/ember-cli/node_modules/.pnpm/markdown-it@14.3.2/node_modules/markdown-it/bin/node_modules:/home/runner/work/ember-cli/ember-cli/node_modules/.pnpm/markdown-it@14.3.2/node_modules/markdown-it/node_modules:/home/runner/work/ember-cli/ember-cli/node_modules/.pnpm/markdown-it@14.3.2/node_modules:/home/runner/work/ember-cli/ember-cli/node_modules/.pnpm/node_modules"
|
|
14
|
+
else
|
|
15
|
+
export NODE_PATH="/home/runner/work/ember-cli/ember-cli/node_modules/.pnpm/markdown-it@14.3.2/node_modules/markdown-it/bin/node_modules:/home/runner/work/ember-cli/ember-cli/node_modules/.pnpm/markdown-it@14.3.2/node_modules/markdown-it/node_modules:/home/runner/work/ember-cli/ember-cli/node_modules/.pnpm/markdown-it@14.3.2/node_modules:/home/runner/work/ember-cli/ember-cli/node_modules/.pnpm/node_modules:$NODE_PATH"
|
|
16
|
+
fi
|
|
17
|
+
if [ -x "$basedir/node" ]; then
|
|
18
|
+
exec "$basedir/node" "$basedir/../markdown-it/bin/markdown-it.mjs" "$@"
|
|
19
|
+
else
|
|
20
|
+
exec node "$basedir/../markdown-it/bin/markdown-it.mjs" "$@"
|
|
21
|
+
fi
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ember-tooling/blueprint-model",
|
|
3
|
+
"version": "0.7.1",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "https://github.com/ember-cli/ember-cli.git",
|
|
7
|
+
"directory": "packages/blueprint-model"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"babel-remove-types": "^2.0.0",
|
|
12
|
+
"chalk": "^5.6.2",
|
|
13
|
+
"content-tag": "^4.0.0",
|
|
14
|
+
"core-object": "^3.1.5",
|
|
15
|
+
"diff": "^8.0.4",
|
|
16
|
+
"ember-cli-normalize-entity-name": "^1.0.0",
|
|
17
|
+
"ember-cli-string-utils": "^1.1.0",
|
|
18
|
+
"fs-extra": "^11.3.4",
|
|
19
|
+
"heimdalljs-logger": "^0.1.10",
|
|
20
|
+
"inflection": "^3.0.2",
|
|
21
|
+
"isbinaryfile": "^5.0.7",
|
|
22
|
+
"lodash": "^4.17.23",
|
|
23
|
+
"markdown-it": "^14.1.0",
|
|
24
|
+
"markdown-it-terminal": "^0.4.0",
|
|
25
|
+
"minimatch": "^10.1.1",
|
|
26
|
+
"promise.hash.helper": "^1.0.8",
|
|
27
|
+
"quick-temp": "^0.1.9",
|
|
28
|
+
"silent-error": "^1.1.1",
|
|
29
|
+
"walk-sync": "^4.0.0"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const fs = require('fs-extra');
|
|
5
|
+
const walkUp = require('./walk-up-path');
|
|
6
|
+
|
|
7
|
+
async function cleanRemove(fileInfo) {
|
|
8
|
+
try {
|
|
9
|
+
await fs.stat(fileInfo.outputPath);
|
|
10
|
+
await fs.remove(fileInfo.outputPath);
|
|
11
|
+
let paths = walkUp(fileInfo.displayPath).map((thePath) => path.join(fileInfo.outputBasePath, thePath));
|
|
12
|
+
|
|
13
|
+
for (let thePath of paths) {
|
|
14
|
+
let childPaths = await fs.readdir(thePath);
|
|
15
|
+
if (childPaths.length > 0) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
await fs.remove(thePath);
|
|
20
|
+
}
|
|
21
|
+
} catch (err) {
|
|
22
|
+
// you tried to destroy a blueprint without first generating it
|
|
23
|
+
// instead of trying to read dirs that don't exist
|
|
24
|
+
// swallow error and carry on
|
|
25
|
+
if (err.code !== 'ENOENT') {
|
|
26
|
+
throw err;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = cleanRemove;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Derive a directory name from a package name.
|
|
7
|
+
* Takes scoped packages into account.
|
|
8
|
+
*
|
|
9
|
+
* @method directoryForPackageName
|
|
10
|
+
* @param {String} packageName
|
|
11
|
+
* @return {String} Derived directory name.
|
|
12
|
+
*/
|
|
13
|
+
module.exports = function directoryForPackageName(packageName) {
|
|
14
|
+
let isScoped = packageName[0] === '@' && packageName.includes('/');
|
|
15
|
+
|
|
16
|
+
if (isScoped) {
|
|
17
|
+
let slashIndex = packageName.indexOf('/');
|
|
18
|
+
let scopeName = packageName.substring(1, slashIndex);
|
|
19
|
+
let packageNameWithoutScope = packageName.substring(slashIndex + 1);
|
|
20
|
+
let pathParts = process.cwd().split(path.sep);
|
|
21
|
+
let parentDirectoryContainsScopeName = pathParts.includes(scopeName);
|
|
22
|
+
|
|
23
|
+
if (parentDirectoryContainsScopeName) {
|
|
24
|
+
return packageNameWithoutScope;
|
|
25
|
+
} else {
|
|
26
|
+
return `${scopeName}-${packageNameWithoutScope}`;
|
|
27
|
+
}
|
|
28
|
+
} else {
|
|
29
|
+
return packageName;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const util = require('util');
|
|
5
|
+
const { applyPatch, createPatch } = require('diff');
|
|
6
|
+
const quickTemp = require('quick-temp');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const SilentError = require('silent-error');
|
|
9
|
+
const openEditor = require('./open-editor');
|
|
10
|
+
const hash = require('promise.hash.helper');
|
|
11
|
+
|
|
12
|
+
const readFile = util.promisify(fs.readFile);
|
|
13
|
+
const writeFile = util.promisify(fs.writeFile);
|
|
14
|
+
|
|
15
|
+
class EditFileDiff {
|
|
16
|
+
constructor(options) {
|
|
17
|
+
this.info = options.info;
|
|
18
|
+
|
|
19
|
+
quickTemp.makeOrRemake(this, 'tmpDifferenceDir');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
edit() {
|
|
23
|
+
return hash({
|
|
24
|
+
input: this.info.render(),
|
|
25
|
+
output: readFile(this.info.outputPath),
|
|
26
|
+
})
|
|
27
|
+
.then(this.invokeEditor.bind(this))
|
|
28
|
+
.then(this.applyPatch.bind(this))
|
|
29
|
+
.finally(this.cleanUp.bind(this));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
cleanUp() {
|
|
33
|
+
quickTemp.remove(this, 'tmpDifferenceDir');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
applyPatch(resultHash) {
|
|
37
|
+
return hash({
|
|
38
|
+
diffString: readFile(resultHash.diffPath),
|
|
39
|
+
currentString: readFile(resultHash.outputPath),
|
|
40
|
+
}).then((result) => {
|
|
41
|
+
let appliedDiff = applyPatch(result.currentString.toString(), result.diffString.toString());
|
|
42
|
+
|
|
43
|
+
if (!appliedDiff) {
|
|
44
|
+
let message = 'Patch was not cleanly applied.';
|
|
45
|
+
this.info.ui.writeLine(`${message} Please choose another action.`);
|
|
46
|
+
throw new SilentError(message);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return writeFile(resultHash.outputPath, appliedDiff);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
invokeEditor(result) {
|
|
54
|
+
let info = this.info;
|
|
55
|
+
let diff = createPatch(info.outputPath, result.output.toString(), result.input);
|
|
56
|
+
let diffPath = path.join(this.tmpDifferenceDir, 'currentDiff.diff');
|
|
57
|
+
|
|
58
|
+
return writeFile(diffPath, diff)
|
|
59
|
+
.then(() => openEditor(diffPath))
|
|
60
|
+
.then(() => ({ outputPath: info.outputPath, diffPath }));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
module.exports = EditFileDiff;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { default: chalk } = require('chalk');
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
If you're here to remove the VITE experiment flag in favor of it being
|
|
7
|
+
permanently on, you can't do that until addressing
|
|
8
|
+
https://github.com/ember-cli/ember-cli/pull/10781#pullrequestreview-3230644293
|
|
9
|
+
|
|
10
|
+
A lot of test coverage would otherwise be lost, because valid tests are being
|
|
11
|
+
run only when the VITE experiment is off.
|
|
12
|
+
*/
|
|
13
|
+
const availableExperiments = Object.freeze(['EMBROIDER', 'CLASSIC', 'VITE']);
|
|
14
|
+
|
|
15
|
+
const deprecatedExperiments = Object.freeze([]);
|
|
16
|
+
const enabledExperiments = Object.freeze(['VITE']);
|
|
17
|
+
const deprecatedExperimentsDeprecationsIssued = [];
|
|
18
|
+
|
|
19
|
+
function isExperimentEnabled(experimentName) {
|
|
20
|
+
if (!availableExperiments.includes(experimentName)) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (process.env.EMBER_CLI_ENABLE_ALL_EXPERIMENTS && deprecatedExperiments.includes(experimentName)) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (process.env.EMBER_CLI_ENABLE_ALL_EXPERIMENTS) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (process.env.EMBER_CLI_CLASSIC && experimentName === 'EMBROIDER') {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let experimentEnvironmentVariable = `EMBER_CLI_${experimentName}`;
|
|
37
|
+
let experimentValue = process.env[experimentEnvironmentVariable];
|
|
38
|
+
|
|
39
|
+
if (deprecatedExperiments.includes(experimentName)) {
|
|
40
|
+
let deprecationPreviouslyIssued = deprecatedExperimentsDeprecationsIssued.includes(experimentName);
|
|
41
|
+
let isSpecifiedByUser = experimentValue !== undefined;
|
|
42
|
+
|
|
43
|
+
if (!deprecationPreviouslyIssued && isSpecifiedByUser) {
|
|
44
|
+
console.warn(
|
|
45
|
+
chalk.yellow(`The ${experimentName} experiment in ember-cli has been deprecated and will be removed.`)
|
|
46
|
+
);
|
|
47
|
+
deprecatedExperimentsDeprecationsIssued.push(experimentName);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (enabledExperiments.includes(experimentName)) {
|
|
52
|
+
return experimentValue !== 'false';
|
|
53
|
+
} else {
|
|
54
|
+
return experimentValue !== undefined && experimentValue !== 'false';
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
module.exports = {
|
|
59
|
+
isExperimentEnabled,
|
|
60
|
+
|
|
61
|
+
// exported for testing purposes
|
|
62
|
+
_deprecatedExperimentsDeprecationsIssued: deprecatedExperimentsDeprecationsIssued,
|
|
63
|
+
};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const util = require('util');
|
|
5
|
+
const { default: chalk } = require('chalk');
|
|
6
|
+
const EditFileDiff = require('./edit-file-diff');
|
|
7
|
+
const EOL = require('os').EOL;
|
|
8
|
+
const rxEOL = new RegExp(EOL, 'g');
|
|
9
|
+
const isBinaryFile = require('isbinaryfile').isBinaryFileSync;
|
|
10
|
+
const hash = require('promise.hash.helper');
|
|
11
|
+
const canEdit = require('./open-editor').canEdit;
|
|
12
|
+
const processTemplate = require('./process-template');
|
|
13
|
+
|
|
14
|
+
const readFile = util.promisify(fs.readFile);
|
|
15
|
+
const lstat = util.promisify(fs.stat);
|
|
16
|
+
|
|
17
|
+
function diffHighlight(line) {
|
|
18
|
+
if (line[0] === '+') {
|
|
19
|
+
return chalk.green(line);
|
|
20
|
+
} else if (line[0] === '-') {
|
|
21
|
+
return chalk.red(line);
|
|
22
|
+
} else if (/^@@/.test(line)) {
|
|
23
|
+
return chalk.cyan(line);
|
|
24
|
+
} else {
|
|
25
|
+
return line;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const NOOP = (_) => _;
|
|
30
|
+
class FileInfo {
|
|
31
|
+
constructor(options) {
|
|
32
|
+
this.action = options.action;
|
|
33
|
+
this.outputBasePath = options.outputBasePath;
|
|
34
|
+
this.outputPath = options.outputPath;
|
|
35
|
+
this.displayPath = options.displayPath;
|
|
36
|
+
this.inputPath = options.inputPath;
|
|
37
|
+
this.templateVariables = options.templateVariables;
|
|
38
|
+
this.replacer = options.replacer || NOOP;
|
|
39
|
+
this.ui = options.ui;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
confirmOverwrite(path) {
|
|
43
|
+
let promptOptions = {
|
|
44
|
+
type: 'expand',
|
|
45
|
+
name: 'answer',
|
|
46
|
+
default: false,
|
|
47
|
+
message: `${chalk.red('Overwrite')} ${path}?`,
|
|
48
|
+
choices: [
|
|
49
|
+
{ key: 'y', name: 'Yes, overwrite', value: 'overwrite' },
|
|
50
|
+
{ key: 'n', name: 'No, skip', value: 'skip' },
|
|
51
|
+
],
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
let outputPathIsFile = false;
|
|
55
|
+
try {
|
|
56
|
+
outputPathIsFile = fs.statSync(this.outputPath).isFile();
|
|
57
|
+
} catch (err) {
|
|
58
|
+
/* ignore */
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let canDiff = !isBinaryFile(this.inputPath) && (!outputPathIsFile || !isBinaryFile(this.outputPath));
|
|
62
|
+
|
|
63
|
+
if (canDiff) {
|
|
64
|
+
promptOptions.choices.push({ key: 'd', name: 'Diff', value: 'diff' });
|
|
65
|
+
|
|
66
|
+
if (canEdit()) {
|
|
67
|
+
promptOptions.choices.push({ key: 'e', name: 'Edit', value: 'edit' });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return this.ui.prompt(promptOptions).then((response) => response.answer);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
displayDiff() {
|
|
75
|
+
let info = this,
|
|
76
|
+
{ createPatch } = require('diff');
|
|
77
|
+
return hash({
|
|
78
|
+
input: this.render(),
|
|
79
|
+
output: readFile(info.outputPath),
|
|
80
|
+
}).then((result) => {
|
|
81
|
+
let diff = createPatch(
|
|
82
|
+
info.outputPath,
|
|
83
|
+
result.output.toString().replace(rxEOL, '\n'),
|
|
84
|
+
result.input.replace(rxEOL, '\n')
|
|
85
|
+
);
|
|
86
|
+
let lines = diff.split('\n');
|
|
87
|
+
|
|
88
|
+
for (let i = 0; i < lines.length; i++) {
|
|
89
|
+
info.ui.write(diffHighlight(lines[i] + EOL));
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async render() {
|
|
95
|
+
if (!this.rendered) {
|
|
96
|
+
let result = await this._render();
|
|
97
|
+
this.rendered = this.replacer(result, this);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return this.rendered;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
_render() {
|
|
104
|
+
let path = this.inputPath;
|
|
105
|
+
let context = this.templateVariables;
|
|
106
|
+
|
|
107
|
+
return readFile(path).then((content) =>
|
|
108
|
+
lstat(path).then((fileStat) => {
|
|
109
|
+
if (isBinaryFile(content, fileStat.size)) {
|
|
110
|
+
return content;
|
|
111
|
+
} else {
|
|
112
|
+
try {
|
|
113
|
+
return processTemplate(content.toString(), context);
|
|
114
|
+
} catch (err) {
|
|
115
|
+
err.message += ` (Error in blueprint template: ${path})`;
|
|
116
|
+
throw err;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
checkForConflict() {
|
|
124
|
+
return this.render().then((input) => {
|
|
125
|
+
input = input.toString().replace(rxEOL, '\n');
|
|
126
|
+
|
|
127
|
+
return readFile(this.outputPath)
|
|
128
|
+
.then((output) => {
|
|
129
|
+
output = output.toString().replace(rxEOL, '\n');
|
|
130
|
+
|
|
131
|
+
return input === output ? 'identical' : 'confirm';
|
|
132
|
+
})
|
|
133
|
+
.catch((e) => {
|
|
134
|
+
if (e.code === 'ENOENT') {
|
|
135
|
+
return 'none';
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
throw e;
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
confirmOverwriteTask() {
|
|
144
|
+
let info = this;
|
|
145
|
+
|
|
146
|
+
return function () {
|
|
147
|
+
function doConfirm() {
|
|
148
|
+
return info.confirmOverwrite(info.displayPath).then((action) => {
|
|
149
|
+
if (action === 'diff') {
|
|
150
|
+
return info.displayDiff().then(doConfirm);
|
|
151
|
+
} else if (action === 'edit') {
|
|
152
|
+
let editFileDiff = new EditFileDiff({ info });
|
|
153
|
+
return editFileDiff
|
|
154
|
+
.edit()
|
|
155
|
+
.then(() => (info.action = action))
|
|
156
|
+
.catch(() => doConfirm())
|
|
157
|
+
.then(() => info);
|
|
158
|
+
} else {
|
|
159
|
+
info.action = action;
|
|
160
|
+
return info;
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return doConfirm();
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
module.exports = FileInfo;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs-extra');
|
|
4
|
+
const EOL = require('os').EOL;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
Inserts the given content into a file. If the `contentsToInsert` string is already
|
|
8
|
+
present in the current contents, the file will not be changed unless `force` option
|
|
9
|
+
is passed.
|
|
10
|
+
|
|
11
|
+
If `options.before` is specified, `contentsToInsert` will be inserted before
|
|
12
|
+
the first instance of that string. If `options.after` is specified, the
|
|
13
|
+
contents will be inserted after the first instance of that string.
|
|
14
|
+
If the string specified by options.before or options.after is not in the file,
|
|
15
|
+
no change will be made. Both of these options support regular expressions.
|
|
16
|
+
|
|
17
|
+
If neither `options.before` nor `options.after` are present, `contentsToInsert`
|
|
18
|
+
will be inserted at the end of the file.
|
|
19
|
+
|
|
20
|
+
It will create a new file if one doesn't exist, unless you set the `options.create`
|
|
21
|
+
option to `false`.
|
|
22
|
+
|
|
23
|
+
Example:
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
// app/router.js
|
|
27
|
+
Router.map(function () {
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
insertIntoFile('app/router.js', ' this.route("admin");', {
|
|
33
|
+
after: 'Router.map(function () {' + EOL
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
// app/router.js
|
|
39
|
+
Router.map(function () {
|
|
40
|
+
this.route("admin");
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
@method insertIntoFile
|
|
45
|
+
@param {String} pathRelativeToProjectRoot
|
|
46
|
+
@param {String} contentsToInsert
|
|
47
|
+
@param {Object} providedOptions
|
|
48
|
+
@return {Promise}
|
|
49
|
+
*/
|
|
50
|
+
async function insertIntoFile(fullPath, contentsToInsert, providedOptions) {
|
|
51
|
+
let options = providedOptions || {};
|
|
52
|
+
|
|
53
|
+
let returnValue = {
|
|
54
|
+
path: fullPath,
|
|
55
|
+
originalContents: '',
|
|
56
|
+
contents: '',
|
|
57
|
+
inserted: false,
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
let exists = fs.existsSync(fullPath);
|
|
61
|
+
|
|
62
|
+
if (exists || (!exists && options.create !== false)) {
|
|
63
|
+
let originalContents = '';
|
|
64
|
+
|
|
65
|
+
if (exists) {
|
|
66
|
+
originalContents = fs.readFileSync(fullPath, { encoding: 'utf8' });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let contentsToWrite = originalContents;
|
|
70
|
+
|
|
71
|
+
let alreadyPresent = originalContents.indexOf(contentsToInsert) > -1;
|
|
72
|
+
let insert = !alreadyPresent;
|
|
73
|
+
let insertBehavior = 'end';
|
|
74
|
+
|
|
75
|
+
if (options.before) {
|
|
76
|
+
insertBehavior = 'before';
|
|
77
|
+
}
|
|
78
|
+
if (options.after) {
|
|
79
|
+
insertBehavior = 'after';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (options.force) {
|
|
83
|
+
insert = true;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (insert) {
|
|
87
|
+
if (insertBehavior === 'end') {
|
|
88
|
+
contentsToWrite += contentsToInsert;
|
|
89
|
+
} else {
|
|
90
|
+
let contentMarker = options[insertBehavior];
|
|
91
|
+
if (contentMarker instanceof RegExp) {
|
|
92
|
+
let matches = contentsToWrite.match(contentMarker);
|
|
93
|
+
if (matches) {
|
|
94
|
+
contentMarker = matches[0];
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
let contentMarkerIndex = contentsToWrite.indexOf(contentMarker);
|
|
98
|
+
|
|
99
|
+
if (contentMarkerIndex !== -1) {
|
|
100
|
+
let insertIndex = contentMarkerIndex;
|
|
101
|
+
if (insertBehavior === 'after') {
|
|
102
|
+
insertIndex += contentMarker.length;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
contentsToWrite =
|
|
106
|
+
contentsToWrite.slice(0, insertIndex) + contentsToInsert + EOL + contentsToWrite.slice(insertIndex);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
returnValue.originalContents = originalContents;
|
|
112
|
+
returnValue.contents = contentsToWrite;
|
|
113
|
+
|
|
114
|
+
if (contentsToWrite !== originalContents) {
|
|
115
|
+
returnValue.inserted = true;
|
|
116
|
+
|
|
117
|
+
await fs.outputFile(fullPath, contentsToWrite);
|
|
118
|
+
return returnValue;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return Promise.resolve(returnValue);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
module.exports = insertIntoFile;
|