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

feat: add cnpm to corepack #333

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
7 changes: 4 additions & 3 deletions .github/workflows/sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ jobs:
run: |
LATEST_BERRY_VERSION=$(curl https://repo.yarnpkg.com/tags | jq -r '.latest.stable')

LATEST_NPM=$(curl https://registry.npmjs.org/npm | jq '.["dist-tags"].latest + "+sha1." + .versions[.["dist-tags"].latest].dist.shasum')
LATEST_PNPM=$(curl https://registry.npmjs.org/pnpm | jq '.["dist-tags"].latest + "+sha1." + .versions[.["dist-tags"].latest].dist.shasum')
LATEST_YARN=$(curl https://registry.npmjs.org/yarn | jq '.["dist-tags"].latest + "+sha1." + .versions[.["dist-tags"].latest].dist.shasum')
LATEST_NPM=$(curl https://registry.npmjs.org/npm/latest | jq '"+sha1." + .dist.shasum')
LATEST_PNPM=$(curl https://registry.npmjs.org/pnpm/latest | jq '"+sha1." + .dist.shasum')
LATEST_YARN=$(curl https://registry.npmjs.org/yarn/latest | jq '"+sha1." + .dist.shasum')
LATEST_CNPM=$(curl https://registry.npmmirror.com/cnpm/latest | jq '"+sha1." + .dist.shasum')
elrrrrrrr marked this conversation as resolved.
Show resolved Hide resolved
LATEST_BERRY=$(jq -n '$version + "+sha224." + $checksum' --arg version "$LATEST_BERRY_VERSION" --arg checksum "$(curl https://repo.yarnpkg.com/"$LATEST_BERRY_VERSION"/packages/yarnpkg-cli/bin/yarn.js | openssl dgst -sha224 | cut -d' ' -f2)")

git --no-pager show HEAD:config.json | jq '. * '"{
Expand Down
38 changes: 38 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,44 @@
}
}
},
"cnpm": {
"default": "9.3.2+sha1.05a307b7c1d34a368b8d30ff159dfd0533c8c328",
"fetchLatestFrom": {
"type": "url",
"url": "https://registry.npmmirror.com/cnpm",
"fields": {
"tags": "latest",
"versions": "tags"
}
},
"transparent": {
"commands": [
[
"cnpm",
"sync"
]
]
},
"ranges": {
"*": {
"url": "https://cdn.npmmirror.com/packages/cnpm/{}/cnpm-{}.tgz",
"bin": {
"cnpm": "./bin/cnpm"
},
"commands": {
"use": ["cnpm", "-v"]
elrrrrrrr marked this conversation as resolved.
Show resolved Hide resolved
},
"registry": {
"type": "url",
"url": "https://registry.npmmirror.com/cnpm",
"fields": {
"versions": "versions",
"tags": "dist-tags"
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably provide mentioned for the npm package, see #339

Suggested change
}
},
"npmRegistry": {
"type": "npm",
"package": "cnpm"
},

}
}
},
"pnpm": {
"default": "8.10.2+sha1.e0b68270e89c817ff88b7be62466a2128c53af02",
"fetchLatestFrom": {
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
"pnpm": "./dist/pnpm.js",
"pnpx": "./dist/pnpx.js",
"yarn": "./dist/yarn.js",
"yarnpkg": "./dist/yarnpkg.js"
"yarnpkg": "./dist/yarnpkg.js",
"cnpm": "./dist/cnpm.js"
},
"executableFiles": [
"./dist/npm.js",
Expand All @@ -92,7 +93,8 @@
"./shims/yarn",
"./shims/yarn.ps1",
"./shims/yarnpkg",
"./shims/yarnpkg.ps1"
"./shims/yarnpkg.ps1",
"./dist/cnpm.js"
]
},
"resolutions": {
Expand Down
2 changes: 1 addition & 1 deletion sources/corepackUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export async function installVersion(installTarget: string, locator: Locator, {s
};
}

const defaultNpmRegistryURL = spec.url.replace(`{}`, version);
const defaultNpmRegistryURL = spec.url.replaceAll(`{}`, version);
const url = process.env.COREPACK_NPM_REGISTRY ?
defaultNpmRegistryURL.replace(
npmRegistryUtils.DEFAULT_NPM_REGISTRY_URL,
Expand Down
2 changes: 1 addition & 1 deletion sources/nodeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function getEndOfLine(content: string) {
}

export function normalizeLineEndings(originalContent: string, newContent: string) {
return newContent.replace(/\r?\n/g, getEndOfLine(originalContent));
return newContent.replaceAll(`{}`, getEndOfLine(originalContent));
elrrrrrrr marked this conversation as resolved.
Show resolved Hide resolved
}

function getIndent(content: string) {
Expand Down
1 change: 1 addition & 0 deletions sources/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export enum SupportedPackageManagers {
Npm = `npm`,
Pnpm = `pnpm`,
Yarn = `yarn`,
Cnpm = `cnpm`,
Comment on lines 5 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: ASCII/alphabetical order

Suggested change
Npm = `npm`,
Pnpm = `pnpm`,
Yarn = `yarn`,
Cnpm = `cnpm`,
Cnpm = `cnpm`,
Npm = `npm`,
Pnpm = `pnpm`,
Yarn = `yarn`,

}

export const SupportedPackageManagerSet = new Set<SupportedPackageManagers>(
Expand Down
42 changes: 40 additions & 2 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const testedPackageManagers: Array<[string, string]> = [
[`pnpm`, `6.6.2`],
[`pnpm`, `6.6.2+sha1.7b4d6b176c1b93b5670ed94c24babb7d80c13854`],
[`pnpm`, `6.6.2+sha224.eb5c0acad3b0f40ecdaa2db9aa5a73134ad256e17e22d1419a2ab073`],
[`cnpm`, `9.3.2`],
elrrrrrrr marked this conversation as resolved.
Show resolved Hide resolved
[`npm`, `6.14.2`],
[`npm`, `6.14.2+sha1.f057d35cd4792c4c511bb1fa332edb43143d07b0`],
[`npm`, `6.14.2+sha224.50512c1eb404900ee78586faa6d756b8d867ff46a328e6fb4cdf3a87`],
Expand All @@ -94,7 +95,7 @@ for (const [name, version] of testedPackageManagers) {
await expect(runCli(cwd, [name, `--version`])).resolves.toMatchObject({
exitCode: 0,
stderr: ``,
stdout: `${version.split(`+`, 1)[0]}\n`,
stdout: expect.stringContaining(version.split(`+`, 1)[0]),
});
});
});
Expand Down Expand Up @@ -221,6 +222,12 @@ it(`should use the pinned version when local projects don't list any spec`, asyn
exitCode: 0,
});

await expect(runCli(cwd, [`cnpm`, `--version`])).resolves.toMatchObject({
stdout: expect.stringContaining(`cnpm@${config.definitions.cnpm.default.split(`+`, 1)[0]}`),
stderr: ``,
exitCode: 0,
});

await expect(runCli(cwd, [`npm`, `--version`])).resolves.toMatchObject({
stdout: `${config.definitions.npm.default.split(`+`, 1)[0]}\n`,
stderr: ``,
Expand Down Expand Up @@ -306,6 +313,11 @@ it(`should refuse to run a different package manager within a configured project
exitCode: 1,
});

await expect(runCli(cwd, [`cnpm`, `--version`])).resolves.toMatchObject({
stdout: `Usage Error: This project is configured to use yarn\n\n$ cnpm ...\n`,
exitCode: 1,
});

// Disable strict checking to workaround the UsageError.
process.env.COREPACK_ENABLE_STRICT = `0`;

Expand All @@ -320,6 +332,11 @@ it(`should refuse to run a different package manager within a configured project
stderr: ``,
exitCode: 0,
});
await expect(runCli(cwd, [`cnpm`, `--version`])).resolves.toMatchObject({
stdout: expect.stringContaining(`cnpm@${config.definitions.cnpm.default.split(`+`, 1)[0]}`),
stderr: ``,
exitCode: 0,
});
} finally {
delete process.env.COREPACK_ENABLE_STRICT;
delete process.env.FORCE_COLOR;
Expand All @@ -346,6 +363,11 @@ it(`should always use fallback version when project spec env is disabled`, async
stderr: ``,
exitCode: 0,
});
await expect(runCli(cwd, [`cnpm`, `--version`])).resolves.toMatchObject({
stdout: expect.stringContaining(`cnpm@${config.definitions.cnpm.default.split(`+`, 1)[0]}`),
stderr: ``,
exitCode: 0,
});
} finally {
delete process.env.COREPACK_ENABLE_PROJECT_SPEC;
}
Expand Down Expand Up @@ -378,6 +400,12 @@ it(`should allow to call "corepack install -g --all" to prepare all package mana
exitCode: 0,
});

await expect(runCli(cwd, [`cnpm`, `--version`])).resolves.toMatchObject({
stdout: expect.stringContaining(`cnpm@${config.definitions.cnpm.default.split(`+`, 1)[0]}`),
stderr: ``,
exitCode: 0,
});

await expect(runCli(cwd, [`npm`, `--version`])).resolves.toMatchObject({
stdout: `${config.definitions.npm.default.split(`+`, 1)[0]}\n`,
stderr: ``,
Expand Down Expand Up @@ -482,7 +510,7 @@ it(`should support hydrating package managers if cache folder was removed`, asyn

it(`should support hydrating multiple package managers from cached archives`, async () => {
await xfs.mktempPromise(async cwd => {
await expect(runCli(cwd, [`pack`, `[email protected]`, `[email protected]`])).resolves.toMatchObject({
await expect(runCli(cwd, [`pack`, `[email protected]`, `[email protected]`, `[email protected]`])).resolves.toMatchObject({
exitCode: 0,
stderr: ``,
});
Expand Down Expand Up @@ -518,6 +546,16 @@ it(`should support hydrating multiple package managers from cached archives`, as
stderr: ``,
exitCode: 0,
});

await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
packageManager: `[email protected]`,
});

await expect(runCli(cwd, [`cnpm`, `--version`])).resolves.toMatchObject({
stdout: expect.stringContaining(`[email protected]`),
stderr: ``,
exitCode: 0,
});
} finally {
delete process.env.COREPACK_ENABLE_NETWORK;
}
Expand Down
Binary file modified tests/nock/4IfgwoxNR5JV5bBojS26Kg-3.dat
Binary file not shown.
Binary file added tests/nock/4IfgwoxNR5JV5bBojS26Kg-4.dat
Binary file not shown.
Binary file modified tests/nock/AL__3okpCdfjA6kGuG2rFQ-1.dat
Binary file not shown.
Binary file added tests/nock/Q-gEXR-fmX_Didj2zQmUiA-1.dat
Binary file not shown.
Binary file modified tests/nock/VurwVdvlR5Rs7xQVs9UCVw-1.dat
Binary file not shown.
Binary file added tests/nock/VurwVdvlR5Rs7xQVs9UCVw-5.dat
Binary file not shown.
Binary file modified tests/nock/bNE0FYc3WlnFGzjHaIdf5A-1.dat
Binary file not shown.
Binary file modified tests/nock/gD354-khxZHN8IJPre6iqw-1.dat
Binary file not shown.
Binary file added tests/nock/gD354-khxZHN8IJPre6iqw-5.dat
Binary file not shown.
Binary file modified tests/nock/h9H6jL9ro_imv61YP24nGg-2.dat
Binary file not shown.
Binary file modified tests/nock/h9H6jL9ro_imv61YP24nGg-3.dat
Binary file not shown.
Binary file added tests/nock/h9H6jL9ro_imv61YP24nGg-4.dat
Binary file not shown.
Binary file added tests/nock/h9H6jL9ro_imv61YP24nGg-5.dat
Binary file not shown.
Binary file added tests/nock/ypWhs6_VxInVlmWLHRm3FA-3.dat
Binary file not shown.