From e7e3d0758d333d77d66bb6b827d4b546e67ba57d Mon Sep 17 00:00:00 2001 From: edison Date: Tue, 15 Oct 2024 09:30:07 +0800 Subject: [PATCH] fix(vue-macros): override catalog (#17) * fix(vue-macros): override catelog --- package.json | 8 ++++++- pnpm-lock.yaml | 16 +++++++++---- tests/vue-macros.ts | 18 ++++++++++++-- types.d.ts | 2 +- utils.ts | 58 ++++++++++++++++++++++----------------------- 5 files changed, 64 insertions(+), 38 deletions(-) diff --git a/package.json b/package.json index 62b14fe..3b630f3 100644 --- a/package.json +++ b/package.json @@ -63,6 +63,12 @@ "rimraf": "^5.0.10", "simple-git-hooks": "^2.11.1", "tsx": "^4.19.1", - "typescript": "^5.6.2" + "typescript": "^5.6.2", + "yaml": "^2.5.1" + }, + "pnpm": { + "overrides": { + "cookie@<0.7.0": ">=0.7.0" + } } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a6c64d8..ebcb290 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,6 +4,9 @@ settings: autoInstallPeers: true excludeLinksFromLockfile: false +overrides: + cookie@<0.7.0: '>=0.7.0' + importers: .: @@ -63,6 +66,9 @@ importers: typescript: specifier: ^5.6.2 version: 5.6.2 + yaml: + specifier: ^2.5.1 + version: 2.5.1 packages: @@ -652,9 +658,9 @@ packages: cookie-signature@1.0.6: resolution: {integrity: sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==} - cookie@0.6.0: - resolution: {integrity: sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==} - engines: {node: '>= 0.6'} + cookie@1.0.1: + resolution: {integrity: sha512-Xd8lFX4LM9QEEwxQpF9J9NTUh8pmdJO0cyRJhFiDoLTk2eH8FXlRv2IFGYVadZpqI3j8fhNrSdKCeYPxiAhLXw==} + engines: {node: '>=18'} core-js@3.37.1: resolution: {integrity: sha512-Xn6qmxrQZyB0FFY8E3bgRXei3lWDJHhvI+u0q9TKIYM49G8pAr0FgnnrFRAmsbptZL1yxRADVXn+x5AGsbBfyw==} @@ -2714,7 +2720,7 @@ snapshots: cookie-signature@1.0.6: {} - cookie@0.6.0: {} + cookie@1.0.1: {} core-js@3.37.1: {} @@ -3000,7 +3006,7 @@ snapshots: body-parser: 1.20.3 content-disposition: 0.5.4 content-type: 1.0.5 - cookie: 0.6.0 + cookie: 1.0.1 cookie-signature: 1.0.6 debug: 2.6.9 depd: 2.0.0 diff --git a/tests/vue-macros.ts b/tests/vue-macros.ts index 5af7c8a..2be9827 100644 --- a/tests/vue-macros.ts +++ b/tests/vue-macros.ts @@ -1,13 +1,27 @@ import { runInRepo } from '../utils.ts' -import { RunOptions } from '../types.ts' +import { Overrides, RunOptions } from '../types.ts' +import YAML from 'yaml' export async function test(options: RunOptions) { + const overrideVueVersion = '@^3' await runInRepo({ ...options, repo: 'vue-macros/vue-macros', branch: 'main', build: 'build', test: ['test:ecosystem'], - overrideVueVersion: '@^3', + overrideVueVersion, + patchFiles: { + 'pnpm-workspace.yaml': (content: string, overrides: Overrides) => { + const data = YAML.parse(content) + Object.keys(overrides).forEach((key) => { + const pkgName = key.replace(overrideVueVersion, '') + if (data.catalog[pkgName]) { + data.catalog[pkgName] = overrides[key] + } + }) + return YAML.stringify(data) + }, + }, }) } diff --git a/types.d.ts b/types.d.ts index 0b71219..298daaa 100644 --- a/types.d.ts +++ b/types.d.ts @@ -23,7 +23,7 @@ export interface RunOptions { beforeInstall?: Task | Task[] beforeBuild?: Task | Task[] beforeTest?: Task | Task[] - patchFiles?: Record string> + patchFiles?: Record string> } type Task = string | { script: string; args?: string[] } | (() => Promise) diff --git a/utils.ts b/utils.ts index 516789a..0942609 100644 --- a/utils.ts +++ b/utils.ts @@ -267,12 +267,40 @@ export async function runInRepo(options: RunOptions & RepoOptions) { ) } + const overrides = options.overrides || {} + const vuePackages = await getVuePackages() + + if (options.release) { + // pkg.pr.new support + for (const pkg of vuePackages) { + let version = options.release + if (options.release.startsWith('@')) { + version = `https://pkg.pr.new/${pkg.name}@${options.release.slice(1)}` + } + if (overrides[pkg.name] && overrides[pkg.name] !== version) { + throw new Error( + `conflicting overrides[${pkg.name}]=${ + overrides[pkg.name] + } and --release=${ + options.release + } config. Use either one or the other`, + ) + } else { + overrides[`${pkg.name}${overrideVueVersion}`] = version + } + } + } else { + for (const pkg of vuePackages) { + overrides[pkg.name] ||= pkg.hashedVersion + } + } + if (patchFiles) { for (const fileName in patchFiles) { const filePath = path.resolve(dir, fileName) const patchFn = patchFiles[fileName] const content = fs.readFileSync(filePath, 'utf-8') - fs.writeFileSync(filePath, patchFn(content)) + fs.writeFileSync(filePath, patchFn(content, overrides)) console.log(`patched file: ${fileName}`) } } @@ -297,36 +325,8 @@ export async function runInRepo(options: RunOptions & RepoOptions) { await beforeTestCommand?.(pkg.scripts) await testCommand?.(pkg.scripts) } - const overrides = options.overrides || {} - - const vuePackages = await getVuePackages() - if (options.release) { - // pkg.pr.new support - for (const pkg of vuePackages) { - let version = options.release - if (options.release.startsWith('@')) { - version = `https://pkg.pr.new/${pkg.name}@${options.release.slice(1)}` - } - if (overrides[pkg.name] && overrides[pkg.name] !== version) { - throw new Error( - `conflicting overrides[${pkg.name}]=${ - overrides[pkg.name] - } and --release=${ - options.release - } config. Use either one or the other`, - ) - } else { - overrides[`${pkg.name}${overrideVueVersion}`] = version - } - } - } else { - for (const pkg of vuePackages) { - overrides[pkg.name] ||= pkg.hashedVersion - } - } await applyPackageOverrides(dir, pkg, overrides) - await beforeBuildCommand?.(pkg.scripts) await buildCommand?.(pkg.scripts) if (test) {