-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve pnpm version detection (#14)
- Loading branch information
1 parent
af757d9
commit 1811668
Showing
7 changed files
with
124 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import { ci } from './ci'; | ||
import { ci } from './ci.js'; | ||
|
||
try { | ||
process.exit(ci().status!); | ||
} catch (error) { | ||
console.error((error as any).message); | ||
process.exit(1); | ||
} | ||
(async () => { | ||
try { | ||
const { status } = await ci(); | ||
process.exit(status!); | ||
} catch (error) { | ||
console.error((error as Error).message); | ||
process.exitCode = 1; | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,64 @@ | ||
export type Version = [number, number, number]; | ||
export type LockVersion = [number, number]; | ||
export type NodeVersion = [number, number, number]; | ||
|
||
const compareSemver = (semverA: Version, semverB: Version) => ( | ||
semverA[0] - semverB[0] | ||
const compareSemver = <Version extends number[]>( | ||
semverA: Version, | ||
semverB: Version, | ||
) => ( | ||
semverA[0] - semverB[0] | ||
|| semverA[1] - semverB[1] | ||
|| semverA[2] - semverB[2] | ||
); | ||
|| 0 | ||
); | ||
|
||
export function getPnpmVersion(nodeVersion: Version) { | ||
// pnpm v7 requires Node v14.19 and up: https://github.com/pnpm/pnpm/blob/v7.0.0/packages/types/package.json#L8 | ||
if (compareSemver(nodeVersion, [14, 19, 0]) >= 0) { | ||
return 'latest'; | ||
} | ||
type PnpmVersion = { | ||
node: NodeVersion; | ||
lock: LockVersion; | ||
}; | ||
const pnpmVersions: [string, PnpmVersion][] = [ | ||
['8', { | ||
// https://github.com/pnpm/pnpm/blob/v8.0.0/packages/types/package.json#L8 | ||
node: [16, 14, 0], | ||
|
||
// pnpm v6 requires Node v12.17 and up: https://github.com/pnpm/pnpm/releases/tag/v6.0.0 | ||
if (compareSemver(nodeVersion, [12, 17, 0]) >= 0) { | ||
return '6'; | ||
} | ||
// https://github.com/pnpm/pnpm/blob/v8.0.0/packages/constants/src/index.ts#L2 | ||
lock: [6, 0], | ||
}], | ||
['7', { | ||
// https://github.com/pnpm/pnpm/blob/v7.0.0/packages/types/package.json#L8 | ||
node: [14, 19, 0], | ||
|
||
return '5'; | ||
} | ||
// https://github.com/pnpm/pnpm/blob/v7.0.0/packages/constants/src/index.ts#L2 | ||
lock: [5, 4], | ||
}], | ||
['6', { | ||
// https://github.com/pnpm/pnpm/blob/v6.0.0/packages/types/package.json#L8 | ||
node: [12, 17, 0], | ||
|
||
// https://github.com/pnpm/pnpm/blob/v6.0.0/packages/constants/src/index.ts#L2 | ||
lock: [5, 3], | ||
}], | ||
['5', { | ||
// https://github.com/pnpm/pnpm/blob/v5.0.0/packages/types/package.json#L8 | ||
node: [10, 13, 0], | ||
|
||
// https://github.com/pnpm/pnpm/blob/v5.0.0/packages/constants/src/index.ts#L2 | ||
lock: [5, 1], | ||
}], | ||
]; | ||
|
||
export const getPnpmVersion = ( | ||
nodeVersion: NodeVersion, | ||
lockfileVersion?: LockVersion, | ||
) => { | ||
const compatibleVersion = pnpmVersions.find(([_version, { node: nodeMinimum, lock }]) => { | ||
const isCompatibleNodeVersion = compareSemver(nodeVersion, nodeMinimum) >= 0; | ||
const isCompatibleLockVersion = ( | ||
!lockfileVersion | ||
|| compareSemver(lockfileVersion, lock) === 0 | ||
); | ||
return isCompatibleNodeVersion && isCompatibleLockVersion; | ||
}); | ||
|
||
const foundMatch = compatibleVersion; | ||
return foundMatch ? `@${foundMatch[0]}` : ''; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
import assert from 'assert'; | ||
import { getPnpmVersion } from '../src/get-pnpm-version'; | ||
import { testSuite, expect } from 'manten'; | ||
import { getPnpmVersion } from '../src/get-pnpm-version.js'; | ||
|
||
assert(getPnpmVersion([14, 19, 0]) === 'latest'); | ||
export default testSuite(({ test }) => { | ||
test('getPnpmVersion', () => { | ||
expect(getPnpmVersion([18, 0, 0])).toBe('@8'); | ||
expect(getPnpmVersion([16, 14, 0])).toBe('@8'); | ||
|
||
assert(getPnpmVersion([14, 18, 0]) === '6'); | ||
expect(getPnpmVersion([14, 19, 0])).toBe('@7'); | ||
|
||
assert(getPnpmVersion([12, 17, 0]) === '6'); | ||
expect(getPnpmVersion([14, 18, 0])).toBe('@6'); | ||
expect(getPnpmVersion([12, 17, 0])).toBe('@6'); | ||
|
||
assert(getPnpmVersion([12, 16, 0]) === '5'); | ||
expect(getPnpmVersion([12, 16, 0])).toBe('@5'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters