Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/nodejs/corepack
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Jul 12, 2024
2 parents a66dfcc + 4f3e28f commit 67c1309
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 191 deletions.
4 changes: 0 additions & 4 deletions .eslintignore

This file was deleted.

12 changes: 0 additions & 12 deletions .eslintrc.js

This file was deleted.

1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"**/.yarn": true,
"**/.pnp.*": true
},
"eslint.experimental.useFlatConfig": true,
"eslint.nodePath": ".yarn/sdks",
"typescript.enablePromptUseWorkspaceTsdk": true,
"editor.codeActionsOnSave": {
Expand Down
23 changes: 23 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import yarnpkg from '@yarnpkg/eslint-config';

// eslint-disable-next-line arca/no-default-export
export default [
{
ignores: [
`.yarn`,
`dist`,
`shims`,
`.pnp.*`,
],
},
...yarnpkg,
{
rules: {
// eslint-disable-next-line @typescript-eslint/naming-convention
'no-restricted-globals': [`error`, {
name: `fetch`,
message: `Use fetch from sources/httpUtils.ts`,
}],
},
},
];
4 changes: 2 additions & 2 deletions genlist.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {readFileSync} from 'fs';
import semver from 'semver';
import semverCompare from 'semver/functions/compare';

const lines = readFileSync(0, `utf8`).split(/\n/).filter(line => line);

lines.sort((a, b) => {
return semver.compare(a, b);
return semverCompare(a, b);
});

for (const version of lines)
Expand Down
11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,23 @@
"@types/semver": "^7.1.0",
"@types/tar": "^6.0.0",
"@types/which": "^3.0.0",
"@typescript-eslint/eslint-plugin": "^6.8.0",
"@typescript-eslint/parser": "^6.8.0",
"@yarnpkg/eslint-config": "^1.0.0",
"@yarnpkg/eslint-config": "^2.0.0",
"@yarnpkg/fslib": "^3.0.0-rc.48",
"@zkochan/cmd-shim": "^6.0.0",
"babel-plugin-dynamic-import-node": "^2.3.3",
"better-sqlite3": "^9.4.1",
"better-sqlite3": "^10.0.0",
"clipanion": "^3.0.1",
"debug": "^4.1.1",
"esbuild": "^0.21.0",
"eslint": "^8.0.0",
"eslint-plugin-arca": "^0.16.0",
"eslint": "^8.57.0",
"jest": "^29.0.0",
"proxy-from-env": "^1.1.0",
"semver": "^7.5.2",
"supports-color": "^9.0.0",
"tar": "^6.2.1",
"ts-node": "^10.0.0",
"typescript": "^5.3.3",
"undici": "^6.6.1",
"undici": "^6.19.2",
"v8-compile-cache": "^2.3.0",
"which": "^4.0.0"
},
Expand Down
10 changes: 6 additions & 4 deletions sources/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import {UsageError} from 'clipanion';
import fs from 'fs';
import path from 'path';
import process from 'process';
import semver from 'semver';
import semverRcompare from 'semver/functions/rcompare';
import semverValid from 'semver/functions/valid';
import semverValidRange from 'semver/ranges/valid';

import defaultConfig from '../config.json';

Expand Down Expand Up @@ -334,7 +336,7 @@ export class Engine {
throw new UsageError(`This package manager (${descriptor.name}) isn't supported by this corepack build`);

let finalDescriptor = descriptor;
if (!semver.valid(descriptor.range) && !semver.validRange(descriptor.range)) {
if (!semverValid(descriptor.range) && !semverValidRange(descriptor.range)) {
if (!allowTags)
throw new UsageError(`Packages managers can't be referenced via tags in this context`);

Expand Down Expand Up @@ -363,7 +365,7 @@ export class Engine {

// If the user asked for a specific version, no need to request the list of
// available versions from the registry.
if (semver.valid(finalDescriptor.range))
if (semverValid(finalDescriptor.range))
return {name: finalDescriptor.name, reference: finalDescriptor.range};

const versions = await Promise.all(Object.keys(definition.ranges).map(async range => {
Expand All @@ -374,7 +376,7 @@ export class Engine {
return versions.filter(version => semverUtils.satisfiesWithPrereleases(version, finalDescriptor.range));
}));

const highestVersion = [...new Set(versions.flat())].sort(semver.rcompare);
const highestVersion = [...new Set(versions.flat())].sort(semverRcompare);
if (highestVersion.length === 0)
return null;

Expand Down
8 changes: 5 additions & 3 deletions sources/commands/Up.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {Command, UsageError} from 'clipanion';
import semver from 'semver';
import semverMajor from 'semver/functions/major';
import semverValid from 'semver/functions/valid';
import semverValidRange from 'semver/ranges/valid';

import type {SupportedPackageManagers} from '../types';

Expand Down Expand Up @@ -33,14 +35,14 @@ export class UpCommand extends BaseCommand {
patterns: [],
});

if (!semver.valid(descriptor.range) && !semver.validRange(descriptor.range))
if (!semverValid(descriptor.range) && !semverValidRange(descriptor.range))
throw new UsageError(`The 'corepack up' command can only be used when your project's packageManager field is set to a semver version or semver range`);

const resolved = await this.context.engine.resolveDescriptor(descriptor, {useCache: false});
if (!resolved)
throw new UsageError(`Failed to successfully resolve '${descriptor.range}' to a valid ${descriptor.name} release`);

const majorVersion = semver.major(resolved.reference);
const majorVersion = semverMajor(resolved.reference);
const majorDescriptor = {name: descriptor.name as SupportedPackageManagers, range: `^${majorVersion}.0.0`};

const highestVersion = await this.context.engine.resolveDescriptor(majorDescriptor, {useCache: false});
Expand Down
36 changes: 17 additions & 19 deletions sources/corepackUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import fs from 'fs';
import type {Dir} from 'fs';
import Module from 'module';
import path from 'path';
import semver from 'semver';
import Range from 'semver/classes/range';
import SemVer from 'semver/classes/semver';
import semverLt from 'semver/functions/lt';
import semverParse from 'semver/functions/parse';
import {setTimeout as setTimeoutPromise} from 'timers/promises';

import * as engine from './Engine';
Expand Down Expand Up @@ -82,9 +85,9 @@ export async function findInstalledVersion(installTarget: string, descriptor: De
}
}

const range = new semver.Range(descriptor.range);
const range = new Range(descriptor.range);
let bestMatch: string | null = null;
let maxSV: semver.SemVer | undefined = undefined;
let maxSV: SemVer | undefined = undefined;

for await (const {name} of cacheDirectory) {
// Some dot-folders tend to pop inside directories, especially on OSX
Expand All @@ -97,7 +100,7 @@ export async function findInstalledVersion(installTarget: string, descriptor: De
// @ts-expect-error TODO: decipher why this produces an error
if (range.test(name) && maxSV?.compare(name) !== 1) {
bestMatch = name;
maxSV = new semver.SemVer(bestMatch);
maxSV = new SemVer(bestMatch);
}
}

Expand Down Expand Up @@ -193,7 +196,7 @@ async function download(installTarget: string, url: string, algo: string, binPat

export async function installVersion(installTarget: string, locator: Locator, {spec}: {spec: PackageManagerSpec}): Promise<InstallSpec> {
const locatorIsASupportedPackageManager = isSupportedPackageManagerLocator(locator);
const locatorReference = locatorIsASupportedPackageManager ? semver.parse(locator.reference)! : parseURLReference(locator);
const locatorReference = locatorIsASupportedPackageManager ? semverParse(locator.reference)! : parseURLReference(locator);
const {version, build} = locatorReference;

const installFolder = path.join(installTarget, locator.name, version);
Expand Down Expand Up @@ -230,12 +233,11 @@ export async function installVersion(installTarget: string, locator: Locator, {s
if (registry.bin) {
binPath = registry.bin;
}
} else {
url = url.replace(
npmRegistryUtils.DEFAULT_NPM_REGISTRY_URL,
() => process.env.COREPACK_NPM_REGISTRY!,
);
}
url = url.replace(
npmRegistryUtils.DEFAULT_NPM_REGISTRY_URL,
() => process.env.COREPACK_NPM_REGISTRY!,
);
}
} else {
url = decodeURIComponent(version);
Expand Down Expand Up @@ -305,11 +307,7 @@ export async function installVersion(installTarget: string, locator: Locator, {s

await fs.promises.mkdir(path.dirname(installFolder), {recursive: true});
try {
if (process.platform === `win32`) {
await renameUnderWindows(tmpFolder, installFolder);
} else {
await fs.promises.rename(tmpFolder, installFolder);
}
await renameSafe(tmpFolder, installFolder);
} catch (err) {
if (
(err as nodeUtils.NodeError).code === `ENOTEMPTY` ||
Expand All @@ -327,9 +325,9 @@ export async function installVersion(installTarget: string, locator: Locator, {s
const lastKnownGood = await engine.getLastKnownGood();
const defaultVersion = engine.getLastKnownGoodFromFileContent(lastKnownGood, locator.name);
if (defaultVersion) {
const currentDefault = semver.parse(defaultVersion)!;
const downloadedVersion = locatorReference as semver.SemVer;
if (currentDefault.major === downloadedVersion.major && semver.lt(currentDefault, downloadedVersion)) {
const currentDefault = semverParse(defaultVersion)!;
const downloadedVersion = locatorReference as SemVer;
if (currentDefault.major === downloadedVersion.major && semverLt(currentDefault, downloadedVersion)) {
await engine.activatePackageManager(lastKnownGood, locator);
}
}
Expand Down Expand Up @@ -405,7 +403,7 @@ export async function runVersion(locator: Locator, installSpec: InstallSpec & {s
// Node.js segfaults when using npm@>=9.7.0 and v8-compile-cache
// $ docker run -it node:20.3.0-slim corepack [email protected] --version
// [SIGSEGV]
if (locator.name !== `npm` || semver.lt(locator.reference, `9.7.0`))
if (locator.name !== `npm` || semverLt(locator.reference, `9.7.0`))
// @ts-expect-error - No types
await import(`v8-compile-cache`);

Expand Down
9 changes: 5 additions & 4 deletions sources/semverUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import semver from 'semver';
import Range from 'semver/classes/range';
import SemVer from 'semver/classes/semver';

/**
* Returns whether the given semver version satisfies the given range. Notably
Expand All @@ -16,17 +17,17 @@ import semver from 'semver';
export function satisfiesWithPrereleases(version: string | null, range: string, loose: boolean = false): boolean {
let semverRange;
try {
semverRange = new semver.Range(range, loose);
semverRange = new Range(range, loose);
} catch (err) {
return false;
}

if (!version)
return false;

let semverVersion: semver.SemVer;
let semverVersion: SemVer;
try {
semverVersion = new semver.SemVer(version, semverRange.loose);
semverVersion = new SemVer(version, semverRange.loose);
if (semverVersion.prerelease) {
semverVersion.prerelease = [];
}
Expand Down
4 changes: 2 additions & 2 deletions sources/specUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {UsageError} from 'clipanion';
import fs from 'fs';
import path from 'path';
import semver from 'semver';
import semverValid from 'semver/functions/valid';

import {PreparedPackageManagerInfo} from './Engine';
import {NodeError} from './nodeUtils';
Expand Down Expand Up @@ -34,7 +34,7 @@ export function parseSpec(raw: unknown, source: string, {enforceExactVersion = t

const isURL = URL.canParse(range);
if (!isURL) {
if (enforceExactVersion && !semver.valid(range))
if (enforceExactVersion && !semverValid(range))
throw new UsageError(`Invalid package manager specification in ${source} (${raw}); expected a semver version${enforceExactVersion ? `` : `, range, or tag`}`);

if (!isSupportedPackageManager(name)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/_registryServer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function generateVersionMetadata(packageName, version) {
shasum,
size: mockPackageTarGz.length,
noattachment: false,
tarball: `${process.env.COREPACK_NPM_REGISTRY}/${packageName}/-/${packageName}-${version}.tgz`,
tarball: `https://registry.npmjs.org/${packageName}/-/${packageName}-${version}.tgz`,
...generateSignature(packageName, version),
},
};
Expand Down
Loading

0 comments on commit 67c1309

Please sign in to comment.