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

fix(use): create package.json when calling corepack use on empty dir #350

Merged
merged 8 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'`,
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
]],
});

Expand Down
13 changes: 9 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 @@ -88,6 +89,8 @@ export async function loadSpec(initialCwd: string): Promise<LoadSpecResult> {
manifestPath: string;
} | null = null;

let content: string;

aduh95 marked this conversation as resolved.
Show resolved Hide resolved
while (nextCwd !== currCwd && (!selection || !selection.data.packageManager)) {
currCwd = nextCwd;
nextCwd = path.dirname(currCwd);
Expand All @@ -96,10 +99,12 @@ 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`);
try {
content = await fs.promises.readFile(manifestPath, `utf8`);
} catch (err) {
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
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: `yarn@1.0.0`,
});

await expect(runCli(cwd, [`use`, `yarn@1.22.4`])).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`, `yarn@1.22.4`])).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: `yarn@1.22.4+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`, `yarn@2.2.2`])).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.