diff options
author | 2023-12-05 10:22:13 -0600 | |
---|---|---|
committer | 2023-12-05 10:22:13 -0600 | |
commit | d1c91add074c2e08056f01df5a6043c9716b7e1f (patch) | |
tree | 77d4deda2c705b9f631ba15fd9dfebd72dc17a35 | |
parent | aa2e76d54f8169682b818f3e8066baa7c913962f (diff) | |
download | astro-d1c91add074c2e08056f01df5a6043c9716b7e1f.tar.gz astro-d1c91add074c2e08056f01df5a6043c9716b7e1f.tar.zst astro-d1c91add074c2e08056f01df5a6043c9716b7e1f.zip |
Improves `@astrojs/upgrade` dependency handling (#9317)
-rw-r--r-- | .changeset/poor-otters-doubt.md | 5 | ||||
-rw-r--r-- | packages/upgrade/src/actions/verify.ts | 27 | ||||
-rw-r--r-- | packages/upgrade/src/index.ts | 4 | ||||
-rw-r--r-- | packages/upgrade/test/verify.test.js | 66 |
4 files changed, 94 insertions, 8 deletions
diff --git a/.changeset/poor-otters-doubt.md b/.changeset/poor-otters-doubt.md new file mode 100644 index 000000000..55be4b145 --- /dev/null +++ b/.changeset/poor-otters-doubt.md @@ -0,0 +1,5 @@ +--- +'@astrojs/upgrade': patch +--- + +Improves dependency handling by ignoring packages that don't use a semver version diff --git a/packages/upgrade/src/actions/verify.ts b/packages/upgrade/src/actions/verify.ts index 82daa9a06..8233a5caf 100644 --- a/packages/upgrade/src/actions/verify.ts +++ b/packages/upgrade/src/actions/verify.ts @@ -66,17 +66,32 @@ async function verifyAstroProject(ctx: Pick<Context, 'cwd' | 'version' | 'packag return true; } -function isAstroPackage(name: string) { +function isAstroPackage(name: string, _version: string) { return name === 'astro' || name.startsWith('@astrojs/'); } -function collectPackageInfo( +function isAllowedPackage(name: string, _version: string) { + return name !== '@astrojs/upgrade'; +} + +function isValidVersion(_name: string, version: string) { + return semverCoerce(version, { loose: true }) !== null; +} + +function isSupportedPackage(name: string, version: string): boolean { + for (const validator of [isAstroPackage, isAllowedPackage, isValidVersion]) { + if (!validator(name, version)) return false; + } + return true; +} + +export function collectPackageInfo( ctx: Pick<Context, 'version' | 'packages'>, - dependencies: Record<string, string>, - devDependencies: Record<string, string> + dependencies: Record<string, string> = {}, + devDependencies: Record<string, string> = {} ) { for (const [name, currentVersion] of Object.entries(dependencies)) { - if (!isAstroPackage(name)) continue; + if (!isSupportedPackage(name, currentVersion)) continue; ctx.packages.push({ name, currentVersion, @@ -84,7 +99,7 @@ function collectPackageInfo( }); } for (const [name, currentVersion] of Object.entries(devDependencies)) { - if (!isAstroPackage(name)) continue; + if (!isSupportedPackage(name, currentVersion)) continue; ctx.packages.push({ name, currentVersion, diff --git a/packages/upgrade/src/index.ts b/packages/upgrade/src/index.ts index 8131e6eb3..8dac82c1c 100644 --- a/packages/upgrade/src/index.ts +++ b/packages/upgrade/src/index.ts @@ -2,7 +2,7 @@ import { getContext } from './actions/context.js'; import { help } from './actions/help.js'; import { install } from './actions/install.js'; -import { verify } from './actions/verify.js'; +import { verify, collectPackageInfo } from './actions/verify.js'; import { setStdout } from './messages.js'; const exit = () => process.exit(0); @@ -29,4 +29,4 @@ export async function main() { process.exit(0); } -export { getContext, install, setStdout, verify }; +export { getContext, install, setStdout, verify, collectPackageInfo }; diff --git a/packages/upgrade/test/verify.test.js b/packages/upgrade/test/verify.test.js new file mode 100644 index 000000000..ec9d2de58 --- /dev/null +++ b/packages/upgrade/test/verify.test.js @@ -0,0 +1,66 @@ +import { expect } from 'chai'; +import { collectPackageInfo } from '../dist/index.js'; + +describe('collectPackageInfo', () => { + const context = { + cwd: '', + version: 'latest', + packageManager: 'npm', + dryRun: true, + packages: [] + }; + + beforeEach(() => { + context.packages = []; + }) + + it('detects astro', async () => { + collectPackageInfo(context, { "astro": "1.0.0" }, {}); + expect(context.packages).deep.equal([{ name: 'astro', currentVersion: '1.0.0', targetVersion: 'latest' }]); + }); + + it('detects @astrojs', async () => { + collectPackageInfo(context, { "@astrojs/preact": "1.0.0" }, {}); + expect(context.packages).deep.equal([{ name: '@astrojs/preact', currentVersion: '1.0.0', targetVersion: 'latest' }]); + }); + + it('supports ^ prefixes', async () => { + collectPackageInfo(context, { "astro": "^1.0.0" }, {}); + expect(context.packages).deep.equal([{ name: 'astro', currentVersion: '^1.0.0', targetVersion: 'latest' }]); + }); + + it('supports ~ prefixes', async () => { + collectPackageInfo(context, { "astro": "~1.0.0" }, {}); + expect(context.packages).deep.equal([{ name: 'astro', currentVersion: '~1.0.0', targetVersion: 'latest' }]); + }); + + it('supports prereleases', async () => { + collectPackageInfo(context, { "astro": "1.0.0-beta.0" }, {}); + expect(context.packages).deep.equal([{ name: 'astro', currentVersion: '1.0.0-beta.0', targetVersion: 'latest' }]); + }); + + it('ignores self', async () => { + collectPackageInfo(context, { "@astrojs/upgrade": "0.0.1" }, {}); + expect(context.packages).deep.equal([]); + }); + + it('ignores linked packages', async () => { + collectPackageInfo(context, { "@astrojs/preact": "link:../packages/preact" }, {}); + expect(context.packages).deep.equal([]); + }); + + it('ignores workspace packages', async () => { + collectPackageInfo(context, { "@astrojs/preact": "workspace:*" }, {}); + expect(context.packages).deep.equal([]); + }); + + it('ignores github packages', async () => { + collectPackageInfo(context, { "@astrojs/preact": "github:withastro/astro" }, {}); + expect(context.packages).deep.equal([]); + }); + + it('ignores tag', async () => { + collectPackageInfo(context, { "@astrojs/preact": "beta" }, {}); + expect(context.packages).deep.equal([]); + }); +}); |