@minimalstuff/tooling 0.5.0 → 0.7.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/README.md +144 -94
- package/dist/cli/index.d.mts +1 -8
- package/dist/cli/index.mjs +445 -163
- package/dist/knip/knip.d.mts +16 -0
- package/dist/knip/knip.mjs +70 -0
- package/dist/oxc/fmt.d.mts +10 -9
- package/dist/oxc/fmt.mjs +33 -13
- package/dist/oxc/lint.d.mts +13 -12
- package/dist/oxc/lint.mjs +81 -79
- package/dist/shared-B_NHkjil.mjs +16 -0
- package/package.json +40 -26
- package/preset/.editorconfig +4 -0
- package/dist/shared-CoSP_HvN.mjs +0 -11
package/dist/cli/index.mjs
CHANGED
|
@@ -1,199 +1,481 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import color from "picocolors";
|
|
3
|
-
import * as p from "@clack/prompts";
|
|
4
2
|
import { cancel, confirm, intro, isCancel, log, multiselect } from "@clack/prompts";
|
|
5
|
-
import { join } from "node:path";
|
|
6
3
|
import { existsSync } from "node:fs";
|
|
7
|
-
import {
|
|
8
|
-
import
|
|
4
|
+
import { basename, dirname, join } from "node:path";
|
|
5
|
+
import color from "picocolors";
|
|
6
|
+
import { mkdir, readFile, readdir, writeFile } from "node:fs/promises";
|
|
9
7
|
import { parse, printParseErrorCode } from "jsonc-parser";
|
|
10
8
|
import { fileURLToPath } from "node:url";
|
|
11
|
-
|
|
9
|
+
import { installPackage } from "@antfu/install-pkg";
|
|
10
|
+
// src/constants.ts
|
|
12
11
|
const TOOLING_PACKAGE_NAME = "@minimalstuff/tooling";
|
|
13
12
|
const OXC_LINT_IMPORT = `${TOOLING_PACKAGE_NAME}/oxc/lint`;
|
|
14
13
|
const OXC_FMT_IMPORT = `${TOOLING_PACKAGE_NAME}/oxc/fmt`;
|
|
15
|
-
`${TOOLING_PACKAGE_NAME}`;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
14
|
+
const KNIP_IMPORT = `${TOOLING_PACKAGE_NAME}/knip`;
|
|
15
|
+
const KNIP_CONFIG_FILE_NAME = "knip.ts";
|
|
16
|
+
const KNIP_CONFIG_FILE_NAMES = [
|
|
17
|
+
KNIP_CONFIG_FILE_NAME,
|
|
18
|
+
"knip.json",
|
|
19
|
+
"knip.jsonc",
|
|
20
|
+
".knip.json",
|
|
21
|
+
".knip.jsonc",
|
|
22
|
+
"knip.config.ts"
|
|
23
|
+
];
|
|
24
|
+
const LEADING_WHITESPACE_PATTERN = /^([ \t]+)\S/m;
|
|
25
|
+
var InvalidJsonFileError = class extends Error {
|
|
26
|
+
constructor(filePath, reason) {
|
|
27
|
+
super(`Invalid JSON in ${filePath}: ${reason}`);
|
|
28
|
+
this.name = "InvalidJsonFileError";
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
function detectIndentation(raw) {
|
|
32
|
+
const match = LEADING_WHITESPACE_PATTERN.exec(raw);
|
|
33
|
+
if (!match?.[1]) return " ";
|
|
34
|
+
const leadingWhitespace = match[1];
|
|
35
|
+
return leadingWhitespace.startsWith(" ") ? " " : leadingWhitespace;
|
|
36
|
+
}
|
|
37
|
+
function parseJsonObject(raw, filePath) {
|
|
38
|
+
const parseErrors = [];
|
|
39
|
+
const parsed = parse(raw, parseErrors, {
|
|
40
|
+
allowTrailingComma: true,
|
|
41
|
+
allowEmptyContent: true
|
|
42
|
+
});
|
|
43
|
+
if (parseErrors.length > 0) throw new InvalidJsonFileError(filePath, parseErrors.map((error) => `${printParseErrorCode(error.error)} @${error.offset}`).join(", "));
|
|
44
|
+
if (parsed === void 0) return {};
|
|
45
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) throw new InvalidJsonFileError(filePath, "expected a JSON object");
|
|
46
|
+
return parsed;
|
|
47
|
+
}
|
|
48
|
+
function stringifyJson(content, indentation) {
|
|
49
|
+
return `${JSON.stringify(content, null, indentation)}\n`;
|
|
50
|
+
}
|
|
51
|
+
async function readJsonDocument(filePath) {
|
|
52
|
+
const raw = await readFile(filePath, "utf-8");
|
|
53
|
+
return {
|
|
54
|
+
content: parseJsonObject(raw, filePath),
|
|
55
|
+
indentation: detectIndentation(raw)
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
async function writeJsonDocument(filePath, document) {
|
|
59
|
+
await writeFile(filePath, stringifyJson(document.content, document.indentation), "utf-8");
|
|
60
|
+
}
|
|
61
|
+
const WORKSPACE_DIRECTORIES = ["apps", "packages"];
|
|
62
|
+
const REACT_PACKAGES = [
|
|
63
|
+
"react",
|
|
64
|
+
"react-dom",
|
|
65
|
+
"@types/react",
|
|
66
|
+
"@types/react-dom"
|
|
67
|
+
];
|
|
68
|
+
const VITEST_PACKAGES = ["vitest"];
|
|
69
|
+
const ADONISJS_SCOPE_PREFIX = "@adonisjs/";
|
|
70
|
+
function collectDependencyNames(manifests) {
|
|
71
|
+
const names = /* @__PURE__ */ new Set();
|
|
72
|
+
for (const manifest of manifests) for (const dependencies of [
|
|
73
|
+
manifest.dependencies,
|
|
74
|
+
manifest.devDependencies,
|
|
75
|
+
manifest.peerDependencies
|
|
76
|
+
]) for (const name of Object.keys(dependencies ?? {})) names.add(name);
|
|
77
|
+
return names;
|
|
78
|
+
}
|
|
79
|
+
function detectPresetFlags(manifests) {
|
|
80
|
+
const names = collectDependencyNames(manifests);
|
|
81
|
+
const hasAnyOf = (candidates) => candidates.some((candidate) => names.has(candidate));
|
|
82
|
+
return {
|
|
83
|
+
react: hasAnyOf(REACT_PACKAGES),
|
|
84
|
+
vitest: hasAnyOf(VITEST_PACKAGES),
|
|
85
|
+
adonisjs: [...names].some((name) => name.startsWith(ADONISJS_SCOPE_PREFIX))
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function toDetectedPresetNames(flags) {
|
|
89
|
+
return Object.keys(flags).filter((name) => flags[name]);
|
|
90
|
+
}
|
|
91
|
+
function toPresetFlags(names) {
|
|
92
|
+
return {
|
|
93
|
+
react: names.includes("react"),
|
|
94
|
+
adonisjs: names.includes("adonisjs"),
|
|
95
|
+
vitest: names.includes("vitest")
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
async function listWorkspaceManifestLocations(cwd) {
|
|
99
|
+
const locations = [];
|
|
100
|
+
for (const workspaceDirectory of WORKSPACE_DIRECTORIES) {
|
|
101
|
+
const directoryPath = join(cwd, workspaceDirectory);
|
|
102
|
+
if (!existsSync(directoryPath)) continue;
|
|
103
|
+
const entries = await readdir(directoryPath, { withFileTypes: true });
|
|
104
|
+
for (const entry of entries) {
|
|
105
|
+
if (!entry.isDirectory()) continue;
|
|
106
|
+
const manifestPath = join(directoryPath, entry.name, "package.json");
|
|
107
|
+
if (!existsSync(manifestPath)) continue;
|
|
108
|
+
locations.push({
|
|
109
|
+
workspacePath: `${workspaceDirectory}/${entry.name}`,
|
|
110
|
+
manifestPath
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return locations;
|
|
115
|
+
}
|
|
116
|
+
async function readWorkspaceManifests(cwd) {
|
|
117
|
+
const rootManifestPath = join(cwd, "package.json");
|
|
118
|
+
if (!existsSync(rootManifestPath)) throw new Error(`No package.json found in ${cwd}`);
|
|
119
|
+
const locations = [{
|
|
120
|
+
workspacePath: ".",
|
|
121
|
+
manifestPath: rootManifestPath
|
|
122
|
+
}, ...await listWorkspaceManifestLocations(cwd)];
|
|
123
|
+
return Promise.all(locations.map(async ({ workspacePath, manifestPath }) => ({
|
|
124
|
+
workspacePath,
|
|
125
|
+
manifest: (await readJsonDocument(manifestPath)).content
|
|
126
|
+
})));
|
|
127
|
+
}
|
|
128
|
+
function findAdonisjsWorkspacePaths(workspaceManifests) {
|
|
129
|
+
return workspaceManifests.filter(({ manifest }) => detectPresetFlags([manifest]).adonisjs).map(({ workspacePath }) => workspacePath);
|
|
130
|
+
}
|
|
131
|
+
async function detectPresets(cwd) {
|
|
132
|
+
return detectPresetFlags((await readWorkspaceManifests(cwd)).map(({ manifest }) => manifest));
|
|
133
|
+
}
|
|
134
|
+
async function detectAdonisjsWorkspacePaths(cwd) {
|
|
135
|
+
return findAdonisjsWorkspacePaths(await readWorkspaceManifests(cwd));
|
|
136
|
+
}
|
|
137
|
+
// src/cli/prompts.ts
|
|
138
|
+
function exitWhenCancelled(answer) {
|
|
139
|
+
if (!isCancel(answer)) return answer;
|
|
140
|
+
cancel("Cancelled");
|
|
141
|
+
return process.exit(0);
|
|
142
|
+
}
|
|
143
|
+
// src/cli/prompt_presets.ts
|
|
144
|
+
const TOOLS_USING_PRESETS = ["oxc", "knip"];
|
|
145
|
+
const NO_PRESETS = toPresetFlags([]);
|
|
146
|
+
async function promptPresets(cwd) {
|
|
147
|
+
const detectedPresets = await detectPresets(cwd);
|
|
148
|
+
return toPresetFlags(exitWhenCancelled(await multiselect({
|
|
149
|
+
message: "Select presets to enable",
|
|
150
|
+
options: [
|
|
151
|
+
{
|
|
152
|
+
value: "react",
|
|
153
|
+
label: "React"
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
value: "adonisjs",
|
|
157
|
+
label: "AdonisJS"
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
value: "vitest",
|
|
161
|
+
label: "Vitest"
|
|
162
|
+
}
|
|
163
|
+
],
|
|
164
|
+
initialValues: toDetectedPresetNames(detectedPresets),
|
|
165
|
+
required: false
|
|
166
|
+
})));
|
|
167
|
+
}
|
|
168
|
+
async function promptPresetsForTools(tools, cwd) {
|
|
169
|
+
if (!TOOLS_USING_PRESETS.some((tool) => tools.includes(tool))) return NO_PRESETS;
|
|
170
|
+
return promptPresets(cwd);
|
|
171
|
+
}
|
|
172
|
+
// src/cli/write_generated_file.ts
|
|
173
|
+
async function writeGeneratedFile(filePath, content) {
|
|
174
|
+
const fileName = basename(filePath);
|
|
175
|
+
if (existsSync(filePath)) {
|
|
176
|
+
if (!exitWhenCancelled(await confirm({ message: `${fileName} already exists. Overwrite it?` }))) {
|
|
177
|
+
log.info(`Skipped ${fileName}`);
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
await writeFile(filePath, content, "utf-8");
|
|
182
|
+
log.success(`${fileName} written`);
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
// src/cli/update_editorconfig.ts
|
|
186
|
+
const presetPath = fileURLToPath(new URL("../../preset/.editorconfig", import.meta.url));
|
|
187
|
+
async function updateEditorConfig(result) {
|
|
188
|
+
if (!result.tools.includes("editorconfig")) return;
|
|
189
|
+
const content = await readFile(presetPath, "utf-8");
|
|
190
|
+
await writeGeneratedFile(join(process.cwd(), ".editorconfig"), content);
|
|
191
|
+
}
|
|
192
|
+
// src/cli/preset_config_file.ts
|
|
193
|
+
const INDENT = " ";
|
|
194
|
+
const INDENT_WIDTH = 2;
|
|
195
|
+
const PRINT_WIDTH = 80;
|
|
196
|
+
function quote(text) {
|
|
197
|
+
return `'${text}'`;
|
|
198
|
+
}
|
|
199
|
+
function buildArrayOptionLines(key, items) {
|
|
200
|
+
const quotedItems = items.map(quote);
|
|
201
|
+
const inlineLine = `${INDENT}${key}: [${quotedItems.join(", ")}],`;
|
|
202
|
+
if (inlineLine.length - 1 + INDENT_WIDTH <= PRINT_WIDTH) return [inlineLine];
|
|
203
|
+
return [
|
|
204
|
+
`${INDENT}${key}: [`,
|
|
205
|
+
...quotedItems.map((item) => `${INDENT}${INDENT}${item},`),
|
|
206
|
+
`${INDENT}],`
|
|
207
|
+
];
|
|
208
|
+
}
|
|
209
|
+
function buildOptionLines(option) {
|
|
210
|
+
if (typeof option.value === "string") return [`${INDENT}${option.key}: ${option.value},`];
|
|
211
|
+
return buildArrayOptionLines(option.key, option.value);
|
|
212
|
+
}
|
|
213
|
+
function buildPresetCall(options) {
|
|
214
|
+
if (options.length === 0) return "minimalstuffPreset()";
|
|
215
|
+
return `minimalstuffPreset({\n${options.flatMap(buildOptionLines).join("\n")}\n})`;
|
|
216
|
+
}
|
|
217
|
+
function buildPresetConfigFile(importPath, options) {
|
|
218
|
+
return `import { minimalstuffPreset } from '${importPath}';\n\nexport default ${buildPresetCall(options)};\n`;
|
|
219
|
+
}
|
|
220
|
+
// src/cli/knip_config_content.ts
|
|
221
|
+
function toKnipAdonisjsOption(adonisjsWorkspacePaths) {
|
|
222
|
+
if (adonisjsWorkspacePaths.includes(".")) return true;
|
|
223
|
+
if (adonisjsWorkspacePaths.length === 0) return void 0;
|
|
224
|
+
return adonisjsWorkspacePaths;
|
|
225
|
+
}
|
|
226
|
+
function toPresetOptions$1(adonisjs) {
|
|
227
|
+
if (adonisjs === void 0) return [];
|
|
228
|
+
if (adonisjs === true) return [{
|
|
229
|
+
key: "adonisjs",
|
|
230
|
+
value: "true"
|
|
231
|
+
}];
|
|
232
|
+
return [{
|
|
233
|
+
key: "adonisjs",
|
|
234
|
+
value: adonisjs
|
|
235
|
+
}];
|
|
236
|
+
}
|
|
237
|
+
function buildKnipConfigContent(adonisjs) {
|
|
238
|
+
return buildPresetConfigFile(KNIP_IMPORT, toPresetOptions$1(adonisjs));
|
|
239
|
+
}
|
|
240
|
+
// src/cli/update_knip.ts
|
|
241
|
+
const KNIP_PACKAGES = ["knip", TOOLING_PACKAGE_NAME];
|
|
242
|
+
function warnAboutConflictingConfigs$1(cwd) {
|
|
243
|
+
const conflictingFileNames = KNIP_CONFIG_FILE_NAMES.filter((fileName) => fileName !== "knip.ts" && existsSync(join(cwd, fileName)));
|
|
244
|
+
for (const fileName of conflictingFileNames) log.warn(`${fileName} found: it will conflict with the generated ${KNIP_CONFIG_FILE_NAME}. Consider removing it.`);
|
|
245
|
+
}
|
|
246
|
+
async function detectAdonisjsOption(cwd, result) {
|
|
247
|
+
if (!result.presetFlags.adonisjs) return void 0;
|
|
248
|
+
return toKnipAdonisjsOption(await detectAdonisjsWorkspacePaths(cwd));
|
|
249
|
+
}
|
|
250
|
+
async function updateKnip(result) {
|
|
251
|
+
if (!result.tools.includes("knip")) return;
|
|
252
|
+
const cwd = process.cwd();
|
|
253
|
+
const adonisjsOption = await detectAdonisjsOption(cwd, result);
|
|
254
|
+
warnAboutConflictingConfigs$1(cwd);
|
|
255
|
+
await writeGeneratedFile(join(cwd, KNIP_CONFIG_FILE_NAME), buildKnipConfigContent(adonisjsOption));
|
|
256
|
+
await installPackage(KNIP_PACKAGES, {
|
|
257
|
+
dev: true,
|
|
258
|
+
cwd
|
|
259
|
+
});
|
|
260
|
+
log.success("Knip configured");
|
|
261
|
+
}
|
|
262
|
+
// src/cli/oxc_config_content.ts
|
|
263
|
+
const OXLINT_PRESET_ORDER = [
|
|
264
|
+
"adonisjs",
|
|
265
|
+
"react",
|
|
266
|
+
"vitest"
|
|
267
|
+
];
|
|
268
|
+
function toPresetOptions(enabledNames) {
|
|
269
|
+
return enabledNames.map((name) => ({
|
|
270
|
+
key: name,
|
|
271
|
+
value: "true"
|
|
272
|
+
}));
|
|
273
|
+
}
|
|
274
|
+
function buildOxlintConfigContent(flags) {
|
|
275
|
+
const enabledNames = OXLINT_PRESET_ORDER.filter((name) => flags[name]);
|
|
276
|
+
return buildPresetConfigFile(OXC_LINT_IMPORT, toPresetOptions(enabledNames));
|
|
277
|
+
}
|
|
278
|
+
function buildOxfmtConfigContent(flags) {
|
|
279
|
+
const enabledNames = flags.adonisjs ? ["adonisjs"] : [];
|
|
280
|
+
return buildPresetConfigFile(OXC_FMT_IMPORT, toPresetOptions(enabledNames));
|
|
281
|
+
}
|
|
282
|
+
// src/cli/vscode_settings.ts
|
|
283
|
+
const VSCODE_SETTINGS = {
|
|
60
284
|
"oxc.typeAware": true,
|
|
61
285
|
"editor.defaultFormatter": "oxc.oxc-vscode"
|
|
62
286
|
};
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
287
|
+
function mergeVscodeSettings(existingSettings, settingsToAdd = VSCODE_SETTINGS) {
|
|
288
|
+
const merged = { ...existingSettings };
|
|
289
|
+
for (const [key, value] of Object.entries(settingsToAdd)) if (!(key in merged)) merged[key] = value;
|
|
290
|
+
return merged;
|
|
291
|
+
}
|
|
292
|
+
async function updateVscodeSettings(cwd) {
|
|
293
|
+
const vscodeDirectory = join(cwd, ".vscode");
|
|
294
|
+
const settingsPath = join(vscodeDirectory, "settings.json");
|
|
295
|
+
await mkdir(vscodeDirectory, { recursive: true });
|
|
296
|
+
const raw = existsSync(settingsPath) ? await readFile(settingsPath, "utf-8") : "";
|
|
297
|
+
const indentation = raw ? detectIndentation(raw) : " ";
|
|
298
|
+
const settings = mergeVscodeSettings(parseJsonObject(raw, settingsPath));
|
|
299
|
+
await writeFile(settingsPath, stringifyJson(settings, indentation), "utf-8");
|
|
300
|
+
log.success(".vscode/settings.json updated");
|
|
301
|
+
}
|
|
302
|
+
// src/cli/update_oxc.ts
|
|
303
|
+
const OXC_PACKAGES = [
|
|
72
304
|
"oxfmt",
|
|
73
305
|
"oxlint",
|
|
74
306
|
"oxlint-tsgolint",
|
|
75
307
|
TOOLING_PACKAGE_NAME
|
|
76
308
|
];
|
|
309
|
+
const OXLINT_CONFIG_FILE = "oxlint.config.ts";
|
|
310
|
+
const OXFMT_CONFIG_FILE = "oxfmt.config.ts";
|
|
311
|
+
const CONFLICTING_OXLINT_CONFIG_FILES = [".oxlintrc.json", ".oxlintrc.jsonc"];
|
|
312
|
+
const CONFLICTING_OXFMT_CONFIG_FILES = [".oxfmtrc.json", ".oxfmtrc.jsonc"];
|
|
313
|
+
function warnAboutConflictingConfigs(cwd) {
|
|
314
|
+
const conflictingFiles = [...CONFLICTING_OXLINT_CONFIG_FILES, ...CONFLICTING_OXFMT_CONFIG_FILES].filter((fileName) => existsSync(join(cwd, fileName)));
|
|
315
|
+
for (const fileName of conflictingFiles) log.warn(`${fileName} found: it will conflict with the generated .ts config. Consider removing it.`);
|
|
316
|
+
}
|
|
77
317
|
async function updateOxc(result) {
|
|
78
318
|
if (!result.tools.includes("oxc")) return;
|
|
79
319
|
const cwd = process.cwd();
|
|
80
|
-
|
|
81
|
-
await
|
|
82
|
-
await
|
|
83
|
-
await updateVscodeSettings(cwd);
|
|
84
|
-
await
|
|
85
|
-
await installPackage(oxcPackages, {
|
|
320
|
+
warnAboutConflictingConfigs(cwd);
|
|
321
|
+
await writeGeneratedFile(join(cwd, OXLINT_CONFIG_FILE), buildOxlintConfigContent(result.presetFlags));
|
|
322
|
+
await writeGeneratedFile(join(cwd, OXFMT_CONFIG_FILE), buildOxfmtConfigContent(result.presetFlags));
|
|
323
|
+
if (result.tools.includes("vscode")) await updateVscodeSettings(cwd);
|
|
324
|
+
await installPackage(OXC_PACKAGES, {
|
|
86
325
|
dev: true,
|
|
87
326
|
cwd
|
|
88
327
|
});
|
|
89
|
-
|
|
328
|
+
log.success("OXC configured (oxlint + oxfmt)");
|
|
90
329
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
330
|
+
// src/cli/package_scripts.ts
|
|
331
|
+
const OXC_SCRIPTS = {
|
|
332
|
+
lint: "oxlint",
|
|
333
|
+
"lint:fix": "oxlint --fix",
|
|
334
|
+
format: "oxfmt && oxlint --fix",
|
|
335
|
+
"format:check": "oxfmt --check",
|
|
336
|
+
typecheck: "tsc --noEmit"
|
|
337
|
+
};
|
|
338
|
+
const KNIP_SCRIPTS = { knip: "knip" };
|
|
339
|
+
const OXC_CHECK_SCRIPT_NAMES = [
|
|
340
|
+
"lint",
|
|
341
|
+
"format:check",
|
|
342
|
+
"typecheck"
|
|
343
|
+
];
|
|
344
|
+
const KNIP_CHECK_SCRIPT_NAMES = ["knip"];
|
|
345
|
+
function toRunCommand(scriptName) {
|
|
346
|
+
return `pnpm ${scriptName}`;
|
|
94
347
|
}
|
|
95
|
-
function
|
|
96
|
-
|
|
97
|
-
return "react" in deps || "react-dom" in deps || "@types/react" in deps || "@types/react-dom" in deps;
|
|
348
|
+
function buildCheckScript(scriptNames) {
|
|
349
|
+
return { check: scriptNames.map(toRunCommand).join(" && ") };
|
|
98
350
|
}
|
|
99
|
-
|
|
100
|
-
const
|
|
101
|
-
const
|
|
351
|
+
function buildScripts(tools) {
|
|
352
|
+
const hasOxc = tools.includes("oxc");
|
|
353
|
+
const hasKnip = tools.includes("knip");
|
|
354
|
+
const checkScriptNames = [...hasOxc ? OXC_CHECK_SCRIPT_NAMES : [], ...hasKnip ? KNIP_CHECK_SCRIPT_NAMES : []];
|
|
102
355
|
return {
|
|
103
|
-
|
|
104
|
-
|
|
356
|
+
...hasOxc && OXC_SCRIPTS,
|
|
357
|
+
...hasKnip && KNIP_SCRIPTS,
|
|
358
|
+
...hasOxc && buildCheckScript(checkScriptNames)
|
|
105
359
|
};
|
|
106
360
|
}
|
|
107
|
-
|
|
108
|
-
const
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const parsed = parse(content, parseErrors, {
|
|
116
|
-
allowTrailingComma: true,
|
|
117
|
-
allowEmptyContent: true
|
|
118
|
-
});
|
|
119
|
-
if (parseErrors.length > 0) {
|
|
120
|
-
const detail = parseErrors.map((e) => `${printParseErrorCode(e.error)} @${e.offset}`).join(", ");
|
|
121
|
-
throw new SyntaxError(`Invalid JSONC in ${settingsPath}: ${detail}`);
|
|
361
|
+
function mergeScripts(existingScripts, scriptsToAdd) {
|
|
362
|
+
const scripts = { ...existingScripts };
|
|
363
|
+
const addedNames = [];
|
|
364
|
+
const skippedNames = [];
|
|
365
|
+
for (const [name, command] of Object.entries(scriptsToAdd)) {
|
|
366
|
+
if (name in scripts) {
|
|
367
|
+
skippedNames.push(name);
|
|
368
|
+
continue;
|
|
122
369
|
}
|
|
123
|
-
|
|
370
|
+
scripts[name] = command;
|
|
371
|
+
addedNames.push(name);
|
|
124
372
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const raw = await readFile(pkgPath, "utf-8");
|
|
131
|
-
const pkg = JSON.parse(raw);
|
|
132
|
-
pkg.scripts ??= {};
|
|
133
|
-
for (const [key, value] of Object.entries(oxcScripts)) pkg.scripts[key] = value;
|
|
134
|
-
await writeFile(pkgPath, JSON.stringify(pkg, null, 2));
|
|
135
|
-
}
|
|
136
|
-
//#endregion
|
|
137
|
-
//#region package.json
|
|
138
|
-
var version = "0.5.0";
|
|
139
|
-
//#endregion
|
|
140
|
-
//#region src/cli/update_pkg.json.ts
|
|
141
|
-
async function updatePkgJson(_) {
|
|
142
|
-
const pathPackageJSON = join(process.cwd(), "package.json");
|
|
143
|
-
const raw = await readFile(pathPackageJSON, "utf-8");
|
|
144
|
-
const userPkgJson = JSON.parse(raw);
|
|
145
|
-
userPkgJson.devDependencies ??= {};
|
|
146
|
-
userPkgJson.devDependencies[TOOLING_PACKAGE_NAME] = `^${version}`;
|
|
147
|
-
userPkgJson.scripts ??= {};
|
|
148
|
-
userPkgJson.scripts.lint = "oxlint .";
|
|
149
|
-
userPkgJson.scripts["lint:fix"] = "oxlint . --fix --fix-suggestions --fix-dangerously";
|
|
150
|
-
userPkgJson.scripts.format = "pnpm lint:fix; oxfmt .";
|
|
151
|
-
userPkgJson.scripts["format:check"] = "oxfmt . --check";
|
|
152
|
-
userPkgJson.scripts.typecheck = "tsc --noEmit";
|
|
153
|
-
userPkgJson.scripts.check = "pnpm run lint && pnpm run format:check && pnpm run typecheck";
|
|
154
|
-
await writeFile(pathPackageJSON, JSON.stringify(userPkgJson, null, 2));
|
|
155
|
-
p.log.success("Package.json updated 🤠");
|
|
156
|
-
}
|
|
157
|
-
//#endregion
|
|
158
|
-
//#region src/cli/update_editorconfig.ts
|
|
159
|
-
const presetPath = fileURLToPath(new URL("../../preset/.editorconfig", import.meta.url));
|
|
160
|
-
async function updateEditorConfig(result) {
|
|
161
|
-
if (!result.tools.includes("editorconfig")) return;
|
|
162
|
-
const cwd = process.cwd();
|
|
163
|
-
const content = await readFile(presetPath, "utf-8");
|
|
164
|
-
await writeFile(join(cwd, ".editorconfig"), content, "utf-8");
|
|
165
|
-
p.log.success(".editorconfig updated");
|
|
373
|
+
return {
|
|
374
|
+
scripts,
|
|
375
|
+
addedNames,
|
|
376
|
+
skippedNames
|
|
377
|
+
};
|
|
166
378
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
379
|
+
// src/cli/update_package_json_scripts.ts
|
|
380
|
+
async function addMissingScripts(cwd, scriptsToAdd) {
|
|
381
|
+
const manifestPath = join(cwd, "package.json");
|
|
382
|
+
const document = await readJsonDocument(manifestPath);
|
|
383
|
+
const manifest = document.content;
|
|
384
|
+
const merge = mergeScripts(manifest.scripts, scriptsToAdd);
|
|
385
|
+
if (merge.addedNames.length === 0) return merge;
|
|
386
|
+
await writeJsonDocument(manifestPath, {
|
|
387
|
+
...document,
|
|
388
|
+
content: {
|
|
389
|
+
...manifest,
|
|
390
|
+
scripts: merge.scripts
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
return merge;
|
|
394
|
+
}
|
|
395
|
+
// src/cli/update_package_scripts.ts
|
|
396
|
+
async function updatePackageScripts(result) {
|
|
397
|
+
const scripts = buildScripts(result.tools);
|
|
398
|
+
if (Object.keys(scripts).length === 0) return;
|
|
399
|
+
const merge = await addMissingScripts(process.cwd(), scripts);
|
|
400
|
+
if (merge.addedNames.length > 0) log.success(`package.json scripts added: ${merge.addedNames.join(", ")}`);
|
|
401
|
+
if (merge.skippedNames.length > 0) log.info(`package.json scripts already defined, kept: ${merge.skippedNames.join(", ")}`);
|
|
402
|
+
}
|
|
403
|
+
// src/cli/index.ts
|
|
404
|
+
const DEFAULT_SELECTED_TOOLS = [
|
|
405
|
+
"editorconfig",
|
|
406
|
+
"oxc",
|
|
407
|
+
"knip",
|
|
408
|
+
"vscode"
|
|
409
|
+
];
|
|
410
|
+
function findGitRoot(startDirectory) {
|
|
411
|
+
let directory = startDirectory;
|
|
412
|
+
while (true) {
|
|
413
|
+
if (existsSync(join(directory, ".git"))) return directory;
|
|
414
|
+
const parent = dirname(directory);
|
|
415
|
+
if (parent === directory) return null;
|
|
416
|
+
directory = parent;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
function existsAtAnyRoot(roots, entryNames) {
|
|
420
|
+
return roots.some((root) => entryNames.some((entryName) => existsSync(join(root, entryName))));
|
|
421
|
+
}
|
|
422
|
+
async function promptTools(cwd) {
|
|
423
|
+
const gitRoot = findGitRoot(cwd);
|
|
424
|
+
const roots = gitRoot && gitRoot !== cwd ? [cwd, gitRoot] : [cwd];
|
|
425
|
+
const hasEditorConfig = existsAtAnyRoot(roots, [".editorconfig"]);
|
|
426
|
+
const hasVscode = existsAtAnyRoot(roots, [".vscode"]);
|
|
427
|
+
const hasKnip = existsAtAnyRoot(roots, KNIP_CONFIG_FILE_NAMES);
|
|
428
|
+
const initialValues = DEFAULT_SELECTED_TOOLS.filter((tool) => {
|
|
429
|
+
if (tool === "editorconfig") return !hasEditorConfig;
|
|
430
|
+
if (tool === "vscode") return !hasVscode;
|
|
431
|
+
if (tool === "knip") return !hasKnip;
|
|
432
|
+
return true;
|
|
433
|
+
});
|
|
434
|
+
return exitWhenCancelled(await multiselect({
|
|
435
|
+
message: "Select tools to configure",
|
|
436
|
+
options: [
|
|
437
|
+
{
|
|
438
|
+
value: "editorconfig",
|
|
439
|
+
label: "EditorConfig",
|
|
440
|
+
...hasEditorConfig && { hint: "already exists" }
|
|
441
|
+
},
|
|
442
|
+
{
|
|
443
|
+
value: "oxc",
|
|
444
|
+
label: "OXC (oxlint + oxfmt)"
|
|
445
|
+
},
|
|
446
|
+
{
|
|
447
|
+
value: "knip",
|
|
448
|
+
label: "Knip (unused files, exports, dependencies)",
|
|
449
|
+
...hasKnip && { hint: "already exists" }
|
|
450
|
+
},
|
|
451
|
+
{
|
|
452
|
+
value: "vscode",
|
|
453
|
+
label: "VSCode settings (.vscode/settings.json)",
|
|
454
|
+
...hasVscode && { hint: "already exists" }
|
|
455
|
+
}
|
|
456
|
+
],
|
|
457
|
+
initialValues,
|
|
458
|
+
required: true
|
|
459
|
+
}));
|
|
173
460
|
}
|
|
174
461
|
async function main() {
|
|
175
462
|
const cwd = process.cwd();
|
|
176
463
|
intro(color.blue(TOOLING_PACKAGE_NAME));
|
|
177
464
|
log.info(`You are about to configure ${TOOLING_PACKAGE_NAME} in the current directory: ${color.green(cwd)}`);
|
|
178
|
-
if (
|
|
179
|
-
const tools = await
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
log.success("All done. Make sure to install the dependencies with `pnpm install`.");
|
|
196
|
-
}
|
|
197
|
-
main();
|
|
198
|
-
//#endregion
|
|
199
|
-
export { DEFAULT_SELECTED_TOOLS };
|
|
465
|
+
if (!exitWhenCancelled(await confirm({ message: "Continue ?" }))) return;
|
|
466
|
+
const tools = await promptTools(cwd);
|
|
467
|
+
const result = {
|
|
468
|
+
tools,
|
|
469
|
+
presetFlags: await promptPresetsForTools(tools, cwd)
|
|
470
|
+
};
|
|
471
|
+
await updateEditorConfig(result);
|
|
472
|
+
await updatePackageScripts(result);
|
|
473
|
+
await updateOxc(result);
|
|
474
|
+
await updateKnip(result);
|
|
475
|
+
log.success("All done.");
|
|
476
|
+
}
|
|
477
|
+
main().catch((error) => {
|
|
478
|
+
log.error(error instanceof Error ? error.message : String(error));
|
|
479
|
+
process.exit(1);
|
|
480
|
+
});
|
|
481
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { KnipConfiguration } from "knip";
|
|
2
|
+
export interface MinimalstuffKnipOptions extends KnipConfiguration {
|
|
3
|
+
/**
|
|
4
|
+
* Apply the AdonisJS conventions (entries, ignores, string-only dependencies).
|
|
5
|
+
* `true` targets the root workspace, a list of paths targets each of those
|
|
6
|
+
* workspaces (monorepo).
|
|
7
|
+
*/
|
|
8
|
+
adonisjs?: boolean | readonly string[];
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Builds the root knip config. With `adonisjs`, the AdonisJS conventions are
|
|
12
|
+
* merged into the targeted workspace(s): arrays (`entry`, `project`, `ignore`,
|
|
13
|
+
* `ignoreDependencies`, ...) are concatenated with yours, other keys of yours
|
|
14
|
+
* win.
|
|
15
|
+
*/
|
|
16
|
+
export declare function minimalstuffPreset(options?: MinimalstuffKnipOptions): KnipConfiguration;
|