Skip to content

Commit

Permalink
Improves @astrojs/upgrade dependency handling (#9317)
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re authored Dec 5, 2023
1 parent aa2e76d commit d1c91ad
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-otters-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/upgrade': patch
---

Improves dependency handling by ignoring packages that don't use a semver version
27 changes: 21 additions & 6 deletions packages/upgrade/src/actions/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,40 @@ 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,
targetVersion: ctx.version,
});
}
for (const [name, currentVersion] of Object.entries(devDependencies)) {
if (!isAstroPackage(name)) continue;
if (!isSupportedPackage(name, currentVersion)) continue;
ctx.packages.push({
name,
currentVersion,
Expand Down
4 changes: 2 additions & 2 deletions packages/upgrade/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -29,4 +29,4 @@ export async function main() {
process.exit(0);
}

export { getContext, install, setStdout, verify };
export { getContext, install, setStdout, verify, collectPackageInfo };
66 changes: 66 additions & 0 deletions packages/upgrade/test/verify.test.js
Original file line number Diff line number Diff line change
@@ -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([]);
});
});

0 comments on commit d1c91ad

Please sign in to comment.