Skip to content

Commit

Permalink
fix(use): create package.json when calling corepack use on empty dir
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Jan 6, 2024
1 parent 9bdd296 commit 56537a5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 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
15 changes: 12 additions & 3 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';
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 @@ export async function loadSpec(initialCwd: string): Promise<LoadSpecResult> {
manifestPath: string;
} | null = null;

let file: FileHandle;

while (nextCwd !== currCwd && (!selection || !selection.data.packageManager)) {
currCwd = nextCwd;
nextCwd = path.dirname(currCwd);
Expand All @@ -96,10 +100,15 @@ export async function loadSpec(initialCwd: string): Promise<LoadSpecResult> {
continue;

const manifestPath = path.join(currCwd, `package.json`);
if (!fs.existsSync(manifestPath))
continue;
try {
file = await fs.promises.open(manifestPath, `r`);
} catch (err) {
if ((err as NodeError)?.code === `ENOENT`) continue;
throw err;
}

const content = await fs.promises.readFile(manifestPath, `utf8`);
const content = await file.readFile(`utf8`);
await file.close();

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

0 comments on commit 56537a5

Please sign in to comment.