Skip to content

Commit 2950a8a

Browse files
aduh95arcanis
andauthored
fix(use): create package.json when calling corepack use on empty dir (#350)
Co-authored-by: Maël Nison <nison.mael@gmail.com>
1 parent 9bdd296 commit 2950a8a

8 files changed

+44
-6
lines changed

sources/commands/Base.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export abstract class BaseCommand extends Command<Context> {
3737
async setLocalPackageManager(info: PreparedPackageManagerInfo) {
3838
const lookup = await specUtils.loadSpec(this.context.cwd);
3939

40-
const content = lookup.target !== `NoProject`
40+
const content = lookup.type !== `NoProject`
4141
? await fs.promises.readFile(lookup.target, `utf8`)
4242
: ``;
4343

sources/commands/Use.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class UseCommand extends BaseCommand {
1616
`,
1717
examples: [[
1818
`Configure the project to use the latest Yarn release`,
19-
`corepack use 'yarn@*'`,
19+
`corepack use yarn`,
2020
]],
2121
});
2222

sources/specUtils.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fs from 'fs';
33
import path from 'path';
44
import semver from 'semver';
55

6+
import {NodeError} from './nodeUtils';
67
import {Descriptor, Locator, isSupportedPackageManager} from './types';
78

89
const nodeModulesRegExp = /[\\/]node_modules[\\/](@[^\\/]*[\\/])?([^@\\/][^\\/]*)$/;
@@ -96,10 +97,13 @@ export async function loadSpec(initialCwd: string): Promise<LoadSpecResult> {
9697
continue;
9798

9899
const manifestPath = path.join(currCwd, `package.json`);
99-
if (!fs.existsSync(manifestPath))
100-
continue;
101-
102-
const content = await fs.promises.readFile(manifestPath, `utf8`);
100+
let content: string;
101+
try {
102+
content = await fs.promises.readFile(manifestPath, `utf8`);
103+
} catch (err) {
104+
if ((err as NodeError)?.code === `ENOENT`) continue;
105+
throw err;
106+
}
103107

104108
let data;
105109
try {

tests/Use.test.ts

+34
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe(`UseCommand`, () => {
1313
it(`should set the package manager in the current project`, async () => {
1414
await xfs.mktempPromise(async cwd => {
1515
await xfs.writeJsonPromise(ppath.join(cwd, `package.json`), {
16+
packageManager: `yarn@1.0.0`,
1617
});
1718

1819
await expect(runCli(cwd, [`use`, `yarn@1.22.4`])).resolves.toMatchObject({
@@ -29,4 +30,37 @@ describe(`UseCommand`, () => {
2930
});
3031
});
3132
});
33+
34+
it(`should create a package.json if absent`, async () => {
35+
await xfs.mktempPromise(async cwd => {
36+
await expect(runCli(cwd, [`use`, `yarn@1.22.4`])).resolves.toMatchObject({
37+
exitCode: 0,
38+
stderr: `warning package.json: No license field\nwarning No license field\n`,
39+
});
40+
41+
await expect(xfs.readJsonPromise(ppath.join(cwd, `package.json`))).resolves.toMatchObject({
42+
packageManager: `yarn@1.22.4+sha256.bc5316aa110b2f564a71a3d6e235be55b98714660870c5b6b2d2d3f12587fb58`,
43+
});
44+
45+
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
46+
exitCode: 0,
47+
stdout: `1.22.4\n`,
48+
stderr: ``,
49+
});
50+
51+
// Ensure Corepack is able to detect package.json in parent directory
52+
const subfolder = ppath.join(cwd, `subfolder`);
53+
await xfs.mkdirPromise(subfolder);
54+
55+
await expect(runCli(subfolder, [`use`, `yarn@2.2.2`])).resolves.toMatchObject({
56+
exitCode: 0,
57+
stderr: ``,
58+
});
59+
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
60+
exitCode: 0,
61+
stdout: `2.2.2\n`,
62+
stderr: ``,
63+
});
64+
});
65+
});
3266
});
2.37 MB
Binary file not shown.
7 Bytes
Binary file not shown.
3.44 MB
Binary file not shown.
7 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)