complete-cli 1.0.1-dev.0
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/LICENSE +9 -0
- package/README.md +7 -0
- package/dist/commands/CheckCommand.js +141 -0
- package/dist/commands/InitCommand.js +64 -0
- package/dist/commands/NukeCommand.js +13 -0
- package/dist/commands/PublishCommand.js +158 -0
- package/dist/commands/UpdateCommand.js +16 -0
- package/dist/commands/check/check.test.js +86 -0
- package/dist/commands/check/getTruncatedText.js +139 -0
- package/dist/commands/init/checkIfProjectPathExists.js +21 -0
- package/dist/commands/init/createProject.js +123 -0
- package/dist/commands/init/getAuthorName.js +17 -0
- package/dist/commands/init/getProjectPath.js +80 -0
- package/dist/commands/init/packageManager.js +35 -0
- package/dist/commands/init/vsCodeInit.js +74 -0
- package/dist/constants.js +17 -0
- package/dist/git.js +128 -0
- package/dist/interfaces/GitHubCLIHostsYAML.js +1 -0
- package/dist/main.js +26 -0
- package/dist/prompt.js +46 -0
- package/dist/validateNoteVersion.js +25 -0
- package/file-templates/dynamic/.github/workflows/setup/action.yml +13 -0
- package/file-templates/dynamic/Node.gitignore +130 -0
- package/file-templates/dynamic/README.md +3 -0
- package/file-templates/dynamic/_gitignore +9 -0
- package/file-templates/dynamic/package.json +37 -0
- package/file-templates/static/.github/workflows/ci.yml +49 -0
- package/file-templates/static/.prettierignore +12 -0
- package/file-templates/static/.vscode/extensions.json +9 -0
- package/file-templates/static/.vscode/settings.json +75 -0
- package/file-templates/static/LICENSE +674 -0
- package/file-templates/static/_cspell.config.jsonc +25 -0
- package/file-templates/static/_gitattributes +37 -0
- package/file-templates/static/eslint.config.mjs +18 -0
- package/file-templates/static/knip.config.js +20 -0
- package/file-templates/static/prettier.config.mjs +24 -0
- package/file-templates/static/scripts/build.ts +5 -0
- package/file-templates/static/scripts/lint.ts +30 -0
- package/file-templates/static/scripts/tsconfig.json +14 -0
- package/file-templates/static/src/main.ts +5 -0
- package/file-templates/static/tsconfig.json +12 -0
- package/package.json +59 -0
- package/src/commands/CheckCommand.ts +249 -0
- package/src/commands/InitCommand.ts +105 -0
- package/src/commands/NukeCommand.ts +17 -0
- package/src/commands/PublishCommand.ts +242 -0
- package/src/commands/UpdateCommand.ts +20 -0
- package/src/commands/check/check.test.ts +123 -0
- package/src/commands/check/getTruncatedText.ts +187 -0
- package/src/commands/init/checkIfProjectPathExists.ts +36 -0
- package/src/commands/init/createProject.ts +197 -0
- package/src/commands/init/getAuthorName.ts +23 -0
- package/src/commands/init/getProjectPath.ts +112 -0
- package/src/commands/init/packageManager.ts +64 -0
- package/src/commands/init/vsCodeInit.ts +115 -0
- package/src/constants.ts +39 -0
- package/src/git.ts +182 -0
- package/src/interfaces/GitHubCLIHostsYAML.ts +7 -0
- package/src/main.ts +34 -0
- package/src/prompt.ts +72 -0
- package/src/validateNoteVersion.ts +39 -0
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { Command, Option } from "clipanion";
|
|
2
|
+
import { isSemanticVersion } from "complete-common";
|
|
3
|
+
import type { PackageManager } from "complete-node";
|
|
4
|
+
import {
|
|
5
|
+
$,
|
|
6
|
+
$s,
|
|
7
|
+
fatalError,
|
|
8
|
+
getPackageJSONField,
|
|
9
|
+
getPackageJSONVersion,
|
|
10
|
+
getPackageManagerInstallCommand,
|
|
11
|
+
getPackageManagerLockFileName,
|
|
12
|
+
getPackageManagersForProject,
|
|
13
|
+
isFile,
|
|
14
|
+
isGitRepository,
|
|
15
|
+
isGitRepositoryClean,
|
|
16
|
+
isLoggedInToNPM,
|
|
17
|
+
readFile,
|
|
18
|
+
updatePackageJSONDependencies,
|
|
19
|
+
writeFile,
|
|
20
|
+
} from "complete-node";
|
|
21
|
+
import path from "node:path";
|
|
22
|
+
import { CWD, DEFAULT_PACKAGE_MANAGER } from "../constants.js";
|
|
23
|
+
|
|
24
|
+
export class PublishCommand extends Command {
|
|
25
|
+
static override paths = [["publish"], ["p"]];
|
|
26
|
+
|
|
27
|
+
// The first positional argument.
|
|
28
|
+
versionBumpType = Option.String({
|
|
29
|
+
required: true,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
dryRun = Option.Boolean("--dry-run", false, {
|
|
33
|
+
description: "Skip committing/uploading & perform a Git reset afterward.",
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
skipLint = Option.Boolean("--skip-lint", false, {
|
|
37
|
+
description: "Skip linting before publishing.",
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
skipUpdate = Option.Boolean("--skip-update", false, {
|
|
41
|
+
description: "Skip updating the npm dependencies.",
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
static override usage = Command.Usage({
|
|
45
|
+
description: "Bump the version & publish a new release.",
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
49
|
+
async execute(): Promise<void> {
|
|
50
|
+
validate();
|
|
51
|
+
prePublish(
|
|
52
|
+
this.versionBumpType,
|
|
53
|
+
this.dryRun,
|
|
54
|
+
this.skipLint,
|
|
55
|
+
this.skipUpdate,
|
|
56
|
+
);
|
|
57
|
+
publish(this.dryRun);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function validate() {
|
|
62
|
+
if (!isGitRepository(CWD)) {
|
|
63
|
+
fatalError(
|
|
64
|
+
"Failed to publish since the current working directory is not inside of a git repository.",
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (!isGitRepositoryClean(CWD)) {
|
|
69
|
+
fatalError(
|
|
70
|
+
"Failed to publish since the Git repository was dirty. Before publishing, you must push any current changes to git. (Version commits should not contain any code changes.)",
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!isFile("package.json")) {
|
|
75
|
+
fatalError(
|
|
76
|
+
'Failed to find the "package.json" file in the current working directory.',
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (!isLoggedInToNPM()) {
|
|
81
|
+
fatalError(
|
|
82
|
+
'Failed to publish since you are not logged in to npm. Try doing "npm login".',
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Before uploading the project, we want to update dependencies, increment the version, and perform
|
|
89
|
+
* some other steps.
|
|
90
|
+
*/
|
|
91
|
+
function prePublish(
|
|
92
|
+
versionBumpType: string,
|
|
93
|
+
dryRun: boolean,
|
|
94
|
+
skipLint: boolean,
|
|
95
|
+
skipUpdate: boolean,
|
|
96
|
+
) {
|
|
97
|
+
const packageManager = getPackageManagerUsedForExistingProject();
|
|
98
|
+
|
|
99
|
+
$s`git pull --rebase`;
|
|
100
|
+
$s`git push`;
|
|
101
|
+
updateDependencies(skipUpdate, dryRun, packageManager);
|
|
102
|
+
incrementVersion(versionBumpType);
|
|
103
|
+
unsetDevelopmentConstants();
|
|
104
|
+
|
|
105
|
+
tryRunNPMScript("build");
|
|
106
|
+
if (!skipLint) {
|
|
107
|
+
tryRunNPMScript("lint");
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function getPackageManagerUsedForExistingProject(): PackageManager {
|
|
112
|
+
const packageManagers = getPackageManagersForProject(CWD);
|
|
113
|
+
if (packageManagers.length > 1) {
|
|
114
|
+
const packageManagerLockFileNames = packageManagers
|
|
115
|
+
.map((packageManager) => getPackageManagerLockFileName(packageManager))
|
|
116
|
+
.map((packageManagerLockFileName) => `"${packageManagerLockFileName}"`)
|
|
117
|
+
.join(" & ");
|
|
118
|
+
fatalError(
|
|
119
|
+
`Multiple different kinds of package manager lock files were found (${packageManagerLockFileNames}). You should delete the ones that you are not using so that this program can correctly detect your package manager.`,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const packageManager = packageManagers[0];
|
|
124
|
+
if (packageManager !== undefined) {
|
|
125
|
+
return packageManager;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return DEFAULT_PACKAGE_MANAGER;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function updateDependencies(
|
|
132
|
+
skipUpdate: boolean,
|
|
133
|
+
dryRun: boolean,
|
|
134
|
+
packageManager: PackageManager,
|
|
135
|
+
) {
|
|
136
|
+
if (skipUpdate) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
console.log('Updating dependencies in the "package.json" file...');
|
|
141
|
+
const hasNewDependencies = updatePackageJSONDependencies(undefined);
|
|
142
|
+
if (hasNewDependencies) {
|
|
143
|
+
const command = getPackageManagerInstallCommand(packageManager);
|
|
144
|
+
const commandParts = command.split(" ");
|
|
145
|
+
$s`${commandParts}`;
|
|
146
|
+
if (!dryRun) {
|
|
147
|
+
gitCommitAllAndPush("chore: update dependencies");
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function gitCommitAllAndPush(message: string) {
|
|
153
|
+
$s`git add --all`;
|
|
154
|
+
$s`git commit --message ${message}`;
|
|
155
|
+
$s`git push`;
|
|
156
|
+
console.log(
|
|
157
|
+
`Committed and pushed to the git repository with a message of: ${message}`,
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function incrementVersion(versionBumpType: string) {
|
|
162
|
+
if (versionBumpType === "none") {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (versionBumpType === "dev") {
|
|
167
|
+
throw new Error(
|
|
168
|
+
'The version bump type of "dev" is not currently supported.',
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (
|
|
173
|
+
versionBumpType !== "major" &&
|
|
174
|
+
versionBumpType !== "minor" &&
|
|
175
|
+
versionBumpType !== "patch" &&
|
|
176
|
+
versionBumpType !== "dev" &&
|
|
177
|
+
!isSemanticVersion(versionBumpType)
|
|
178
|
+
) {
|
|
179
|
+
fatalError(
|
|
180
|
+
'The version must be one of "major", "minor", "patch", "dev", "none", or a specific semantic version like "1.2.3".',
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// We always use `npm` here to avoid differences with the version command between package
|
|
185
|
+
// managers. The "--no-git-tag-version" flag will prevent npm from both making a commit and adding
|
|
186
|
+
// a tag.
|
|
187
|
+
$s`npm version ${versionBumpType} --no-git-tag-version`;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function unsetDevelopmentConstants() {
|
|
191
|
+
const constantsTSPath = path.join(CWD, "src", "constants.ts");
|
|
192
|
+
if (!isFile(constantsTSPath)) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const constantsTS = readFile(constantsTSPath);
|
|
197
|
+
const newConstantsTS = constantsTS
|
|
198
|
+
.replace("const IS_DEV = true", "const IS_DEV = false")
|
|
199
|
+
.replace("const DEBUG = true", "const DEBUG = false");
|
|
200
|
+
writeFile(constantsTSPath, newConstantsTS);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function tryRunNPMScript(scriptName: string) {
|
|
204
|
+
console.log(`Running: ${scriptName}`);
|
|
205
|
+
|
|
206
|
+
const $$ = $({
|
|
207
|
+
reject: false,
|
|
208
|
+
});
|
|
209
|
+
const { exitCode } = $$.sync`npm run ${scriptName}`;
|
|
210
|
+
|
|
211
|
+
if (exitCode !== 0) {
|
|
212
|
+
$s`git reset --hard`; // Revert the version changes.
|
|
213
|
+
fatalError(`Failed to run "${scriptName}".`);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function publish(dryRun: boolean) {
|
|
218
|
+
const projectName = getPackageJSONField(undefined, "name");
|
|
219
|
+
const version = getPackageJSONVersion(undefined);
|
|
220
|
+
|
|
221
|
+
if (dryRun) {
|
|
222
|
+
$s`git reset --hard`; // Revert the version changes.
|
|
223
|
+
} else {
|
|
224
|
+
const releaseGitCommitMessage = getReleaseGitCommitMessage(version);
|
|
225
|
+
gitCommitAllAndPush(releaseGitCommitMessage);
|
|
226
|
+
|
|
227
|
+
// - The "--access=public" flag is only technically needed for the first publish (unless the
|
|
228
|
+
// package is a scoped package), but it is saved here for posterity.
|
|
229
|
+
// - The "--ignore-scripts" flag is needed since the "npm publish" command will run the
|
|
230
|
+
// "publish" script in the "package.json" file, causing an infinite loop.
|
|
231
|
+
$s`npm publish --access=public --ignore-scripts`;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const dryRunSuffix = dryRun ? " (dry-run)" : "";
|
|
235
|
+
console.log(
|
|
236
|
+
`Published ${projectName} version ${version} successfully${dryRunSuffix}.`,
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function getReleaseGitCommitMessage(version: string): string {
|
|
241
|
+
return `chore: release ${version}`;
|
|
242
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Command } from "clipanion";
|
|
2
|
+
import { updatePackageJSONDependencies } from "complete-node";
|
|
3
|
+
|
|
4
|
+
export class UpdateCommand extends Command {
|
|
5
|
+
static override paths = [["update"], ["u"]];
|
|
6
|
+
|
|
7
|
+
static override usage = Command.Usage({
|
|
8
|
+
description:
|
|
9
|
+
'Invoke "npm-check-updates" to update the dependencies inside of the "package.json" file and then install them.',
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
13
|
+
async execute(): Promise<void> {
|
|
14
|
+
const hasNewDependencies = updatePackageJSONDependencies();
|
|
15
|
+
const msg = hasNewDependencies
|
|
16
|
+
? "Successfully installed new Node.js dependencies."
|
|
17
|
+
: "There were no new dependency updates from npm.";
|
|
18
|
+
console.log(msg);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { strictEqual } from "node:assert";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { getTruncatedText } from "./getTruncatedText.js";
|
|
4
|
+
|
|
5
|
+
test("no markers", () => {
|
|
6
|
+
const templateText = `
|
|
7
|
+
line 1
|
|
8
|
+
line 2
|
|
9
|
+
line 3
|
|
10
|
+
`.trim();
|
|
11
|
+
|
|
12
|
+
const { text } = getTruncatedText("test", templateText, new Set(), new Set());
|
|
13
|
+
strictEqual(text, templateText);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("customization marker", () => {
|
|
17
|
+
const templateText = `
|
|
18
|
+
line 1
|
|
19
|
+
@template-customization-start
|
|
20
|
+
line 2
|
|
21
|
+
@template-customization-end
|
|
22
|
+
line 3
|
|
23
|
+
`.trim();
|
|
24
|
+
|
|
25
|
+
const expectedTemplateText = `
|
|
26
|
+
line 1
|
|
27
|
+
line 3
|
|
28
|
+
`.trim();
|
|
29
|
+
|
|
30
|
+
const { text } = getTruncatedText("test", templateText, new Set(), new Set());
|
|
31
|
+
strictEqual(text, expectedTemplateText);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("ignore block marker part 1", () => {
|
|
35
|
+
const templateText = `
|
|
36
|
+
line 1
|
|
37
|
+
@template-ignore-block-start
|
|
38
|
+
// line 2
|
|
39
|
+
@template-ignore-block-end
|
|
40
|
+
line 3
|
|
41
|
+
`.trim();
|
|
42
|
+
|
|
43
|
+
const parsedTemplateText = `
|
|
44
|
+
line 1
|
|
45
|
+
line 3
|
|
46
|
+
`.trim();
|
|
47
|
+
|
|
48
|
+
const { text, ignoreLines } = getTruncatedText(
|
|
49
|
+
"test",
|
|
50
|
+
templateText,
|
|
51
|
+
new Set(),
|
|
52
|
+
new Set(),
|
|
53
|
+
);
|
|
54
|
+
strictEqual(text, parsedTemplateText);
|
|
55
|
+
strictEqual(ignoreLines.size, 1);
|
|
56
|
+
strictEqual([...ignoreLines][0], "line 2");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("ignore block marker part 2", () => {
|
|
60
|
+
const templateText = `
|
|
61
|
+
line 1
|
|
62
|
+
line 2
|
|
63
|
+
line 3
|
|
64
|
+
`.trim();
|
|
65
|
+
|
|
66
|
+
const expectedTemplateText = `
|
|
67
|
+
line 1
|
|
68
|
+
line 3
|
|
69
|
+
`.trim();
|
|
70
|
+
|
|
71
|
+
const { text } = getTruncatedText(
|
|
72
|
+
"test",
|
|
73
|
+
templateText,
|
|
74
|
+
new Set(["line 2"]),
|
|
75
|
+
new Set(),
|
|
76
|
+
);
|
|
77
|
+
strictEqual(text, expectedTemplateText);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("ignore next line part 1", () => {
|
|
81
|
+
const templateText = `
|
|
82
|
+
line 1
|
|
83
|
+
@template-ignore-next-line
|
|
84
|
+
line 2
|
|
85
|
+
line 3
|
|
86
|
+
`.trim();
|
|
87
|
+
|
|
88
|
+
const expectedTemplateText = `
|
|
89
|
+
line 1
|
|
90
|
+
line 3
|
|
91
|
+
`.trim();
|
|
92
|
+
|
|
93
|
+
const { text, linesBeforeIgnore } = getTruncatedText(
|
|
94
|
+
"test",
|
|
95
|
+
templateText,
|
|
96
|
+
new Set(),
|
|
97
|
+
new Set(),
|
|
98
|
+
);
|
|
99
|
+
strictEqual(linesBeforeIgnore.size, 1);
|
|
100
|
+
strictEqual([...linesBeforeIgnore][0], "line 1");
|
|
101
|
+
strictEqual(text, expectedTemplateText);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("ignore next line part 2", () => {
|
|
105
|
+
const templateText = `
|
|
106
|
+
line 1
|
|
107
|
+
line 2
|
|
108
|
+
line 3
|
|
109
|
+
`.trim();
|
|
110
|
+
|
|
111
|
+
const expectedTemplateText = `
|
|
112
|
+
line 1
|
|
113
|
+
line 3
|
|
114
|
+
`.trim();
|
|
115
|
+
|
|
116
|
+
const { text } = getTruncatedText(
|
|
117
|
+
"test",
|
|
118
|
+
templateText,
|
|
119
|
+
new Set(),
|
|
120
|
+
new Set(["line 1"]),
|
|
121
|
+
);
|
|
122
|
+
strictEqual(text, expectedTemplateText);
|
|
123
|
+
});
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { getEnumValues, trimPrefix } from "complete-common";
|
|
2
|
+
import {
|
|
3
|
+
fatalError,
|
|
4
|
+
getPackageManagerLockFileNames,
|
|
5
|
+
PackageManager,
|
|
6
|
+
} from "complete-node";
|
|
7
|
+
|
|
8
|
+
const MARKER_CUSTOMIZATION_START = "@template-customization-start";
|
|
9
|
+
const MARKER_CUSTOMIZATION_END = "@template-customization-end";
|
|
10
|
+
const MARKER_IGNORE_BLOCK_START = "@template-ignore-block-start";
|
|
11
|
+
const MARKER_IGNORE_BLOCK_END = "@template-ignore-block-end";
|
|
12
|
+
const MARKER_IGNORE_NEXT_LINE = "@template-ignore-next-line";
|
|
13
|
+
|
|
14
|
+
const PACKAGE_MANAGER_STRINGS = [
|
|
15
|
+
"PACKAGE_MANAGER_NAME",
|
|
16
|
+
"PACKAGE_MANAGER_INSTALL_COMMAND",
|
|
17
|
+
"PACKAGE_MANAGER_LOCK_FILE_NAME",
|
|
18
|
+
...getEnumValues(PackageManager),
|
|
19
|
+
...getPackageManagerLockFileNames(),
|
|
20
|
+
] as const;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param fileName Used to perform some specific rules based on the template file name.
|
|
24
|
+
* @param text The text to parse.
|
|
25
|
+
* @param ignoreLines A set of lines to remove from the text.
|
|
26
|
+
* @param linesBeforeIgnore A set of lines that will trigger the subsequent line to be ignored.
|
|
27
|
+
* @returns The text of the file with all text removed between any flagged markers (and other
|
|
28
|
+
* specific hard-coded exclusions), as well as an array of lines that had a
|
|
29
|
+
* "ignore-next-line" marker below them.
|
|
30
|
+
*/
|
|
31
|
+
export function getTruncatedText(
|
|
32
|
+
fileName: string,
|
|
33
|
+
text: string,
|
|
34
|
+
ignoreLines: ReadonlySet<string>,
|
|
35
|
+
linesBeforeIgnore: ReadonlySet<string>,
|
|
36
|
+
): {
|
|
37
|
+
text: string;
|
|
38
|
+
ignoreLines: ReadonlySet<string>;
|
|
39
|
+
linesBeforeIgnore: ReadonlySet<string>;
|
|
40
|
+
} {
|
|
41
|
+
const lines = text.split("\n");
|
|
42
|
+
|
|
43
|
+
const newLines: string[] = [];
|
|
44
|
+
const newIgnoreLines = new Set<string>();
|
|
45
|
+
const newLinesBeforeIgnore = new Set<string>();
|
|
46
|
+
|
|
47
|
+
let isSkipping = false;
|
|
48
|
+
let isIgnoring = false;
|
|
49
|
+
let shouldIgnoreNextLine = false;
|
|
50
|
+
let previousLine = "";
|
|
51
|
+
|
|
52
|
+
for (const line of lines) {
|
|
53
|
+
if (line.trim() === "") {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (ignoreLines.has(line.trim())) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (shouldIgnoreNextLine) {
|
|
62
|
+
shouldIgnoreNextLine = false;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (linesBeforeIgnore.has(line)) {
|
|
67
|
+
shouldIgnoreNextLine = true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// -------------
|
|
71
|
+
// Marker checks
|
|
72
|
+
// -------------
|
|
73
|
+
|
|
74
|
+
if (line.includes(MARKER_CUSTOMIZATION_START)) {
|
|
75
|
+
isSkipping = true;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (line.includes(MARKER_CUSTOMIZATION_END)) {
|
|
80
|
+
isSkipping = false;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (line.includes(MARKER_IGNORE_BLOCK_START)) {
|
|
85
|
+
isIgnoring = true;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (line.includes(MARKER_IGNORE_BLOCK_END)) {
|
|
90
|
+
isIgnoring = false;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (line.includes(MARKER_IGNORE_NEXT_LINE)) {
|
|
95
|
+
shouldIgnoreNextLine = true;
|
|
96
|
+
|
|
97
|
+
// We mark the previous line so that we know the next line to skip in the template.
|
|
98
|
+
if (previousLine.trim() === "") {
|
|
99
|
+
fatalError(
|
|
100
|
+
`You cannot have a "${MARKER_IGNORE_NEXT_LINE}" marker before a blank line in the "${fileName}" file.`,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
newLinesBeforeIgnore.add(previousLine);
|
|
104
|
+
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (isIgnoring) {
|
|
109
|
+
const baseLine = trimPrefix(line.trim(), "// ");
|
|
110
|
+
newIgnoreLines.add(baseLine);
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// --------------------
|
|
115
|
+
// Specific file checks
|
|
116
|
+
// --------------------
|
|
117
|
+
|
|
118
|
+
// We should ignore imports in JavaScript or TypeScript files.
|
|
119
|
+
if (fileName.endsWith(".js") || fileName.endsWith(".ts")) {
|
|
120
|
+
if (line === "import {") {
|
|
121
|
+
isSkipping = true;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (line.startsWith("} from ")) {
|
|
126
|
+
isSkipping = false;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (line.startsWith("import ")) {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// End-users can have different ignored words.
|
|
136
|
+
if (
|
|
137
|
+
fileName === "cspell.config.jsonc" ||
|
|
138
|
+
fileName === "_cspell.config.jsonc"
|
|
139
|
+
) {
|
|
140
|
+
if (line.match(/"words": \[.*]/) !== null) {
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (line.includes('"words": [')) {
|
|
145
|
+
isSkipping = true;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if ((line.endsWith("]") || line.endsWith("],")) && isSkipping) {
|
|
150
|
+
isSkipping = false;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (fileName === "ci.yml" || fileName === "action.yml") {
|
|
156
|
+
// End-users can have different package managers.
|
|
157
|
+
if (hasPackageManagerString(line)) {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Ignore comments, since end-users are expected to delete the explanations.
|
|
162
|
+
if (line.match(/^\s*#/) !== null) {
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// ------------
|
|
168
|
+
// Final checks
|
|
169
|
+
// ------------
|
|
170
|
+
|
|
171
|
+
if (!isSkipping) {
|
|
172
|
+
newLines.push(line);
|
|
173
|
+
previousLine = line;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const newText = newLines.join("\n");
|
|
178
|
+
return {
|
|
179
|
+
text: newText,
|
|
180
|
+
ignoreLines: newIgnoreLines,
|
|
181
|
+
linesBeforeIgnore: newLinesBeforeIgnore,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function hasPackageManagerString(line: string) {
|
|
186
|
+
return PACKAGE_MANAGER_STRINGS.some((string) => line.includes(string));
|
|
187
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import {
|
|
3
|
+
deleteFileOrDirectory,
|
|
4
|
+
fileOrDirectoryExists,
|
|
5
|
+
isDirectory,
|
|
6
|
+
} from "complete-node";
|
|
7
|
+
import { CWD } from "../../constants.js";
|
|
8
|
+
import { getInputYesNo, promptEnd, promptLog } from "../../prompt.js";
|
|
9
|
+
|
|
10
|
+
export async function checkIfProjectPathExists(
|
|
11
|
+
projectPath: string,
|
|
12
|
+
yes: boolean,
|
|
13
|
+
): Promise<void> {
|
|
14
|
+
if (projectPath === CWD || !fileOrDirectoryExists(projectPath)) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const fileType = isDirectory(projectPath) ? "directory" : "file";
|
|
19
|
+
|
|
20
|
+
if (yes) {
|
|
21
|
+
deleteFileOrDirectory(projectPath);
|
|
22
|
+
promptLog(`Deleted ${fileType}: ${chalk.green(projectPath)}`);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
promptLog(
|
|
27
|
+
`A ${fileType} already exists with a name of: ${chalk.green(projectPath)}`,
|
|
28
|
+
);
|
|
29
|
+
const shouldDelete = await getInputYesNo("Do you want me to delete it?");
|
|
30
|
+
|
|
31
|
+
if (!shouldDelete) {
|
|
32
|
+
promptEnd("Ok then. Goodbye.");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
deleteFileOrDirectory(projectPath);
|
|
36
|
+
}
|