Skip to content
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'`,
Comment thread
aduh95 marked this conversation as resolved.
Outdated
]],
});

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;

Comment thread
aduh95 marked this conversation as resolved.
Outdated
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) {
Comment thread
aduh95 marked this conversation as 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.