Skip to content

Commit

Permalink
fix(use): create package.json when calling corepack use on empty …
Browse files Browse the repository at this point in the history
…dir (#350)



Co-authored-by: Maël Nison <[email protected]>
  • Loading branch information
aduh95 and arcanis authored Jan 12, 2024
1 parent 9bdd296 commit 2950a8a
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 6 deletions.
2 changes: 1 addition & 1 deletion sources/commands/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export abstract class BaseCommand extends Command<Context> {
async setLocalPackageManager(info: PreparedPackageManagerInfo) {
const lookup = await specUtils.loadSpec(this.context.cwd);

const content = lookup.target !== `NoProject`
const content = lookup.type !== `NoProject`
? await fs.promises.readFile(lookup.target, `utf8`)
: ``;

Expand Down
2 changes: 1 addition & 1 deletion sources/commands/Use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class UseCommand extends BaseCommand {
`,
examples: [[
`Configure the project to use the latest Yarn release`,
`corepack use 'yarn@*'`,
`corepack use yarn`,
]],
});

Expand Down
12 changes: 8 additions & 4 deletions sources/specUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from 'fs';
import path from 'path';
import semver from 'semver';

import {NodeError} from './nodeUtils';
import {Descriptor, Locator, isSupportedPackageManager} from './types';

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

const manifestPath = path.join(currCwd, `package.json`);
if (!fs.existsSync(manifestPath))
continue;

const content = await fs.promises.readFile(manifestPath, `utf8`);
let content: string;
try {
content = await fs.promises.readFile(manifestPath, `utf8`);
} catch (err) {
if ((err as NodeError)?.code === `ENOENT`) continue;
throw err;
}

let data;
try {
Expand Down
34 changes: 34 additions & 0 deletions tests/Use.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe(`UseCommand`, () => {
it(`should set the package manager in the current project`, async () => {
await xfs.mktempPromise(async cwd => {
await xfs.writeJsonPromise(ppath.join(cwd, `package.json`), {
packageManager: `[email protected]`,
});

await expect(runCli(cwd, [`use`, `[email protected]`])).resolves.toMatchObject({
Expand All @@ -29,4 +30,37 @@ describe(`UseCommand`, () => {
});
});
});

it(`should create a package.json if absent`, async () => {
await xfs.mktempPromise(async cwd => {
await expect(runCli(cwd, [`use`, `[email protected]`])).resolves.toMatchObject({
exitCode: 0,
stderr: `warning package.json: No license field\nwarning No license field\n`,
});

await expect(xfs.readJsonPromise(ppath.join(cwd, `package.json`))).resolves.toMatchObject({
packageManager: `[email protected]+sha256.bc5316aa110b2f564a71a3d6e235be55b98714660870c5b6b2d2d3f12587fb58`,
});

await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
exitCode: 0,
stdout: `1.22.4\n`,
stderr: ``,
});

// Ensure Corepack is able to detect package.json in parent directory
const subfolder = ppath.join(cwd, `subfolder`);
await xfs.mkdirPromise(subfolder);

await expect(runCli(subfolder, [`use`, `[email protected]`])).resolves.toMatchObject({
exitCode: 0,
stderr: ``,
});
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
exitCode: 0,
stdout: `2.2.2\n`,
stderr: ``,
});
});
});
});
Binary file added tests/nock/WyEcK5FmdvhvOqnb2mHi5A-1.dat
Binary file not shown.
Binary file added tests/nock/WyEcK5FmdvhvOqnb2mHi5A-2.dat
Binary file not shown.
Binary file added tests/nock/WyEcK5FmdvhvOqnb2mHi5A-3.dat
Binary file not shown.
Binary file added tests/nock/WyEcK5FmdvhvOqnb2mHi5A-4.dat
Binary file not shown.

0 comments on commit 2950a8a

Please sign in to comment.