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 2 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
14 changes: 10 additions & 4 deletions sources/specUtils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {UsageError} from 'clipanion';
import {FileHandle} from 'fs/promises';

Check warning on line 2 in sources/specUtils.ts

View workflow job for this annotation

GitHub Actions / Testing chores

'FileHandle' is defined but never used
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 +90,8 @@
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 +100,12 @@
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
18 changes: 18 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,21 @@ 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,
});

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`,
});
});
});
});
Loading