Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default [
name: `fetch`,
message: `Use fetch from sources/httpUtils.ts`,
}],
'@typescript-eslint/consistent-type-imports': `error`,
'@typescript-eslint/no-unused-vars': [`error`, {
caughtErrors: `none`,
}],
Expand Down
2 changes: 1 addition & 1 deletion genlist.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {readFileSync} from 'fs';
import semverCompare from 'semver/functions/compare';
import semverCompare from 'semver/functions/compare.js';

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

Expand Down
10 changes: 5 additions & 5 deletions mkshims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import cmdShim from '@zkochan/cmd-shim';
import fs from 'fs';
import path from 'path';

import {Engine} from './sources/Engine';
import {SupportedPackageManagerSet} from './sources/types';
import {Engine} from './sources/Engine.ts';
import {SupportedPackageManagerSet} from './sources/types.ts';

const engine = new Engine();

const distDir = path.join(__dirname, `dist`);
const shimsDir = path.join(__dirname, `shims`);
const distDir = path.join(import.meta.dirname, `dist`);
const shimsDir = path.join(import.meta.dirname, `shims`);

const physicalNodewinDir = path.join(shimsDir, `nodewin`);
const virtualNodewinDir = path.join(physicalNodewinDir, `node_modules/corepack`);
Expand Down Expand Up @@ -59,7 +59,7 @@ async function main() {

// Last note: cmdShim generates shims with relative paths, so it doesn't matter
// that the target files don't truly exist, as long as we mock the `stat` function.
const remapPath = (p: string) => path.resolve(__dirname, path.relative(virtualNodewinDir, p));
const remapPath = (p: string) => path.resolve(import.meta.dirname, path.relative(virtualNodewinDir, p));

const easyStatFs = Object.assign(Object.create(fs), {
readFile: (p: string, encoding: BufferEncoding, cb: (err: any, str: string) => void) => fs.readFile(remapPath(p), encoding, cb),
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"semver": "^7.6.3",
"supports-color": "^10.0.0",
"tar": "^7.5.11",
"tsx": "^4.16.2",
"typescript": "^5.7.3",
"v8-compile-cache": "^2.3.0",
"vitest": "^4.0.5",
Expand All @@ -45,10 +44,10 @@
}
},
"scripts": {
"build": "run clean && run build:bundle && tsx ./mkshims.ts",
"build": "run clean && run build:bundle && node ./mkshims.ts",
"build:bundle": "esbuild ./sources/_lib.ts --bundle --platform=node --target=node22.22.2 --external:corepack --outfile='./dist/lib/corepack.cjs' --resolve-extensions='.ts,.mjs,.js'",
"clean": "run rimraf dist shims",
"corepack": "tsx ./sources/_cli.ts",
"corepack": "node ./sources/_cli.ts",
"lint": "eslint .",
"prepack": "yarn build",
"postpack": "run clean",
Expand Down
47 changes: 26 additions & 21 deletions sources/Engine.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import {UsageError} from 'clipanion';
import fs from 'fs';
import path from 'path';
import process from 'process';
import semverRcompare from 'semver/functions/rcompare';
import semverValid from 'semver/functions/valid';
import semverValidRange from 'semver/ranges/valid';

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

import * as corepackUtils from './corepackUtils';
import * as debugUtils from './debugUtils';
import * as folderUtils from './folderUtils';
import type {NodeError} from './nodeUtils';
import * as semverUtils from './semverUtils';
import * as specUtils from './specUtils';
import {Config, Descriptor, LazyLocator, Locator} from './types';
import {SupportedPackageManagers, SupportedPackageManagerSet} from './types';
import {isSupportedPackageManager, PackageManagerSpec} from './types';
import {UsageError} from 'clipanion';
import fs from 'fs';
import path from 'path';
import process from 'process';
import semverRcompare from 'semver/functions/rcompare.js';
import semverValid from 'semver/functions/valid.js';
import semverValidRange from 'semver/ranges/valid.js';

import defaultConfig from '../config.json' with {type: 'json'};

import * as corepackUtils from './corepackUtils.ts';
import * as debugUtils from './debugUtils.ts';
import * as folderUtils from './folderUtils.ts';
import type {NodeError} from './nodeUtils.ts';
import * as semverUtils from './semverUtils.ts';
import * as specUtils from './specUtils.ts';
import type {Config, Descriptor, LazyLocator, Locator} from './types.ts';
import type {SupportedPackageManagers} from './types.ts';
import {SupportedPackageManagerSet} from './types.ts';
import type {PackageManagerSpec} from './types.ts';
import {isSupportedPackageManager} from './types.ts';

export type PreparedPackageManagerInfo = Awaited<ReturnType<Engine[`ensurePackageManager`]>>;

Expand Down Expand Up @@ -94,7 +96,10 @@ export async function activatePackageManager(lastKnownGood: Record<string, strin
}

export class Engine {
constructor(public config: Config = defaultConfig as Config) {
config: Config;

constructor(config: Config = defaultConfig as Config) {
this.config = config;
}

getPackageManagerFor(binaryName: string): SupportedPackageManagers | null {
Expand Down Expand Up @@ -161,7 +166,7 @@ export class Engine {
async getDefaultDescriptors() {
const locators: Array<Descriptor> = [];

for (const name of SupportedPackageManagerSet as Set<SupportedPackageManagers>)
for (const name of SupportedPackageManagerSet)
locators.push({name, range: await this.getDefaultVersion(name)});

return locators;
Expand Down
2 changes: 1 addition & 1 deletion sources/_cli.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import {runMain} from './main';
import {runMain} from './main.ts';

runMain(process.argv.slice(2));
2 changes: 1 addition & 1 deletion sources/_lib.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {runMain} from './main';
export {runMain} from './main.ts';
10 changes: 5 additions & 5 deletions sources/commands/Base.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Command, UsageError} from 'clipanion';
import {Command, UsageError} from 'clipanion';

import {PreparedPackageManagerInfo} from '../Engine';
import * as corepackUtils from '../corepackUtils';
import {Context} from '../main';
import * as specUtils from '../specUtils';
import type {PreparedPackageManagerInfo} from '../Engine.ts';
import * as corepackUtils from '../corepackUtils.ts';
import type {Context} from '../main.ts';
import * as specUtils from '../specUtils.ts';

export abstract class BaseCommand extends Command<Context> {
async resolvePatternsToDescriptors({patterns}: {patterns: Array<string>}) {
Expand Down
4 changes: 2 additions & 2 deletions sources/commands/Cache.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Command} from 'clipanion';
import fs from 'fs';

import {getInstallFolder} from '../folderUtils';
import type {Context} from '../main';
import {getInstallFolder} from '../folderUtils.ts';
import type {Context} from '../main.ts';

export class CacheCommand extends Command<Context> {
static paths = [
Expand Down
8 changes: 4 additions & 4 deletions sources/commands/Disable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import fs from 'f
import path from 'path';
import which from 'which';

import * as corepackUtils from '../corepackUtils';
import {Context} from '../main';
import type {NodeError} from '../nodeUtils';
import {isSupportedPackageManager, SupportedPackageManagerSetWithoutNpm} from '../types';
import * as corepackUtils from '../corepackUtils.ts';
import type {Context} from '../main.ts';
import type {NodeError} from '../nodeUtils.ts';
import {isSupportedPackageManager, SupportedPackageManagerSetWithoutNpm} from '../types.ts';

export class DisableCommand extends Command<Context> {
static paths = [
Expand Down
6 changes: 3 additions & 3 deletions sources/commands/Enable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import fs from 'f
import path from 'path';
import which from 'which';

import * as corepackUtils from '../corepackUtils';
import {Context} from '../main';
import {isSupportedPackageManager, SupportedPackageManagerSetWithoutNpm} from '../types';
import * as corepackUtils from '../corepackUtils.ts';
import type {Context} from '../main.ts';
import {isSupportedPackageManager, SupportedPackageManagerSetWithoutNpm} from '../types.ts';

export class EnableCommand extends Command<Context> {
static paths = [
Expand Down
15 changes: 8 additions & 7 deletions sources/commands/InstallGlobal.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {Command, Option, UsageError} from 'clipanion';
import fs from 'fs';
import path from 'path';
import {Command, Option, UsageError} from 'clipanion';
import fs from 'fs';
import path from 'path';

import * as folderUtils from '../folderUtils';
import * as specUtils from '../specUtils';
import {Descriptor, Locator, isSupportedPackageManager} from '../types';
import * as folderUtils from '../folderUtils.ts';
import * as specUtils from '../specUtils.ts';
import type {Descriptor, Locator} from '../types.ts';
import {isSupportedPackageManager} from '../types.ts';

import {BaseCommand} from './Base';
import {BaseCommand} from './Base.ts';

export class InstallGlobalCommand extends BaseCommand {
static paths = [
Expand Down
2 changes: 1 addition & 1 deletion sources/commands/InstallLocal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Command, UsageError} from 'clipanion';

import {BaseCommand} from './Base';
import {BaseCommand} from './Base.ts';

export class InstallLocalCommand extends BaseCommand {
static paths = [
Expand Down
4 changes: 2 additions & 2 deletions sources/commands/Pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {Command, Option, UsageError} from 'clipanion';
import {mkdir} from 'fs/promises';
import path from 'path';

import * as folderUtils from '../folderUtils';
import * as folderUtils from '../folderUtils.ts';

import {BaseCommand} from './Base';
import {BaseCommand} from './Base.ts';

export class PackCommand extends BaseCommand {
static paths = [
Expand Down
10 changes: 5 additions & 5 deletions sources/commands/Up.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {Command, UsageError} from 'clipanion';
import semverMajor from 'semver/functions/major';
import semverValid from 'semver/functions/valid';
import semverValidRange from 'semver/ranges/valid';
import semverMajor from 'semver/functions/major.js';
import semverValid from 'semver/functions/valid.js';
import semverValidRange from 'semver/ranges/valid.js';

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

import {BaseCommand} from './Base';
import {BaseCommand} from './Base.ts';

export class UpCommand extends BaseCommand {
static paths = [
Expand Down
2 changes: 1 addition & 1 deletion sources/commands/Use.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Command, Option, UsageError} from 'clipanion';

import {BaseCommand} from './Base';
import {BaseCommand} from './Base.ts';

export class UseCommand extends BaseCommand {
static paths = [
Expand Down
6 changes: 3 additions & 3 deletions sources/commands/deprecated/Hydrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {Command, Option, UsageError} from 'clipanion';
import {mkdir} from 'fs/promises';
import path from 'path';

import * as folderUtils from '../../folderUtils';
import {Context} from '../../main';
import {isSupportedPackageManager} from '../../types';
import * as folderUtils from '../../folderUtils.ts';
import type {Context} from '../../main.ts';
import {isSupportedPackageManager} from '../../types.ts';

export class HydrateCommand extends Command<Context> {
static paths = [
Expand Down
8 changes: 4 additions & 4 deletions sources/commands/deprecated/Prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import {Command, Option, UsageError} from 'clipanion';
import {mkdir} from 'fs/promises';
import path from 'path';

import * as folderUtils from '../../folderUtils';
import {Context} from '../../main';
import * as specUtils from '../../specUtils';
import {Descriptor} from '../../types';
import * as folderUtils from '../../folderUtils.ts';
import type {Context} from '../../main.ts';
import * as specUtils from '../../specUtils.ts';
import type {Descriptor} from '../../types.ts';

export class PrepareCommand extends Command<Context> {
static paths = [
Expand Down
40 changes: 20 additions & 20 deletions sources/corepackUtils.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import {createHash} from 'crypto';
import {once} from 'events';
import fs from 'fs';
import type {Dir} from 'fs';
import Module from 'module';
import path from 'path';
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';
import * as debugUtils from './debugUtils';
import * as folderUtils from './folderUtils';
import * as httpUtils from './httpUtils';
import * as nodeUtils from './nodeUtils';
import * as npmRegistryUtils from './npmRegistryUtils';
import {RegistrySpec, Descriptor, Locator, PackageManagerSpec} from './types';
import {BinList, BinSpec, InstallSpec, DownloadSpec} from './types';
import {createHash} from 'crypto';
import {once} from 'events';
import fs from 'fs';
import type {Dir} from 'fs';
import Module from 'module';
import path from 'path';
import Range from 'semver/classes/range.js';
import SemVer from 'semver/classes/semver.js';
import semverLt from 'semver/functions/lt.js';
import semverParse from 'semver/functions/parse.js';
import {setTimeout as setTimeoutPromise} from 'timers/promises';

import * as engine from './Engine.ts';
import * as debugUtils from './debugUtils.ts';
import * as folderUtils from './folderUtils.ts';
import * as httpUtils from './httpUtils.ts';
import * as nodeUtils from './nodeUtils.ts';
import * as npmRegistryUtils from './npmRegistryUtils.ts';
import type {RegistrySpec, Descriptor, Locator, PackageManagerSpec} from './types.ts';
import type {BinList, BinSpec, InstallSpec, DownloadSpec} from './types.ts';

const YARN_SWITCH_REGEX = /[/\\]switch[/\\]bin[/\\]/;

Expand Down
2 changes: 1 addition & 1 deletion sources/folderUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {homedir, tmpdir} from 'os';
import {join} from 'path';
import process from 'process';

import type {NodeError} from './nodeUtils';
import type {NodeError} from './nodeUtils.ts';

/**
* If the install folder structure changes then increment this number.
Expand Down
2 changes: 1 addition & 1 deletion sources/httpUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {once} from 'events';
import {stderr, stdin} from 'process';
import {Readable} from 'stream';

import {DEFAULT_NPM_REGISTRY_URL} from './npmRegistryUtils';
import {DEFAULT_NPM_REGISTRY_URL} from './npmRegistryUtils.ts';

async function fetch(input: string | URL, init?: RequestInit) {
if (process.env.COREPACK_ENABLE_NETWORK === `0`)
Expand Down
Loading