diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_hooks_commented_staged_key/package.json b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_hooks_commented_staged_key/package.json new file mode 100644 index 0000000000..207cee24d8 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_hooks_commented_staged_key/package.json @@ -0,0 +1,16 @@ +{ + "name": "migration-hooks-commented-staged-key", + "devDependencies": { + "vite-plus": "latest" + }, + "lint-staged": { + "*.ts": "vp check --fix" + }, + "devEngines": { + "packageManager": { + "name": "npm", + "version": "11.16.0", + "onFail": "download" + } + } +} diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_hooks_commented_staged_key/snapshots.toml b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_hooks_commented_staged_key/snapshots.toml new file mode 100644 index 0000000000..797ccbec54 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_hooks_commented_staged_key/snapshots.toml @@ -0,0 +1,11 @@ +[[case]] +name = "migration_hooks_commented_staged_key" +vp = "global" +comment = "A commented-out `staged:` must not count as existing staged config, or package.json lint-staged is deleted without being merged." +skip-platforms = [{ os = "linux", libc = "musl" }] +steps = [ + { argv = ["git", "init"], snapshot = false, continue-on-failure = true }, + { argv = ["vp", "migrate", "--no-interactive", "--hooks"], continue-on-failure = true }, + { argv = ["vpt", "print-file", "package.json"], comment = "lint-staged should be removed only because it was merged", continue-on-failure = true }, + { argv = ["vpt", "print-file", "vite.config.ts"], comment = "staged config should be merged from package.json", continue-on-failure = true }, +] diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_hooks_commented_staged_key/snapshots/migration_hooks_commented_staged_key.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_hooks_commented_staged_key/snapshots/migration_hooks_commented_staged_key.md new file mode 100644 index 0000000000..870a52a84c --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_hooks_commented_staged_key/snapshots/migration_hooks_commented_staged_key.md @@ -0,0 +1,63 @@ +# migration_hooks_commented_staged_key + +A commented-out `staged:` must not count as existing staged config, or package.json lint-staged is deleted without being merged. + +## `git init` + + +## `vp migrate --no-interactive --hooks` + +``` +VITE+ - The Unified Toolchain for the Web + +◇ Updated . to Vite+ +• Node npm +• Dependencies: + vite-plus latest → + vite → +• 1 config update applied +• Git hooks configured +• Package manager settings configured +``` + +## `vpt print-file package.json` + +lint-staged should be removed only because it was merged + +``` +{ + "name": "migration-hooks-commented-staged-key", + "devDependencies": { + "vite-plus": "" + }, + "devEngines": { + "packageManager": { + "name": "npm", + "version": "", + "onFail": "download" + } + }, + "overrides": { + "vite": "npm:@voidzero-dev/vite-plus-core@" + }, + "scripts": { + "prepare": "vp config" + } +} +``` + +## `vpt print-file vite.config.ts` + +staged config should be merged from package.json + +``` +import { defineConfig } from 'vite-plus'; + +// TODO: move lint-staged here as staged: { ... } +export default defineConfig({ + staged: { + "*.ts": "vp check --fix" + }, + +}); +``` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_hooks_commented_staged_key/vite.config.ts b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_hooks_commented_staged_key/vite.config.ts new file mode 100644 index 0000000000..4aa35a5250 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_hooks_commented_staged_key/vite.config.ts @@ -0,0 +1,4 @@ +import { defineConfig } from 'vite-plus'; + +// TODO: move lint-staged here as staged: { ... } +export default defineConfig({}); diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_lintstagedrc_quoted_staged_key/.lintstagedrc.json b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_lintstagedrc_quoted_staged_key/.lintstagedrc.json new file mode 100644 index 0000000000..66d1e72d92 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_lintstagedrc_quoted_staged_key/.lintstagedrc.json @@ -0,0 +1,3 @@ +{ + "*.css": "stylelint --fix" +} diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_lintstagedrc_quoted_staged_key/package.json b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_lintstagedrc_quoted_staged_key/package.json new file mode 100644 index 0000000000..6d075d0ec2 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_lintstagedrc_quoted_staged_key/package.json @@ -0,0 +1,7 @@ +{ + "name": "migration-lintstagedrc-quoted-staged-key", + "devDependencies": { + "lint-staged": "^16.2.6", + "vite": "^7.0.0" + } +} diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_lintstagedrc_quoted_staged_key/snapshots.toml b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_lintstagedrc_quoted_staged_key/snapshots.toml new file mode 100644 index 0000000000..05e789892c --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_lintstagedrc_quoted_staged_key/snapshots.toml @@ -0,0 +1,10 @@ +[[case]] +name = "migration_lintstagedrc_quoted_staged_key" +vp = "global" +comment = "A quoted `'staged'` key is existing staged config, so the standalone file must be kept instead of merged as a duplicate key." +steps = [ + { argv = ["git", "init"], snapshot = false, continue-on-failure = true }, + { argv = ["vp", "migrate", "--no-interactive"], comment = "should warn when staged already exists in vite.config.ts", continue-on-failure = true }, + { argv = ["vpt", "stat-file", ".lintstagedrc.json", "--assert", "file"], comment = "lintstagedrc.json should still exist", continue-on-failure = true }, + { argv = ["vpt", "print-file", "vite.config.ts"], comment = "vite config should have a single staged key", continue-on-failure = true }, +] diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_lintstagedrc_quoted_staged_key/snapshots/migration_lintstagedrc_quoted_staged_key.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_lintstagedrc_quoted_staged_key/snapshots/migration_lintstagedrc_quoted_staged_key.md new file mode 100644 index 0000000000..35e47d401b --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_lintstagedrc_quoted_staged_key/snapshots/migration_lintstagedrc_quoted_staged_key.md @@ -0,0 +1,44 @@ +# migration_lintstagedrc_quoted_staged_key + +A quoted `'staged'` key is existing staged config, so the standalone file must be kept instead of merged as a duplicate key. + +## `git init` + + +## `vp migrate --no-interactive` + +should warn when staged already exists in vite.config.ts + +``` +VITE+ - The Unified Toolchain for the Web + +◇ Migrated . to Vite+ +• Node pnpm +• Git hooks configured +! Warnings: + - .lintstagedrc.json found but "staged" already exists in vite.config.ts — please merge manually +``` + +## `vpt stat-file .lintstagedrc.json --assert file` + +lintstagedrc.json should still exist + +``` +.lintstagedrc.json: file +``` + +## `vpt print-file vite.config.ts` + +vite config should have a single staged key + +``` +import { defineConfig } from 'vite-plus'; + +export default defineConfig({ + fmt: {}, + lint: {"jsPlugins":[{"name":"vite-plus","specifier":"vite-plus/oxlint-plugin"}],"rules":{"vite-plus/prefer-vite-plus-imports":"error"},"options":{"typeAware":true,"typeCheck":true}}, + 'staged': { + '*.js': 'vp check --fix', + }, +}); +``` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_lintstagedrc_quoted_staged_key/vite.config.ts b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_lintstagedrc_quoted_staged_key/vite.config.ts new file mode 100644 index 0000000000..198e94c333 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_lintstagedrc_quoted_staged_key/vite.config.ts @@ -0,0 +1,7 @@ +import { defineConfig } from 'vite-plus'; + +export default defineConfig({ + 'staged': { + '*.js': 'vp check --fix', + }, +}); diff --git a/packages/cli/src/migration/migrator/vite-config.ts b/packages/cli/src/migration/migrator/vite-config.ts index aa54d9d786..cc1b3d8cd1 100644 --- a/packages/cli/src/migration/migrator/vite-config.ts +++ b/packages/cli/src/migration/migrator/vite-config.ts @@ -938,9 +938,7 @@ export function hasStagedConfigInViteConfig(projectPath: string): boolean { if (!configs.viteConfig) { return false; } - const viteConfigPath = path.join(projectPath, configs.viteConfig); - const content = fs.readFileSync(viteConfigPath, 'utf8'); - return /\bstaged\s*:/.test(content); + return hasConfigKey(path.join(projectPath, configs.viteConfig), 'staged'); } /**