Skip to content

Commit

Permalink
feat: add cnpm
Browse files Browse the repository at this point in the history
  • Loading branch information
elrrrrrrr committed Dec 7, 2023
1 parent b7aa576 commit faaac09
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 8 deletions.
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')
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@

/shims
/coverage
package-lock.json
node_modules
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.1+sha1.426460446f57dcf31b5a90fb59e35a8353d0d186",
"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"]
},
"registry": {
"type": "url",
"url": "https://registry.npmmirror.com/cnpm",
"fields": {
"versions": "versions",
"tags": "dist-tags"
}
}
}
}
},
"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.replace(/\{\}/g, version);
const url = process.env.COREPACK_NPM_REGISTRY ?
defaultNpmRegistryURL.replace(
npmRegistryUtils.DEFAULT_NPM_REGISTRY_URL,
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`,
}

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.1`],
[`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.stringMatching(new RegExp(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.stringMatching(new RegExp(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.stringMatching(new RegExp(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.stringMatching(new RegExp(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.stringMatching(new RegExp(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.stringMatching(/cnpm@9\.3\.1/),
stderr: ``,
exitCode: 0,
});
} finally {
delete process.env.COREPACK_ENABLE_NETWORK;
}
Expand Down

0 comments on commit faaac09

Please sign in to comment.