@adbayb/stack 2.36.0 → 2.36.1-next-2d037ee
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/dist/index.js
CHANGED
|
@@ -1,809 +1,778 @@
|
|
|
1
|
-
import { helpers, termost } from
|
|
2
|
-
import { createRequire } from
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { fdir } from
|
|
1
|
+
import { helpers, termost } from "termost";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import { cpSync, existsSync, readFileSync, readdirSync, renameSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { chmod, mkdir, rm, symlink, writeFile } from "node:fs/promises";
|
|
6
|
+
import { fdir } from "fdir";
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
//#region src/helpers.ts
|
|
9
|
+
const require = createRequire(import.meta.url);
|
|
9
10
|
function assert(expectedCondition, createError) {
|
|
10
|
-
|
|
11
|
-
throw createError();
|
|
12
|
-
}
|
|
11
|
+
if (!expectedCondition) throw createError();
|
|
13
12
|
}
|
|
14
13
|
/**
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
error: "red",
|
|
36
|
-
information: "blue",
|
|
37
|
-
success: "green"
|
|
38
|
-
};
|
|
39
|
-
log(helpers.format(`
|
|
14
|
+
* Helper to format log messages with a welcoming bot.
|
|
15
|
+
* @param input - Message factory.
|
|
16
|
+
* @param input.title - Title input.
|
|
17
|
+
* @param input.description - Description input.
|
|
18
|
+
* @param input.body - Body input.
|
|
19
|
+
* @param input.type - Message type.
|
|
20
|
+
* @example
|
|
21
|
+
* botMessage(
|
|
22
|
+
* {
|
|
23
|
+
* title: "Oops, an error occurred",
|
|
24
|
+
* description:
|
|
25
|
+
* "Keep calm and carry on with some coffee ☕️",
|
|
26
|
+
* body: String(previousTaskError),
|
|
27
|
+
* type: "error",
|
|
28
|
+
* },
|
|
29
|
+
* );
|
|
30
|
+
*/
|
|
31
|
+
const botMessage = (input) => {
|
|
32
|
+
const { type } = input;
|
|
33
|
+
(type === "error" ? console.error : console.log)(helpers.format(`
|
|
40
34
|
╭─────╮
|
|
41
35
|
│ ◠ ◠ ${input.title}
|
|
42
36
|
│ ${type === "error" ? "◠" : "◡"} │ ${input.description}
|
|
43
37
|
╰─────╯
|
|
44
38
|
${input.body ? `
|
|
45
39
|
${input.body}
|
|
46
|
-
` : ""}`, {
|
|
47
|
-
|
|
48
|
-
|
|
40
|
+
` : ""}`, { color: {
|
|
41
|
+
error: "red",
|
|
42
|
+
information: "blue",
|
|
43
|
+
success: "green"
|
|
44
|
+
}[type] }));
|
|
49
45
|
};
|
|
50
46
|
/**
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
47
|
+
* Resolve a relative path to an absolute one resolved from the generated project root directory.
|
|
48
|
+
* @param path - The relative path.
|
|
49
|
+
* @returns The resolved absolute path.
|
|
50
|
+
* @example
|
|
51
|
+
* resolveFromProjectDirectory(".gitignore");
|
|
52
|
+
*/
|
|
53
|
+
const resolveFromProjectDirectory = (path) => {
|
|
54
|
+
return resolve(process.cwd(), path);
|
|
58
55
|
};
|
|
59
56
|
/**
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
try {
|
|
164
|
-
return await helpers.exec(command, {
|
|
165
|
-
hasLiveOutput: true
|
|
166
|
-
});
|
|
167
|
-
} catch (error) {
|
|
168
|
-
throw createError("changeset", error);
|
|
169
|
-
}
|
|
57
|
+
* Resolve a relative path to an absolute one resolved from the `stack` node module directory.
|
|
58
|
+
* @param path - The relative path.
|
|
59
|
+
* @returns The resolved absolute path.
|
|
60
|
+
* @example
|
|
61
|
+
* resolveFromStackDirectory("./templates");
|
|
62
|
+
*/
|
|
63
|
+
const resolveFromStackDirectory = (path) => {
|
|
64
|
+
return resolve(import.meta.dirname, "../", path);
|
|
65
|
+
};
|
|
66
|
+
const createError = (bin, error) => {
|
|
67
|
+
const errorMessage = `\`${bin}\` command failed.\n${String(error)}`;
|
|
68
|
+
if (error instanceof Error) {
|
|
69
|
+
error.message = errorMessage;
|
|
70
|
+
return error;
|
|
71
|
+
}
|
|
72
|
+
return new Error(errorMessage);
|
|
73
|
+
};
|
|
74
|
+
const getNpmVersion = async () => {
|
|
75
|
+
try {
|
|
76
|
+
return await helpers.exec("pnpm -v");
|
|
77
|
+
} catch {
|
|
78
|
+
throw createError("pnpm", "The project must use `pnpm` as a node package manager tool. Follow this installation guide https://pnpm.io/installation");
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
const getStackCommand = (command) => {
|
|
82
|
+
return `pnpm stack ${command}`;
|
|
83
|
+
};
|
|
84
|
+
const hasDependency = (packageName) => {
|
|
85
|
+
return Boolean(require.resolve(packageName));
|
|
86
|
+
};
|
|
87
|
+
const setPackageManager = async () => {
|
|
88
|
+
/**
|
|
89
|
+
* Corepack is downloaded remotely to get always up-to-date npm registry fingerprints since they're hardcoded.
|
|
90
|
+
* @see {@link https://github.com/nodejs/corepack/issues/613}
|
|
91
|
+
*/
|
|
92
|
+
return helpers.exec("npx corepack enable");
|
|
93
|
+
};
|
|
94
|
+
const request = { async get(url, responseType) {
|
|
95
|
+
const response = await fetch(url);
|
|
96
|
+
if (!response.ok) throw createError("fetch", `Failed to fetch resources from ${url} (${JSON.stringify({
|
|
97
|
+
status: response.status,
|
|
98
|
+
statusText: response.statusText
|
|
99
|
+
})})`);
|
|
100
|
+
return responseType === "text" ? response.text() : response.json();
|
|
101
|
+
} };
|
|
102
|
+
const eslint = (options) => async (files = []) => {
|
|
103
|
+
let eslintFiles = [];
|
|
104
|
+
if (files.length === 0) eslintFiles.push(".");
|
|
105
|
+
else {
|
|
106
|
+
eslintFiles = files.filter((file) => {
|
|
107
|
+
return ESLINT_EXTENSIONS.some((extension) => file.endsWith(extension));
|
|
108
|
+
});
|
|
109
|
+
if (eslintFiles.length === 0) return;
|
|
110
|
+
}
|
|
111
|
+
const arguments_ = [
|
|
112
|
+
...eslintFiles,
|
|
113
|
+
"--cache",
|
|
114
|
+
`--cache-location ${resolveFromProjectDirectory("node_modules/.cache/.eslintcache")}`,
|
|
115
|
+
"--no-error-on-unmatched-pattern"
|
|
116
|
+
];
|
|
117
|
+
if (options.isFixMode) arguments_.push("--fix");
|
|
118
|
+
try {
|
|
119
|
+
return await helpers.exec(`eslint ${arguments_.join(" ")}`);
|
|
120
|
+
} catch (error) {
|
|
121
|
+
throw createError("eslint", error);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
const turbo = async (command, options = {}) => {
|
|
125
|
+
try {
|
|
126
|
+
const { excludeExamples = false, hasLiveOutput = true, ...restOptions } = options;
|
|
127
|
+
return await helpers.exec(`turbo run ${command} ${excludeExamples ? "--filter !@examples/*" : ""}`, {
|
|
128
|
+
...restOptions,
|
|
129
|
+
hasLiveOutput
|
|
130
|
+
});
|
|
131
|
+
} catch (error) {
|
|
132
|
+
throw createError("turbo", error);
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
const logCheckableFiles = (files) => {
|
|
136
|
+
if (files.length === 0) {
|
|
137
|
+
helpers.message("The whole project will be checked.", {
|
|
138
|
+
label: false,
|
|
139
|
+
lineBreak: {
|
|
140
|
+
end: true,
|
|
141
|
+
start: false
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
helpers.message(files.join("\n "), {
|
|
147
|
+
label: "Following files will be checked:",
|
|
148
|
+
lineBreak: {
|
|
149
|
+
end: true,
|
|
150
|
+
start: false
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
const changeset = async (command) => {
|
|
155
|
+
try {
|
|
156
|
+
return await helpers.exec(command, { hasLiveOutput: true });
|
|
157
|
+
} catch (error) {
|
|
158
|
+
throw createError("changeset", error);
|
|
159
|
+
}
|
|
170
160
|
};
|
|
171
161
|
const ESLINT_EXTENSIONS = [
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
162
|
+
"js",
|
|
163
|
+
"jsx",
|
|
164
|
+
"cjs",
|
|
165
|
+
"mjs",
|
|
166
|
+
"ts",
|
|
167
|
+
"tsx",
|
|
168
|
+
"cts",
|
|
169
|
+
"mts",
|
|
170
|
+
"md",
|
|
171
|
+
"mdx"
|
|
182
172
|
];
|
|
183
173
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
174
|
+
//#endregion
|
|
175
|
+
//#region src/commands/build.ts
|
|
176
|
+
const createBuildCommand = (program) => {
|
|
177
|
+
program.command({
|
|
178
|
+
description: "Build the project in production mode",
|
|
179
|
+
name: "build"
|
|
180
|
+
}).task({ async handler() {
|
|
181
|
+
await turbo("build");
|
|
182
|
+
} });
|
|
193
183
|
};
|
|
194
184
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
});
|
|
185
|
+
//#endregion
|
|
186
|
+
//#region src/commands/check/checkCode.ts
|
|
187
|
+
const checkCode = eslint({ isFixMode: false });
|
|
198
188
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
189
|
+
//#endregion
|
|
190
|
+
//#region src/commands/check/checkCommit.ts
|
|
191
|
+
const checkCommit = async () => {
|
|
192
|
+
try {
|
|
193
|
+
return await helpers.exec("commitlint --extends \"@commitlint/config-conventional\" --edit");
|
|
194
|
+
} catch (error) {
|
|
195
|
+
throw createError("commitlint", error);
|
|
196
|
+
}
|
|
205
197
|
};
|
|
206
198
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
}
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
const
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
};
|
|
295
|
-
return (package_)=>{
|
|
296
|
-
lint(package_, "development");
|
|
297
|
-
lint(package_, "production");
|
|
298
|
-
};
|
|
299
|
-
};
|
|
300
|
-
const createPackageError = (message, context)=>{
|
|
301
|
-
return createError("stack check", context ? `\`${context.name}\` consumed by \`${context.consumedBy}\` doesn't conform to package guidelines.\n${message}` : message);
|
|
199
|
+
//#endregion
|
|
200
|
+
//#region src/commands/check/checkDependency.ts
|
|
201
|
+
const checkDependency = async () => {
|
|
202
|
+
const stdout = await helpers.exec("pnpm recursive ls --json");
|
|
203
|
+
const checkDependencyVersionMismatch = createPackagesVersionMismatchChecker();
|
|
204
|
+
const packages = JSON.parse(stdout).map((package_) => {
|
|
205
|
+
const packagePath = join(package_.path, "package.json");
|
|
206
|
+
assert(package_.name, () => createPackageError(`\`${packagePath}\` must have a name field.`));
|
|
207
|
+
const packageContent = require(packagePath);
|
|
208
|
+
const peerDependencies = packageContent.peerDependencies ?? {};
|
|
209
|
+
const devDependencies = packageContent.devDependencies ?? {};
|
|
210
|
+
return {
|
|
211
|
+
dependencies: packageContent.dependencies ?? {},
|
|
212
|
+
devDependencies,
|
|
213
|
+
name: package_.name,
|
|
214
|
+
peerDependencies
|
|
215
|
+
};
|
|
216
|
+
});
|
|
217
|
+
for (const package_ of packages) {
|
|
218
|
+
checkDependencyVersionMismatch(package_);
|
|
219
|
+
checkDependencyVersionRange(package_);
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
const checkDependencyVersionRange = ({ dependencies, devDependencies, name, peerDependencies }) => {
|
|
223
|
+
for (const dependencyName of Object.keys(devDependencies)) {
|
|
224
|
+
const version = devDependencies[dependencyName];
|
|
225
|
+
assertVersion(version, {
|
|
226
|
+
consumedBy: name,
|
|
227
|
+
name: dependencyName
|
|
228
|
+
});
|
|
229
|
+
if (version !== "workspace:*" && !isExcluded(version) && !/^\d/.test(version)) throw createPackageError(`As a dev dependency, \`${dependencyName}\` version must be fixed (or set as "workspace:*" for local packages) to reduce accidental breaking change risks due to an implicit semver upgrade.`, {
|
|
230
|
+
consumedBy: name,
|
|
231
|
+
name: dependencyName
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
for (const dependencyName of Object.keys(dependencies)) {
|
|
235
|
+
const version = dependencies[dependencyName];
|
|
236
|
+
assertVersion(version, {
|
|
237
|
+
consumedBy: name,
|
|
238
|
+
name: dependencyName
|
|
239
|
+
});
|
|
240
|
+
if (version !== "workspace:^" && !hasCaret(version) && !isExcluded(version)) throw createPackageError(`As a dependency, \`${dependencyName}\` version must be prefixed with a caret (or set as "workspace:^" for local packages) to optimize the size (whether of installation or bundle output) on the consumer side.`, {
|
|
241
|
+
consumedBy: name,
|
|
242
|
+
name: dependencyName
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
for (const dependencyName of Object.keys(peerDependencies)) {
|
|
246
|
+
const version = peerDependencies[dependencyName];
|
|
247
|
+
assertVersion(version, {
|
|
248
|
+
consumedBy: name,
|
|
249
|
+
name: dependencyName
|
|
250
|
+
});
|
|
251
|
+
if (!hasCaret(version) && !isExcluded(version)) throw createPackageError(`As a peer dependency, \`${dependencyName}\` version must be explicit (i.e. the "workspace:^" protocol a version resolver is not allowed) and prefixed with a caret to optimize the size (whether of installation or bundle output) on the consumer side.`, {
|
|
252
|
+
consumedBy: name,
|
|
253
|
+
name: dependencyName
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
const createPackagesVersionMismatchChecker = () => {
|
|
258
|
+
const monorepoDependencies = /* @__PURE__ */ new Map();
|
|
259
|
+
const monorepoDevelopmentDependencies = /* @__PURE__ */ new Map();
|
|
260
|
+
const lint = (package_, type) => {
|
|
261
|
+
const packageName = package_.name;
|
|
262
|
+
const isDevelopment = type === "development";
|
|
263
|
+
const store = isDevelopment ? monorepoDevelopmentDependencies : monorepoDependencies;
|
|
264
|
+
const dependencies = isDevelopment ? package_.devDependencies : package_.dependencies;
|
|
265
|
+
for (const dependencyName of Object.keys(dependencies)) {
|
|
266
|
+
const depVersion = dependencies[dependencyName];
|
|
267
|
+
if (!depVersion) continue;
|
|
268
|
+
const storedVersion = store.get(dependencyName);
|
|
269
|
+
if (!storedVersion) {
|
|
270
|
+
store.set(dependencyName, depVersion);
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
if (!(depVersion === storedVersion)) throw createPackageError(`Mismatched versions: received version \`${depVersion}\` while others use \`${storedVersion}\`. To prevent issues with singleton-like code (React contexts, …), please make sure to update all packages to use the same \`${dependencyName}\` version (either \`${storedVersion}\` or \`${depVersion}\`).`, {
|
|
274
|
+
consumedBy: packageName,
|
|
275
|
+
name: dependencyName
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
return (package_) => {
|
|
280
|
+
lint(package_, "development");
|
|
281
|
+
lint(package_, "production");
|
|
282
|
+
};
|
|
283
|
+
};
|
|
284
|
+
const createPackageError = (message, context) => {
|
|
285
|
+
return createError("stack check", context ? `\`${context.name}\` consumed by \`${context.consumedBy}\` doesn't conform to package guidelines.\n${message}` : message);
|
|
302
286
|
};
|
|
303
287
|
function assertVersion(version, { consumedBy, name }) {
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
288
|
+
assert(version, () => createPackageError(`\`${name}\` must have a valid version specified (current version equals to \`${String(version)}\`).`, {
|
|
289
|
+
consumedBy,
|
|
290
|
+
name
|
|
291
|
+
}));
|
|
308
292
|
}
|
|
309
|
-
const isExcluded = (version)=>{
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
return isNpmProtocol || isPreReleaseVersion;
|
|
293
|
+
const isExcluded = (version) => {
|
|
294
|
+
const isPreReleaseVersion = /\d+\.\d+\.\d+-(alpha|beta|experimental|next|rc).*/.exec(version);
|
|
295
|
+
return version.startsWith("npm:") || isPreReleaseVersion;
|
|
313
296
|
};
|
|
314
|
-
const hasCaret = (version)=>version.startsWith("^");
|
|
297
|
+
const hasCaret = (version) => version.startsWith("^");
|
|
315
298
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
299
|
+
//#endregion
|
|
300
|
+
//#region src/commands/check/checkType.ts
|
|
301
|
+
const checkType = async () => {
|
|
302
|
+
try {
|
|
303
|
+
return await helpers.exec("pnpm --parallel exec tsc --noEmit");
|
|
304
|
+
} catch (error) {
|
|
305
|
+
throw createError("tsc", error);
|
|
306
|
+
}
|
|
322
307
|
};
|
|
323
308
|
|
|
309
|
+
//#endregion
|
|
310
|
+
//#region src/commands/check/check.ts
|
|
324
311
|
const ONLY_VALUES = [
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
312
|
+
"commit",
|
|
313
|
+
"code",
|
|
314
|
+
"dependency",
|
|
315
|
+
"type"
|
|
329
316
|
];
|
|
330
|
-
const createCheckCommand = (program)=>{
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
const ifFilterDefinedAndNotEqualTo = (filter)=>(context)=>{
|
|
391
|
-
return context.filter !== undefined && context.filter !== filter;
|
|
392
|
-
};
|
|
317
|
+
const createCheckCommand = (program) => {
|
|
318
|
+
program.command({
|
|
319
|
+
description: "Check code health (static analysis)",
|
|
320
|
+
name: "check"
|
|
321
|
+
}).option({
|
|
322
|
+
defaultValue: void 0,
|
|
323
|
+
description: `Filter the compliance check to run (accepted value: ${ONLY_VALUES.join(", ")})`,
|
|
324
|
+
key: "filter",
|
|
325
|
+
name: "filter"
|
|
326
|
+
}).task({
|
|
327
|
+
handler(_, argv) {
|
|
328
|
+
logCheckableFiles(argv.operands);
|
|
329
|
+
},
|
|
330
|
+
skip: ifFilterDefinedAndNotEqualTo("code")
|
|
331
|
+
}).task({
|
|
332
|
+
async handler() {
|
|
333
|
+
await turbo("build", {
|
|
334
|
+
excludeExamples: true,
|
|
335
|
+
hasLiveOutput: false
|
|
336
|
+
});
|
|
337
|
+
},
|
|
338
|
+
label: label$4("Prepare the project"),
|
|
339
|
+
skip({ filter }) {
|
|
340
|
+
return filter === "commit";
|
|
341
|
+
}
|
|
342
|
+
}).task({
|
|
343
|
+
async handler() {
|
|
344
|
+
await checkDependency();
|
|
345
|
+
},
|
|
346
|
+
label: label$4("Check dependency compliance"),
|
|
347
|
+
skip: ifFilterDefinedAndNotEqualTo("dependency")
|
|
348
|
+
}).task({
|
|
349
|
+
async handler(_, argv) {
|
|
350
|
+
const filenames = argv.operands;
|
|
351
|
+
await checkCode(filenames);
|
|
352
|
+
},
|
|
353
|
+
label: label$4("Check code compliance"),
|
|
354
|
+
skip: ifFilterDefinedAndNotEqualTo("code")
|
|
355
|
+
}).task({
|
|
356
|
+
async handler() {
|
|
357
|
+
await checkType();
|
|
358
|
+
},
|
|
359
|
+
label: label$4("Check type compliance"),
|
|
360
|
+
skip(context, argv) {
|
|
361
|
+
return ifFilterDefinedAndNotEqualTo("type")(context) || !hasDependency("typescript") || argv.operands.length > 0;
|
|
362
|
+
}
|
|
363
|
+
}).task({
|
|
364
|
+
async handler() {
|
|
365
|
+
await checkCommit();
|
|
366
|
+
},
|
|
367
|
+
label: label$4("Check commit compliance"),
|
|
368
|
+
skip(context) {
|
|
369
|
+
return context.filter !== "commit";
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
};
|
|
373
|
+
const label$4 = (message) => `${message} 🧐`;
|
|
374
|
+
const ifFilterDefinedAndNotEqualTo = (filter) => (context) => {
|
|
375
|
+
return context.filter !== void 0 && context.filter !== filter;
|
|
376
|
+
};
|
|
393
377
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
};
|
|
433
|
-
const label$3 = (message)
|
|
434
|
-
const cleanFiles = async (files)=>{
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
};
|
|
440
|
-
const isDirectoryExistAndNotEmpty = (path)=>{
|
|
441
|
-
|
|
442
|
-
};
|
|
443
|
-
const retrieveIgnoredFiles = async ()=>{
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
const PRESERVE_FILES = [
|
|
448
|
-
"node_modules"
|
|
449
|
-
];
|
|
378
|
+
//#endregion
|
|
379
|
+
//#region src/commands/clean.ts
|
|
380
|
+
const createCleanCommand = (program) => {
|
|
381
|
+
program.command({
|
|
382
|
+
description: "Clean the project",
|
|
383
|
+
name: "clean"
|
|
384
|
+
}).task({
|
|
385
|
+
async handler() {
|
|
386
|
+
const cachePath = "node_modules/.cache";
|
|
387
|
+
const files = await retrieveIgnoredFiles();
|
|
388
|
+
if (isDirectoryExistAndNotEmpty(resolveFromProjectDirectory(cachePath))) files.push(cachePath);
|
|
389
|
+
return files;
|
|
390
|
+
},
|
|
391
|
+
key: "files",
|
|
392
|
+
label: label$3("Retrieve removable files")
|
|
393
|
+
}).task({
|
|
394
|
+
async handler({ files }) {
|
|
395
|
+
if (files.length === 0) return;
|
|
396
|
+
await cleanFiles(files);
|
|
397
|
+
},
|
|
398
|
+
label({ files }) {
|
|
399
|
+
return files.length > 0 ? label$3("Clean assets") : "Already clean ✨";
|
|
400
|
+
}
|
|
401
|
+
}).task({
|
|
402
|
+
handler({ files }) {
|
|
403
|
+
helpers.message(files.join("\n "), {
|
|
404
|
+
label: "Removed assets",
|
|
405
|
+
lineBreak: {
|
|
406
|
+
end: false,
|
|
407
|
+
start: true
|
|
408
|
+
},
|
|
409
|
+
type: "information"
|
|
410
|
+
});
|
|
411
|
+
},
|
|
412
|
+
skip({ files }) {
|
|
413
|
+
return files.length === 0;
|
|
414
|
+
}
|
|
415
|
+
});
|
|
416
|
+
};
|
|
417
|
+
const label$3 = (message) => `${message} 🧹`;
|
|
418
|
+
const cleanFiles = async (files) => {
|
|
419
|
+
return Promise.all(files.map(async (file) => rm(file, {
|
|
420
|
+
force: true,
|
|
421
|
+
recursive: true
|
|
422
|
+
})));
|
|
423
|
+
};
|
|
424
|
+
const isDirectoryExistAndNotEmpty = (path) => {
|
|
425
|
+
return existsSync(path) && readdirSync(path).length > 0;
|
|
426
|
+
};
|
|
427
|
+
const retrieveIgnoredFiles = async () => {
|
|
428
|
+
return (await helpers.exec("git clean -fdXn")).split(/\n|\r\n/).filter((cleanOutput) => !PRESERVE_FILES.some((excludedFile) => cleanOutput.includes(excludedFile))).map((cleanOutput) => cleanOutput.split(" ").at(-1));
|
|
429
|
+
};
|
|
430
|
+
const PRESERVE_FILES = ["node_modules"];
|
|
450
431
|
|
|
451
|
-
|
|
432
|
+
//#endregion
|
|
433
|
+
//#region package.json
|
|
434
|
+
var version = "2.36.1-next-2d037ee";
|
|
452
435
|
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
await helpers.exec("git add -A");
|
|
572
|
-
await helpers.exec('git commit -m "chore: initial commit"');
|
|
573
|
-
},
|
|
574
|
-
label: label$2("Commit")
|
|
575
|
-
}).task({
|
|
576
|
-
handler ({ data }) {
|
|
577
|
-
botMessage({
|
|
578
|
-
description: `Run \`cd ./${data.projectName}\` and Enjoy 🚀`,
|
|
579
|
-
title: "The project was successfully created",
|
|
580
|
-
type: "success"
|
|
581
|
-
});
|
|
582
|
-
}
|
|
583
|
-
});
|
|
584
|
-
};
|
|
585
|
-
const label$2 = (message)=>`${message} 🔨`;
|
|
436
|
+
//#endregion
|
|
437
|
+
//#region src/commands/create.ts
|
|
438
|
+
const createCreateCommand = (program) => {
|
|
439
|
+
program.command({
|
|
440
|
+
description: "Scaffold a new project",
|
|
441
|
+
name: "create"
|
|
442
|
+
}).task({ handler() {
|
|
443
|
+
botMessage({
|
|
444
|
+
description: "I can guarantee you a project creation in under 1 minute 🚀",
|
|
445
|
+
title: `I'm Stack v${version}, your bot assistant`,
|
|
446
|
+
type: "information"
|
|
447
|
+
});
|
|
448
|
+
} }).task({
|
|
449
|
+
async handler() {
|
|
450
|
+
await getNpmVersion();
|
|
451
|
+
},
|
|
452
|
+
label: label$2("Check pre-requisites")
|
|
453
|
+
}).input({
|
|
454
|
+
key: "inputName",
|
|
455
|
+
label: "What's your project name?",
|
|
456
|
+
type: "text"
|
|
457
|
+
}).input({
|
|
458
|
+
key: "inputDescription",
|
|
459
|
+
label: "How would you describe it?",
|
|
460
|
+
type: "text"
|
|
461
|
+
}).input({
|
|
462
|
+
defaultValue: "git@github.com:adbayb/xxx.git",
|
|
463
|
+
key: "inputUrl",
|
|
464
|
+
label: "Where will it be stored? (Git remote URL)",
|
|
465
|
+
type: "text"
|
|
466
|
+
}).input({
|
|
467
|
+
defaultValue: "single-project",
|
|
468
|
+
key: "inputTemplate",
|
|
469
|
+
label: "Which template you would like to apply?",
|
|
470
|
+
options: ["single-project", "multi-projects"],
|
|
471
|
+
type: "select"
|
|
472
|
+
}).task({
|
|
473
|
+
async handler({ inputDescription, inputName, inputUrl }) {
|
|
474
|
+
if (!inputName) throw createError("stack create", "The project name is not optional. Make sure to provide a valid value (non-empty string).");
|
|
475
|
+
const { repoName, repoOwner } = (inputUrl.startsWith("git") ? /^git@.*:(?<repoOwner>.*)\/(?<repoName>.*)\.git$/ : /^https?:\/\/.*\/(?<repoOwner>.*)\/(?<repoName>.*)\.git$/).exec(inputUrl)?.groups ?? {};
|
|
476
|
+
if (!repoOwner || !repoName) throw createError("git", "The owner and repository name can not be extracted. Please make sure to follow either `/^git@.*:(?<repoOwner>.*)/(?<repoName>.*).git$/` or `/^https?://.*/(?<repoOwner>.*)/(?<repoName>.*).git$/` pattern.");
|
|
477
|
+
const nodeVersion = await request.get("https://resolve-node.vercel.app/lts", "text");
|
|
478
|
+
const { version: npmVersion } = await request.get("https://registry.npmjs.org/pnpm/latest", "json");
|
|
479
|
+
return {
|
|
480
|
+
licenseYear: (/* @__PURE__ */ new Date()).getFullYear().toString(),
|
|
481
|
+
nodeVersion: nodeVersion.replace("v", ""),
|
|
482
|
+
npmVersion: String(npmVersion),
|
|
483
|
+
projectDescription: inputDescription.charAt(0).toUpperCase() + inputDescription.slice(1),
|
|
484
|
+
projectName: inputName.toLowerCase(),
|
|
485
|
+
projectUrl: inputUrl,
|
|
486
|
+
repoId: `${repoOwner}/${repoName}`
|
|
487
|
+
};
|
|
488
|
+
},
|
|
489
|
+
key: "data",
|
|
490
|
+
label: label$2("Check and format input")
|
|
491
|
+
}).task({
|
|
492
|
+
async handler({ data }) {
|
|
493
|
+
const projectPath = resolve(process.cwd(), data.projectName);
|
|
494
|
+
await mkdir(projectPath);
|
|
495
|
+
process.chdir(projectPath);
|
|
496
|
+
},
|
|
497
|
+
label({ data }) {
|
|
498
|
+
return label$2(`Create \`${data.projectName}\` folder`);
|
|
499
|
+
}
|
|
500
|
+
}).task({
|
|
501
|
+
async handler({ data }) {
|
|
502
|
+
await helpers.exec("git init");
|
|
503
|
+
await helpers.exec(`git remote add origin ${data.projectUrl}`);
|
|
504
|
+
},
|
|
505
|
+
label: label$2("Initialize `git`")
|
|
506
|
+
}).task({
|
|
507
|
+
handler({ data, inputTemplate }) {
|
|
508
|
+
applyTemplate(inputTemplate, data);
|
|
509
|
+
},
|
|
510
|
+
label: label$2("Apply template")
|
|
511
|
+
}).task({
|
|
512
|
+
async handler({ data: { projectName }, inputTemplate }) {
|
|
513
|
+
await symlink(join(inputTemplate === "single-project" ? projectName : join("libraries", projectName), "README.md"), "./README.md");
|
|
514
|
+
},
|
|
515
|
+
label: label$2("Create a symlink to `README.md` file")
|
|
516
|
+
}).task({
|
|
517
|
+
async handler() {
|
|
518
|
+
await setPackageManager();
|
|
519
|
+
},
|
|
520
|
+
label: label$2("Set up the package manager")
|
|
521
|
+
}).task({
|
|
522
|
+
async handler({ data }) {
|
|
523
|
+
const localDevelopmentDependencies = ["quickbundle", "vitest"];
|
|
524
|
+
const globalDevelopmentDependencies = ["@adbayb/stack"];
|
|
525
|
+
try {
|
|
526
|
+
await helpers.exec(`pnpm add ${globalDevelopmentDependencies.join(" ")} --save-dev --ignore-workspace-root-check`);
|
|
527
|
+
await helpers.exec(`pnpm add ${localDevelopmentDependencies.join(" ")} --save-dev --filter ${data.projectName}`);
|
|
528
|
+
await helpers.exec("pnpm install");
|
|
529
|
+
} catch (error) {
|
|
530
|
+
throw createError("pnpm", error);
|
|
531
|
+
}
|
|
532
|
+
},
|
|
533
|
+
label: label$2("Install dependencies")
|
|
534
|
+
}).task({
|
|
535
|
+
async handler() {
|
|
536
|
+
await helpers.exec("stack install");
|
|
537
|
+
},
|
|
538
|
+
label: label$2("Run `stack install`")
|
|
539
|
+
}).task({
|
|
540
|
+
async handler() {
|
|
541
|
+
await helpers.exec("git add -A");
|
|
542
|
+
await helpers.exec("git commit -m \"chore: initial commit\"");
|
|
543
|
+
},
|
|
544
|
+
label: label$2("Commit")
|
|
545
|
+
}).task({ handler({ data }) {
|
|
546
|
+
botMessage({
|
|
547
|
+
description: `Run \`cd ./${data.projectName}\` and Enjoy 🚀`,
|
|
548
|
+
title: "The project was successfully created",
|
|
549
|
+
type: "success"
|
|
550
|
+
});
|
|
551
|
+
} });
|
|
552
|
+
};
|
|
553
|
+
const label$2 = (message) => `${message} 🔨`;
|
|
586
554
|
/**
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
555
|
+
* A simple template engine to evaluate dynamic expressions and apply side effets (such as hydrating a content with values from an input object) on impacted template files.
|
|
556
|
+
* @param template - The selected template.
|
|
557
|
+
* @param dataModel - Data model mapping the template expression key with its corresponding value.
|
|
558
|
+
* @example
|
|
559
|
+
* applyTemplate(
|
|
560
|
+
* { toReplace: "value" },
|
|
561
|
+
* );
|
|
562
|
+
*/
|
|
563
|
+
const applyTemplate = (template, dataModel) => {
|
|
564
|
+
const templateExtension = ".tmpl";
|
|
565
|
+
const templateRootPath = resolveFromStackDirectory(join("./templates", template));
|
|
566
|
+
const projectRootPath = resolveFromProjectDirectory("./");
|
|
567
|
+
const templateExpressionRegExp = /{{(.*?)}}/g;
|
|
568
|
+
const evaluate = (content) => {
|
|
569
|
+
return content.replaceAll(templateExpressionRegExp, (_, key) => dataModel[key] || "");
|
|
570
|
+
};
|
|
571
|
+
/** Copy the template before mutations. */
|
|
572
|
+
cpSync(templateRootPath, projectRootPath, {
|
|
573
|
+
force: true,
|
|
574
|
+
recursive: true
|
|
575
|
+
});
|
|
576
|
+
/** Template file mutations. */
|
|
577
|
+
new fdir().withBasePath().glob(`**/*${templateExtension}`).crawl(projectRootPath).sync().forEach((templateFilePath) => {
|
|
578
|
+
const projectFilePath = templateFilePath.slice(0, templateFilePath.lastIndexOf(templateExtension));
|
|
579
|
+
const content = evaluate(readFileSync(templateFilePath, "utf8"));
|
|
580
|
+
renameSync(templateFilePath, projectFilePath);
|
|
581
|
+
writeFileSync(projectFilePath, content, "utf8");
|
|
582
|
+
});
|
|
583
|
+
/** Template folder mutations. */
|
|
584
|
+
new fdir().withBasePath().onlyDirs().filter((path) => {
|
|
585
|
+
return templateExpressionRegExp.test(path);
|
|
586
|
+
}).crawl(projectRootPath).sync().toSorted((a, b) => b.length - a.length).forEach((templateFolderPath) => {
|
|
587
|
+
renameSync(templateFolderPath, templateFolderPath.replaceAll(templateExpressionRegExp, (_, dataModelKey) => {
|
|
588
|
+
return dataModel[dataModelKey];
|
|
589
|
+
}));
|
|
590
|
+
});
|
|
621
591
|
};
|
|
622
592
|
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
];
|
|
626
|
-
const fixFormatting = async (files)=>{
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
try {
|
|
644
|
-
return await helpers.exec(`prettier ${arguments_.join(" ")}`);
|
|
645
|
-
} catch (error) {
|
|
646
|
-
throw createError("prettier", error);
|
|
647
|
-
}
|
|
593
|
+
//#endregion
|
|
594
|
+
//#region src/commands/fix/fixFormatting.ts
|
|
595
|
+
const PRETTIER_IGNORE_FILES = ["pnpm-lock.yaml"];
|
|
596
|
+
const fixFormatting = async (files) => {
|
|
597
|
+
let prettierFiles = [];
|
|
598
|
+
if (files.length === 0) prettierFiles.push(`"**/!(${PRETTIER_IGNORE_FILES.join("|")})"`);
|
|
599
|
+
else {
|
|
600
|
+
prettierFiles = files.filter((file) => {
|
|
601
|
+
return !PRETTIER_IGNORE_FILES.some((filename) => file.endsWith(filename));
|
|
602
|
+
});
|
|
603
|
+
if (prettierFiles.length === 0) return;
|
|
604
|
+
}
|
|
605
|
+
const arguments_ = [...prettierFiles];
|
|
606
|
+
if (existsSync(resolveFromProjectDirectory(".gitignore"))) arguments_.push("--ignore-path .gitignore");
|
|
607
|
+
arguments_.push("--write", "--ignore-unknown", "--no-error-on-unmatched-pattern");
|
|
608
|
+
try {
|
|
609
|
+
return await helpers.exec(`prettier ${arguments_.join(" ")}`);
|
|
610
|
+
} catch (error) {
|
|
611
|
+
throw createError("prettier", error);
|
|
612
|
+
}
|
|
648
613
|
};
|
|
649
614
|
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
});
|
|
615
|
+
//#endregion
|
|
616
|
+
//#region src/commands/fix/fixLinter.ts
|
|
617
|
+
const fixLinter = eslint({ isFixMode: true });
|
|
653
618
|
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
};
|
|
682
|
-
const label$1 = (message)
|
|
619
|
+
//#endregion
|
|
620
|
+
//#region src/commands/fix/fix.ts
|
|
621
|
+
const createFixCommand = (program) => {
|
|
622
|
+
program.command({
|
|
623
|
+
description: "Fix auto-fixable issues",
|
|
624
|
+
name: "fix"
|
|
625
|
+
}).task({ handler(_, argv) {
|
|
626
|
+
logCheckableFiles(argv.operands);
|
|
627
|
+
} }).task({
|
|
628
|
+
async handler() {
|
|
629
|
+
await turbo("build", {
|
|
630
|
+
excludeExamples: true,
|
|
631
|
+
hasLiveOutput: false
|
|
632
|
+
});
|
|
633
|
+
},
|
|
634
|
+
label: label$1("Prepare the project")
|
|
635
|
+
}).task({
|
|
636
|
+
async handler(_, argv) {
|
|
637
|
+
await fixLinter(argv.operands);
|
|
638
|
+
},
|
|
639
|
+
label: label$1("Fix linter issues")
|
|
640
|
+
}).task({
|
|
641
|
+
async handler(_, argv) {
|
|
642
|
+
await fixFormatting(argv.operands);
|
|
643
|
+
},
|
|
644
|
+
label: label$1("Fix formatting issues")
|
|
645
|
+
});
|
|
646
|
+
};
|
|
647
|
+
const label$1 = (message) => `${message} 🚑`;
|
|
683
648
|
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
};
|
|
702
|
-
const label = (message)
|
|
703
|
-
const installGitHook = async (hook, content)=>{
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
649
|
+
//#endregion
|
|
650
|
+
//#region src/commands/install.ts
|
|
651
|
+
const createInstallCommand = (program) => {
|
|
652
|
+
program.command({
|
|
653
|
+
description: "Install required setup",
|
|
654
|
+
name: "install"
|
|
655
|
+
}).task({
|
|
656
|
+
async handler() {
|
|
657
|
+
await installGitHook("pre-commit", `${getStackCommand(`fix $(node -e 'console.log(require("child_process").execSync("git status --porcelain", {encoding: "utf8"}).split(/${String.raw`\n|\r\n`}/).filter(Boolean).map(item => item.split(" ").at(-1)).join(" "))')`)} && git add -A`);
|
|
658
|
+
},
|
|
659
|
+
label: label("Install `git.pre-commit` hook")
|
|
660
|
+
}).task({
|
|
661
|
+
async handler() {
|
|
662
|
+
await installGitHook("commit-msg", getStackCommand("check --filter commit"));
|
|
663
|
+
},
|
|
664
|
+
label: label("Install `git.commit-msg` hook")
|
|
665
|
+
});
|
|
666
|
+
};
|
|
667
|
+
const label = (message) => `${message} 📲`;
|
|
668
|
+
const installGitHook = async (hook, content) => {
|
|
669
|
+
const filename = resolveFromProjectDirectory(`.git/hooks/${hook}`);
|
|
670
|
+
await writeFile(filename, content);
|
|
671
|
+
return chmod(filename, "0755");
|
|
707
672
|
};
|
|
708
673
|
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
674
|
+
//#endregion
|
|
675
|
+
//#region src/commands/release.ts
|
|
676
|
+
const createReleaseCommand = (program) => {
|
|
677
|
+
program.command({
|
|
678
|
+
description: "Log, version, and publish package(s)",
|
|
679
|
+
name: "release"
|
|
680
|
+
}).option({
|
|
681
|
+
description: "Add a new changelog entry",
|
|
682
|
+
key: "log",
|
|
683
|
+
name: "log"
|
|
684
|
+
}).option({
|
|
685
|
+
description: "Add an empty changelog entry",
|
|
686
|
+
key: "emptyLog",
|
|
687
|
+
name: "empty-log"
|
|
688
|
+
}).option({
|
|
689
|
+
description: "Bump the package(s) version",
|
|
690
|
+
key: "tag",
|
|
691
|
+
name: "tag"
|
|
692
|
+
}).option({
|
|
693
|
+
description: "Publish package(s) to the registry",
|
|
694
|
+
key: "publish",
|
|
695
|
+
name: "publish"
|
|
696
|
+
}).task({
|
|
697
|
+
async handler() {
|
|
698
|
+
helpers.message("New changelog entry\n");
|
|
699
|
+
await changeset("changeset");
|
|
700
|
+
},
|
|
701
|
+
skip: ifNotEqualTo("log")
|
|
702
|
+
}).task({
|
|
703
|
+
async handler() {
|
|
704
|
+
helpers.message("New empty changelog entry\n");
|
|
705
|
+
await changeset("changeset --empty");
|
|
706
|
+
},
|
|
707
|
+
skip: ifNotEqualTo("emptyLog")
|
|
708
|
+
}).task({
|
|
709
|
+
async handler() {
|
|
710
|
+
helpers.message("Bumping the package(s) version\n");
|
|
711
|
+
await changeset("changeset version && pnpm install --no-frozen-lockfile");
|
|
712
|
+
},
|
|
713
|
+
skip: ifNotEqualTo("tag")
|
|
714
|
+
}).task({
|
|
715
|
+
async handler() {
|
|
716
|
+
helpers.message("Publishing package(s) to the registry\n");
|
|
717
|
+
await changeset("stack build && pnpm changeset publish");
|
|
718
|
+
},
|
|
719
|
+
skip: ifNotEqualTo("publish")
|
|
720
|
+
});
|
|
721
|
+
};
|
|
722
|
+
const ifNotEqualTo = (validOption) => (context) => {
|
|
723
|
+
return !context[validOption];
|
|
724
|
+
};
|
|
758
725
|
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
726
|
+
//#endregion
|
|
727
|
+
//#region src/commands/start.ts
|
|
728
|
+
const createStartCommand = (program) => {
|
|
729
|
+
program.command({
|
|
730
|
+
description: "Start the project in production mode",
|
|
731
|
+
name: "start"
|
|
732
|
+
}).task({ async handler() {
|
|
733
|
+
await turbo("start");
|
|
734
|
+
} });
|
|
768
735
|
};
|
|
769
736
|
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
737
|
+
//#endregion
|
|
738
|
+
//#region src/commands/test.ts
|
|
739
|
+
const createTestCommand = (program) => {
|
|
740
|
+
program.command({
|
|
741
|
+
description: "Test the code execution",
|
|
742
|
+
name: "test"
|
|
743
|
+
}).task({ async handler() {
|
|
744
|
+
await turbo("test");
|
|
745
|
+
} });
|
|
779
746
|
};
|
|
780
747
|
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
748
|
+
//#endregion
|
|
749
|
+
//#region src/commands/watch.ts
|
|
750
|
+
const createWatchCommand = (program) => {
|
|
751
|
+
program.command({
|
|
752
|
+
description: "Build and start the project in development mode",
|
|
753
|
+
name: "watch"
|
|
754
|
+
}).task({ async handler() {
|
|
755
|
+
await turbo("watch");
|
|
756
|
+
} });
|
|
790
757
|
};
|
|
791
758
|
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
759
|
+
//#endregion
|
|
760
|
+
//#region src/index.ts
|
|
761
|
+
const createProgram = (...commandFactories) => {
|
|
762
|
+
const program = termost({
|
|
763
|
+
description: "Toolbox to easily scaffold and maintain a project",
|
|
764
|
+
name: "stack",
|
|
765
|
+
onException() {
|
|
766
|
+
botMessage({
|
|
767
|
+
description: "Keep calm and carry on with some coffee ☕️",
|
|
768
|
+
title: "Oops, an error occurred",
|
|
769
|
+
type: "error"
|
|
770
|
+
});
|
|
771
|
+
},
|
|
772
|
+
version
|
|
773
|
+
});
|
|
774
|
+
for (const commandBuilder of commandFactories) commandBuilder(program);
|
|
808
775
|
};
|
|
809
776
|
createProgram(createCreateCommand, createInstallCommand, createCleanCommand, createCheckCommand, createFixCommand, createStartCommand, createBuildCommand, createWatchCommand, createTestCommand, createReleaseCommand);
|
|
777
|
+
|
|
778
|
+
//#endregion
|