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