Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cli] Install can match with alpha versions now #4247

Merged
merged 2 commits into from Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions cli/src/commands/__tests__/install-test.js
Expand Up @@ -364,6 +364,49 @@ describe('install (command)', () => {
});
});

it('installs version matched libdef for alpha versions', () => {
return fakeProjectEnv(async FLOWPROJ_DIR => {
// Create some dependencies
await Promise.all([
touchFile(path.join(FLOWPROJ_DIR, '.flowconfig')),
writePkgJson(path.join(FLOWPROJ_DIR, 'package.json'), {
name: 'test',
devDependencies: {
'flow-bin': '^0.43.0',
},
dependencies: {
foo: '1.2.3-alpha.5',
},
}),
mkdirp(path.join(FLOWPROJ_DIR, 'node_modules', 'foo')),
mkdirp(path.join(FLOWPROJ_DIR, 'node_modules', 'flow-bin')),
]);

// Run the install command
await run({
...defaultRunProps,
ignoreDeps: [],
});

// Installs libdefs
expect(
await Promise.all([
fs.exists(
path.join(
FLOWPROJ_DIR,
'flow-typed',
'npm',
'flow-bin_v0.x.x.js',
),
),
fs.exists(
path.join(FLOWPROJ_DIR, 'flow-typed', 'npm', 'foo_v1.x.x.js'),
),
]),
).toEqual([true, true]);
});
});

it('installs available libdefs using PnP', () => {
return fakeProjectEnv(async FLOWPROJ_DIR => {
// Create some dependencies
Expand Down
30 changes: 16 additions & 14 deletions cli/src/lib/npm/npmLibDefs.js
Expand Up @@ -255,7 +255,7 @@ function validateVersionNumPart(part: string, partName: string): number {
}

export function pkgVersionMatch(
pkgSemverRaw: string,
pkgSemver: string,
libDefSemverRaw: string,
): boolean {
// The package version should be treated as a semver implicitly prefixed by
Expand All @@ -275,19 +275,21 @@ export function pkgVersionMatch(
return libDefSemverRaw;
})();

const pkgSemver = (() => {
// If pkg version is prefixed with `>=` we should be treated as `^`
// Normally `>=` would mean anything greater than a particular version so
// ">=2.1.0" would match 2.1.0 up to anything such as 3.4.5
// But in the case of flow types, an import of a lib should probably match
// the lowest version that matches the range to assume backwards compatibility usage
const gtEq = '>=';
if (pkgSemverRaw.startsWith(gtEq)) {
return pkgSemverRaw.replace(gtEq, '^');
}

return pkgSemverRaw;
})();
// If pkg version is prefixed with `>=` we should be treated as `^`
// Normally `>=` would mean anything greater than a particular version so
// ">=2.1.0" would match 2.1.0 up to anything such as 3.4.5
// But in the case of flow types, an import of a lib should probably match
// the lowest version that matches the range to assume backwards compatibility usage
const gtEq = '>=';
if (pkgSemver.startsWith(gtEq)) {
pkgSemver = pkgSemver.replace(gtEq, '^');
}
// Libraries that are released with an alpha versioning (eg: v6.0.0-alpha.0)
// Need to have the alpha version details removed to have a semver match against
// a libdef version.
if (pkgSemver.includes('-')) {
pkgSemver = pkgSemver.substring(0, pkgSemver.indexOf('-'));
gantoine marked this conversation as resolved.
Show resolved Hide resolved
}

if (semver.valid(pkgSemver)) {
// Test the single package version against the LibDef range
Expand Down