Skip to content

Commit faaac09

Browse files
committed
feat: add cnpm
1 parent b7aa576 commit faaac09

File tree

7 files changed

+90
-8
lines changed

7 files changed

+90
-8
lines changed

.github/workflows/sync.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ jobs:
2222
run: |
2323
LATEST_BERRY_VERSION=$(curl https://repo.yarnpkg.com/tags | jq -r '.latest.stable')
2424
25-
LATEST_NPM=$(curl https://registry.npmjs.org/npm | jq '.["dist-tags"].latest + "+sha1." + .versions[.["dist-tags"].latest].dist.shasum')
26-
LATEST_PNPM=$(curl https://registry.npmjs.org/pnpm | jq '.["dist-tags"].latest + "+sha1." + .versions[.["dist-tags"].latest].dist.shasum')
27-
LATEST_YARN=$(curl https://registry.npmjs.org/yarn | jq '.["dist-tags"].latest + "+sha1." + .versions[.["dist-tags"].latest].dist.shasum')
25+
LATEST_NPM=$(curl https://registry.npmjs.org/npm/latest | jq '"+sha1." + .dist.shasum')
26+
LATEST_PNPM=$(curl https://registry.npmjs.org/pnpm/latest | jq '"+sha1." + .dist.shasum')
27+
LATEST_YARN=$(curl https://registry.npmjs.org/yarn/latest | jq '"+sha1." + .dist.shasum')
28+
LATEST_CNPM=$(curl https://registry.npmmirror.com/cnpm/latest | jq '"+sha1." + .dist.shasum')
2829
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)")
2930
3031
git --no-pager show HEAD:config.json | jq '. * '"{

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010

1111
/shims
1212
/coverage
13+
package-lock.json
14+
node_modules

config.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,44 @@
3737
}
3838
}
3939
},
40+
"cnpm": {
41+
"default": "9.3.1+sha1.426460446f57dcf31b5a90fb59e35a8353d0d186",
42+
"fetchLatestFrom": {
43+
"type": "url",
44+
"url": "https://registry.npmmirror.com/cnpm",
45+
"fields": {
46+
"tags": "latest",
47+
"versions": "tags"
48+
}
49+
},
50+
"transparent": {
51+
"commands": [
52+
[
53+
"cnpm",
54+
"sync"
55+
]
56+
]
57+
},
58+
"ranges": {
59+
"*": {
60+
"url": "https://cdn.npmmirror.com/packages/cnpm/{}/cnpm-{}.tgz",
61+
"bin": {
62+
"cnpm": "./bin/cnpm"
63+
},
64+
"commands": {
65+
"use": ["cnpm", "-v"]
66+
},
67+
"registry": {
68+
"type": "url",
69+
"url": "https://registry.npmmirror.com/cnpm",
70+
"fields": {
71+
"versions": "versions",
72+
"tags": "dist-tags"
73+
}
74+
}
75+
}
76+
}
77+
},
4078
"pnpm": {
4179
"default": "8.10.2+sha1.e0b68270e89c817ff88b7be62466a2128c53af02",
4280
"fetchLatestFrom": {

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@
7171
"pnpm": "./dist/pnpm.js",
7272
"pnpx": "./dist/pnpx.js",
7373
"yarn": "./dist/yarn.js",
74-
"yarnpkg": "./dist/yarnpkg.js"
74+
"yarnpkg": "./dist/yarnpkg.js",
75+
"cnpm": "./dist/cnpm.js"
7576
},
7677
"executableFiles": [
7778
"./dist/npm.js",
@@ -92,7 +93,8 @@
9293
"./shims/yarn",
9394
"./shims/yarn.ps1",
9495
"./shims/yarnpkg",
95-
"./shims/yarnpkg.ps1"
96+
"./shims/yarnpkg.ps1",
97+
"./dist/cnpm.js"
9698
]
9799
},
98100
"resolutions": {

sources/corepackUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export async function installVersion(installTarget: string, locator: Locator, {s
117117
};
118118
}
119119

120-
const defaultNpmRegistryURL = spec.url.replace(`{}`, version);
120+
const defaultNpmRegistryURL = spec.url.replace(/\{\}/g, version);
121121
const url = process.env.COREPACK_NPM_REGISTRY ?
122122
defaultNpmRegistryURL.replace(
123123
npmRegistryUtils.DEFAULT_NPM_REGISTRY_URL,

sources/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export enum SupportedPackageManagers {
55
Npm = `npm`,
66
Pnpm = `pnpm`,
77
Yarn = `yarn`,
8+
Cnpm = `cnpm`,
89
}
910

1011
export const SupportedPackageManagerSet = new Set<SupportedPackageManagers>(

tests/main.test.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ const testedPackageManagers: Array<[string, string]> = [
7979
[`pnpm`, `6.6.2`],
8080
[`pnpm`, `6.6.2+sha1.7b4d6b176c1b93b5670ed94c24babb7d80c13854`],
8181
[`pnpm`, `6.6.2+sha224.eb5c0acad3b0f40ecdaa2db9aa5a73134ad256e17e22d1419a2ab073`],
82+
[`cnpm`, `9.3.1`],
8283
[`npm`, `6.14.2`],
8384
[`npm`, `6.14.2+sha1.f057d35cd4792c4c511bb1fa332edb43143d07b0`],
8485
[`npm`, `6.14.2+sha224.50512c1eb404900ee78586faa6d756b8d867ff46a328e6fb4cdf3a87`],
@@ -94,7 +95,7 @@ for (const [name, version] of testedPackageManagers) {
9495
await expect(runCli(cwd, [name, `--version`])).resolves.toMatchObject({
9596
exitCode: 0,
9697
stderr: ``,
97-
stdout: `${version.split(`+`, 1)[0]}\n`,
98+
stdout: expect.stringMatching(new RegExp(version.split(`+`, 1)[0])),
9899
});
99100
});
100101
});
@@ -221,6 +222,12 @@ it(`should use the pinned version when local projects don't list any spec`, asyn
221222
exitCode: 0,
222223
});
223224

225+
await expect(runCli(cwd, [`cnpm`, `--version`])).resolves.toMatchObject({
226+
stdout: expect.stringMatching(new RegExp(config.definitions.cnpm.default.split(`+`, 1)[0])),
227+
stderr: ``,
228+
exitCode: 0,
229+
});
230+
224231
await expect(runCli(cwd, [`npm`, `--version`])).resolves.toMatchObject({
225232
stdout: `${config.definitions.npm.default.split(`+`, 1)[0]}\n`,
226233
stderr: ``,
@@ -306,6 +313,11 @@ it(`should refuse to run a different package manager within a configured project
306313
exitCode: 1,
307314
});
308315

316+
await expect(runCli(cwd, [`cnpm`, `--version`])).resolves.toMatchObject({
317+
stdout: `Usage Error: This project is configured to use yarn\n\n$ cnpm ...\n`,
318+
exitCode: 1,
319+
});
320+
309321
// Disable strict checking to workaround the UsageError.
310322
process.env.COREPACK_ENABLE_STRICT = `0`;
311323

@@ -320,6 +332,11 @@ it(`should refuse to run a different package manager within a configured project
320332
stderr: ``,
321333
exitCode: 0,
322334
});
335+
await expect(runCli(cwd, [`cnpm`, `--version`])).resolves.toMatchObject({
336+
stdout: expect.stringMatching(new RegExp(config.definitions.cnpm.default.split(`+`, 1)[0])),
337+
stderr: ``,
338+
exitCode: 0,
339+
});
323340
} finally {
324341
delete process.env.COREPACK_ENABLE_STRICT;
325342
delete process.env.FORCE_COLOR;
@@ -346,6 +363,11 @@ it(`should always use fallback version when project spec env is disabled`, async
346363
stderr: ``,
347364
exitCode: 0,
348365
});
366+
await expect(runCli(cwd, [`cnpm`, `--version`])).resolves.toMatchObject({
367+
stdout: expect.stringMatching(new RegExp(config.definitions.cnpm.default.split(`+`, 1)[0])),
368+
stderr: ``,
369+
exitCode: 0,
370+
});
349371
} finally {
350372
delete process.env.COREPACK_ENABLE_PROJECT_SPEC;
351373
}
@@ -378,6 +400,12 @@ it(`should allow to call "corepack install -g --all" to prepare all package mana
378400
exitCode: 0,
379401
});
380402

403+
await expect(runCli(cwd, [`cnpm`, `--version`])).resolves.toMatchObject({
404+
stdout: expect.stringMatching(new RegExp(config.definitions.cnpm.default.split(`+`, 1)[0])),
405+
stderr: ``,
406+
exitCode: 0,
407+
});
408+
381409
await expect(runCli(cwd, [`npm`, `--version`])).resolves.toMatchObject({
382410
stdout: `${config.definitions.npm.default.split(`+`, 1)[0]}\n`,
383411
stderr: ``,
@@ -482,7 +510,7 @@ it(`should support hydrating package managers if cache folder was removed`, asyn
482510

483511
it(`should support hydrating multiple package managers from cached archives`, async () => {
484512
await xfs.mktempPromise(async cwd => {
485-
await expect(runCli(cwd, [`pack`, `[email protected]`, `[email protected]`])).resolves.toMatchObject({
513+
await expect(runCli(cwd, [`pack`, `[email protected]`, `[email protected]`, `[email protected]`])).resolves.toMatchObject({
486514
exitCode: 0,
487515
stderr: ``,
488516
});
@@ -518,6 +546,16 @@ it(`should support hydrating multiple package managers from cached archives`, as
518546
stderr: ``,
519547
exitCode: 0,
520548
});
549+
550+
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
551+
packageManager: `[email protected]`,
552+
});
553+
554+
await expect(runCli(cwd, [`cnpm`, `--version`])).resolves.toMatchObject({
555+
stdout: expect.stringMatching(/cnpm@9\.3\.1/),
556+
stderr: ``,
557+
exitCode: 0,
558+
});
521559
} finally {
522560
delete process.env.COREPACK_ENABLE_NETWORK;
523561
}

0 commit comments

Comments
 (0)