vite-plus 0.1.22 → 0.1.24
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/binding/index.cjs +119 -52
- package/binding/index.d.cts +3222 -2
- package/dist/agent-Nuk-9l77.js +2470 -0
- package/dist/bin.js +13 -8
- package/dist/cli-truncate-CWsmbK3p.js +867 -0
- package/dist/{compat-OlmU9EQz.js → compat-DXZgnEyq.js} +1 -1
- package/dist/config/bin.js +18 -5
- package/dist/{constants-kDaYqyWd.js → constants-DCBWlNrn.js} +7 -2
- package/dist/create/bin.d.ts +1 -1
- package/dist/create/bin.js +194 -87
- package/dist/{define-config-IMCGDS2K.d.ts → define-config-COdn-tsn.d.ts} +7 -5
- package/dist/define-config.cjs +1 -1
- package/dist/define-config.d.ts +1 -1
- package/dist/define-config.js +1 -1
- package/dist/dist-Bapm49IR.js +3 -0
- package/dist/{dist-owlRxmBM.js → dist-BgQuvbtq.js} +136 -110
- package/dist/fmt.d.ts +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/is-fullwidth-code-point-BUNlIICg.js +8 -0
- package/dist/lint.d.ts +1 -1
- package/dist/{log-update-NRrY6krx.js → log-update-lyIiuflf.js} +111 -24
- package/dist/migration/bin.js +81 -39
- package/dist/{oxlint-plugin-config-BkQeR4FR.js → oxlint-plugin-config-B89iKTKN.js} +1 -1
- package/dist/oxlint-plugin.d.ts +2 -4
- package/dist/oxlint-plugin.js +1 -1
- package/dist/pack-bin.js +1 -1
- package/dist/pack.d.ts +1 -1
- package/dist/{package-BoLLED6j.js → package-PmBUZ-ve.js} +2 -2
- package/dist/staged/bin.js +157 -424
- package/dist/strip-ansi-C3wrWz9t.js +853 -0
- package/dist/{agent-BWLe0i9g.js → tsconfig-DFb5BKyT.js} +681 -2114
- package/dist/version.js +5 -5
- package/dist/versions.js +7 -7
- package/dist/{workspace-Bi_9spVt.js → workspace-NL-m9wgM.js} +22 -21
- package/dist/wrap-ansi-CeQuiQ31.js +2 -0
- package/dist/{wrap-ansi-DtUeUCjE.js → wrap-ansi-k7Dn4VtV.js} +1 -1
- package/docs/_data/team.ts +3 -3
- package/docs/config/run.md +39 -4
- package/docs/guide/cache.md +10 -1
- package/docs/guide/env.md +3 -0
- package/docs/guide/ide-integration.md +2 -2
- package/docs/guide/index.md +4 -0
- package/docs/guide/install.md +19 -0
- package/docs/guide/migrate.md +1 -1
- package/docs/guide/run.md +2 -0
- package/docs/guide/troubleshooting.md +6 -29
- package/docs/guide/upgrade.md +12 -1
- package/package.json +20 -20
- package/templates/monorepo/_gitignore +1 -0
- package/dist/cli-truncate-B62YnW2m.js +0 -138
- package/dist/dist-DZfItHAr.js +0 -3
- package/dist/slice-ansi-e4todZeH.js +0 -113
- package/dist/strip-ansi-D-eYYcD2.js +0 -198
- package/dist/tsconfig-BVyzXJ_o.js +0 -517
- package/dist/wrap-ansi-3S3qJ7j8.js +0 -2
- /package/dist/{chunk-q7NCDQ7-.js → chunk-DnnnRqeS.js} +0 -0
- /package/dist/{define-config-GqLoRwH9.cjs → define-config-BR1Y88zz.cjs} +0 -0
- /package/dist/{define-config-CzWdQTt2.js → define-config-BRC7qPNE.js} +0 -0
- /package/dist/{help-DK5wuu34.js → help-YP84FSEz.js} +0 -0
- /package/dist/{lib-DpwyUJWo.js → lib-L3DWSRQp.js} +0 -0
- /package/dist/{main-DhsO6ndq.js → main-DpJl3LoU.js} +0 -0
- /package/dist/{pack-K7H72Cum.d.ts → pack-Ciiho0Tq.d.ts} +0 -0
- /package/dist/{report-CYPv1VK1.js → report-DgSBQUdz.js} +0 -0
- /package/dist/{resolve-vite-config-C5AjksTj.js → resolve-vite-config-TTvhycU1.js} +0 -0
- /package/dist/{terminal-D_Kg-AA6.js → terminal-uTv0ZaMr.js} +0 -0
|
@@ -0,0 +1,2470 @@
|
|
|
1
|
+
import { r as __toESM } from "./chunk-DnnnRqeS.js";
|
|
2
|
+
import { a as VITE_PLUS_OVERRIDE_PACKAGES, c as isForceOverrideMode, i as VITE_PLUS_NAME, n as BASEURL_TSCONFIG_WARNING, o as VITE_PLUS_VERSION } from "./constants-DCBWlNrn.js";
|
|
3
|
+
import { i as ensureVitePlusImportRuleDefaults, r as createDefaultVitePlusLintConfig } from "./oxlint-plugin-config-B89iKTKN.js";
|
|
4
|
+
import { A as R, C as log, E as select, M as runCommandSilently, N as require_cross_spawn, a as removeDeprecatedTsconfigFalseOption, i as hasBaseUrlInTsconfig, n as findTsconfigFiles, o as rewriteTypesInTsconfig, s as cancelAndExit, u as getSpinner, v as PackageManager, w as multiselect, x as confirm, y as require_semver } from "./tsconfig-DFb5BKyT.js";
|
|
5
|
+
import { t as require_dist } from "./dist-BgQuvbtq.js";
|
|
6
|
+
import { c as editJsonFile, l as isJsonFile, n as detectPackageMetadata, u as readJsonFile } from "./package-PmBUZ-ve.js";
|
|
7
|
+
import { n as addMigrationWarning, t as addManualStep } from "./report-DgSBQUdz.js";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { hasConfigKey, mergeJsonConfig, mergeTsdownConfig, rewriteEslint, rewriteImportsInDirectory, rewritePrettier, rewriteScripts } from "../binding/index.js";
|
|
10
|
+
import fs from "node:fs";
|
|
11
|
+
import { styleText } from "node:util";
|
|
12
|
+
import fsPromises from "node:fs/promises";
|
|
13
|
+
//#region src/utils/path.ts
|
|
14
|
+
var import_cross_spawn = /* @__PURE__ */ __toESM(require_cross_spawn(), 1);
|
|
15
|
+
var import_semver = /* @__PURE__ */ __toESM(require_semver(), 1);
|
|
16
|
+
var import_dist = require_dist();
|
|
17
|
+
function findPkgRoot() {
|
|
18
|
+
let dir = import.meta.dirname;
|
|
19
|
+
while (dir !== path.dirname(dir)) {
|
|
20
|
+
if (fs.existsSync(path.join(dir, "package.json"))) return dir;
|
|
21
|
+
dir = path.dirname(dir);
|
|
22
|
+
}
|
|
23
|
+
return dir;
|
|
24
|
+
}
|
|
25
|
+
const pkgRoot = findPkgRoot();
|
|
26
|
+
const templatesDir = path.join(pkgRoot, "templates");
|
|
27
|
+
const rulesDir = path.join(pkgRoot, "rules");
|
|
28
|
+
function displayRelative(to, from = process.cwd()) {
|
|
29
|
+
return path.relative(from, to).replaceAll("\\", "/");
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/utils/yaml.ts
|
|
33
|
+
function readYamlFile(file) {
|
|
34
|
+
return (0, import_dist.parse)(fs.readFileSync(file, "utf-8"));
|
|
35
|
+
}
|
|
36
|
+
function editYamlFile(file, callback) {
|
|
37
|
+
const doc = (0, import_dist.parseDocument)(fs.readFileSync(file, "utf-8"));
|
|
38
|
+
callback(doc);
|
|
39
|
+
fs.writeFileSync(file, doc.toString({ singleQuote: true }), "utf-8");
|
|
40
|
+
}
|
|
41
|
+
function scalarString(value) {
|
|
42
|
+
return new import_dist.Scalar(value);
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/migration/detector.ts
|
|
46
|
+
const PRETTIER_PACKAGE_JSON_CONFIG = "package.json#prettier";
|
|
47
|
+
const PRETTIER_CONFIG_FILES = [
|
|
48
|
+
".prettierrc",
|
|
49
|
+
".prettierrc.json",
|
|
50
|
+
".prettierrc.jsonc",
|
|
51
|
+
".prettierrc.yaml",
|
|
52
|
+
".prettierrc.yml",
|
|
53
|
+
".prettierrc.toml",
|
|
54
|
+
".prettierrc.js",
|
|
55
|
+
".prettierrc.cjs",
|
|
56
|
+
".prettierrc.mjs",
|
|
57
|
+
".prettierrc.ts",
|
|
58
|
+
".prettierrc.cts",
|
|
59
|
+
".prettierrc.mts",
|
|
60
|
+
"prettier.config.js",
|
|
61
|
+
"prettier.config.cjs",
|
|
62
|
+
"prettier.config.mjs",
|
|
63
|
+
"prettier.config.ts",
|
|
64
|
+
"prettier.config.cts",
|
|
65
|
+
"prettier.config.mts"
|
|
66
|
+
];
|
|
67
|
+
function detectConfigs(projectPath) {
|
|
68
|
+
const configs = {};
|
|
69
|
+
for (const config of [
|
|
70
|
+
"vite.config.ts",
|
|
71
|
+
"vite.config.mts",
|
|
72
|
+
"vite.config.cts",
|
|
73
|
+
"vite.config.js",
|
|
74
|
+
"vite.config.mjs",
|
|
75
|
+
"vite.config.cjs"
|
|
76
|
+
]) if (fs.existsSync(path.join(projectPath, config))) {
|
|
77
|
+
configs.viteConfig = config;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
for (const config of [
|
|
81
|
+
"vitest.config.ts",
|
|
82
|
+
"vitest.config.mts",
|
|
83
|
+
"vitest.config.cts",
|
|
84
|
+
"vitest.config.js",
|
|
85
|
+
"vitest.config.mjs",
|
|
86
|
+
"vitest.config.cjs"
|
|
87
|
+
]) if (fs.existsSync(path.join(projectPath, config))) {
|
|
88
|
+
configs.vitestConfig = config;
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
for (const config of [
|
|
92
|
+
"tsdown.config.ts",
|
|
93
|
+
"tsdown.config.mts",
|
|
94
|
+
"tsdown.config.cts",
|
|
95
|
+
"tsdown.config.js",
|
|
96
|
+
"tsdown.config.mjs",
|
|
97
|
+
"tsdown.config.cjs",
|
|
98
|
+
"tsdown.config.json",
|
|
99
|
+
"tsdown.config"
|
|
100
|
+
]) if (fs.existsSync(path.join(projectPath, config))) {
|
|
101
|
+
configs.tsdownConfig = config;
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
for (const config of [".oxlintrc.json", ".oxlintrc.jsonc"]) if (fs.existsSync(path.join(projectPath, config))) {
|
|
105
|
+
configs.oxlintConfig = config;
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
for (const config of [".oxfmtrc.json", ".oxfmtrc.jsonc"]) if (fs.existsSync(path.join(projectPath, config))) {
|
|
109
|
+
configs.oxfmtConfig = config;
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
for (const config of [
|
|
113
|
+
"eslint.config.js",
|
|
114
|
+
"eslint.config.mjs",
|
|
115
|
+
"eslint.config.cjs",
|
|
116
|
+
"eslint.config.ts",
|
|
117
|
+
"eslint.config.mts",
|
|
118
|
+
"eslint.config.cts"
|
|
119
|
+
]) if (fs.existsSync(path.join(projectPath, config))) {
|
|
120
|
+
configs.eslintConfig = config;
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
for (const config of [
|
|
124
|
+
".eslintrc",
|
|
125
|
+
".eslintrc.json",
|
|
126
|
+
".eslintrc.js",
|
|
127
|
+
".eslintrc.cjs",
|
|
128
|
+
".eslintrc.yaml",
|
|
129
|
+
".eslintrc.yml"
|
|
130
|
+
]) if (fs.existsSync(path.join(projectPath, config))) {
|
|
131
|
+
configs.eslintLegacyConfig = config;
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
for (const config of PRETTIER_CONFIG_FILES) if (fs.existsSync(path.join(projectPath, config))) {
|
|
135
|
+
configs.prettierConfig = config;
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
if (fs.existsSync(path.join(projectPath, ".prettierignore"))) configs.prettierIgnore = true;
|
|
139
|
+
if (fs.existsSync(path.join(projectPath, ".nvmrc"))) configs.nvmrcFile = true;
|
|
140
|
+
const packageJsonPath = path.join(projectPath, "package.json");
|
|
141
|
+
if (fs.existsSync(packageJsonPath)) try {
|
|
142
|
+
const content = fs.readFileSync(packageJsonPath, "utf8");
|
|
143
|
+
const pkg = JSON.parse(content);
|
|
144
|
+
if (!configs.prettierConfig && pkg.prettier) configs.prettierConfig = PRETTIER_PACKAGE_JSON_CONFIG;
|
|
145
|
+
const voltaNode = pkg.volta?.node;
|
|
146
|
+
if (typeof voltaNode === "string") configs.voltaNode = voltaNode;
|
|
147
|
+
} catch {}
|
|
148
|
+
return configs;
|
|
149
|
+
}
|
|
150
|
+
//#endregion
|
|
151
|
+
//#region src/migration/migrator.ts
|
|
152
|
+
const LINT_STAGED_JSON_CONFIG_FILES = [".lintstagedrc.json", ".lintstagedrc"];
|
|
153
|
+
const LINT_STAGED_OTHER_CONFIG_FILES = [
|
|
154
|
+
".lintstagedrc.yaml",
|
|
155
|
+
".lintstagedrc.yml",
|
|
156
|
+
".lintstagedrc.mjs",
|
|
157
|
+
"lint-staged.config.mjs",
|
|
158
|
+
".lintstagedrc.cjs",
|
|
159
|
+
"lint-staged.config.cjs",
|
|
160
|
+
".lintstagedrc.js",
|
|
161
|
+
"lint-staged.config.js",
|
|
162
|
+
".lintstagedrc.ts",
|
|
163
|
+
"lint-staged.config.ts",
|
|
164
|
+
".lintstagedrc.mts",
|
|
165
|
+
"lint-staged.config.mts",
|
|
166
|
+
".lintstagedrc.cts",
|
|
167
|
+
"lint-staged.config.cts"
|
|
168
|
+
];
|
|
169
|
+
const LINT_STAGED_ALL_CONFIG_FILES = [...LINT_STAGED_JSON_CONFIG_FILES, ...LINT_STAGED_OTHER_CONFIG_FILES];
|
|
170
|
+
const REMOVE_PACKAGES = [
|
|
171
|
+
"oxlint",
|
|
172
|
+
"oxlint-tsgolint",
|
|
173
|
+
"oxfmt",
|
|
174
|
+
"tsdown",
|
|
175
|
+
"@vitest/browser",
|
|
176
|
+
"@vitest/browser-preview",
|
|
177
|
+
"@vitest/browser-playwright",
|
|
178
|
+
"@vitest/browser-webdriverio"
|
|
179
|
+
];
|
|
180
|
+
const BROWSER_PROVIDER_PEER_DEPS = {
|
|
181
|
+
"@vitest/browser-playwright": "playwright",
|
|
182
|
+
"@vitest/browser-webdriverio": "webdriverio"
|
|
183
|
+
};
|
|
184
|
+
const PUBLIC_PEER_DEPENDENCY_FALLBACKS = {
|
|
185
|
+
vite: "*",
|
|
186
|
+
vitest: "*"
|
|
187
|
+
};
|
|
188
|
+
const OXLINT_NATIVE_PLUGINS = new Set([
|
|
189
|
+
"eslint",
|
|
190
|
+
"react",
|
|
191
|
+
"unicorn",
|
|
192
|
+
"typescript",
|
|
193
|
+
"oxc",
|
|
194
|
+
"import",
|
|
195
|
+
"jsdoc",
|
|
196
|
+
"jest",
|
|
197
|
+
"vitest",
|
|
198
|
+
"jsx-a11y",
|
|
199
|
+
"nextjs",
|
|
200
|
+
"react-perf",
|
|
201
|
+
"promise",
|
|
202
|
+
"node",
|
|
203
|
+
"vue"
|
|
204
|
+
]);
|
|
205
|
+
function warnMigration(message, report) {
|
|
206
|
+
addMigrationWarning(report, message);
|
|
207
|
+
if (!report) log.warn(message);
|
|
208
|
+
}
|
|
209
|
+
function infoMigration(message, report) {
|
|
210
|
+
addManualStep(report, message);
|
|
211
|
+
if (!report) log.info(message);
|
|
212
|
+
}
|
|
213
|
+
function checkViteVersion(projectPath) {
|
|
214
|
+
return checkPackageVersion(projectPath, "vite", "7.0.0");
|
|
215
|
+
}
|
|
216
|
+
function checkVitestVersion(projectPath) {
|
|
217
|
+
return checkPackageVersion(projectPath, "vitest", "4.0.0");
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Check the package version is supported by auto migration
|
|
221
|
+
* @param projectPath - The path to the project
|
|
222
|
+
* @param name - The name of the package
|
|
223
|
+
* @param minVersion - The minimum version of the package
|
|
224
|
+
* @returns true if the package version is supported by auto migration
|
|
225
|
+
*/
|
|
226
|
+
function checkPackageVersion(projectPath, name, minVersion) {
|
|
227
|
+
const metadata = detectPackageMetadata(projectPath, name);
|
|
228
|
+
if (!metadata || metadata.name !== name) return true;
|
|
229
|
+
if (import_semver.default.satisfies(metadata.version, `<${minVersion}`)) {
|
|
230
|
+
const packageJsonFilePath = path.join(projectPath, "package.json");
|
|
231
|
+
log.error(`✘ ${name}@${metadata.version} in ${displayRelative(packageJsonFilePath)} is not supported by auto migration`);
|
|
232
|
+
log.info(`Please upgrade ${name} to version >=${minVersion} first`);
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
function detectEslintProject(projectPath, packages) {
|
|
238
|
+
const packageJsonPath = path.join(projectPath, "package.json");
|
|
239
|
+
if (!fs.existsSync(packageJsonPath)) return { hasDependency: false };
|
|
240
|
+
const pkg = readJsonFile(packageJsonPath);
|
|
241
|
+
let hasDependency = !!(pkg.devDependencies?.eslint || pkg.dependencies?.eslint);
|
|
242
|
+
const configs = detectConfigs(projectPath);
|
|
243
|
+
let configFile = configs.eslintConfig;
|
|
244
|
+
const legacyConfigFile = configs.eslintLegacyConfig;
|
|
245
|
+
if (!hasDependency && packages) for (const wp of packages) {
|
|
246
|
+
const pkgJsonPath = path.join(projectPath, wp.path, "package.json");
|
|
247
|
+
if (!fs.existsSync(pkgJsonPath)) continue;
|
|
248
|
+
const wpPkg = readJsonFile(pkgJsonPath);
|
|
249
|
+
if (wpPkg.devDependencies?.eslint || wpPkg.dependencies?.eslint) {
|
|
250
|
+
hasDependency = true;
|
|
251
|
+
break;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return {
|
|
255
|
+
hasDependency,
|
|
256
|
+
configFile,
|
|
257
|
+
legacyConfigFile
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Run a `vp dlx @oxlint/migrate` step with graceful error handling.
|
|
262
|
+
* Returns true on success, false on failure (spawn error or non-zero exit).
|
|
263
|
+
*/
|
|
264
|
+
async function runOxlintMigrateStep(vpBin, cwd, migratePackage, args, spinner, failMessage, manualHint) {
|
|
265
|
+
try {
|
|
266
|
+
const result = await runCommandSilently({
|
|
267
|
+
command: vpBin,
|
|
268
|
+
args: [
|
|
269
|
+
"dlx",
|
|
270
|
+
migratePackage,
|
|
271
|
+
...args
|
|
272
|
+
],
|
|
273
|
+
cwd,
|
|
274
|
+
envs: process.env
|
|
275
|
+
});
|
|
276
|
+
if (result.exitCode !== 0) {
|
|
277
|
+
spinner.stop(failMessage);
|
|
278
|
+
const stderr = result.stderr.toString().trim();
|
|
279
|
+
if (stderr) log.warn(`⚠ ${stderr}`);
|
|
280
|
+
log.info(manualHint);
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
return true;
|
|
284
|
+
} catch {
|
|
285
|
+
spinner.stop(failMessage);
|
|
286
|
+
log.info(manualHint);
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
async function migrateEslintToOxlint(projectPath, interactive, eslintConfigFile, packages, options) {
|
|
291
|
+
const vpBin = process.env.VP_CLI_BIN ?? "vp";
|
|
292
|
+
const spinner = options?.silent ? {
|
|
293
|
+
start: () => {},
|
|
294
|
+
stop: () => {},
|
|
295
|
+
pause: () => {},
|
|
296
|
+
resume: () => {},
|
|
297
|
+
cancel: () => {},
|
|
298
|
+
error: () => {},
|
|
299
|
+
clear: () => {},
|
|
300
|
+
message: () => {},
|
|
301
|
+
isCancelled: false
|
|
302
|
+
} : getSpinner(interactive);
|
|
303
|
+
if (eslintConfigFile) {
|
|
304
|
+
const { versions } = await import("./versions.js");
|
|
305
|
+
const migratePackage = `@oxlint/migrate@${versions.oxlint}`;
|
|
306
|
+
const migrateArgs = [
|
|
307
|
+
"--merge",
|
|
308
|
+
...!hasBaseUrlInTsconfig(projectPath) ? ["--type-aware"] : [],
|
|
309
|
+
"--with-nursery",
|
|
310
|
+
"--details"
|
|
311
|
+
];
|
|
312
|
+
spinner.start("Migrating ESLint config to Oxlint...");
|
|
313
|
+
if (!await runOxlintMigrateStep(vpBin, projectPath, migratePackage, migrateArgs, spinner, "ESLint migration failed", `You can run \`vp dlx ${migratePackage} ${migrateArgs.join(" ")}\` manually later`)) return false;
|
|
314
|
+
spinner.stop("ESLint config migrated to .oxlintrc.json");
|
|
315
|
+
spinner.start("Replacing ESLint comments with Oxlint equivalents...");
|
|
316
|
+
if (await runOxlintMigrateStep(vpBin, projectPath, migratePackage, ["--replace-eslint-comments"], spinner, "ESLint comment replacement failed", `You can run \`vp dlx ${migratePackage} --replace-eslint-comments\` manually later`)) spinner.stop("ESLint comments replaced");
|
|
317
|
+
}
|
|
318
|
+
if (options?.report) options.report.eslintMigrated = true;
|
|
319
|
+
const preserveJsPlugins = collectJsPluginPackageNames(projectPath);
|
|
320
|
+
const cleanupTargets = [projectPath, ...(packages ?? []).map((p) => path.join(projectPath, p.path))];
|
|
321
|
+
for (const target of cleanupTargets) {
|
|
322
|
+
if (!fs.existsSync(path.join(target, "package.json"))) continue;
|
|
323
|
+
deleteEslintConfigFiles(target, options?.report, options?.silent);
|
|
324
|
+
rewriteEslintPackageJson(path.join(target, "package.json"), preserveJsPlugins);
|
|
325
|
+
rewriteEslintLintStagedConfigFiles(target, options?.report);
|
|
326
|
+
}
|
|
327
|
+
return true;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Read `<projectPath>/.oxlintrc.json` (if any) and collect the package
|
|
331
|
+
* names referenced via `lint.jsPlugins[]` string entries. Object-form
|
|
332
|
+
* entries (`{ name, specifier }`) and local-path specifiers (`./X`,
|
|
333
|
+
* `../X`, `/X`) are excluded — neither maps to a `package.json` entry
|
|
334
|
+
* we'd accidentally strip.
|
|
335
|
+
*/
|
|
336
|
+
function collectJsPluginPackageNames(projectPath) {
|
|
337
|
+
const out = /* @__PURE__ */ new Set();
|
|
338
|
+
const oxlintConfigPath = path.join(projectPath, ".oxlintrc.json");
|
|
339
|
+
if (!fs.existsSync(oxlintConfigPath)) return out;
|
|
340
|
+
let config;
|
|
341
|
+
try {
|
|
342
|
+
config = readJsonFile(oxlintConfigPath, true);
|
|
343
|
+
} catch {
|
|
344
|
+
return out;
|
|
345
|
+
}
|
|
346
|
+
const collectFrom = (jsPlugins) => {
|
|
347
|
+
for (const entry of jsPlugins ?? []) {
|
|
348
|
+
if (typeof entry !== "string") continue;
|
|
349
|
+
if (entry.startsWith("./") || entry.startsWith("../") || entry.startsWith("/")) continue;
|
|
350
|
+
out.add(entry);
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
collectFrom(config.jsPlugins);
|
|
354
|
+
if (Array.isArray(config.overrides)) for (const override of config.overrides) collectFrom(override.jsPlugins);
|
|
355
|
+
return out;
|
|
356
|
+
}
|
|
357
|
+
function deleteEslintConfigFiles(basePath, report, silent = false) {
|
|
358
|
+
const configs = detectConfigs(basePath);
|
|
359
|
+
for (const file of [configs.eslintConfig, configs.eslintLegacyConfig]) if (file) {
|
|
360
|
+
const configPath = path.join(basePath, file);
|
|
361
|
+
if (fs.existsSync(configPath)) {
|
|
362
|
+
fs.unlinkSync(configPath);
|
|
363
|
+
if (report) report.removedConfigCount++;
|
|
364
|
+
if (!silent) log.success(`✔ Removed ${displayRelative(configPath)}`);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
const ESLINT_ECOSYSTEM_NAMES = new Set([
|
|
369
|
+
"eslint",
|
|
370
|
+
"typescript-eslint",
|
|
371
|
+
"eslintrc",
|
|
372
|
+
"eslint-utils",
|
|
373
|
+
"eslint-visitor-keys",
|
|
374
|
+
"eslint-scope",
|
|
375
|
+
"eslint-define-config",
|
|
376
|
+
"eslint-doc-generator",
|
|
377
|
+
"@typescript-eslint/eslint-plugin",
|
|
378
|
+
"@typescript-eslint/parser",
|
|
379
|
+
"@typescript-eslint/rule-tester"
|
|
380
|
+
]);
|
|
381
|
+
const ESLINT_ECOSYSTEM_PREFIXES = [
|
|
382
|
+
"eslint-plugin-",
|
|
383
|
+
"eslint-config-",
|
|
384
|
+
"eslint-formatter-"
|
|
385
|
+
];
|
|
386
|
+
const ESLINT_ECOSYSTEM_SCOPES = [
|
|
387
|
+
"@eslint/",
|
|
388
|
+
"@eslint-community/",
|
|
389
|
+
"@angular-eslint/"
|
|
390
|
+
];
|
|
391
|
+
/**
|
|
392
|
+
* Decide whether a dependency entry should be removed alongside `eslint`
|
|
393
|
+
* itself. The set is intentionally broad: anything whose only purpose is
|
|
394
|
+
* to extend, configure, format, or wire ESLint becomes dead weight after
|
|
395
|
+
* migration. `@types/<X>` packages are checked symmetrically with `<X>`
|
|
396
|
+
* so type-only counterparts of removed runtime packages also go.
|
|
397
|
+
*/
|
|
398
|
+
function isEslintEcosystemDep(name) {
|
|
399
|
+
const stripped = name.startsWith("@types/") ? name.slice(7) : name;
|
|
400
|
+
if (ESLINT_ECOSYSTEM_NAMES.has(stripped)) return true;
|
|
401
|
+
if (ESLINT_ECOSYSTEM_PREFIXES.some((p) => stripped.startsWith(p))) return true;
|
|
402
|
+
if (ESLINT_ECOSYSTEM_SCOPES.some((s) => stripped.startsWith(s))) return true;
|
|
403
|
+
// @vitest/eslint-plugin
|
|
404
|
+
if (/^@[^/]+\/eslint-(plugin|config|formatter)(-.+)?$/.test(stripped)) return true;
|
|
405
|
+
return false;
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* Rewrite a project's `package.json` after ESLint has been migrated to
|
|
409
|
+
* Oxlint: drop every ESLint-ecosystem dependency (see
|
|
410
|
+
* `isEslintEcosystemDep`), strip empty containers, and rewrite eslint
|
|
411
|
+
* tokens in scripts / lint-staged. Applied uniformly to the root and to
|
|
412
|
+
* every workspace package — the migration treats the whole workspace as
|
|
413
|
+
* in scope for adoption, so a half-cleanup at the workspace level would
|
|
414
|
+
* be inconsistent with the rest of the flow (which already replaces
|
|
415
|
+
* vite-related overrides and adds vite-plus across all packages).
|
|
416
|
+
*
|
|
417
|
+
* `preserveJsPlugins` names packages that `@oxlint/migrate` referenced
|
|
418
|
+
* via `lint.jsPlugins` and that Oxlint will need to `import()` at lint
|
|
419
|
+
* time. They override `isEslintEcosystemDep` so the generated config
|
|
420
|
+
* isn't immediately invalidated by the cleanup step.
|
|
421
|
+
*/
|
|
422
|
+
function rewriteEslintPackageJson(packageJsonPath, preserveJsPlugins = /* @__PURE__ */ new Set()) {
|
|
423
|
+
editJsonFile(packageJsonPath, (pkg) => {
|
|
424
|
+
let changed = false;
|
|
425
|
+
for (const field of [
|
|
426
|
+
"devDependencies",
|
|
427
|
+
"dependencies",
|
|
428
|
+
"peerDependencies",
|
|
429
|
+
"optionalDependencies"
|
|
430
|
+
]) {
|
|
431
|
+
const deps = pkg[field];
|
|
432
|
+
if (!deps) continue;
|
|
433
|
+
let removedAny = false;
|
|
434
|
+
for (const name of Object.keys(deps)) {
|
|
435
|
+
if (preserveJsPlugins.has(name)) continue;
|
|
436
|
+
if (isEslintEcosystemDep(name)) {
|
|
437
|
+
delete deps[name];
|
|
438
|
+
changed = true;
|
|
439
|
+
removedAny = true;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
if (removedAny && Object.keys(deps).length === 0) delete pkg[field];
|
|
443
|
+
}
|
|
444
|
+
if (pkg.scripts) {
|
|
445
|
+
const updated = rewriteEslint(JSON.stringify(pkg.scripts));
|
|
446
|
+
if (updated) {
|
|
447
|
+
pkg.scripts = JSON.parse(updated);
|
|
448
|
+
changed = true;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
if (pkg["lint-staged"]) {
|
|
452
|
+
const updated = rewriteEslint(JSON.stringify(pkg["lint-staged"]));
|
|
453
|
+
if (updated) {
|
|
454
|
+
pkg["lint-staged"] = JSON.parse(updated);
|
|
455
|
+
changed = true;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return changed ? pkg : void 0;
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Rewrite tool references in lint-staged config files (JSON ones are rewritten,
|
|
463
|
+
* non-JSON ones get a warning).
|
|
464
|
+
*/
|
|
465
|
+
function rewriteToolLintStagedConfigFiles(projectPath, rewriteFn, toolName, report) {
|
|
466
|
+
for (const filename of LINT_STAGED_JSON_CONFIG_FILES) {
|
|
467
|
+
const configPath = path.join(projectPath, filename);
|
|
468
|
+
if (!fs.existsSync(configPath)) continue;
|
|
469
|
+
if (filename === ".lintstagedrc" && !isJsonFile(configPath)) {
|
|
470
|
+
warnMigration(`${displayRelative(configPath)} is not JSON — please update ${toolName} references manually`, report);
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
editJsonFile(configPath, (config) => {
|
|
474
|
+
const updated = rewriteFn(JSON.stringify(config));
|
|
475
|
+
if (updated) return JSON.parse(updated);
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
for (const filename of LINT_STAGED_OTHER_CONFIG_FILES) {
|
|
479
|
+
const configPath = path.join(projectPath, filename);
|
|
480
|
+
if (!fs.existsSync(configPath)) continue;
|
|
481
|
+
warnMigration(`${displayRelative(configPath)} — please update ${toolName} references manually`, report);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
function rewriteEslintLintStagedConfigFiles(projectPath, report) {
|
|
485
|
+
rewriteToolLintStagedConfigFiles(projectPath, rewriteEslint, "eslint", report);
|
|
486
|
+
}
|
|
487
|
+
function detectPrettierProject(projectPath, packages) {
|
|
488
|
+
const packageJsonPath = path.join(projectPath, "package.json");
|
|
489
|
+
if (!fs.existsSync(packageJsonPath)) return { hasDependency: false };
|
|
490
|
+
const pkg = readJsonFile(packageJsonPath);
|
|
491
|
+
let hasDependency = !!(pkg.devDependencies?.prettier || pkg.dependencies?.prettier);
|
|
492
|
+
const configFile = detectConfigs(projectPath).prettierConfig;
|
|
493
|
+
if (!hasDependency && packages) for (const wp of packages) {
|
|
494
|
+
const pkgJsonPath = path.join(projectPath, wp.path, "package.json");
|
|
495
|
+
if (!fs.existsSync(pkgJsonPath)) continue;
|
|
496
|
+
const wpPkg = readJsonFile(pkgJsonPath);
|
|
497
|
+
if (wpPkg.devDependencies?.prettier || wpPkg.dependencies?.prettier) {
|
|
498
|
+
hasDependency = true;
|
|
499
|
+
break;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
return {
|
|
503
|
+
hasDependency,
|
|
504
|
+
configFile
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Run `vp fmt --migrate=prettier` step with graceful error handling.
|
|
509
|
+
* Returns true on success, false on failure.
|
|
510
|
+
*/
|
|
511
|
+
async function runPrettierMigrateStep(vpBin, cwd, spinner, failMessage, manualHint) {
|
|
512
|
+
try {
|
|
513
|
+
const result = await runCommandSilently({
|
|
514
|
+
command: vpBin,
|
|
515
|
+
args: ["fmt", "--migrate=prettier"],
|
|
516
|
+
cwd,
|
|
517
|
+
envs: process.env
|
|
518
|
+
});
|
|
519
|
+
if (result.exitCode !== 0) {
|
|
520
|
+
spinner.stop(failMessage);
|
|
521
|
+
const stderr = result.stderr.toString().trim();
|
|
522
|
+
if (stderr) log.warn(`⚠ ${stderr}`);
|
|
523
|
+
log.info(manualHint);
|
|
524
|
+
return false;
|
|
525
|
+
}
|
|
526
|
+
return true;
|
|
527
|
+
} catch {
|
|
528
|
+
spinner.stop(failMessage);
|
|
529
|
+
log.info(manualHint);
|
|
530
|
+
return false;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
async function migratePrettierToOxfmt(projectPath, interactive, prettierConfigFile, packages, options) {
|
|
534
|
+
const vpBin = process.env.VP_CLI_BIN ?? "vp";
|
|
535
|
+
const spinner = options?.silent ? {
|
|
536
|
+
start: () => {},
|
|
537
|
+
stop: () => {},
|
|
538
|
+
pause: () => {},
|
|
539
|
+
resume: () => {},
|
|
540
|
+
cancel: () => {},
|
|
541
|
+
error: () => {},
|
|
542
|
+
clear: () => {},
|
|
543
|
+
message: () => {},
|
|
544
|
+
isCancelled: false
|
|
545
|
+
} : getSpinner(interactive);
|
|
546
|
+
if (prettierConfigFile) {
|
|
547
|
+
let tempPrettierConfig;
|
|
548
|
+
if (prettierConfigFile === "package.json#prettier") {
|
|
549
|
+
const pkg = readJsonFile(path.join(projectPath, "package.json"));
|
|
550
|
+
if (pkg.prettier) {
|
|
551
|
+
tempPrettierConfig = path.join(projectPath, ".prettierrc.json");
|
|
552
|
+
fs.writeFileSync(tempPrettierConfig, JSON.stringify(pkg.prettier, null, 2));
|
|
553
|
+
} else return true;
|
|
554
|
+
}
|
|
555
|
+
try {
|
|
556
|
+
spinner.start("Migrating Prettier config to Oxfmt...");
|
|
557
|
+
if (!await runPrettierMigrateStep(vpBin, projectPath, spinner, "Prettier migration failed", "You can run `vp fmt --migrate=prettier` manually later")) return false;
|
|
558
|
+
spinner.stop("Prettier config migrated to .oxfmtrc.json");
|
|
559
|
+
} finally {
|
|
560
|
+
if (tempPrettierConfig) try {
|
|
561
|
+
fs.unlinkSync(tempPrettierConfig);
|
|
562
|
+
} catch {}
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
if (options?.report) options.report.prettierMigrated = true;
|
|
566
|
+
deletePrettierConfigFiles(projectPath, options?.report, options?.silent);
|
|
567
|
+
rewritePrettierPackageJson(path.join(projectPath, "package.json"));
|
|
568
|
+
if (packages) for (const pkg of packages) rewritePrettierPackageJson(path.join(projectPath, pkg.path, "package.json"));
|
|
569
|
+
rewritePrettierLintStagedConfigFiles(projectPath, options?.report);
|
|
570
|
+
const prettierIgnorePath = path.join(projectPath, ".prettierignore");
|
|
571
|
+
if (fs.existsSync(prettierIgnorePath)) warnMigration(`${displayRelative(prettierIgnorePath)} found — Oxfmt supports .prettierignore, but using the \`ignorePatterns\` option is recommended.`, options?.report);
|
|
572
|
+
return true;
|
|
573
|
+
}
|
|
574
|
+
function deletePrettierConfigFiles(basePath, report, silent = false) {
|
|
575
|
+
const configs = detectConfigs(basePath);
|
|
576
|
+
if (configs.prettierConfig && configs.prettierConfig !== "package.json#prettier") {
|
|
577
|
+
const configPath = path.join(basePath, configs.prettierConfig);
|
|
578
|
+
if (fs.existsSync(configPath)) {
|
|
579
|
+
fs.unlinkSync(configPath);
|
|
580
|
+
if (report) report.removedConfigCount++;
|
|
581
|
+
if (!silent) log.success(`✔ Removed ${displayRelative(configPath)}`);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
for (const file of PRETTIER_CONFIG_FILES) {
|
|
585
|
+
if (file === configs.prettierConfig) continue;
|
|
586
|
+
const configPath = path.join(basePath, file);
|
|
587
|
+
if (fs.existsSync(configPath)) {
|
|
588
|
+
fs.unlinkSync(configPath);
|
|
589
|
+
if (report) report.removedConfigCount++;
|
|
590
|
+
if (!silent) log.success(`✔ Removed ${displayRelative(configPath)}`);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
editJsonFile(path.join(basePath, "package.json"), (pkg) => {
|
|
594
|
+
if (pkg.prettier) {
|
|
595
|
+
delete pkg.prettier;
|
|
596
|
+
return pkg;
|
|
597
|
+
}
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
function rewritePrettierPackageJson(packageJsonPath) {
|
|
601
|
+
if (!fs.existsSync(packageJsonPath)) return;
|
|
602
|
+
editJsonFile(packageJsonPath, (pkg) => {
|
|
603
|
+
let changed = false;
|
|
604
|
+
if (pkg.devDependencies) {
|
|
605
|
+
for (const dep of Object.keys(pkg.devDependencies)) if (dep === "prettier" || dep.startsWith("prettier-plugin-")) {
|
|
606
|
+
delete pkg.devDependencies[dep];
|
|
607
|
+
changed = true;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
if (pkg.dependencies) {
|
|
611
|
+
for (const dep of Object.keys(pkg.dependencies)) if (dep === "prettier" || dep.startsWith("prettier-plugin-")) {
|
|
612
|
+
delete pkg.dependencies[dep];
|
|
613
|
+
changed = true;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
if (pkg.scripts) {
|
|
617
|
+
const updated = rewritePrettier(JSON.stringify(pkg.scripts));
|
|
618
|
+
if (updated) {
|
|
619
|
+
pkg.scripts = JSON.parse(updated);
|
|
620
|
+
changed = true;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
if (pkg["lint-staged"]) {
|
|
624
|
+
const updated = rewritePrettier(JSON.stringify(pkg["lint-staged"]));
|
|
625
|
+
if (updated) {
|
|
626
|
+
pkg["lint-staged"] = JSON.parse(updated);
|
|
627
|
+
changed = true;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
return changed ? pkg : void 0;
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
function rewritePrettierLintStagedConfigFiles(projectPath, report) {
|
|
634
|
+
rewriteToolLintStagedConfigFiles(projectPath, rewritePrettier, "prettier", report);
|
|
635
|
+
}
|
|
636
|
+
function cleanupDeprecatedTsconfigOptions(projectPath, silent = false, report) {
|
|
637
|
+
const deprecatedOptions = ["esModuleInterop", "allowSyntheticDefaultImports"];
|
|
638
|
+
const files = findTsconfigFiles(projectPath);
|
|
639
|
+
for (const filePath of files) for (const name of deprecatedOptions) if (removeDeprecatedTsconfigFalseOption(filePath, name)) {
|
|
640
|
+
if (report) report.removedConfigCount++;
|
|
641
|
+
if (!silent) log.success(`✔ Removed ${name}: false from ${displayRelative(filePath)}`);
|
|
642
|
+
warnMigration(`Removed \`"${name}": false\` from ${displayRelative(filePath)} — this option has been deprecated. See https://github.com/oxc-project/tsgolint/issues/351, https://github.com/microsoft/TypeScript/issues/62529`, report);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
function rewriteTsconfigTypes(projectPath, silent = false, report) {
|
|
646
|
+
const files = findTsconfigFiles(projectPath);
|
|
647
|
+
for (const filePath of files) if (rewriteTypesInTsconfig(filePath)) {
|
|
648
|
+
if (report) report.removedConfigCount++;
|
|
649
|
+
if (!silent) log.success(`✔ Rewrote types in ${displayRelative(filePath)}`);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
const FRAMEWORK_SHIMS = {
|
|
653
|
+
vue: [
|
|
654
|
+
"declare module '*.vue' {",
|
|
655
|
+
" import type { DefineComponent } from 'vue';",
|
|
656
|
+
" const component: DefineComponent<{}, {}, unknown>;",
|
|
657
|
+
" export default component;",
|
|
658
|
+
"}"
|
|
659
|
+
].join("\n"),
|
|
660
|
+
astro: "/// <reference types=\"astro/client\" />"
|
|
661
|
+
};
|
|
662
|
+
function detectFramework(projectPath) {
|
|
663
|
+
const packageJsonPath = path.join(projectPath, "package.json");
|
|
664
|
+
if (!fs.existsSync(packageJsonPath)) return [];
|
|
665
|
+
const pkg = readJsonFile(packageJsonPath);
|
|
666
|
+
const allDeps = {
|
|
667
|
+
...pkg.dependencies,
|
|
668
|
+
...pkg.devDependencies
|
|
669
|
+
};
|
|
670
|
+
return ["vue", "astro"].filter((framework) => !!allDeps[framework]);
|
|
671
|
+
}
|
|
672
|
+
function getEnvDtsPath(projectPath) {
|
|
673
|
+
const srcEnvDts = path.join(projectPath, "src", "env.d.ts");
|
|
674
|
+
const rootEnvDts = path.join(projectPath, "env.d.ts");
|
|
675
|
+
for (const candidate of [srcEnvDts, rootEnvDts]) if (fs.existsSync(candidate)) return candidate;
|
|
676
|
+
return fs.existsSync(path.join(projectPath, "src")) ? srcEnvDts : rootEnvDts;
|
|
677
|
+
}
|
|
678
|
+
function hasFrameworkShim(projectPath, framework) {
|
|
679
|
+
const dirsToScan = [projectPath, path.join(projectPath, "src")];
|
|
680
|
+
for (const dir of dirsToScan) {
|
|
681
|
+
if (!fs.existsSync(dir)) continue;
|
|
682
|
+
let entries;
|
|
683
|
+
try {
|
|
684
|
+
entries = fs.readdirSync(dir);
|
|
685
|
+
} catch {
|
|
686
|
+
continue;
|
|
687
|
+
}
|
|
688
|
+
for (const entry of entries) {
|
|
689
|
+
if (!entry.endsWith(".d.ts")) continue;
|
|
690
|
+
const content = fs.readFileSync(path.join(dir, entry), "utf-8");
|
|
691
|
+
if (framework === "astro") {
|
|
692
|
+
if (content.includes("astro/client")) return true;
|
|
693
|
+
} else if (content.includes(`'*.${framework}'`) || content.includes(`"*.${framework}"`)) return true;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
return false;
|
|
697
|
+
}
|
|
698
|
+
function addFrameworkShim(projectPath, framework, report) {
|
|
699
|
+
const envDtsPath = getEnvDtsPath(projectPath);
|
|
700
|
+
const shim = FRAMEWORK_SHIMS[framework];
|
|
701
|
+
if (fs.existsSync(envDtsPath)) {
|
|
702
|
+
const existing = fs.readFileSync(envDtsPath, "utf-8");
|
|
703
|
+
fs.writeFileSync(envDtsPath, `${existing.trimEnd()}\n\n${shim}\n`, "utf-8");
|
|
704
|
+
} else {
|
|
705
|
+
fs.mkdirSync(path.dirname(envDtsPath), { recursive: true });
|
|
706
|
+
fs.writeFileSync(envDtsPath, `${shim}\n`, "utf-8");
|
|
707
|
+
}
|
|
708
|
+
if (report) report.frameworkShimAdded = true;
|
|
709
|
+
}
|
|
710
|
+
/**
|
|
711
|
+
* Rewrite standalone project to add vite-plus dependencies
|
|
712
|
+
* @param projectPath - The path to the project
|
|
713
|
+
*/
|
|
714
|
+
function rewriteStandaloneProject(projectPath, workspaceInfo, skipStagedMigration, silent = false, report) {
|
|
715
|
+
const packageJsonPath = path.join(projectPath, "package.json");
|
|
716
|
+
if (!fs.existsSync(packageJsonPath)) return;
|
|
717
|
+
const packageManager = workspaceInfo.packageManager;
|
|
718
|
+
const catalogDependencyResolver = createCatalogDependencyResolver(projectPath, packageManager);
|
|
719
|
+
let extractedStagedConfig = null;
|
|
720
|
+
let remainingPnpmOverrides;
|
|
721
|
+
let shouldRewritePnpmWorkspaceYaml = false;
|
|
722
|
+
let shouldAddPnpmWorkspaceVitePlusOverride = false;
|
|
723
|
+
let usePnpmWorkspaceYaml = false;
|
|
724
|
+
editJsonFile(packageJsonPath, (pkg) => {
|
|
725
|
+
if (packageManager === PackageManager.yarn) pkg.resolutions = {
|
|
726
|
+
...pkg.resolutions,
|
|
727
|
+
...VITE_PLUS_OVERRIDE_PACKAGES
|
|
728
|
+
};
|
|
729
|
+
else if (packageManager === PackageManager.npm || packageManager === PackageManager.bun) pkg.overrides = {
|
|
730
|
+
...pkg.overrides,
|
|
731
|
+
...VITE_PLUS_OVERRIDE_PACKAGES
|
|
732
|
+
};
|
|
733
|
+
else if (packageManager === PackageManager.pnpm) {
|
|
734
|
+
usePnpmWorkspaceYaml = !pkg.pnpm;
|
|
735
|
+
if (usePnpmWorkspaceYaml) {
|
|
736
|
+
shouldRewritePnpmWorkspaceYaml = true;
|
|
737
|
+
shouldAddPnpmWorkspaceVitePlusOverride = isForceOverrideMode();
|
|
738
|
+
}
|
|
739
|
+
const overrideKeys = Object.keys(VITE_PLUS_OVERRIDE_PACKAGES);
|
|
740
|
+
if (!usePnpmWorkspaceYaml) pkg.pnpm = {
|
|
741
|
+
...pkg.pnpm,
|
|
742
|
+
overrides: {
|
|
743
|
+
...pkg.pnpm?.overrides,
|
|
744
|
+
...VITE_PLUS_OVERRIDE_PACKAGES,
|
|
745
|
+
...isForceOverrideMode() ? { [VITE_PLUS_NAME]: VITE_PLUS_VERSION } : {}
|
|
746
|
+
},
|
|
747
|
+
peerDependencyRules: {
|
|
748
|
+
...pkg.pnpm?.peerDependencyRules,
|
|
749
|
+
allowAny: [...new Set([...pkg.pnpm?.peerDependencyRules?.allowAny ?? [], ...overrideKeys])],
|
|
750
|
+
allowedVersions: {
|
|
751
|
+
...pkg.pnpm?.peerDependencyRules?.allowedVersions,
|
|
752
|
+
...Object.fromEntries(overrideKeys.map((key) => [key, "*"]))
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
};
|
|
756
|
+
else remainingPnpmOverrides = cleanupPnpmOverridesForWorkspaceYaml(pkg, overrideKeys);
|
|
757
|
+
for (const key in pkg.pnpm?.overrides) if (key.includes(">")) {
|
|
758
|
+
const splits = key.split(">");
|
|
759
|
+
if (splits[splits.length - 1].trim() === "vite") delete pkg.pnpm.overrides[key];
|
|
760
|
+
}
|
|
761
|
+
for (const key of [...overrideKeys, ...REMOVE_PACKAGES]) if (pkg.resolutions?.[key]) delete pkg.resolutions[key];
|
|
762
|
+
}
|
|
763
|
+
extractedStagedConfig = rewritePackageJson(pkg, packageManager, usePnpmWorkspaceYaml, skipStagedMigration, catalogDependencyResolver);
|
|
764
|
+
if (!pkg.devDependencies?.["vite-plus"] || isForceOverrideMode()) {
|
|
765
|
+
const version = usePnpmWorkspaceYaml && !VITE_PLUS_VERSION.startsWith("file:") ? "catalog:" : VITE_PLUS_VERSION;
|
|
766
|
+
pkg.devDependencies = {
|
|
767
|
+
...pkg.devDependencies,
|
|
768
|
+
[VITE_PLUS_NAME]: version
|
|
769
|
+
};
|
|
770
|
+
}
|
|
771
|
+
return pkg;
|
|
772
|
+
});
|
|
773
|
+
if (shouldRewritePnpmWorkspaceYaml) rewritePnpmWorkspaceYaml(projectPath);
|
|
774
|
+
if (remainingPnpmOverrides) migratePnpmOverridesToWorkspaceYaml(projectPath, remainingPnpmOverrides);
|
|
775
|
+
if (shouldAddPnpmWorkspaceVitePlusOverride) migratePnpmOverridesToWorkspaceYaml(projectPath, { [VITE_PLUS_NAME]: VITE_PLUS_VERSION });
|
|
776
|
+
if (packageManager === PackageManager.yarn) rewriteYarnrcYml(projectPath);
|
|
777
|
+
if (extractedStagedConfig) {
|
|
778
|
+
if (mergeStagedConfigToViteConfig(projectPath, extractedStagedConfig, silent, report)) removeLintStagedFromPackageJson(packageJsonPath);
|
|
779
|
+
}
|
|
780
|
+
if (!skipStagedMigration) rewriteLintStagedConfigFile(projectPath, report);
|
|
781
|
+
cleanupDeprecatedTsconfigOptions(projectPath, silent, report);
|
|
782
|
+
rewriteTsconfigTypes(projectPath, silent, report);
|
|
783
|
+
mergeViteConfigFiles(projectPath, silent, report, workspaceInfo.packages);
|
|
784
|
+
injectLintTypeCheckDefaults(projectPath, silent, report);
|
|
785
|
+
injectFmtDefaults(projectPath, silent, report);
|
|
786
|
+
mergeTsdownConfigFile(projectPath, silent, report);
|
|
787
|
+
rewriteAllImports(projectPath, silent, report);
|
|
788
|
+
setPackageManager(projectPath, workspaceInfo.downloadPackageManager);
|
|
789
|
+
}
|
|
790
|
+
/**
|
|
791
|
+
* Rewrite monorepo to add vite-plus dependencies
|
|
792
|
+
* @param workspaceInfo - The workspace info
|
|
793
|
+
*/
|
|
794
|
+
function rewriteMonorepo(workspaceInfo, skipStagedMigration, silent = false, report) {
|
|
795
|
+
const catalogDependencyResolver = createCatalogDependencyResolver(workspaceInfo.rootDir, workspaceInfo.packageManager);
|
|
796
|
+
if (workspaceInfo.packageManager === PackageManager.pnpm) rewritePnpmWorkspaceYaml(workspaceInfo.rootDir);
|
|
797
|
+
else if (workspaceInfo.packageManager === PackageManager.yarn) rewriteYarnrcYml(workspaceInfo.rootDir);
|
|
798
|
+
else if (workspaceInfo.packageManager === PackageManager.bun) rewriteBunCatalog(workspaceInfo.rootDir);
|
|
799
|
+
rewriteRootWorkspacePackageJson(workspaceInfo.rootDir, workspaceInfo.packageManager, skipStagedMigration, catalogDependencyResolver, workspaceInfo.packages);
|
|
800
|
+
const workspaceContext = {
|
|
801
|
+
rootDir: workspaceInfo.rootDir,
|
|
802
|
+
packages: workspaceInfo.packages
|
|
803
|
+
};
|
|
804
|
+
for (const pkg of workspaceInfo.packages) rewriteMonorepoProject(path.join(workspaceInfo.rootDir, pkg.path), workspaceInfo.packageManager, skipStagedMigration, silent, report, catalogDependencyResolver, workspaceContext);
|
|
805
|
+
if (!skipStagedMigration) rewriteLintStagedConfigFile(workspaceInfo.rootDir, report);
|
|
806
|
+
cleanupDeprecatedTsconfigOptions(workspaceInfo.rootDir, silent, report);
|
|
807
|
+
rewriteTsconfigTypes(workspaceInfo.rootDir, silent, report);
|
|
808
|
+
mergeViteConfigFiles(workspaceInfo.rootDir, silent, report, workspaceInfo.packages);
|
|
809
|
+
injectLintTypeCheckDefaults(workspaceInfo.rootDir, silent, report);
|
|
810
|
+
injectFmtDefaults(workspaceInfo.rootDir, silent, report);
|
|
811
|
+
mergeTsdownConfigFile(workspaceInfo.rootDir, silent, report);
|
|
812
|
+
rewriteAllImports(workspaceInfo.rootDir, silent, report);
|
|
813
|
+
setPackageManager(workspaceInfo.rootDir, workspaceInfo.downloadPackageManager);
|
|
814
|
+
}
|
|
815
|
+
/**
|
|
816
|
+
* Rewrite monorepo project to add vite-plus dependencies
|
|
817
|
+
* @param projectPath - The path to the project
|
|
818
|
+
* @param workspaceContext - Full workspace info, used so the lint-config
|
|
819
|
+
* sanitizer can see hoisted deps living elsewhere in the workspace,
|
|
820
|
+
* not just this sub-package's own `package.json`. `rootDir` is the
|
|
821
|
+
* workspace root (paths in `packages` are relative to it); `packages`
|
|
822
|
+
* is the workspace package list.
|
|
823
|
+
*/
|
|
824
|
+
function rewriteMonorepoProject(projectPath, packageManager, skipStagedMigration, silent = false, report, catalogDependencyResolver, workspaceContext) {
|
|
825
|
+
cleanupDeprecatedTsconfigOptions(projectPath, silent, report);
|
|
826
|
+
rewriteTsconfigTypes(projectPath, silent, report);
|
|
827
|
+
mergeViteConfigFiles(projectPath, silent, report, workspaceContext?.packages, workspaceContext?.rootDir);
|
|
828
|
+
mergeTsdownConfigFile(projectPath, silent, report);
|
|
829
|
+
const packageJsonPath = path.join(projectPath, "package.json");
|
|
830
|
+
if (!fs.existsSync(packageJsonPath)) return;
|
|
831
|
+
let extractedStagedConfig = null;
|
|
832
|
+
editJsonFile(packageJsonPath, (pkg) => {
|
|
833
|
+
extractedStagedConfig = rewritePackageJson(pkg, packageManager, true, skipStagedMigration, catalogDependencyResolver);
|
|
834
|
+
return pkg;
|
|
835
|
+
});
|
|
836
|
+
if (extractedStagedConfig) {
|
|
837
|
+
if (mergeStagedConfigToViteConfig(projectPath, extractedStagedConfig, silent, report)) removeLintStagedFromPackageJson(packageJsonPath);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
/**
|
|
841
|
+
* Rewrite pnpm-workspace.yaml to add vite-plus dependencies
|
|
842
|
+
* @param projectPath - The path to the project
|
|
843
|
+
*/
|
|
844
|
+
function rewritePnpmWorkspaceYaml(projectPath) {
|
|
845
|
+
const pnpmWorkspaceYamlPath = path.join(projectPath, "pnpm-workspace.yaml");
|
|
846
|
+
if (!fs.existsSync(pnpmWorkspaceYamlPath)) fs.writeFileSync(pnpmWorkspaceYamlPath, "");
|
|
847
|
+
editYamlFile(pnpmWorkspaceYamlPath, (doc) => {
|
|
848
|
+
rewriteCatalog(doc);
|
|
849
|
+
const overrides = doc.getIn(["overrides"]);
|
|
850
|
+
for (const key of Object.keys(VITE_PLUS_OVERRIDE_PACKAGES)) {
|
|
851
|
+
const version = getCatalogDependencySpec(getYamlMapScalarStringValue(overrides, key), VITE_PLUS_OVERRIDE_PACKAGES[key], true);
|
|
852
|
+
doc.setIn(["overrides", scalarString(key)], scalarString(version));
|
|
853
|
+
}
|
|
854
|
+
const updatedOverrides = doc.getIn(["overrides"]);
|
|
855
|
+
for (const item of updatedOverrides.items) if (item.key.value.includes(">")) {
|
|
856
|
+
const splits = item.key.value.split(">");
|
|
857
|
+
if (splits[splits.length - 1].trim() === "vite") updatedOverrides.delete(item.key);
|
|
858
|
+
}
|
|
859
|
+
let allowAny = doc.getIn(["peerDependencyRules", "allowAny"]);
|
|
860
|
+
if (!allowAny) allowAny = new import_dist.YAMLSeq();
|
|
861
|
+
const existing = new Set(allowAny.items.map((n) => n.value));
|
|
862
|
+
for (const key of Object.keys(VITE_PLUS_OVERRIDE_PACKAGES)) if (!existing.has(key)) allowAny.add(scalarString(key));
|
|
863
|
+
doc.setIn(["peerDependencyRules", "allowAny"], allowAny);
|
|
864
|
+
let allowedVersions = doc.getIn(["peerDependencyRules", "allowedVersions"]);
|
|
865
|
+
if (!allowedVersions) allowedVersions = new import_dist.YAMLMap();
|
|
866
|
+
for (const key of Object.keys(VITE_PLUS_OVERRIDE_PACKAGES)) allowedVersions.set(scalarString(key), scalarString("*"));
|
|
867
|
+
doc.setIn(["peerDependencyRules", "allowedVersions"], allowedVersions);
|
|
868
|
+
if (doc.has("minimumReleaseAge")) {
|
|
869
|
+
const excludes = [
|
|
870
|
+
"vite-plus",
|
|
871
|
+
"@voidzero-dev/*",
|
|
872
|
+
"oxlint",
|
|
873
|
+
"@oxlint/*",
|
|
874
|
+
"oxlint-tsgolint",
|
|
875
|
+
"@oxlint-tsgolint/*",
|
|
876
|
+
"oxfmt",
|
|
877
|
+
"@oxfmt/*"
|
|
878
|
+
];
|
|
879
|
+
let minimumReleaseAgeExclude = doc.getIn(["minimumReleaseAgeExclude"]);
|
|
880
|
+
if (!minimumReleaseAgeExclude) minimumReleaseAgeExclude = new import_dist.YAMLSeq();
|
|
881
|
+
const existing = new Set(minimumReleaseAgeExclude.items.map((n) => n.value));
|
|
882
|
+
for (const exclude of excludes) if (!existing.has(exclude)) minimumReleaseAgeExclude.add(scalarString(exclude));
|
|
883
|
+
doc.setIn(["minimumReleaseAgeExclude"], minimumReleaseAgeExclude);
|
|
884
|
+
}
|
|
885
|
+
});
|
|
886
|
+
}
|
|
887
|
+
/**
|
|
888
|
+
* Clean up pnpm.overrides and peerDependencyRules from package.json when migrating
|
|
889
|
+
* to pnpm-workspace.yaml. Returns any remaining non-Vite overrides that need to be
|
|
890
|
+
* moved to pnpm-workspace.yaml.
|
|
891
|
+
*/
|
|
892
|
+
function cleanupPnpmOverridesForWorkspaceYaml(pkg, overrideKeys) {
|
|
893
|
+
const catalogOverrides = {};
|
|
894
|
+
const overrides = pkg.pnpm?.overrides;
|
|
895
|
+
for (const key of [...overrideKeys, ...REMOVE_PACKAGES]) {
|
|
896
|
+
const value = overrides?.[key];
|
|
897
|
+
if (value) {
|
|
898
|
+
if (overrideKeys.includes(key) && value.startsWith("catalog:")) catalogOverrides[key] = value;
|
|
899
|
+
delete overrides[key];
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
for (const key in pkg.pnpm?.overrides) if (key.includes(">")) {
|
|
903
|
+
const splits = key.split(">");
|
|
904
|
+
if (splits[splits.length - 1].trim() === "vite") delete pkg.pnpm.overrides[key];
|
|
905
|
+
}
|
|
906
|
+
let remaining;
|
|
907
|
+
if (Object.keys(catalogOverrides).length > 0) remaining = { ...catalogOverrides };
|
|
908
|
+
if (pkg.pnpm?.overrides && Object.keys(pkg.pnpm.overrides).length > 0) remaining = {
|
|
909
|
+
...remaining,
|
|
910
|
+
...pkg.pnpm.overrides
|
|
911
|
+
};
|
|
912
|
+
delete pkg.pnpm?.overrides;
|
|
913
|
+
cleanupPeerDependencyRules(pkg.pnpm?.peerDependencyRules, overrideKeys);
|
|
914
|
+
if (pkg.pnpm?.peerDependencyRules && Object.keys(pkg.pnpm.peerDependencyRules).length === 0) delete pkg.pnpm.peerDependencyRules;
|
|
915
|
+
if (pkg.pnpm && Object.keys(pkg.pnpm).length === 0) delete pkg.pnpm;
|
|
916
|
+
return remaining;
|
|
917
|
+
}
|
|
918
|
+
/**
|
|
919
|
+
* Move remaining non-Vite pnpm.overrides from package.json to pnpm-workspace.yaml.
|
|
920
|
+
* pnpm ignores workspace-level overrides when pnpm.overrides exists in package.json,
|
|
921
|
+
* so all overrides must live in pnpm-workspace.yaml.
|
|
922
|
+
*/
|
|
923
|
+
function migratePnpmOverridesToWorkspaceYaml(projectPath, overrides) {
|
|
924
|
+
editYamlFile(path.join(projectPath, "pnpm-workspace.yaml"), (doc) => {
|
|
925
|
+
for (const [key, value] of Object.entries(overrides)) doc.setIn(["overrides", scalarString(key)], scalarString(value));
|
|
926
|
+
});
|
|
927
|
+
}
|
|
928
|
+
/**
|
|
929
|
+
* Remove only Vite-managed entries from peerDependencyRules, preserving custom ones.
|
|
930
|
+
*/
|
|
931
|
+
function cleanupPeerDependencyRules(peerDependencyRules, overrideKeys) {
|
|
932
|
+
if (!peerDependencyRules) return;
|
|
933
|
+
if (Array.isArray(peerDependencyRules.allowAny)) {
|
|
934
|
+
peerDependencyRules.allowAny = peerDependencyRules.allowAny.filter((key) => !overrideKeys.includes(key));
|
|
935
|
+
if (peerDependencyRules.allowAny.length === 0) delete peerDependencyRules.allowAny;
|
|
936
|
+
}
|
|
937
|
+
if (peerDependencyRules.allowedVersions) {
|
|
938
|
+
for (const key of overrideKeys) delete peerDependencyRules.allowedVersions[key];
|
|
939
|
+
if (Object.keys(peerDependencyRules.allowedVersions).length === 0) delete peerDependencyRules.allowedVersions;
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
/**
|
|
943
|
+
* Rewrite .yarnrc.yml to add vite-plus dependencies
|
|
944
|
+
* @param projectPath - The path to the project
|
|
945
|
+
*/
|
|
946
|
+
function rewriteYarnrcYml(projectPath) {
|
|
947
|
+
const yarnrcYmlPath = path.join(projectPath, ".yarnrc.yml");
|
|
948
|
+
if (!fs.existsSync(yarnrcYmlPath)) fs.writeFileSync(yarnrcYmlPath, "");
|
|
949
|
+
editYamlFile(yarnrcYmlPath, (doc) => {
|
|
950
|
+
if (!doc.has("nodeLinker")) doc.set("nodeLinker", "node-modules");
|
|
951
|
+
rewriteCatalog(doc);
|
|
952
|
+
});
|
|
953
|
+
}
|
|
954
|
+
/**
|
|
955
|
+
* Rewrite catalog in pnpm-workspace.yaml or .yarnrc.yml
|
|
956
|
+
* @param doc - The document to rewrite
|
|
957
|
+
*/
|
|
958
|
+
function getCatalogDependencySpec(currentValue, version, supportCatalog, options) {
|
|
959
|
+
if (options?.dependencyField === "peerDependencies") {
|
|
960
|
+
if (currentValue?.startsWith("catalog:") && options.dependencyName) {
|
|
961
|
+
const resolved = options.catalogDependencyResolver?.(currentValue, options.dependencyName);
|
|
962
|
+
if (resolved && !isVitePlusOverrideSpec(resolved)) return resolved;
|
|
963
|
+
return PUBLIC_PEER_DEPENDENCY_FALLBACKS[options.dependencyName] ?? currentValue;
|
|
964
|
+
}
|
|
965
|
+
return currentValue ?? version;
|
|
966
|
+
}
|
|
967
|
+
if (options?.dependencyField === "optionalDependencies" && options?.packageManager === PackageManager.yarn) return version;
|
|
968
|
+
if (!supportCatalog || version.startsWith("file:")) return version;
|
|
969
|
+
return currentValue?.startsWith("catalog:") ? currentValue : "catalog:";
|
|
970
|
+
}
|
|
971
|
+
function isVitePlusOverrideSpec(value) {
|
|
972
|
+
return Object.values(VITE_PLUS_OVERRIDE_PACKAGES).includes(value) || value.startsWith("npm:@voidzero-dev/vite-plus-");
|
|
973
|
+
}
|
|
974
|
+
function createCatalogDependencyResolver(projectPath, packageManager) {
|
|
975
|
+
if (packageManager === PackageManager.pnpm) {
|
|
976
|
+
const pnpmWorkspaceYamlPath = path.join(projectPath, "pnpm-workspace.yaml");
|
|
977
|
+
if (!fs.existsSync(pnpmWorkspaceYamlPath)) return;
|
|
978
|
+
const doc = readYamlFile(pnpmWorkspaceYamlPath);
|
|
979
|
+
return createCatalogDependencyResolverFromCatalogs(doc?.catalog, doc?.catalogs);
|
|
980
|
+
}
|
|
981
|
+
if (packageManager === PackageManager.yarn) {
|
|
982
|
+
const yarnrcYmlPath = path.join(projectPath, ".yarnrc.yml");
|
|
983
|
+
if (!fs.existsSync(yarnrcYmlPath)) return;
|
|
984
|
+
const doc = readYamlFile(yarnrcYmlPath);
|
|
985
|
+
return createCatalogDependencyResolverFromCatalogs(doc?.catalog, doc?.catalogs);
|
|
986
|
+
}
|
|
987
|
+
if (packageManager === PackageManager.bun) {
|
|
988
|
+
const packageJsonPath = path.join(projectPath, "package.json");
|
|
989
|
+
if (!fs.existsSync(packageJsonPath)) return;
|
|
990
|
+
const pkg = readJsonFile(packageJsonPath);
|
|
991
|
+
const workspacesObj = pkg.workspaces && !Array.isArray(pkg.workspaces) ? pkg.workspaces : void 0;
|
|
992
|
+
const fromWorkspaces = createCatalogDependencyResolverFromCatalogs(workspacesObj?.catalog, workspacesObj?.catalogs);
|
|
993
|
+
const fromPkg = createCatalogDependencyResolverFromCatalogs(pkg.catalog, pkg.catalogs);
|
|
994
|
+
return (catalogSpec, dependencyName) => fromWorkspaces(catalogSpec, dependencyName) ?? fromPkg(catalogSpec, dependencyName);
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
function createCatalogDependencyResolverFromCatalogs(catalog, catalogs) {
|
|
998
|
+
return (catalogSpec, dependencyName) => {
|
|
999
|
+
const catalogName = catalogSpec.slice(8);
|
|
1000
|
+
if (catalogName && catalogName !== "default") return catalogs?.[catalogName]?.[dependencyName];
|
|
1001
|
+
return catalog?.[dependencyName];
|
|
1002
|
+
};
|
|
1003
|
+
}
|
|
1004
|
+
function getYamlMapScalarStringValue(map, key) {
|
|
1005
|
+
if (!(map instanceof import_dist.YAMLMap)) return;
|
|
1006
|
+
for (const item of map.items) if (item.key instanceof import_dist.Scalar && item.key.value === key && item.value instanceof import_dist.Scalar && typeof item.value.value === "string") return item.value.value;
|
|
1007
|
+
}
|
|
1008
|
+
function rewriteCatalog(doc) {
|
|
1009
|
+
for (const [key, value] of Object.entries(VITE_PLUS_OVERRIDE_PACKAGES)) {
|
|
1010
|
+
if (value.startsWith("file:")) continue;
|
|
1011
|
+
doc.setIn(["catalog", key], scalarString(value));
|
|
1012
|
+
}
|
|
1013
|
+
if (!VITE_PLUS_VERSION.startsWith("file:")) doc.setIn(["catalog", VITE_PLUS_NAME], scalarString(VITE_PLUS_VERSION));
|
|
1014
|
+
for (const name of REMOVE_PACKAGES) {
|
|
1015
|
+
const path = ["catalog", name];
|
|
1016
|
+
if (doc.hasIn(path)) doc.deleteIn(path);
|
|
1017
|
+
}
|
|
1018
|
+
const catalogs = doc.getIn(["catalogs"]);
|
|
1019
|
+
if (!(catalogs instanceof import_dist.YAMLMap)) return;
|
|
1020
|
+
for (const item of catalogs.items) {
|
|
1021
|
+
const catalogName = item.key instanceof import_dist.Scalar ? item.key.value : void 0;
|
|
1022
|
+
if (typeof catalogName !== "string" || !(item.value instanceof import_dist.YAMLMap)) continue;
|
|
1023
|
+
for (const [key, value] of Object.entries(VITE_PLUS_OVERRIDE_PACKAGES)) {
|
|
1024
|
+
const catalogPath = [
|
|
1025
|
+
"catalogs",
|
|
1026
|
+
catalogName,
|
|
1027
|
+
key
|
|
1028
|
+
];
|
|
1029
|
+
if (!value.startsWith("file:") && doc.hasIn(catalogPath)) doc.setIn(catalogPath, scalarString(value));
|
|
1030
|
+
}
|
|
1031
|
+
const vitePlusPath = [
|
|
1032
|
+
"catalogs",
|
|
1033
|
+
catalogName,
|
|
1034
|
+
VITE_PLUS_NAME
|
|
1035
|
+
];
|
|
1036
|
+
if (!VITE_PLUS_VERSION.startsWith("file:") && doc.hasIn(vitePlusPath)) doc.setIn(vitePlusPath, scalarString(VITE_PLUS_VERSION));
|
|
1037
|
+
for (const name of REMOVE_PACKAGES) {
|
|
1038
|
+
const catalogPath = [
|
|
1039
|
+
"catalogs",
|
|
1040
|
+
catalogName,
|
|
1041
|
+
name
|
|
1042
|
+
];
|
|
1043
|
+
if (doc.hasIn(catalogPath)) doc.deleteIn(catalogPath);
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
function rewriteCatalogObject(catalog, addMissing) {
|
|
1048
|
+
for (const [key, value] of Object.entries(VITE_PLUS_OVERRIDE_PACKAGES)) {
|
|
1049
|
+
if (value.startsWith("file:") || !addMissing && !(key in catalog)) continue;
|
|
1050
|
+
catalog[key] = value;
|
|
1051
|
+
}
|
|
1052
|
+
if (!VITE_PLUS_VERSION.startsWith("file:") && (addMissing || "vite-plus" in catalog)) catalog[VITE_PLUS_NAME] = VITE_PLUS_VERSION;
|
|
1053
|
+
for (const name of REMOVE_PACKAGES) delete catalog[name];
|
|
1054
|
+
}
|
|
1055
|
+
function rewriteCatalogsObject(catalogs) {
|
|
1056
|
+
for (const catalog of Object.values(catalogs)) rewriteCatalogObject(catalog, false);
|
|
1057
|
+
}
|
|
1058
|
+
/**
|
|
1059
|
+
* Write catalog entries to root package.json for bun.
|
|
1060
|
+
* Bun stores catalogs in package.json under the `catalog` key,
|
|
1061
|
+
* unlike pnpm which uses pnpm-workspace.yaml.
|
|
1062
|
+
* @see https://bun.sh/docs/pm/catalogs
|
|
1063
|
+
*/
|
|
1064
|
+
function rewriteBunCatalog(projectPath) {
|
|
1065
|
+
const packageJsonPath = path.join(projectPath, "package.json");
|
|
1066
|
+
if (!fs.existsSync(packageJsonPath)) return;
|
|
1067
|
+
editJsonFile(packageJsonPath, (pkg) => {
|
|
1068
|
+
const workspacesObj = pkg.workspaces && !Array.isArray(pkg.workspaces) ? pkg.workspaces : void 0;
|
|
1069
|
+
const useWorkspacesCatalog = workspacesObj?.catalog != null || pkg.catalog == null && workspacesObj?.catalogs != null;
|
|
1070
|
+
const catalog = { ...useWorkspacesCatalog ? workspacesObj?.catalog : pkg.catalog };
|
|
1071
|
+
rewriteCatalogObject(catalog, true);
|
|
1072
|
+
if (useWorkspacesCatalog) {
|
|
1073
|
+
workspacesObj.catalog = catalog;
|
|
1074
|
+
if (pkg.catalog) rewriteCatalogObject(pkg.catalog, false);
|
|
1075
|
+
} else {
|
|
1076
|
+
pkg.catalog = catalog;
|
|
1077
|
+
if (workspacesObj?.catalog) rewriteCatalogObject(workspacesObj.catalog, false);
|
|
1078
|
+
}
|
|
1079
|
+
if (workspacesObj?.catalogs) rewriteCatalogsObject(workspacesObj.catalogs);
|
|
1080
|
+
if (pkg.catalogs) rewriteCatalogsObject(pkg.catalogs);
|
|
1081
|
+
const overrides = { ...pkg.overrides };
|
|
1082
|
+
for (const [key, value] of Object.entries(VITE_PLUS_OVERRIDE_PACKAGES)) overrides[key] = getCatalogDependencySpec(overrides[key], value, true);
|
|
1083
|
+
pkg.overrides = overrides;
|
|
1084
|
+
return pkg;
|
|
1085
|
+
});
|
|
1086
|
+
}
|
|
1087
|
+
/**
|
|
1088
|
+
* Rewrite root workspace package.json to add vite-plus dependencies
|
|
1089
|
+
* @param projectPath - The path to the project
|
|
1090
|
+
*/
|
|
1091
|
+
function rewriteRootWorkspacePackageJson(projectPath, packageManager, skipStagedMigration, catalogDependencyResolver, packages) {
|
|
1092
|
+
const packageJsonPath = path.join(projectPath, "package.json");
|
|
1093
|
+
if (!fs.existsSync(packageJsonPath)) return;
|
|
1094
|
+
let remainingPnpmOverrides;
|
|
1095
|
+
editJsonFile(packageJsonPath, (pkg) => {
|
|
1096
|
+
if (packageManager === PackageManager.yarn) pkg.resolutions = {
|
|
1097
|
+
...pkg.resolutions,
|
|
1098
|
+
...VITE_PLUS_OVERRIDE_PACKAGES
|
|
1099
|
+
};
|
|
1100
|
+
else if (packageManager === PackageManager.npm) pkg.overrides = {
|
|
1101
|
+
...pkg.overrides,
|
|
1102
|
+
...VITE_PLUS_OVERRIDE_PACKAGES
|
|
1103
|
+
};
|
|
1104
|
+
else if (packageManager === PackageManager.bun) {} else if (packageManager === PackageManager.pnpm) {
|
|
1105
|
+
const overrideKeys = Object.keys(VITE_PLUS_OVERRIDE_PACKAGES);
|
|
1106
|
+
if (isForceOverrideMode()) pkg.pnpm = {
|
|
1107
|
+
...pkg.pnpm,
|
|
1108
|
+
overrides: {
|
|
1109
|
+
...pkg.pnpm?.overrides,
|
|
1110
|
+
...VITE_PLUS_OVERRIDE_PACKAGES,
|
|
1111
|
+
[VITE_PLUS_NAME]: VITE_PLUS_VERSION
|
|
1112
|
+
}
|
|
1113
|
+
};
|
|
1114
|
+
else {
|
|
1115
|
+
for (const key of [...overrideKeys, ...REMOVE_PACKAGES]) if (pkg.resolutions?.[key]) delete pkg.resolutions[key];
|
|
1116
|
+
remainingPnpmOverrides = cleanupPnpmOverridesForWorkspaceYaml(pkg, overrideKeys);
|
|
1117
|
+
}
|
|
1118
|
+
for (const key in pkg.pnpm?.overrides) if (key.includes(">")) {
|
|
1119
|
+
const splits = key.split(">");
|
|
1120
|
+
if (splits[splits.length - 1].trim() === "vite") delete pkg.pnpm.overrides[key];
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
if (!pkg.devDependencies?.["vite-plus"]) pkg.devDependencies = {
|
|
1124
|
+
...pkg.devDependencies,
|
|
1125
|
+
[VITE_PLUS_NAME]: packageManager === PackageManager.npm || VITE_PLUS_VERSION.startsWith("file:") ? VITE_PLUS_VERSION : "catalog:"
|
|
1126
|
+
};
|
|
1127
|
+
return pkg;
|
|
1128
|
+
});
|
|
1129
|
+
if (remainingPnpmOverrides) migratePnpmOverridesToWorkspaceYaml(projectPath, remainingPnpmOverrides);
|
|
1130
|
+
rewriteMonorepoProject(projectPath, packageManager, skipStagedMigration, void 0, void 0, catalogDependencyResolver, packages ? {
|
|
1131
|
+
rootDir: projectPath,
|
|
1132
|
+
packages
|
|
1133
|
+
} : void 0);
|
|
1134
|
+
}
|
|
1135
|
+
const RULES_YAML_PATH = path.join(rulesDir, "vite-tools.yml");
|
|
1136
|
+
const PREPARE_RULES_YAML_PATH = path.join(rulesDir, "vite-prepare.yml");
|
|
1137
|
+
let cachedRulesYaml;
|
|
1138
|
+
let cachedRulesYamlNoLintStaged;
|
|
1139
|
+
let cachedPrepareRulesYaml;
|
|
1140
|
+
function readRulesYaml() {
|
|
1141
|
+
cachedRulesYaml ??= fs.readFileSync(RULES_YAML_PATH, "utf8");
|
|
1142
|
+
return cachedRulesYaml;
|
|
1143
|
+
}
|
|
1144
|
+
function getScriptRulesYaml(skipStagedMigration) {
|
|
1145
|
+
const yaml = readRulesYaml();
|
|
1146
|
+
if (!skipStagedMigration) return yaml;
|
|
1147
|
+
cachedRulesYamlNoLintStaged ??= yaml.split("\n\n\n").filter((block) => !block.includes("id: replace-lint-staged")).join("\n\n\n");
|
|
1148
|
+
return cachedRulesYamlNoLintStaged;
|
|
1149
|
+
}
|
|
1150
|
+
function readPrepareRulesYaml() {
|
|
1151
|
+
cachedPrepareRulesYaml ??= fs.readFileSync(PREPARE_RULES_YAML_PATH, "utf8");
|
|
1152
|
+
return cachedPrepareRulesYaml;
|
|
1153
|
+
}
|
|
1154
|
+
function rewritePackageJson(pkg, packageManager, isMonorepo, skipStagedMigration, catalogDependencyResolver) {
|
|
1155
|
+
if (pkg.scripts) {
|
|
1156
|
+
const updated = rewriteScripts(JSON.stringify(pkg.scripts), getScriptRulesYaml(skipStagedMigration));
|
|
1157
|
+
if (updated) pkg.scripts = JSON.parse(updated);
|
|
1158
|
+
}
|
|
1159
|
+
let extractedStagedConfig = null;
|
|
1160
|
+
if (!skipStagedMigration && pkg["lint-staged"]) {
|
|
1161
|
+
const config = pkg["lint-staged"];
|
|
1162
|
+
const updated = rewriteScripts(JSON.stringify(config), readRulesYaml());
|
|
1163
|
+
extractedStagedConfig = updated ? JSON.parse(updated) : config;
|
|
1164
|
+
}
|
|
1165
|
+
const supportCatalog = !!isMonorepo && packageManager !== PackageManager.npm;
|
|
1166
|
+
let needVitePlus = false;
|
|
1167
|
+
const dependencyGroups = [
|
|
1168
|
+
{
|
|
1169
|
+
dependencyField: "devDependencies",
|
|
1170
|
+
dependencies: pkg.devDependencies
|
|
1171
|
+
},
|
|
1172
|
+
{
|
|
1173
|
+
dependencyField: "dependencies",
|
|
1174
|
+
dependencies: pkg.dependencies
|
|
1175
|
+
},
|
|
1176
|
+
{
|
|
1177
|
+
dependencyField: "peerDependencies",
|
|
1178
|
+
dependencies: pkg.peerDependencies
|
|
1179
|
+
},
|
|
1180
|
+
{
|
|
1181
|
+
dependencyField: "optionalDependencies",
|
|
1182
|
+
dependencies: pkg.optionalDependencies
|
|
1183
|
+
}
|
|
1184
|
+
];
|
|
1185
|
+
for (const [key, version] of Object.entries(VITE_PLUS_OVERRIDE_PACKAGES)) for (const { dependencyField, dependencies } of dependencyGroups) if (dependencies?.[key]) {
|
|
1186
|
+
dependencies[key] = getCatalogDependencySpec(dependencies[key], version, supportCatalog, {
|
|
1187
|
+
dependencyField,
|
|
1188
|
+
dependencyName: key,
|
|
1189
|
+
packageManager,
|
|
1190
|
+
catalogDependencyResolver
|
|
1191
|
+
});
|
|
1192
|
+
needVitePlus = true;
|
|
1193
|
+
}
|
|
1194
|
+
for (const name of REMOVE_PACKAGES) {
|
|
1195
|
+
let wasRemoved = false;
|
|
1196
|
+
for (const { dependencies } of dependencyGroups) if (dependencies?.[name]) {
|
|
1197
|
+
delete dependencies[name];
|
|
1198
|
+
wasRemoved = true;
|
|
1199
|
+
}
|
|
1200
|
+
if (wasRemoved) needVitePlus = true;
|
|
1201
|
+
const peerDep = BROWSER_PROVIDER_PEER_DEPS[name];
|
|
1202
|
+
if (wasRemoved && peerDep && !pkg.devDependencies?.[peerDep] && !pkg.dependencies?.[peerDep] && !pkg.peerDependencies?.[peerDep] && !pkg.optionalDependencies?.[peerDep]) {
|
|
1203
|
+
pkg.devDependencies ??= {};
|
|
1204
|
+
pkg.devDependencies[peerDep] = "*";
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
const canonicalVitePlusSpec = supportCatalog && !VITE_PLUS_VERSION.startsWith("file:") ? "catalog:" : VITE_PLUS_VERSION;
|
|
1208
|
+
const existingVitePlus = pkg.devDependencies?.[VITE_PLUS_NAME];
|
|
1209
|
+
const shouldNormalizeExistingVitePlus = !!existingVitePlus && supportCatalog && existingVitePlus !== canonicalVitePlusSpec && !isProtocolPinnedSpec(existingVitePlus);
|
|
1210
|
+
if (needVitePlus || shouldNormalizeExistingVitePlus) pkg.devDependencies = {
|
|
1211
|
+
...pkg.devDependencies,
|
|
1212
|
+
[VITE_PLUS_NAME]: canonicalVitePlusSpec
|
|
1213
|
+
};
|
|
1214
|
+
if (needVitePlus) {
|
|
1215
|
+
const installableDeps = {
|
|
1216
|
+
...pkg.dependencies,
|
|
1217
|
+
...pkg.devDependencies,
|
|
1218
|
+
...pkg.optionalDependencies
|
|
1219
|
+
};
|
|
1220
|
+
if (!installableDeps.vitest && Object.keys(installableDeps).some((name) => name.includes("vitest"))) {
|
|
1221
|
+
const ver = VITE_PLUS_OVERRIDE_PACKAGES.vitest;
|
|
1222
|
+
pkg.devDependencies ??= {};
|
|
1223
|
+
pkg.devDependencies.vitest = getCatalogDependencySpec(void 0, ver, supportCatalog);
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
return extractedStagedConfig;
|
|
1227
|
+
}
|
|
1228
|
+
function isProtocolPinnedSpec(spec) {
|
|
1229
|
+
return /^(catalog:|workspace:|link:|file:|npm:|github:|git[+:]|https?:\/\/)/.test(spec);
|
|
1230
|
+
}
|
|
1231
|
+
function removeLintStagedFromPackageJson(packageJsonPath) {
|
|
1232
|
+
editJsonFile(packageJsonPath, (pkg) => {
|
|
1233
|
+
if (pkg["lint-staged"]) {
|
|
1234
|
+
delete pkg["lint-staged"];
|
|
1235
|
+
return pkg;
|
|
1236
|
+
}
|
|
1237
|
+
});
|
|
1238
|
+
}
|
|
1239
|
+
function rewriteLintStagedConfigFile(projectPath, report) {
|
|
1240
|
+
let hasUnsupported = false;
|
|
1241
|
+
for (const filename of LINT_STAGED_JSON_CONFIG_FILES) {
|
|
1242
|
+
const configPath = path.join(projectPath, filename);
|
|
1243
|
+
if (!fs.existsSync(configPath)) continue;
|
|
1244
|
+
if (filename === ".lintstagedrc" && !isJsonFile(configPath)) {
|
|
1245
|
+
warnMigration(`${displayRelative(configPath)} is not JSON format — please migrate to "staged" in vite.config.ts manually`, report);
|
|
1246
|
+
hasUnsupported = true;
|
|
1247
|
+
continue;
|
|
1248
|
+
}
|
|
1249
|
+
if (!hasStagedConfigInViteConfig(projectPath)) {
|
|
1250
|
+
const config = readJsonFile(configPath);
|
|
1251
|
+
const updated = rewriteScripts(JSON.stringify(config), readRulesYaml());
|
|
1252
|
+
if (!mergeStagedConfigToViteConfig(projectPath, updated ? JSON.parse(updated) : config, true, report)) continue;
|
|
1253
|
+
fs.unlinkSync(configPath);
|
|
1254
|
+
if (report) report.inlinedLintStagedConfigCount++;
|
|
1255
|
+
} else warnMigration(`${displayRelative(configPath)} found but "staged" already exists in vite.config.ts — please merge manually`, report);
|
|
1256
|
+
}
|
|
1257
|
+
for (const filename of LINT_STAGED_OTHER_CONFIG_FILES) {
|
|
1258
|
+
const configPath = path.join(projectPath, filename);
|
|
1259
|
+
if (!fs.existsSync(configPath)) continue;
|
|
1260
|
+
warnMigration(`${displayRelative(configPath)} — please migrate to "staged" in vite.config.ts manually`, report);
|
|
1261
|
+
hasUnsupported = true;
|
|
1262
|
+
}
|
|
1263
|
+
if (hasUnsupported) infoMigration("Only \"staged\" in vite.config.ts is supported. See https://viteplus.dev/guide/migrate#lint-staged", report);
|
|
1264
|
+
}
|
|
1265
|
+
/**
|
|
1266
|
+
* Ensure vite.config.ts exists, create it if not
|
|
1267
|
+
* @returns The vite config filename
|
|
1268
|
+
*/
|
|
1269
|
+
function ensureViteConfig(projectPath, configs, silent = false, report) {
|
|
1270
|
+
if (!configs.viteConfig) {
|
|
1271
|
+
configs.viteConfig = "vite.config.ts";
|
|
1272
|
+
const viteConfigPath = path.join(projectPath, "vite.config.ts");
|
|
1273
|
+
fs.writeFileSync(viteConfigPath, `import { defineConfig } from '${VITE_PLUS_NAME}';
|
|
1274
|
+
|
|
1275
|
+
export default defineConfig({});
|
|
1276
|
+
`);
|
|
1277
|
+
if (report) report.createdViteConfigCount++;
|
|
1278
|
+
if (!silent) log.success(`✔ Created vite.config.ts in ${displayRelative(viteConfigPath)}`);
|
|
1279
|
+
}
|
|
1280
|
+
return configs.viteConfig;
|
|
1281
|
+
}
|
|
1282
|
+
/**
|
|
1283
|
+
* Merge tsdown.config.* into vite.config.ts
|
|
1284
|
+
* - For JSON files: merge content directly into `pack` field and delete the JSON file
|
|
1285
|
+
* - For TS/JS files: import the config file
|
|
1286
|
+
*/
|
|
1287
|
+
function mergeTsdownConfigFile(projectPath, silent = false, report) {
|
|
1288
|
+
const configs = detectConfigs(projectPath);
|
|
1289
|
+
if (!configs.tsdownConfig) return;
|
|
1290
|
+
const viteConfig = ensureViteConfig(projectPath, configs, silent, report);
|
|
1291
|
+
const fullViteConfigPath = path.join(projectPath, viteConfig);
|
|
1292
|
+
const fullTsdownConfigPath = path.join(projectPath, configs.tsdownConfig);
|
|
1293
|
+
if (configs.tsdownConfig.endsWith(".json")) {
|
|
1294
|
+
mergeAndRemoveJsonConfig(projectPath, viteConfig, configs.tsdownConfig, "pack", silent, report);
|
|
1295
|
+
return;
|
|
1296
|
+
}
|
|
1297
|
+
const result = mergeTsdownConfig(fullViteConfigPath, `./${configs.tsdownConfig}`);
|
|
1298
|
+
if (result.updated) {
|
|
1299
|
+
fs.writeFileSync(fullViteConfigPath, result.content);
|
|
1300
|
+
if (report) report.tsdownImportCount++;
|
|
1301
|
+
if (!silent) log.success(`✔ Added import for ${displayRelative(fullTsdownConfigPath)} in ${displayRelative(fullViteConfigPath)}`);
|
|
1302
|
+
}
|
|
1303
|
+
infoMigration(`Please manually merge ${displayRelative(fullTsdownConfigPath)} into ${displayRelative(fullViteConfigPath)}, see https://viteplus.dev/guide/migrate#tsdown`, report);
|
|
1304
|
+
}
|
|
1305
|
+
/**
|
|
1306
|
+
* Best-effort: derive the Oxlint rule-namespace a JS plugin package
|
|
1307
|
+
* contributes. Mirrors the conventions @oxlint/migrate uses when
|
|
1308
|
+
* translating ESLint configs, and the conventions Oxlint-native plugin
|
|
1309
|
+
* authors use (`oxlint-plugin-<name>` — see posva/pinia-colada in the
|
|
1310
|
+
* wild):
|
|
1311
|
+
* `eslint-plugin-unocss` → `unocss` (rules: `unocss/order`)
|
|
1312
|
+
* `oxlint-plugin-posva` → `posva` (rules: `posva/foo`)
|
|
1313
|
+
* `@stylistic/eslint-plugin` → `@stylistic` (rules: `@stylistic/indent`)
|
|
1314
|
+
* `@stylistic/eslint-plugin-ts` → `@stylistic/ts` (rules: `@stylistic/ts/indent`)
|
|
1315
|
+
* `@scope/oxlint-plugin-x` → `@scope/x`
|
|
1316
|
+
* anything else → the package name verbatim
|
|
1317
|
+
*/
|
|
1318
|
+
function deriveJsPluginNamespace(packageName) {
|
|
1319
|
+
for (const prefix of ["eslint-plugin-", "oxlint-plugin-"]) if (packageName.startsWith(prefix)) return packageName.slice(prefix.length) || packageName;
|
|
1320
|
+
const scoped = packageName.match(/^(@[^/]+)\/(?:eslint|oxlint)-plugin(?:-(.+))?$/);
|
|
1321
|
+
if (scoped) return scoped[2] ? `${scoped[1]}/${scoped[2]}` : scoped[1];
|
|
1322
|
+
return packageName;
|
|
1323
|
+
}
|
|
1324
|
+
/**
|
|
1325
|
+
* Collect every dependency name declared across the root + workspace
|
|
1326
|
+
* `package.json` files after the ESLint cleanup has run. Used to verify
|
|
1327
|
+
* that JS plugins referenced by the generated `.oxlintrc.json` are
|
|
1328
|
+
* actually installable.
|
|
1329
|
+
*/
|
|
1330
|
+
function collectInstalledPackageNames(projectPath, packages) {
|
|
1331
|
+
const names = /* @__PURE__ */ new Set();
|
|
1332
|
+
const paths = [projectPath, ...(packages ?? []).map((p) => path.join(projectPath, p.path))];
|
|
1333
|
+
for (const dir of paths) {
|
|
1334
|
+
const pkgJsonPath = path.join(dir, "package.json");
|
|
1335
|
+
if (!fs.existsSync(pkgJsonPath)) continue;
|
|
1336
|
+
let pkg;
|
|
1337
|
+
try {
|
|
1338
|
+
pkg = readJsonFile(pkgJsonPath);
|
|
1339
|
+
} catch {
|
|
1340
|
+
continue;
|
|
1341
|
+
}
|
|
1342
|
+
for (const field of [
|
|
1343
|
+
"devDependencies",
|
|
1344
|
+
"dependencies",
|
|
1345
|
+
"peerDependencies",
|
|
1346
|
+
"optionalDependencies"
|
|
1347
|
+
]) {
|
|
1348
|
+
const deps = pkg[field];
|
|
1349
|
+
if (deps) for (const name of Object.keys(deps)) names.add(name);
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
return names;
|
|
1353
|
+
}
|
|
1354
|
+
/**
|
|
1355
|
+
* Test whether a rule key (e.g. `@stylistic/ts/indent`) belongs to any
|
|
1356
|
+
* namespace in `namespaces`. We can't just split on the first `/` —
|
|
1357
|
+
* `@stylistic/eslint-plugin-ts` contributes the multi-segment namespace
|
|
1358
|
+
* `@stylistic/ts`, so the lookup has to try progressively longer
|
|
1359
|
+
* prefixes until one matches or we run out of slashes.
|
|
1360
|
+
*/
|
|
1361
|
+
function ruleKeyMatchesNamespace(key, namespaces) {
|
|
1362
|
+
if (!key.includes("/")) return true;
|
|
1363
|
+
let idx = key.indexOf("/");
|
|
1364
|
+
while (idx !== -1) {
|
|
1365
|
+
if (namespaces.has(key.slice(0, idx))) return true;
|
|
1366
|
+
idx = key.indexOf("/", idx + 1);
|
|
1367
|
+
}
|
|
1368
|
+
return false;
|
|
1369
|
+
}
|
|
1370
|
+
/** Filter a rules object to only entries whose namespace is recognized. */
|
|
1371
|
+
function filterRulesAgainstNamespaces(rules, namespaces) {
|
|
1372
|
+
const out = {};
|
|
1373
|
+
for (const [key, value] of Object.entries(rules)) if (ruleKeyMatchesNamespace(key, namespaces)) out[key] = value;
|
|
1374
|
+
return out;
|
|
1375
|
+
}
|
|
1376
|
+
/**
|
|
1377
|
+
* Sort a jsPlugins array into installed entries (kept) and string
|
|
1378
|
+
* entries for packages that aren't present in the workspace. Object-form
|
|
1379
|
+
* entries (`{ name, specifier }`) and string entries that look like
|
|
1380
|
+
* local paths (`./X`, `/X`, `../X`) are passed through — Oxlint resolves
|
|
1381
|
+
* them itself.
|
|
1382
|
+
*/
|
|
1383
|
+
function partitionJsPlugins(entries, availablePackages) {
|
|
1384
|
+
const kept = [];
|
|
1385
|
+
const dropped = [];
|
|
1386
|
+
for (const entry of entries) {
|
|
1387
|
+
if (typeof entry !== "string") {
|
|
1388
|
+
kept.push(entry);
|
|
1389
|
+
continue;
|
|
1390
|
+
}
|
|
1391
|
+
if (entry.startsWith("./") || entry.startsWith("../") || entry.startsWith("/")) {
|
|
1392
|
+
kept.push(entry);
|
|
1393
|
+
continue;
|
|
1394
|
+
}
|
|
1395
|
+
if (availablePackages.has(entry)) kept.push(entry);
|
|
1396
|
+
else dropped.push(entry);
|
|
1397
|
+
}
|
|
1398
|
+
return {
|
|
1399
|
+
kept,
|
|
1400
|
+
dropped
|
|
1401
|
+
};
|
|
1402
|
+
}
|
|
1403
|
+
/** Build the set of rule-key namespaces backed by a given jsPlugins set. */
|
|
1404
|
+
function jsPluginsToNamespaces(entries) {
|
|
1405
|
+
const ns = /* @__PURE__ */ new Set();
|
|
1406
|
+
for (const entry of entries) if (typeof entry === "string") ns.add(deriveJsPluginNamespace(entry));
|
|
1407
|
+
else if (entry && typeof entry === "object" && "name" in entry && entry.name) ns.add(entry.name);
|
|
1408
|
+
ns.delete("");
|
|
1409
|
+
return ns;
|
|
1410
|
+
}
|
|
1411
|
+
/**
|
|
1412
|
+
* Sanitize the `.oxlintrc.json` produced by `@oxlint/migrate` (in-place)
|
|
1413
|
+
* before it gets merged into `vite.config.ts`. Drop references that
|
|
1414
|
+
* won't resolve at lint time and warn the user.
|
|
1415
|
+
*
|
|
1416
|
+
* Why: `@oxlint/migrate` can emit `jsPlugins[]` / `plugins[]` / `rules`
|
|
1417
|
+
* entries referring to packages the user never installed (e.g.
|
|
1418
|
+
* translating `@unocss/eslint-config` into `eslint-plugin-unocss`),
|
|
1419
|
+
* to plugins outside Oxlint's native set, or under namespaces no
|
|
1420
|
+
* surviving plugin contributes. Without sanitization, `vp lint` aborts
|
|
1421
|
+
* with "Failed to load JS plugin" / "Plugin not found" before running
|
|
1422
|
+
* any rule. This produces a degraded-but-functional config instead.
|
|
1423
|
+
*
|
|
1424
|
+
* Per-override entries (`overrides[].jsPlugins`, `.plugins`, `.rules`)
|
|
1425
|
+
* are sanitized independently — an override can introduce its own
|
|
1426
|
+
* jsPlugin, so namespace availability is computed per-override (base
|
|
1427
|
+
* namespaces ∪ the override's own surviving jsPlugins' namespaces).
|
|
1428
|
+
*/
|
|
1429
|
+
function sanitizeMigratedOxlintConfig(config, availablePackages, report) {
|
|
1430
|
+
const allDroppedJsPlugins = /* @__PURE__ */ new Set();
|
|
1431
|
+
const allDroppedPlugins = /* @__PURE__ */ new Set();
|
|
1432
|
+
const baseSplit = partitionJsPlugins(config.jsPlugins ?? [], availablePackages);
|
|
1433
|
+
for (const n of baseSplit.dropped) allDroppedJsPlugins.add(n);
|
|
1434
|
+
if (config.jsPlugins && baseSplit.dropped.length > 0) config.jsPlugins = baseSplit.kept;
|
|
1435
|
+
const baseNamespaces = new Set(OXLINT_NATIVE_PLUGINS);
|
|
1436
|
+
for (const ns of jsPluginsToNamespaces(baseSplit.kept)) baseNamespaces.add(ns);
|
|
1437
|
+
if (config.plugins) {
|
|
1438
|
+
const keptPlugins = [];
|
|
1439
|
+
for (const p of config.plugins) if (baseNamespaces.has(p)) keptPlugins.push(p);
|
|
1440
|
+
else allDroppedPlugins.add(p);
|
|
1441
|
+
if (keptPlugins.length !== config.plugins.length) config.plugins = keptPlugins;
|
|
1442
|
+
}
|
|
1443
|
+
if (config.rules) {
|
|
1444
|
+
const filtered = filterRulesAgainstNamespaces(config.rules, baseNamespaces);
|
|
1445
|
+
if (Object.keys(filtered).length !== Object.keys(config.rules).length) config.rules = filtered;
|
|
1446
|
+
}
|
|
1447
|
+
if (Array.isArray(config.overrides)) for (const override of config.overrides) {
|
|
1448
|
+
let overrideSurvivors = [];
|
|
1449
|
+
if (override.jsPlugins) {
|
|
1450
|
+
const split = partitionJsPlugins(override.jsPlugins, availablePackages);
|
|
1451
|
+
for (const n of split.dropped) allDroppedJsPlugins.add(n);
|
|
1452
|
+
if (split.dropped.length > 0) override.jsPlugins = split.kept;
|
|
1453
|
+
overrideSurvivors = split.kept;
|
|
1454
|
+
}
|
|
1455
|
+
const overrideNamespaces = new Set(baseNamespaces);
|
|
1456
|
+
for (const ns of jsPluginsToNamespaces(overrideSurvivors)) overrideNamespaces.add(ns);
|
|
1457
|
+
if (override.plugins) {
|
|
1458
|
+
const keptOverridePlugins = [];
|
|
1459
|
+
for (const p of override.plugins) if (overrideNamespaces.has(p)) keptOverridePlugins.push(p);
|
|
1460
|
+
else allDroppedPlugins.add(p);
|
|
1461
|
+
if (keptOverridePlugins.length !== override.plugins.length) override.plugins = keptOverridePlugins;
|
|
1462
|
+
}
|
|
1463
|
+
if (override.rules) {
|
|
1464
|
+
const filtered = filterRulesAgainstNamespaces(override.rules, overrideNamespaces);
|
|
1465
|
+
if (Object.keys(filtered).length !== Object.keys(override.rules).length) override.rules = filtered;
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
if (allDroppedJsPlugins.size > 0) warnMigration(`Stripped JS plugin reference(s) from the generated lint config: ${[...allDroppedJsPlugins].join(", ")}. No matching package is present in this workspace, so loading them at lint time would fail. If you want their Oxlint coverage back, install each package (e.g. \`vp install <name>\`) and add its name back to \`lint.jsPlugins\` in vite.config.ts.`, report);
|
|
1469
|
+
if (allDroppedPlugins.size > 0) warnMigration(`Stripped unknown plugin reference(s) from the generated lint config: ${[...allDroppedPlugins].join(", ")}. These aren't native Oxlint plugins and no surviving JS plugin contributes them.`, report);
|
|
1470
|
+
}
|
|
1471
|
+
/**
|
|
1472
|
+
* Merge oxlint and oxfmt config into vite.config.ts
|
|
1473
|
+
*/
|
|
1474
|
+
function mergeViteConfigFiles(projectPath, silent = false, report, packages, workspaceRoot) {
|
|
1475
|
+
const configs = detectConfigs(projectPath);
|
|
1476
|
+
if (!configs.oxfmtConfig && !configs.oxlintConfig) return;
|
|
1477
|
+
const viteConfig = ensureViteConfig(projectPath, configs, silent, report);
|
|
1478
|
+
if (configs.oxlintConfig) {
|
|
1479
|
+
const fullOxlintPath = path.join(projectPath, configs.oxlintConfig);
|
|
1480
|
+
const oxlintJson = readJsonFile(fullOxlintPath, true);
|
|
1481
|
+
if (!oxlintJson.options) oxlintJson.options = {};
|
|
1482
|
+
if (!hasBaseUrlInTsconfig(projectPath)) {
|
|
1483
|
+
if (oxlintJson.options.typeAware === void 0) oxlintJson.options.typeAware = true;
|
|
1484
|
+
if (oxlintJson.options.typeCheck === void 0) oxlintJson.options.typeCheck = true;
|
|
1485
|
+
} else warnMigration(BASEURL_TSCONFIG_WARNING, report);
|
|
1486
|
+
sanitizeMigratedOxlintConfig(oxlintJson, collectInstalledPackageNames(workspaceRoot ?? projectPath, packages), report);
|
|
1487
|
+
const normalizedOxlintConfig = ensureVitePlusImportRuleDefaults(oxlintJson);
|
|
1488
|
+
fs.writeFileSync(fullOxlintPath, JSON.stringify(normalizedOxlintConfig, null, 2));
|
|
1489
|
+
mergeAndRemoveJsonConfig(projectPath, viteConfig, configs.oxlintConfig, "lint", silent, report);
|
|
1490
|
+
}
|
|
1491
|
+
if (configs.oxfmtConfig) mergeAndRemoveJsonConfig(projectPath, viteConfig, configs.oxfmtConfig, "fmt", silent, report);
|
|
1492
|
+
}
|
|
1493
|
+
/**
|
|
1494
|
+
* Inject typeAware and typeCheck defaults into vite.config.ts lint config.
|
|
1495
|
+
* Called after mergeViteConfigFiles() to handle the case where no .oxlintrc.json exists
|
|
1496
|
+
* (e.g., newly created projects from create-vite templates).
|
|
1497
|
+
*/
|
|
1498
|
+
function injectLintTypeCheckDefaults(projectPath, silent = false, report) {
|
|
1499
|
+
if (hasBaseUrlInTsconfig(projectPath)) {
|
|
1500
|
+
warnMigration(BASEURL_TSCONFIG_WARNING, report);
|
|
1501
|
+
return;
|
|
1502
|
+
}
|
|
1503
|
+
injectConfigDefaults(projectPath, "lint", ".vite-plus-lint-init.oxlintrc.json", JSON.stringify(createDefaultVitePlusLintConfig({ includeTypeAwareDefaults: true })), silent, report);
|
|
1504
|
+
}
|
|
1505
|
+
function injectFmtDefaults(projectPath, silent = false, report) {
|
|
1506
|
+
injectConfigDefaults(projectPath, "fmt", ".vite-plus-fmt-init.oxfmtrc.json", JSON.stringify({}), silent, report);
|
|
1507
|
+
}
|
|
1508
|
+
/**
|
|
1509
|
+
* Wire `create.defaultTemplate: '<scope>'` into the new monorepo's
|
|
1510
|
+
* `vite.config.ts`. The caller is `bin.ts`, only when scaffolding a
|
|
1511
|
+
* monorepo from a bundled `@org` manifest entry — that's the case where
|
|
1512
|
+
* the user just picked a template from a specific org and naturally
|
|
1513
|
+
* wants subsequent `vp create` invocations from the workspace to default
|
|
1514
|
+
* to that same org's picker.
|
|
1515
|
+
*/
|
|
1516
|
+
function injectCreateDefaultTemplate(projectPath, scope, silent = false, report) {
|
|
1517
|
+
if (!scope) return;
|
|
1518
|
+
injectConfigDefaults(projectPath, "create", ".vite-plus-create-init.json", JSON.stringify({ defaultTemplate: scope }), silent, report);
|
|
1519
|
+
}
|
|
1520
|
+
function injectConfigDefaults(projectPath, configKey, tempFileName, tempFileContent, silent, report) {
|
|
1521
|
+
const configs = detectConfigs(projectPath);
|
|
1522
|
+
if (configs.viteConfig && hasConfigKey(path.join(projectPath, configs.viteConfig), configKey)) return;
|
|
1523
|
+
const viteConfig = ensureViteConfig(projectPath, configs, silent, report);
|
|
1524
|
+
const tempConfigPath = path.join(projectPath, tempFileName);
|
|
1525
|
+
fs.writeFileSync(tempConfigPath, tempFileContent);
|
|
1526
|
+
const fullViteConfigPath = path.join(projectPath, viteConfig);
|
|
1527
|
+
let result;
|
|
1528
|
+
try {
|
|
1529
|
+
result = mergeJsonConfig(fullViteConfigPath, tempConfigPath, configKey);
|
|
1530
|
+
} finally {
|
|
1531
|
+
fs.rmSync(tempConfigPath, { force: true });
|
|
1532
|
+
}
|
|
1533
|
+
if (result.updated) fs.writeFileSync(fullViteConfigPath, result.content);
|
|
1534
|
+
}
|
|
1535
|
+
function mergeAndRemoveJsonConfig(projectPath, viteConfigPath, jsonConfigPath, configKey, silent = false, report) {
|
|
1536
|
+
const fullViteConfigPath = path.join(projectPath, viteConfigPath);
|
|
1537
|
+
const fullJsonConfigPath = path.join(projectPath, jsonConfigPath);
|
|
1538
|
+
if (hasConfigKey(fullViteConfigPath, configKey)) {
|
|
1539
|
+
fs.unlinkSync(fullJsonConfigPath);
|
|
1540
|
+
if (!silent) log.info(`${configKey} config already present in ${displayRelative(fullViteConfigPath)} — removed redundant ${displayRelative(fullJsonConfigPath)}`);
|
|
1541
|
+
return;
|
|
1542
|
+
}
|
|
1543
|
+
const result = mergeJsonConfig(fullViteConfigPath, fullJsonConfigPath, configKey);
|
|
1544
|
+
if (result.updated) {
|
|
1545
|
+
fs.writeFileSync(fullViteConfigPath, result.content);
|
|
1546
|
+
fs.unlinkSync(fullJsonConfigPath);
|
|
1547
|
+
if (report) report.mergedConfigCount++;
|
|
1548
|
+
if (!silent) log.success(`✔ Merged ${displayRelative(fullJsonConfigPath)} into ${displayRelative(fullViteConfigPath)}`);
|
|
1549
|
+
} else {
|
|
1550
|
+
warnMigration(`Failed to merge ${displayRelative(fullJsonConfigPath)} into ${displayRelative(fullViteConfigPath)}`, report);
|
|
1551
|
+
infoMigration("Please complete the merge manually and follow the instructions in the documentation: https://viteplus.dev/config/", report);
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
/**
|
|
1555
|
+
* Merge a staged config object into vite.config.ts as `staged: { ... }`.
|
|
1556
|
+
* Writes the config to a temp JSON file, calls mergeJsonConfig NAPI, then cleans up.
|
|
1557
|
+
*/
|
|
1558
|
+
function mergeStagedConfigToViteConfig(projectPath, stagedConfig, silent = false, report) {
|
|
1559
|
+
const viteConfig = ensureViteConfig(projectPath, detectConfigs(projectPath), silent, report);
|
|
1560
|
+
const fullViteConfigPath = path.join(projectPath, viteConfig);
|
|
1561
|
+
const tempJsonPath = path.join(projectPath, ".staged-config-temp.json");
|
|
1562
|
+
fs.writeFileSync(tempJsonPath, JSON.stringify(stagedConfig, null, 2));
|
|
1563
|
+
let result;
|
|
1564
|
+
try {
|
|
1565
|
+
result = mergeJsonConfig(fullViteConfigPath, tempJsonPath, "staged");
|
|
1566
|
+
} finally {
|
|
1567
|
+
fs.unlinkSync(tempJsonPath);
|
|
1568
|
+
}
|
|
1569
|
+
if (result.updated) {
|
|
1570
|
+
fs.writeFileSync(fullViteConfigPath, result.content);
|
|
1571
|
+
if (report) report.mergedStagedConfigCount++;
|
|
1572
|
+
if (!silent) log.success(`✔ Merged staged config into ${displayRelative(fullViteConfigPath)}`);
|
|
1573
|
+
return true;
|
|
1574
|
+
} else {
|
|
1575
|
+
warnMigration(`Failed to merge staged config into ${displayRelative(fullViteConfigPath)}`, report);
|
|
1576
|
+
infoMigration(`Please add staged config to ${displayRelative(fullViteConfigPath)} manually, see https://viteplus.dev/guide/migrate#lint-staged`, report);
|
|
1577
|
+
return false;
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1580
|
+
/**
|
|
1581
|
+
* Check if vite.config.ts already has a `staged` config key.
|
|
1582
|
+
*/
|
|
1583
|
+
function hasStagedConfigInViteConfig(projectPath) {
|
|
1584
|
+
const configs = detectConfigs(projectPath);
|
|
1585
|
+
if (!configs.viteConfig) return false;
|
|
1586
|
+
const viteConfigPath = path.join(projectPath, configs.viteConfig);
|
|
1587
|
+
const content = fs.readFileSync(viteConfigPath, "utf8");
|
|
1588
|
+
return /\bstaged\s*:/.test(content);
|
|
1589
|
+
}
|
|
1590
|
+
/**
|
|
1591
|
+
* Rewrite imports in all TypeScript/JavaScript files under a directory
|
|
1592
|
+
* This rewrites vite/vitest imports to @voidzero-dev/vite-plus
|
|
1593
|
+
* @param projectPath - The root directory to search for files
|
|
1594
|
+
*/
|
|
1595
|
+
function rewriteAllImports(projectPath, silent = false, report) {
|
|
1596
|
+
const result = rewriteImportsInDirectory(projectPath);
|
|
1597
|
+
const modified = result.modifiedFiles.length;
|
|
1598
|
+
const errors = result.errors.length;
|
|
1599
|
+
if (report) {
|
|
1600
|
+
report.rewrittenImportFileCount += modified;
|
|
1601
|
+
report.rewrittenImportErrors.push(...result.errors.map((error) => ({
|
|
1602
|
+
path: displayRelative(error.path),
|
|
1603
|
+
message: error.message
|
|
1604
|
+
})));
|
|
1605
|
+
}
|
|
1606
|
+
if (!silent && modified > 0) {
|
|
1607
|
+
log.success(`Rewrote imports in ${modified === 1 ? "one file" : `${modified} files`}`);
|
|
1608
|
+
log.info(result.modifiedFiles.map((file) => ` ${displayRelative(file)}`).join("\n"));
|
|
1609
|
+
}
|
|
1610
|
+
if (errors > 0) if (report) warnMigration(`${errors === 1 ? "one file had an error" : `${errors} files had errors`} while rewriting imports`, report);
|
|
1611
|
+
else {
|
|
1612
|
+
log.warn(`⚠ ${errors === 1 ? "one file had an error" : `${errors} files had errors`}:`);
|
|
1613
|
+
for (const error of result.errors) log.error(` ${displayRelative(error.path)}: ${error.message}`);
|
|
1614
|
+
}
|
|
1615
|
+
}
|
|
1616
|
+
/**
|
|
1617
|
+
* Check if the project has an unsupported husky version (<9.0.0).
|
|
1618
|
+
* Uses `semver.coerce` to handle ranges like `^8.0.0` → `8.0.0`.
|
|
1619
|
+
* When the specifier is a catalog reference (e.g. `"catalog:"`), resolves
|
|
1620
|
+
* it from the active package manager's catalog first — a `catalog:` spec is
|
|
1621
|
+
* only meaningful to the manager that owns the workspace, so we never read a
|
|
1622
|
+
* leftover/foreign catalog file. When it is still not coercible (e.g.
|
|
1623
|
+
* `"latest"`), falls back to the installed version in node_modules via
|
|
1624
|
+
* `detectPackageMetadata`.
|
|
1625
|
+
* Returns a reason string if hooks migration should be skipped, or null
|
|
1626
|
+
* if husky is absent or compatible.
|
|
1627
|
+
*/
|
|
1628
|
+
function checkUnsupportedHuskyVersion(projectPath, deps, prodDeps, packageManager) {
|
|
1629
|
+
const huskyVersion = deps?.husky ?? prodDeps?.husky;
|
|
1630
|
+
if (!huskyVersion) return null;
|
|
1631
|
+
let coerced = import_semver.default.coerce(huskyVersion);
|
|
1632
|
+
if (coerced == null && packageManager != null && huskyVersion.startsWith("catalog:")) {
|
|
1633
|
+
const resolved = createCatalogDependencyResolver(projectPath, packageManager)?.(huskyVersion, "husky");
|
|
1634
|
+
if (resolved) coerced = import_semver.default.coerce(resolved);
|
|
1635
|
+
}
|
|
1636
|
+
if (coerced == null) {
|
|
1637
|
+
const installed = detectPackageMetadata(projectPath, "husky");
|
|
1638
|
+
if (installed) coerced = import_semver.default.coerce(installed.version);
|
|
1639
|
+
if (coerced == null) return `Could not determine husky version from "${huskyVersion}" — please specify a semver-compatible version (e.g., "^9.0.0") and re-run migration.`;
|
|
1640
|
+
}
|
|
1641
|
+
if (import_semver.default.satisfies(coerced, "<9.0.0")) return "Detected husky <9.0.0 — please upgrade to husky v9+ first, then re-run migration.";
|
|
1642
|
+
return null;
|
|
1643
|
+
}
|
|
1644
|
+
const OTHER_HOOK_TOOLS = [
|
|
1645
|
+
"simple-git-hooks",
|
|
1646
|
+
"lefthook",
|
|
1647
|
+
"yorkie"
|
|
1648
|
+
];
|
|
1649
|
+
const REPLACED_HOOK_PACKAGES = ["husky", "lint-staged"];
|
|
1650
|
+
function removeReplacedHookPackages(packageJsonPath) {
|
|
1651
|
+
editJsonFile(packageJsonPath, (pkg) => {
|
|
1652
|
+
for (const name of REPLACED_HOOK_PACKAGES) {
|
|
1653
|
+
if (pkg.devDependencies?.[name]) delete pkg.devDependencies[name];
|
|
1654
|
+
if (pkg.dependencies?.[name]) delete pkg.dependencies[name];
|
|
1655
|
+
}
|
|
1656
|
+
return pkg;
|
|
1657
|
+
});
|
|
1658
|
+
}
|
|
1659
|
+
/**
|
|
1660
|
+
* Walk up from `startPath` looking for `.git` (directory or file — submodules
|
|
1661
|
+
* use a `.git` file). Returns the directory that contains `.git`, or `null`.
|
|
1662
|
+
*/
|
|
1663
|
+
function findGitRoot(startPath) {
|
|
1664
|
+
let dir = startPath;
|
|
1665
|
+
while (true) {
|
|
1666
|
+
if (fs.existsSync(path.join(dir, ".git"))) return dir;
|
|
1667
|
+
const parent = path.dirname(dir);
|
|
1668
|
+
if (parent === dir) return null;
|
|
1669
|
+
dir = parent;
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
/**
|
|
1673
|
+
* Normalize "husky install [dir]" → "husky [dir]" so downstream regex
|
|
1674
|
+
* and ast-grep rules can match a single pattern.
|
|
1675
|
+
*/
|
|
1676
|
+
function collapseHuskyInstall(script) {
|
|
1677
|
+
return script.replace("husky install ", "husky ").replace("husky install", "husky");
|
|
1678
|
+
}
|
|
1679
|
+
/**
|
|
1680
|
+
* High-level helper: detect old hooks dir, set up git hooks, and rewrite
|
|
1681
|
+
* the prepare script. Returns true if hooks were successfully installed.
|
|
1682
|
+
*/
|
|
1683
|
+
function installGitHooks(projectPath, silent = false, report, packageManager) {
|
|
1684
|
+
if (setupGitHooks(projectPath, getOldHooksDir(projectPath), silent, report, packageManager)) {
|
|
1685
|
+
rewritePrepareScript(projectPath);
|
|
1686
|
+
return true;
|
|
1687
|
+
}
|
|
1688
|
+
return false;
|
|
1689
|
+
}
|
|
1690
|
+
/**
|
|
1691
|
+
* Read-only probe: extract the old husky hooks directory from `scripts.prepare`
|
|
1692
|
+
* without modifying package.json. Returns undefined when no husky reference is found.
|
|
1693
|
+
*/
|
|
1694
|
+
function getOldHooksDir(rootDir) {
|
|
1695
|
+
const packageJsonPath = path.join(rootDir, "package.json");
|
|
1696
|
+
if (!fs.existsSync(packageJsonPath)) return;
|
|
1697
|
+
const pkg = readJsonFile(packageJsonPath);
|
|
1698
|
+
if (!pkg.scripts?.prepare) return;
|
|
1699
|
+
const match = collapseHuskyInstall(pkg.scripts.prepare).match(/\bhusky(?:\s+([\w./-]+))?/);
|
|
1700
|
+
if (!match) return;
|
|
1701
|
+
return match[1] ?? ".husky";
|
|
1702
|
+
}
|
|
1703
|
+
/**
|
|
1704
|
+
* Pre-flight check: verify that git hooks can be set up for this project.
|
|
1705
|
+
* Returns `null` if hooks setup can proceed, or a warning reason string
|
|
1706
|
+
* explaining why hooks setup should be skipped.
|
|
1707
|
+
*
|
|
1708
|
+
* These checks are deterministic and read-only — they do not modify
|
|
1709
|
+
* the project in any way, making them safe to call before migration.
|
|
1710
|
+
*
|
|
1711
|
+
* `packageManager` is the project's detected manager; it scopes `catalog:`
|
|
1712
|
+
* resolution to that manager's catalog so a foreign catalog file is ignored.
|
|
1713
|
+
*/
|
|
1714
|
+
function preflightGitHooksSetup(projectPath, packageManager) {
|
|
1715
|
+
const gitRoot = findGitRoot(projectPath);
|
|
1716
|
+
if (gitRoot && path.resolve(projectPath) !== path.resolve(gitRoot)) return "Subdirectory project detected — skipping git hooks setup. Configure hooks at the repository root.";
|
|
1717
|
+
const packageJsonPath = path.join(projectPath, "package.json");
|
|
1718
|
+
if (!fs.existsSync(packageJsonPath)) return null;
|
|
1719
|
+
const pkgContent = readJsonFile(packageJsonPath);
|
|
1720
|
+
const deps = pkgContent.devDependencies;
|
|
1721
|
+
const prodDeps = pkgContent.dependencies;
|
|
1722
|
+
for (const tool of OTHER_HOOK_TOOLS) if (deps?.[tool] || prodDeps?.[tool] || pkgContent[tool]) return `Detected ${tool} — skipping git hooks setup. Please configure git hooks manually.`;
|
|
1723
|
+
const huskyReason = checkUnsupportedHuskyVersion(projectPath, deps, prodDeps, packageManager);
|
|
1724
|
+
if (huskyReason) return huskyReason;
|
|
1725
|
+
if (hasUnsupportedLintStagedConfig(projectPath)) return "Unsupported lint-staged config format — skipping git hooks setup. Please configure git hooks manually.";
|
|
1726
|
+
return null;
|
|
1727
|
+
}
|
|
1728
|
+
/**
|
|
1729
|
+
* Set up git hooks with husky + lint-staged via vp commands.
|
|
1730
|
+
* Skips if another hook tool is detected (warns user).
|
|
1731
|
+
* Returns true if hooks were successfully set up, false if skipped.
|
|
1732
|
+
*/
|
|
1733
|
+
function setupGitHooks(projectPath, oldHooksDir, silent = false, report, packageManager) {
|
|
1734
|
+
const reason = preflightGitHooksSetup(projectPath, packageManager);
|
|
1735
|
+
if (reason) {
|
|
1736
|
+
warnMigration(reason, report);
|
|
1737
|
+
return false;
|
|
1738
|
+
}
|
|
1739
|
+
const packageJsonPath = path.join(projectPath, "package.json");
|
|
1740
|
+
if (!fs.existsSync(packageJsonPath)) return false;
|
|
1741
|
+
const gitRoot = findGitRoot(projectPath);
|
|
1742
|
+
const isCustomDir = oldHooksDir != null && oldHooksDir !== ".husky";
|
|
1743
|
+
const hooksDir = isCustomDir ? oldHooksDir : ".vite-hooks";
|
|
1744
|
+
editJsonFile(packageJsonPath, (pkg) => {
|
|
1745
|
+
if (!pkg.scripts) pkg.scripts = {};
|
|
1746
|
+
if (!pkg.scripts.prepare) pkg.scripts.prepare = "vp config";
|
|
1747
|
+
else if (!pkg.scripts.prepare.includes("vp config") && !/\bhusky\b/.test(pkg.scripts.prepare)) pkg.scripts.prepare = `vp config && ${pkg.scripts.prepare}`;
|
|
1748
|
+
return pkg;
|
|
1749
|
+
});
|
|
1750
|
+
let stagedMerged = hasStagedConfigInViteConfig(projectPath);
|
|
1751
|
+
const hasStandaloneConfig = hasStandaloneLintStagedConfig(projectPath);
|
|
1752
|
+
if (!stagedMerged && !hasStandaloneConfig) {
|
|
1753
|
+
const stagedConfig = readJsonFile(packageJsonPath)?.["lint-staged"] ?? DEFAULT_STAGED_CONFIG;
|
|
1754
|
+
const updated = rewriteScripts(JSON.stringify(stagedConfig), readRulesYaml());
|
|
1755
|
+
stagedMerged = mergeStagedConfigToViteConfig(projectPath, updated ? JSON.parse(updated) : stagedConfig, silent, report);
|
|
1756
|
+
}
|
|
1757
|
+
if (stagedMerged) removeLintStagedFromPackageJson(packageJsonPath);
|
|
1758
|
+
if (oldHooksDir && !isCustomDir) {
|
|
1759
|
+
const oldDir = path.join(projectPath, oldHooksDir);
|
|
1760
|
+
if (fs.existsSync(oldDir)) {
|
|
1761
|
+
const targetDir = path.join(projectPath, hooksDir);
|
|
1762
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
1763
|
+
for (const entry of fs.readdirSync(oldDir, { withFileTypes: true })) {
|
|
1764
|
+
if (entry.isDirectory() || entry.name.startsWith(".")) continue;
|
|
1765
|
+
const src = path.join(oldDir, entry.name);
|
|
1766
|
+
const dest = path.join(targetDir, entry.name);
|
|
1767
|
+
fs.copyFileSync(src, dest);
|
|
1768
|
+
fs.chmodSync(dest, 493);
|
|
1769
|
+
}
|
|
1770
|
+
fs.rmSync(oldDir, {
|
|
1771
|
+
recursive: true,
|
|
1772
|
+
force: true
|
|
1773
|
+
});
|
|
1774
|
+
}
|
|
1775
|
+
}
|
|
1776
|
+
if (stagedMerged) createPreCommitHook(projectPath, hooksDir);
|
|
1777
|
+
if (!gitRoot) {
|
|
1778
|
+
removeReplacedHookPackages(packageJsonPath);
|
|
1779
|
+
return true;
|
|
1780
|
+
}
|
|
1781
|
+
if (oldHooksDir) {
|
|
1782
|
+
const checkResult = import_cross_spawn.default.sync("git", [
|
|
1783
|
+
"config",
|
|
1784
|
+
"--local",
|
|
1785
|
+
"core.hooksPath"
|
|
1786
|
+
], {
|
|
1787
|
+
cwd: projectPath,
|
|
1788
|
+
stdio: "pipe"
|
|
1789
|
+
});
|
|
1790
|
+
const existingPath = checkResult.status === 0 ? checkResult.stdout?.toString().trim() : "";
|
|
1791
|
+
if (existingPath === `${oldHooksDir}/_` || existingPath === oldHooksDir) import_cross_spawn.default.sync("git", [
|
|
1792
|
+
"config",
|
|
1793
|
+
"--local",
|
|
1794
|
+
"--unset",
|
|
1795
|
+
"core.hooksPath"
|
|
1796
|
+
], {
|
|
1797
|
+
cwd: projectPath,
|
|
1798
|
+
stdio: "pipe"
|
|
1799
|
+
});
|
|
1800
|
+
}
|
|
1801
|
+
const vpBin = process.env.VP_CLI_BIN ?? "vp";
|
|
1802
|
+
const configArgs = isCustomDir ? [
|
|
1803
|
+
"config",
|
|
1804
|
+
"--hooks-only",
|
|
1805
|
+
"--hooks-dir",
|
|
1806
|
+
hooksDir
|
|
1807
|
+
] : ["config", "--hooks-only"];
|
|
1808
|
+
const configResult = import_cross_spawn.default.sync(vpBin, configArgs, {
|
|
1809
|
+
cwd: projectPath,
|
|
1810
|
+
stdio: "pipe"
|
|
1811
|
+
});
|
|
1812
|
+
if (configResult.status === 0) {
|
|
1813
|
+
const stdout = configResult.stdout?.toString().trim() ?? "";
|
|
1814
|
+
if (stdout) {
|
|
1815
|
+
warnMigration(`Git hooks not configured — ${stdout}`, report);
|
|
1816
|
+
return false;
|
|
1817
|
+
}
|
|
1818
|
+
removeReplacedHookPackages(packageJsonPath);
|
|
1819
|
+
if (report) report.gitHooksConfigured = true;
|
|
1820
|
+
if (!silent) log.success("✔ Git hooks configured");
|
|
1821
|
+
return true;
|
|
1822
|
+
}
|
|
1823
|
+
warnMigration("Failed to install git hooks", report);
|
|
1824
|
+
return false;
|
|
1825
|
+
}
|
|
1826
|
+
/**
|
|
1827
|
+
* Check if a standalone lint-staged config file exists
|
|
1828
|
+
*/
|
|
1829
|
+
function hasStandaloneLintStagedConfig(projectPath) {
|
|
1830
|
+
return LINT_STAGED_ALL_CONFIG_FILES.some((file) => fs.existsSync(path.join(projectPath, file)));
|
|
1831
|
+
}
|
|
1832
|
+
/**
|
|
1833
|
+
* Check if a standalone lint-staged config exists in a format that can't be
|
|
1834
|
+
* auto-migrated to "staged" in vite.config.ts (non-JSON files like .yaml,
|
|
1835
|
+
* .mjs, .cjs, .js, or a non-JSON .lintstagedrc).
|
|
1836
|
+
*/
|
|
1837
|
+
function hasUnsupportedLintStagedConfig(projectPath) {
|
|
1838
|
+
for (const filename of LINT_STAGED_OTHER_CONFIG_FILES) if (fs.existsSync(path.join(projectPath, filename))) return true;
|
|
1839
|
+
const lintstagedrcPath = path.join(projectPath, ".lintstagedrc");
|
|
1840
|
+
if (fs.existsSync(lintstagedrcPath) && !isJsonFile(lintstagedrcPath)) return true;
|
|
1841
|
+
return false;
|
|
1842
|
+
}
|
|
1843
|
+
/**
|
|
1844
|
+
* Create pre-commit hook file in the hooks directory.
|
|
1845
|
+
*/
|
|
1846
|
+
const STALE_LINT_STAGED_PATTERNS = [/^((?:[A-Z_][A-Z0-9_]*(?:=\S*)?\s+)*)(pnpm|pnpm exec|npx|yarn|yarn run|npm exec|npm run|bunx|bun run|bun x)\s+lint-staged\b/, /^((?:[A-Z_][A-Z0-9_]*(?:=\S*)?\s+)*)lint-staged\b/];
|
|
1847
|
+
const DEFAULT_STAGED_CONFIG = { "*": "vp check --fix" };
|
|
1848
|
+
/**
|
|
1849
|
+
* Ensure the pre-commit hook exists with `vp staged`, and that
|
|
1850
|
+
* vite.config.ts contains a `staged` block (using the default config
|
|
1851
|
+
* if none is present). Called by `vp config` after hook installation.
|
|
1852
|
+
*/
|
|
1853
|
+
function ensurePreCommitHook(projectPath, dir = ".vite-hooks") {
|
|
1854
|
+
if (!hasStagedConfigInViteConfig(projectPath)) mergeStagedConfigToViteConfig(projectPath, DEFAULT_STAGED_CONFIG, true);
|
|
1855
|
+
createPreCommitHook(projectPath, dir);
|
|
1856
|
+
}
|
|
1857
|
+
function createPreCommitHook(projectPath, dir = ".vite-hooks") {
|
|
1858
|
+
const huskyDir = path.join(projectPath, dir);
|
|
1859
|
+
fs.mkdirSync(huskyDir, { recursive: true });
|
|
1860
|
+
const hookPath = path.join(huskyDir, "pre-commit");
|
|
1861
|
+
if (fs.existsSync(hookPath)) {
|
|
1862
|
+
const existing = fs.readFileSync(hookPath, "utf8");
|
|
1863
|
+
if (existing.includes("vp staged")) return;
|
|
1864
|
+
const lines = existing.split("\n");
|
|
1865
|
+
let replaced = false;
|
|
1866
|
+
const result = [];
|
|
1867
|
+
for (const line of lines) {
|
|
1868
|
+
const trimmed = line.trim();
|
|
1869
|
+
if (!replaced) {
|
|
1870
|
+
let matched = false;
|
|
1871
|
+
for (const pattern of STALE_LINT_STAGED_PATTERNS) {
|
|
1872
|
+
const match = pattern.exec(trimmed);
|
|
1873
|
+
if (match) {
|
|
1874
|
+
const parts = [
|
|
1875
|
+
match[1]?.trim() ?? "",
|
|
1876
|
+
"vp staged",
|
|
1877
|
+
trimmed.slice(match[0].length).trim()
|
|
1878
|
+
].filter(Boolean);
|
|
1879
|
+
result.push(parts.join(" "));
|
|
1880
|
+
replaced = true;
|
|
1881
|
+
matched = true;
|
|
1882
|
+
break;
|
|
1883
|
+
}
|
|
1884
|
+
}
|
|
1885
|
+
if (matched) continue;
|
|
1886
|
+
}
|
|
1887
|
+
result.push(line);
|
|
1888
|
+
}
|
|
1889
|
+
if (!replaced) fs.writeFileSync(hookPath, `${result.join("\n").trimEnd()}\nvp staged\n`);
|
|
1890
|
+
else fs.writeFileSync(hookPath, result.join("\n"));
|
|
1891
|
+
} else {
|
|
1892
|
+
fs.writeFileSync(hookPath, "vp staged\n");
|
|
1893
|
+
fs.chmodSync(hookPath, 493);
|
|
1894
|
+
}
|
|
1895
|
+
}
|
|
1896
|
+
/**
|
|
1897
|
+
* Rewrite only `scripts.prepare` in the root package.json using vite-prepare.yml rules.
|
|
1898
|
+
* Collapses "husky install" → "husky" before applying ast-grep so that the
|
|
1899
|
+
* replace-husky rule produces "vp config" with any directory argument preserved.
|
|
1900
|
+
* Returns the old husky hooks dir (if any) for migration to .vite-hooks.
|
|
1901
|
+
* Called only when hooks are being set up (not with --no-hooks).
|
|
1902
|
+
*/
|
|
1903
|
+
function rewritePrepareScript(rootDir) {
|
|
1904
|
+
const packageJsonPath = path.join(rootDir, "package.json");
|
|
1905
|
+
if (!fs.existsSync(packageJsonPath)) return;
|
|
1906
|
+
let oldDir;
|
|
1907
|
+
editJsonFile(packageJsonPath, (pkg) => {
|
|
1908
|
+
if (!pkg.scripts?.prepare) return pkg;
|
|
1909
|
+
const prepare = collapseHuskyInstall(pkg.scripts.prepare);
|
|
1910
|
+
const updated = rewriteScripts(JSON.stringify({ prepare }), readPrepareRulesYaml());
|
|
1911
|
+
if (updated) {
|
|
1912
|
+
let newPrepare = JSON.parse(updated).prepare;
|
|
1913
|
+
newPrepare = newPrepare.replace(/\bvp config(?:\s+(?!-)([\w./-]+))?/, (_match, dir) => {
|
|
1914
|
+
oldDir = dir ?? ".husky";
|
|
1915
|
+
return dir ? `vp config --hooks-dir ${dir}` : "vp config";
|
|
1916
|
+
});
|
|
1917
|
+
pkg.scripts.prepare = newPrepare;
|
|
1918
|
+
} else if (prepare !== pkg.scripts.prepare) pkg.scripts.prepare = prepare;
|
|
1919
|
+
return pkg;
|
|
1920
|
+
});
|
|
1921
|
+
return oldDir;
|
|
1922
|
+
}
|
|
1923
|
+
function setPackageManager(projectDir, downloadPackageManager) {
|
|
1924
|
+
editJsonFile(path.join(projectDir, "package.json"), (pkg) => {
|
|
1925
|
+
if (!pkg.packageManager) pkg.packageManager = `${downloadPackageManager.name}@${downloadPackageManager.version}`;
|
|
1926
|
+
return pkg;
|
|
1927
|
+
});
|
|
1928
|
+
}
|
|
1929
|
+
/**
|
|
1930
|
+
* Detect a .nvmrc file in the project directory.
|
|
1931
|
+
* If not found, check for a Volta node version in package.json.
|
|
1932
|
+
* If either is found, return the relevant info for migration.
|
|
1933
|
+
* Returns undefined if not found or .node-version already exists.
|
|
1934
|
+
*/
|
|
1935
|
+
function detectNodeVersionManagerFile(projectPath) {
|
|
1936
|
+
if (fs.existsSync(path.join(projectPath, ".node-version"))) return;
|
|
1937
|
+
const configs = detectConfigs(projectPath);
|
|
1938
|
+
if (configs.nvmrcFile) return configs.voltaNode ? {
|
|
1939
|
+
file: ".nvmrc",
|
|
1940
|
+
voltaPresent: true
|
|
1941
|
+
} : { file: ".nvmrc" };
|
|
1942
|
+
if (configs.voltaNode) return {
|
|
1943
|
+
file: "package.json",
|
|
1944
|
+
voltaNodeVersion: configs.voltaNode
|
|
1945
|
+
};
|
|
1946
|
+
}
|
|
1947
|
+
/**
|
|
1948
|
+
* Parse a version alias from a .nvmrc file into a .node-version compatible string.
|
|
1949
|
+
* Accepts the first line of .nvmrc (pre-trimmed).
|
|
1950
|
+
* Returns null for unsupported aliases like "system", "default", "iojs".
|
|
1951
|
+
*/
|
|
1952
|
+
function parseNvmrcVersion(alias) {
|
|
1953
|
+
const version = alias.trim();
|
|
1954
|
+
if (!version) return null;
|
|
1955
|
+
if (version === "node" || version === "stable") return "lts/*";
|
|
1956
|
+
if (version === "iojs" || version === "system" || version === "default") return null;
|
|
1957
|
+
if (version.startsWith("lts/")) return version;
|
|
1958
|
+
const normalized = version.startsWith("v") ? version.slice(1) : version;
|
|
1959
|
+
if (!normalized || !import_semver.default.validRange(normalized)) return null;
|
|
1960
|
+
return normalized;
|
|
1961
|
+
}
|
|
1962
|
+
/**
|
|
1963
|
+
* Migrate .nvmrc or Volta node version from package.json to .node-version.
|
|
1964
|
+
* - For .nvmrc: the source file is removed after migration.
|
|
1965
|
+
* - For package.json (Volta): the volta field is left as-is; removal is left to the user's discretion.
|
|
1966
|
+
* Returns true on success, false if migration was skipped or failed.
|
|
1967
|
+
*/
|
|
1968
|
+
function migrateNodeVersionManagerFile(projectPath, detection, report) {
|
|
1969
|
+
const nodeVersionPath = path.join(projectPath, ".node-version");
|
|
1970
|
+
if (detection.file === "package.json") {
|
|
1971
|
+
const { voltaNodeVersion } = detection;
|
|
1972
|
+
const resolvedVersion = voltaNodeVersion === "lts" ? "lts/*" : voltaNodeVersion;
|
|
1973
|
+
if (!import_semver.default.valid(resolvedVersion) && resolvedVersion !== "lts/*") {
|
|
1974
|
+
warnMigration(`package.json volta.node "${voltaNodeVersion}" is not an exact version. Pin an exact version (e.g. ${voltaNodeVersion}.0 or run \`volta pin node@${voltaNodeVersion}\`) then re-run migration.`, report);
|
|
1975
|
+
return false;
|
|
1976
|
+
}
|
|
1977
|
+
fs.writeFileSync(nodeVersionPath, `${resolvedVersion}\n`);
|
|
1978
|
+
if (report) {
|
|
1979
|
+
report.manualSteps.push("Remove the \"volta\" field from package.json");
|
|
1980
|
+
report.nodeVersionFileMigrated = true;
|
|
1981
|
+
} else log.info("You can now remove the \"volta\" field from package.json manually.");
|
|
1982
|
+
return true;
|
|
1983
|
+
}
|
|
1984
|
+
const sourcePath = path.join(projectPath, ".nvmrc");
|
|
1985
|
+
const originalAlias = fs.readFileSync(sourcePath, "utf8").split("\n")[0]?.trim() ?? "";
|
|
1986
|
+
const version = parseNvmrcVersion(originalAlias);
|
|
1987
|
+
if (!version) {
|
|
1988
|
+
warnMigration(".nvmrc contains an unsupported version alias. Create .node-version manually with your desired Node.js version.", report);
|
|
1989
|
+
return false;
|
|
1990
|
+
}
|
|
1991
|
+
if (version === "lts/*" && (originalAlias === "node" || originalAlias === "stable")) log.info(`"${originalAlias}" in .nvmrc is not a specific version; automatically mapping to "lts/*"`);
|
|
1992
|
+
fs.writeFileSync(nodeVersionPath, `${version}\n`);
|
|
1993
|
+
fs.unlinkSync(sourcePath);
|
|
1994
|
+
if (report) {
|
|
1995
|
+
report.nodeVersionFileMigrated = true;
|
|
1996
|
+
if (detection.voltaPresent) report.manualSteps.push("Remove the \"volta\" field from package.json");
|
|
1997
|
+
} else if (detection.voltaPresent) log.info("You can now remove the \"volta\" field from package.json manually.");
|
|
1998
|
+
return true;
|
|
1999
|
+
}
|
|
2000
|
+
function warnPackageLevelEslint() {
|
|
2001
|
+
log.warn("ESLint detected in workspace packages but no root config found. Package-level ESLint must be migrated manually.");
|
|
2002
|
+
}
|
|
2003
|
+
const INCOMPATIBLE_ESLINT_INTEGRATIONS = ["@nuxt/eslint"];
|
|
2004
|
+
/**
|
|
2005
|
+
* Detect framework-ESLint integration packages whose ESLint migration is
|
|
2006
|
+
* known to be incompatible. Returns the offending package name, or
|
|
2007
|
+
* `undefined` if none is present.
|
|
2008
|
+
*/
|
|
2009
|
+
function detectIncompatibleEslintIntegration(projectPath, packages) {
|
|
2010
|
+
const candidates = [projectPath, ...(packages ?? []).map((p) => path.join(projectPath, p.path))];
|
|
2011
|
+
for (const candidate of candidates) {
|
|
2012
|
+
const pkgJsonPath = path.join(candidate, "package.json");
|
|
2013
|
+
if (!fs.existsSync(pkgJsonPath)) continue;
|
|
2014
|
+
let pkg;
|
|
2015
|
+
try {
|
|
2016
|
+
pkg = readJsonFile(pkgJsonPath);
|
|
2017
|
+
} catch {
|
|
2018
|
+
continue;
|
|
2019
|
+
}
|
|
2020
|
+
for (const name of INCOMPATIBLE_ESLINT_INTEGRATIONS) if (pkg.devDependencies?.[name] || pkg.dependencies?.[name]) return name;
|
|
2021
|
+
}
|
|
2022
|
+
}
|
|
2023
|
+
function warnIncompatibleEslintIntegration(name) {
|
|
2024
|
+
log.warn(`${name} detected — automatic ESLint migration is skipped. ${name} wires ESLint into a framework-specific flow that Vite+ cannot migrate cleanly yet. Your ESLint setup is preserved. To migrate manually, remove ${name} from package.json and re-run \`vp migrate\`.`);
|
|
2025
|
+
}
|
|
2026
|
+
function warnLegacyEslintConfig(legacyConfigFile) {
|
|
2027
|
+
log.warn(`Legacy ESLint configuration detected (${legacyConfigFile}). Automatic migration to Oxlint requires ESLint v9+ with flat config format (eslint.config.*). Please upgrade to ESLint v9 first: https://eslint.org/docs/latest/use/migrate-to-9.0.0`);
|
|
2028
|
+
}
|
|
2029
|
+
async function confirmEslintMigration(interactive) {
|
|
2030
|
+
if (interactive) {
|
|
2031
|
+
const confirmed = await confirm({
|
|
2032
|
+
message: "Migrate ESLint rules to Oxlint using @oxlint/migrate?\n " + styleText("gray", "Oxlint is Vite+'s built-in linter — significantly faster than ESLint with compatible rule support. @oxlint/migrate converts your existing rules automatically."),
|
|
2033
|
+
initialValue: true
|
|
2034
|
+
});
|
|
2035
|
+
if (R(confirmed)) cancelAndExit();
|
|
2036
|
+
return confirmed;
|
|
2037
|
+
}
|
|
2038
|
+
return true;
|
|
2039
|
+
}
|
|
2040
|
+
async function promptEslintMigration(projectPath, interactive, packages) {
|
|
2041
|
+
const incompatible = detectIncompatibleEslintIntegration(projectPath, packages);
|
|
2042
|
+
if (incompatible) {
|
|
2043
|
+
warnIncompatibleEslintIntegration(incompatible);
|
|
2044
|
+
return false;
|
|
2045
|
+
}
|
|
2046
|
+
const eslintProject = detectEslintProject(projectPath, packages);
|
|
2047
|
+
if (eslintProject.hasDependency && !eslintProject.configFile && eslintProject.legacyConfigFile) {
|
|
2048
|
+
warnLegacyEslintConfig(eslintProject.legacyConfigFile);
|
|
2049
|
+
return false;
|
|
2050
|
+
}
|
|
2051
|
+
if (!eslintProject.hasDependency) return false;
|
|
2052
|
+
if (!eslintProject.configFile) {
|
|
2053
|
+
warnPackageLevelEslint();
|
|
2054
|
+
return false;
|
|
2055
|
+
}
|
|
2056
|
+
if (!await confirmEslintMigration(interactive)) return false;
|
|
2057
|
+
if (!await migrateEslintToOxlint(projectPath, interactive, eslintProject.configFile, packages)) cancelAndExit("ESLint migration failed.", 1);
|
|
2058
|
+
return true;
|
|
2059
|
+
}
|
|
2060
|
+
function warnPackageLevelPrettier() {
|
|
2061
|
+
log.warn("Prettier detected in workspace packages but no root config found. Package-level Prettier must be migrated manually.");
|
|
2062
|
+
}
|
|
2063
|
+
async function confirmPrettierMigration(interactive) {
|
|
2064
|
+
if (interactive) {
|
|
2065
|
+
const confirmed = await confirm({
|
|
2066
|
+
message: "Migrate Prettier to Oxfmt?\n " + styleText("gray", "Oxfmt is Vite+'s built-in formatter that replaces Prettier with faster performance. Your configuration will be converted automatically."),
|
|
2067
|
+
initialValue: true
|
|
2068
|
+
});
|
|
2069
|
+
if (R(confirmed)) cancelAndExit();
|
|
2070
|
+
return confirmed;
|
|
2071
|
+
}
|
|
2072
|
+
log.info("Prettier configuration detected. Auto-migrating to Oxfmt...");
|
|
2073
|
+
return true;
|
|
2074
|
+
}
|
|
2075
|
+
async function promptPrettierMigration(projectPath, interactive, packages) {
|
|
2076
|
+
const prettierProject = detectPrettierProject(projectPath, packages);
|
|
2077
|
+
if (!prettierProject.hasDependency) return false;
|
|
2078
|
+
if (!prettierProject.configFile) {
|
|
2079
|
+
warnPackageLevelPrettier();
|
|
2080
|
+
return false;
|
|
2081
|
+
}
|
|
2082
|
+
if (!await confirmPrettierMigration(interactive)) return false;
|
|
2083
|
+
if (!await migratePrettierToOxfmt(projectPath, interactive, prettierProject.configFile, packages)) cancelAndExit("Prettier migration failed.", 1);
|
|
2084
|
+
return true;
|
|
2085
|
+
}
|
|
2086
|
+
//#endregion
|
|
2087
|
+
//#region src/utils/agent.ts
|
|
2088
|
+
const AGENTS = [
|
|
2089
|
+
{
|
|
2090
|
+
id: "agents",
|
|
2091
|
+
label: "AGENTS.md",
|
|
2092
|
+
targetPath: "AGENTS.md",
|
|
2093
|
+
hint: "Codex, Amp, OpenCode, and similar agents",
|
|
2094
|
+
aliases: [
|
|
2095
|
+
"agents.md",
|
|
2096
|
+
"chatgpt",
|
|
2097
|
+
"chatgpt-codex",
|
|
2098
|
+
"codex",
|
|
2099
|
+
"amp",
|
|
2100
|
+
"kilo",
|
|
2101
|
+
"kilo-code",
|
|
2102
|
+
"kiro",
|
|
2103
|
+
"kiro-cli",
|
|
2104
|
+
"opencode",
|
|
2105
|
+
"other"
|
|
2106
|
+
]
|
|
2107
|
+
},
|
|
2108
|
+
{
|
|
2109
|
+
id: "claude",
|
|
2110
|
+
label: "CLAUDE.md",
|
|
2111
|
+
targetPath: "CLAUDE.md",
|
|
2112
|
+
hint: "Claude Code",
|
|
2113
|
+
aliases: ["claude.md", "claude-code"]
|
|
2114
|
+
},
|
|
2115
|
+
{
|
|
2116
|
+
id: "gemini",
|
|
2117
|
+
label: "GEMINI.md",
|
|
2118
|
+
targetPath: "GEMINI.md",
|
|
2119
|
+
hint: "Gemini CLI",
|
|
2120
|
+
aliases: ["gemini.md", "gemini-cli"]
|
|
2121
|
+
},
|
|
2122
|
+
{
|
|
2123
|
+
id: "copilot",
|
|
2124
|
+
label: ".github/copilot-instructions.md",
|
|
2125
|
+
targetPath: ".github/copilot-instructions.md",
|
|
2126
|
+
hint: "GitHub Copilot",
|
|
2127
|
+
aliases: ["github-copilot", "copilot-instructions.md"]
|
|
2128
|
+
},
|
|
2129
|
+
{
|
|
2130
|
+
id: "cursor",
|
|
2131
|
+
label: ".cursor/rules/viteplus.mdc",
|
|
2132
|
+
targetPath: ".cursor/rules/viteplus.mdc",
|
|
2133
|
+
hint: "Cursor",
|
|
2134
|
+
aliases: ["viteplus.mdc"]
|
|
2135
|
+
},
|
|
2136
|
+
{
|
|
2137
|
+
id: "jetbrains",
|
|
2138
|
+
label: ".aiassistant/rules/viteplus.md",
|
|
2139
|
+
targetPath: ".aiassistant/rules/viteplus.md",
|
|
2140
|
+
hint: "JetBrains AI Assistant",
|
|
2141
|
+
aliases: [
|
|
2142
|
+
"jetbrains",
|
|
2143
|
+
"jetbrains-ai-assistant",
|
|
2144
|
+
"aiassistant",
|
|
2145
|
+
"viteplus.md"
|
|
2146
|
+
]
|
|
2147
|
+
}
|
|
2148
|
+
];
|
|
2149
|
+
const AGENT_DEFAULT_ID = "agents";
|
|
2150
|
+
const AGENT_STANDARD_PATH = "AGENTS.md";
|
|
2151
|
+
const COPILOT_AGENT_ID = "copilot";
|
|
2152
|
+
const COPILOT_SETUP_WORKFLOW_PATH = ".github/workflows/copilot-setup-steps.yml";
|
|
2153
|
+
const AGENT_INSTRUCTIONS_START_MARKER = "<!--VITE PLUS START-->";
|
|
2154
|
+
const AGENT_INSTRUCTIONS_END_MARKER = "<!--VITE PLUS END-->";
|
|
2155
|
+
const AGENT_ALIASES = Object.fromEntries(AGENTS.flatMap((option) => (option.aliases ?? []).map((alias) => [normalizeAgentName(alias), option.id])));
|
|
2156
|
+
const COPILOT_SETUP_WORKFLOW_CONTENT = `name: "Copilot Setup Steps"
|
|
2157
|
+
|
|
2158
|
+
on:
|
|
2159
|
+
workflow_dispatch:
|
|
2160
|
+
push:
|
|
2161
|
+
paths:
|
|
2162
|
+
- .github/workflows/copilot-setup-steps.yml
|
|
2163
|
+
pull_request:
|
|
2164
|
+
paths:
|
|
2165
|
+
- .github/workflows/copilot-setup-steps.yml
|
|
2166
|
+
|
|
2167
|
+
jobs:
|
|
2168
|
+
copilot-setup-steps:
|
|
2169
|
+
runs-on: ubuntu-latest
|
|
2170
|
+
permissions:
|
|
2171
|
+
contents: read
|
|
2172
|
+
steps:
|
|
2173
|
+
- name: Checkout code
|
|
2174
|
+
uses: actions/checkout@v6
|
|
2175
|
+
with:
|
|
2176
|
+
persist-credentials: false
|
|
2177
|
+
- name: Set up Vite+
|
|
2178
|
+
uses: voidzero-dev/setup-vp@v1
|
|
2179
|
+
with:
|
|
2180
|
+
cache: true
|
|
2181
|
+
run-install: true
|
|
2182
|
+
- name: Verify Vite+
|
|
2183
|
+
run: vp --version
|
|
2184
|
+
`;
|
|
2185
|
+
async function selectAgentTargets({ interactive, agent, onCancel }) {
|
|
2186
|
+
if (agent === false) return {
|
|
2187
|
+
targetPaths: void 0,
|
|
2188
|
+
selectedAgents: []
|
|
2189
|
+
};
|
|
2190
|
+
if (interactive && !agent) {
|
|
2191
|
+
const selectedAgentIds = await multiselect({
|
|
2192
|
+
message: "Which coding agent instruction files should Vite+ create?",
|
|
2193
|
+
options: AGENTS.map((option) => ({
|
|
2194
|
+
label: option.label,
|
|
2195
|
+
value: option.id,
|
|
2196
|
+
hint: option.hint
|
|
2197
|
+
})),
|
|
2198
|
+
initialValues: [AGENT_DEFAULT_ID],
|
|
2199
|
+
required: false
|
|
2200
|
+
});
|
|
2201
|
+
if (R(selectedAgentIds)) {
|
|
2202
|
+
onCancel();
|
|
2203
|
+
return {
|
|
2204
|
+
targetPaths: void 0,
|
|
2205
|
+
selectedAgents: []
|
|
2206
|
+
};
|
|
2207
|
+
}
|
|
2208
|
+
if (selectedAgentIds.length === 0) return {
|
|
2209
|
+
targetPaths: void 0,
|
|
2210
|
+
selectedAgents: []
|
|
2211
|
+
};
|
|
2212
|
+
const selectedAgents = resolveAgentOptions(selectedAgentIds);
|
|
2213
|
+
return {
|
|
2214
|
+
targetPaths: getAgentTargetPaths(selectedAgents),
|
|
2215
|
+
selectedAgents
|
|
2216
|
+
};
|
|
2217
|
+
}
|
|
2218
|
+
const selectedAgents = resolveAgentOptions(agent ?? AGENT_DEFAULT_ID);
|
|
2219
|
+
return {
|
|
2220
|
+
targetPaths: getAgentTargetPaths(selectedAgents),
|
|
2221
|
+
selectedAgents
|
|
2222
|
+
};
|
|
2223
|
+
}
|
|
2224
|
+
async function selectAgentTargetPaths({ interactive, agent, onCancel }) {
|
|
2225
|
+
return (await selectAgentTargets({
|
|
2226
|
+
interactive,
|
|
2227
|
+
agent,
|
|
2228
|
+
onCancel
|
|
2229
|
+
})).targetPaths;
|
|
2230
|
+
}
|
|
2231
|
+
function detectExistingAgentTargetPaths(projectRoot) {
|
|
2232
|
+
const detectedPaths = [];
|
|
2233
|
+
const seenTargetPaths = /* @__PURE__ */ new Set();
|
|
2234
|
+
for (const option of AGENTS) {
|
|
2235
|
+
if (seenTargetPaths.has(option.targetPath)) continue;
|
|
2236
|
+
seenTargetPaths.add(option.targetPath);
|
|
2237
|
+
const targetPath = path.join(projectRoot, option.targetPath);
|
|
2238
|
+
if (fs.existsSync(targetPath) && !fs.lstatSync(targetPath).isSymbolicLink()) detectedPaths.push(option.targetPath);
|
|
2239
|
+
}
|
|
2240
|
+
return detectedPaths.length > 0 ? detectedPaths : void 0;
|
|
2241
|
+
}
|
|
2242
|
+
/**
|
|
2243
|
+
* Silently update agent instruction files that contain Vite+ markers.
|
|
2244
|
+
* - No agent files → no writes
|
|
2245
|
+
* - No Vite+ markers → no writes
|
|
2246
|
+
* - Markers present, content up to date → no writes
|
|
2247
|
+
* - Markers present, content outdated → update marked section
|
|
2248
|
+
*/
|
|
2249
|
+
function updateExistingAgentInstructions(projectRoot) {
|
|
2250
|
+
const targetPaths = detectExistingAgentTargetPaths(projectRoot);
|
|
2251
|
+
if (!targetPaths) return;
|
|
2252
|
+
const templatePath = path.join(pkgRoot, "AGENTS.md");
|
|
2253
|
+
if (!fs.existsSync(templatePath)) return;
|
|
2254
|
+
const templateContent = fs.readFileSync(templatePath, "utf-8");
|
|
2255
|
+
for (const targetPath of targetPaths) try {
|
|
2256
|
+
const fullPath = path.join(projectRoot, targetPath);
|
|
2257
|
+
const existing = fs.readFileSync(fullPath, "utf-8");
|
|
2258
|
+
const updated = replaceMarkedAgentInstructionsSection(existing, templateContent);
|
|
2259
|
+
if (updated !== void 0 && updated !== existing) fs.writeFileSync(fullPath, updated);
|
|
2260
|
+
} catch {}
|
|
2261
|
+
}
|
|
2262
|
+
function resolveAgentOptions(agent) {
|
|
2263
|
+
const agentNames = parseAgentNames(agent);
|
|
2264
|
+
const resolvedAgentNames = agentNames.length > 0 ? agentNames : [AGENT_DEFAULT_ID];
|
|
2265
|
+
const dedupedAgents = [];
|
|
2266
|
+
const seenAgentIds = /* @__PURE__ */ new Set();
|
|
2267
|
+
for (const name of resolvedAgentNames) {
|
|
2268
|
+
const option = resolveSingleAgentOption(name);
|
|
2269
|
+
if (seenAgentIds.has(option.id)) continue;
|
|
2270
|
+
seenAgentIds.add(option.id);
|
|
2271
|
+
dedupedAgents.push(option);
|
|
2272
|
+
}
|
|
2273
|
+
return dedupedAgents;
|
|
2274
|
+
}
|
|
2275
|
+
function getAgentTargetPaths(agents) {
|
|
2276
|
+
const dedupedTargetPaths = [];
|
|
2277
|
+
const seenTargetPaths = /* @__PURE__ */ new Set();
|
|
2278
|
+
for (const agent of agents) {
|
|
2279
|
+
if (seenTargetPaths.has(agent.targetPath)) continue;
|
|
2280
|
+
seenTargetPaths.add(agent.targetPath);
|
|
2281
|
+
dedupedTargetPaths.push(agent.targetPath);
|
|
2282
|
+
}
|
|
2283
|
+
return dedupedTargetPaths;
|
|
2284
|
+
}
|
|
2285
|
+
function parseAgentNames(agent) {
|
|
2286
|
+
if (!agent) return [];
|
|
2287
|
+
return (Array.isArray(agent) ? agent : [agent]).filter((value) => typeof value === "string").flatMap((value) => value.split(",")).map((value) => value.trim()).filter((value) => value.length > 0);
|
|
2288
|
+
}
|
|
2289
|
+
function resolveSingleAgentOption(agent) {
|
|
2290
|
+
const normalized = normalizeAgentName(agent);
|
|
2291
|
+
const alias = AGENT_ALIASES[normalized];
|
|
2292
|
+
const resolved = alias ? normalizeAgentName(alias) : normalized;
|
|
2293
|
+
return AGENTS.find((option) => normalizeAgentName(option.id) === resolved || normalizeAgentName(option.label) === resolved || normalizeAgentName(option.targetPath) === resolved || option.aliases?.some((candidate) => normalizeAgentName(candidate) === resolved)) ?? AGENTS.find((option) => option.id === AGENT_DEFAULT_ID);
|
|
2294
|
+
}
|
|
2295
|
+
/**
|
|
2296
|
+
* Detect agent instruction files that would conflict (exist without markers).
|
|
2297
|
+
* Returns only files that need a user decision (append or skip).
|
|
2298
|
+
* Read-only — does not write or modify any files.
|
|
2299
|
+
*/
|
|
2300
|
+
async function detectAgentConflicts({ projectRoot, targetPaths }) {
|
|
2301
|
+
if (!targetPaths || targetPaths.length === 0) return [];
|
|
2302
|
+
const sourcePath = path.join(pkgRoot, "AGENTS.md");
|
|
2303
|
+
if (!fs.existsSync(sourcePath)) return [];
|
|
2304
|
+
const incomingContent = await fsPromises.readFile(sourcePath, "utf-8");
|
|
2305
|
+
const shouldLinkToAgents = targetPaths.includes(AGENT_STANDARD_PATH);
|
|
2306
|
+
const orderedPaths = shouldLinkToAgents ? [AGENT_STANDARD_PATH, ...targetPaths.filter((p) => p !== AGENT_STANDARD_PATH)] : targetPaths;
|
|
2307
|
+
const conflicts = [];
|
|
2308
|
+
const seenDestinationPaths = /* @__PURE__ */ new Set();
|
|
2309
|
+
const seenRealPaths = /* @__PURE__ */ new Set();
|
|
2310
|
+
for (const targetPathToCheck of orderedPaths) {
|
|
2311
|
+
const destinationPath = path.join(projectRoot, targetPathToCheck);
|
|
2312
|
+
const destinationKey = path.resolve(destinationPath);
|
|
2313
|
+
if (seenDestinationPaths.has(destinationKey)) continue;
|
|
2314
|
+
seenDestinationPaths.add(destinationKey);
|
|
2315
|
+
if (shouldLinkToAgents && targetPathToCheck !== AGENT_STANDARD_PATH) {
|
|
2316
|
+
if (await getExistingPathKind(destinationPath) !== "file") continue;
|
|
2317
|
+
}
|
|
2318
|
+
if (fs.existsSync(destinationPath)) {
|
|
2319
|
+
if (fs.lstatSync(destinationPath).isSymbolicLink()) continue;
|
|
2320
|
+
const destinationRealPath = await fsPromises.realpath(destinationPath);
|
|
2321
|
+
if (seenRealPaths.has(destinationRealPath)) continue;
|
|
2322
|
+
if (replaceMarkedAgentInstructionsSection(await fsPromises.readFile(destinationPath, "utf-8"), incomingContent) !== void 0) {
|
|
2323
|
+
seenRealPaths.add(destinationRealPath);
|
|
2324
|
+
continue;
|
|
2325
|
+
}
|
|
2326
|
+
conflicts.push({ targetPath: targetPathToCheck });
|
|
2327
|
+
seenRealPaths.add(destinationRealPath);
|
|
2328
|
+
}
|
|
2329
|
+
}
|
|
2330
|
+
return conflicts;
|
|
2331
|
+
}
|
|
2332
|
+
async function writeAgentInstructions({ projectRoot, targetPath, targetPaths, interactive, conflictDecisions, silent = false }) {
|
|
2333
|
+
const paths = [...targetPaths ?? [], ...targetPath ? [targetPath] : []];
|
|
2334
|
+
if (paths.length === 0) return;
|
|
2335
|
+
const sourcePath = path.join(pkgRoot, "AGENTS.md");
|
|
2336
|
+
if (!fs.existsSync(sourcePath)) {
|
|
2337
|
+
if (!silent) log.warn("Agent instructions template not found; skipping.");
|
|
2338
|
+
return;
|
|
2339
|
+
}
|
|
2340
|
+
const seenDestinationPaths = /* @__PURE__ */ new Set();
|
|
2341
|
+
const seenRealPaths = /* @__PURE__ */ new Set();
|
|
2342
|
+
const incomingContent = await fsPromises.readFile(sourcePath, "utf-8");
|
|
2343
|
+
const shouldLinkToAgents = paths.includes(AGENT_STANDARD_PATH);
|
|
2344
|
+
const orderedPaths = shouldLinkToAgents ? [AGENT_STANDARD_PATH, ...paths.filter((p) => p !== AGENT_STANDARD_PATH)] : paths;
|
|
2345
|
+
for (const targetPathToWrite of orderedPaths) {
|
|
2346
|
+
const destinationPath = path.join(projectRoot, targetPathToWrite);
|
|
2347
|
+
const destinationKey = path.resolve(destinationPath);
|
|
2348
|
+
if (seenDestinationPaths.has(destinationKey)) continue;
|
|
2349
|
+
seenDestinationPaths.add(destinationKey);
|
|
2350
|
+
await fsPromises.mkdir(path.dirname(destinationPath), { recursive: true });
|
|
2351
|
+
if (shouldLinkToAgents && targetPathToWrite !== AGENT_STANDARD_PATH) {
|
|
2352
|
+
if (await tryLinkTargetToAgents(projectRoot, targetPathToWrite, silent)) continue;
|
|
2353
|
+
}
|
|
2354
|
+
if (fs.existsSync(destinationPath)) {
|
|
2355
|
+
if (fs.lstatSync(destinationPath).isSymbolicLink()) {
|
|
2356
|
+
if (!silent) log.info(`Skipped writing ${targetPathToWrite} (symlink)`);
|
|
2357
|
+
continue;
|
|
2358
|
+
}
|
|
2359
|
+
const destinationRealPath = await fsPromises.realpath(destinationPath);
|
|
2360
|
+
if (seenRealPaths.has(destinationRealPath)) {
|
|
2361
|
+
if (!silent) log.info(`Skipped writing ${targetPathToWrite} (duplicate target)`);
|
|
2362
|
+
continue;
|
|
2363
|
+
}
|
|
2364
|
+
const existingContent = await fsPromises.readFile(destinationPath, "utf-8");
|
|
2365
|
+
const updatedContent = replaceMarkedAgentInstructionsSection(existingContent, incomingContent);
|
|
2366
|
+
if (updatedContent !== void 0) {
|
|
2367
|
+
if (updatedContent !== existingContent) await fsPromises.writeFile(destinationPath, updatedContent);
|
|
2368
|
+
seenRealPaths.add(destinationRealPath);
|
|
2369
|
+
continue;
|
|
2370
|
+
}
|
|
2371
|
+
let conflictAction;
|
|
2372
|
+
const preResolved = conflictDecisions?.get(targetPathToWrite);
|
|
2373
|
+
if (preResolved) conflictAction = preResolved;
|
|
2374
|
+
else if (interactive) {
|
|
2375
|
+
const action = await select({
|
|
2376
|
+
message: `Agent instructions already exist at ${targetPathToWrite}.\n ` + styleText("gray", "The Vite+ template includes guidance on `vp` commands, the build pipeline, and project conventions."),
|
|
2377
|
+
options: [{
|
|
2378
|
+
label: "Append",
|
|
2379
|
+
value: "append",
|
|
2380
|
+
hint: "Add template content to the end"
|
|
2381
|
+
}, {
|
|
2382
|
+
label: "Skip",
|
|
2383
|
+
value: "skip",
|
|
2384
|
+
hint: "Leave existing file unchanged"
|
|
2385
|
+
}],
|
|
2386
|
+
initialValue: "skip"
|
|
2387
|
+
});
|
|
2388
|
+
conflictAction = R(action) || action === "skip" ? "skip" : "append";
|
|
2389
|
+
} else conflictAction = "skip";
|
|
2390
|
+
if (conflictAction === "append") await appendAgentContent(destinationPath, targetPathToWrite, existingContent, incomingContent, silent);
|
|
2391
|
+
else {
|
|
2392
|
+
const suffix = !preResolved && !interactive ? " (already exists)" : "";
|
|
2393
|
+
if (!silent) log.info(`Skipped writing ${targetPathToWrite}${suffix}`);
|
|
2394
|
+
}
|
|
2395
|
+
seenRealPaths.add(destinationRealPath);
|
|
2396
|
+
continue;
|
|
2397
|
+
}
|
|
2398
|
+
await fsPromises.writeFile(destinationPath, incomingContent);
|
|
2399
|
+
if (!silent) log.success(`Wrote agent instructions to ${targetPathToWrite}`);
|
|
2400
|
+
seenRealPaths.add(await fsPromises.realpath(destinationPath));
|
|
2401
|
+
}
|
|
2402
|
+
}
|
|
2403
|
+
async function writeCopilotSetupWorkflow({ projectRoot, silent = false }) {
|
|
2404
|
+
const destinationPath = path.join(projectRoot, COPILOT_SETUP_WORKFLOW_PATH);
|
|
2405
|
+
await fsPromises.mkdir(path.dirname(destinationPath), { recursive: true });
|
|
2406
|
+
if (fs.existsSync(destinationPath)) {
|
|
2407
|
+
if (!silent) log.info(`Skipped writing ${COPILOT_SETUP_WORKFLOW_PATH} (already exists)`);
|
|
2408
|
+
return;
|
|
2409
|
+
}
|
|
2410
|
+
await fsPromises.writeFile(destinationPath, COPILOT_SETUP_WORKFLOW_CONTENT);
|
|
2411
|
+
if (!silent) log.success(`Wrote Copilot setup workflow to ${COPILOT_SETUP_WORKFLOW_PATH}`);
|
|
2412
|
+
}
|
|
2413
|
+
async function appendAgentContent(destinationPath, targetPath, existingContent, incomingContent, silent = false) {
|
|
2414
|
+
const separator = existingContent.endsWith("\n") ? "" : "\n";
|
|
2415
|
+
await fsPromises.appendFile(destinationPath, `${separator}\n${incomingContent}`);
|
|
2416
|
+
if (!silent) log.success(`Appended agent instructions to ${targetPath}`);
|
|
2417
|
+
}
|
|
2418
|
+
function normalizeAgentName(value) {
|
|
2419
|
+
return value.trim().toLowerCase().replace(/[^a-z0-9]+/g, "");
|
|
2420
|
+
}
|
|
2421
|
+
function replaceMarkedAgentInstructionsSection(existing, incoming) {
|
|
2422
|
+
const existingRange = getMarkedRange(existing, AGENT_INSTRUCTIONS_START_MARKER, AGENT_INSTRUCTIONS_END_MARKER);
|
|
2423
|
+
if (!existingRange) return;
|
|
2424
|
+
const incomingRange = getMarkedRange(incoming, AGENT_INSTRUCTIONS_START_MARKER, AGENT_INSTRUCTIONS_END_MARKER);
|
|
2425
|
+
if (!incomingRange) return;
|
|
2426
|
+
return `${existing.slice(0, existingRange.start)}${incoming.slice(incomingRange.start, incomingRange.end)}${existing.slice(existingRange.end)}`;
|
|
2427
|
+
}
|
|
2428
|
+
async function tryLinkTargetToAgents(projectRoot, targetPath, silent = false) {
|
|
2429
|
+
const destinationPath = path.join(projectRoot, targetPath);
|
|
2430
|
+
const agentsPath = path.join(projectRoot, AGENT_STANDARD_PATH);
|
|
2431
|
+
const symlinkTarget = path.relative(path.dirname(destinationPath), agentsPath);
|
|
2432
|
+
const existing = await getExistingPathKind(destinationPath);
|
|
2433
|
+
if (existing === "file") return false;
|
|
2434
|
+
if (existing === "symlink") {
|
|
2435
|
+
const currentLink = await fsPromises.readlink(destinationPath);
|
|
2436
|
+
if (path.resolve(path.dirname(destinationPath), currentLink) === agentsPath) {
|
|
2437
|
+
if (!silent) log.info(`Skipped linking ${targetPath} (already linked to ${AGENT_STANDARD_PATH})`);
|
|
2438
|
+
return true;
|
|
2439
|
+
}
|
|
2440
|
+
await fsPromises.unlink(destinationPath);
|
|
2441
|
+
}
|
|
2442
|
+
try {
|
|
2443
|
+
await fsPromises.symlink(symlinkTarget, destinationPath);
|
|
2444
|
+
} catch (err) {
|
|
2445
|
+
if (err.code === "EPERM") {
|
|
2446
|
+
await fsPromises.copyFile(agentsPath, destinationPath);
|
|
2447
|
+
if (!silent) log.success(`Copied ${AGENT_STANDARD_PATH} to ${targetPath}`);
|
|
2448
|
+
return true;
|
|
2449
|
+
}
|
|
2450
|
+
throw err;
|
|
2451
|
+
}
|
|
2452
|
+
if (!silent) log.success(`Linked ${targetPath} to ${AGENT_STANDARD_PATH}`);
|
|
2453
|
+
return true;
|
|
2454
|
+
}
|
|
2455
|
+
async function getExistingPathKind(filePath) {
|
|
2456
|
+
if (!fs.existsSync(filePath)) return "missing";
|
|
2457
|
+
return (await fsPromises.lstat(filePath)).isSymbolicLink() ? "symlink" : "file";
|
|
2458
|
+
}
|
|
2459
|
+
function getMarkedRange(content, startMarker, endMarker) {
|
|
2460
|
+
const start = content.indexOf(startMarker);
|
|
2461
|
+
if (start === -1) return;
|
|
2462
|
+
const endMarkerIndex = content.indexOf(endMarker, start + startMarker.length);
|
|
2463
|
+
if (endMarkerIndex === -1) return;
|
|
2464
|
+
return {
|
|
2465
|
+
start,
|
|
2466
|
+
end: endMarkerIndex + endMarker.length
|
|
2467
|
+
};
|
|
2468
|
+
}
|
|
2469
|
+
//#endregion
|
|
2470
|
+
export { promptEslintMigration as A, editYamlFile as B, injectLintTypeCheckDefaults as C, migrateNodeVersionManagerFile as D, migrateEslintToOxlint as E, setPackageManager as F, displayRelative as H, warnIncompatibleEslintIntegration as I, warnLegacyEslintConfig as L, rewriteMonorepo as M, rewriteMonorepoProject as N, migratePrettierToOxfmt as O, rewriteStandaloneProject as P, warnPackageLevelEslint as R, injectCreateDefaultTemplate as S, mergeViteConfigFiles as T, templatesDir as U, readYamlFile as V, detectNodeVersionManagerFile as _, selectAgentTargets as a, hasFrameworkShim as b, writeCopilotSetupWorkflow as c, checkVitestVersion as d, confirmEslintMigration as f, detectIncompatibleEslintIntegration as g, detectFramework as h, selectAgentTargetPaths as i, promptPrettierMigration as j, preflightGitHooksSetup as k, addFrameworkShim as l, detectEslintProject as m, detectAgentConflicts as n, updateExistingAgentInstructions as o, confirmPrettierMigration as p, detectExistingAgentTargetPaths as r, writeAgentInstructions as s, COPILOT_AGENT_ID as t, checkViteVersion as u, detectPrettierProject as v, installGitHooks as w, hasStagedConfigInViteConfig as x, ensurePreCommitHook as y, warnPackageLevelPrettier as z };
|