Skip to content

Commit

Permalink
fix: better error message when running init outside a project (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
alec-chernicki authored Jan 16, 2024
1 parent 91a4ff7 commit 00fa1ae
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-rockets-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"commonality": patch
---

Adds better error message when commonality init is run outside a project
16 changes: 14 additions & 2 deletions apps/commonality/src/cli/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
installCommonality,
getInstallChecks,
getInstallCommonality,
getUseTypeScript,
getCreateConfig,
installChecks,
createConfig,
Expand Down Expand Up @@ -154,6 +153,19 @@ export const action = async ({
}
};

const safeGetRootDirectory = async () => {
try {
return await getRootDirectory();
} catch {
console.log(
`${c.bold(
'\nUnable to find a lockfile',
)}\n\nThis command must be run within a project managed by a JavaScript package manager`,
);
process.exit(1);
}
};

export const init = command
.name('init')
.description('Setup Commonality in your project')
Expand All @@ -169,7 +181,7 @@ export const init = command
installChecks?: boolean;
verbose?: boolean;
}) => {
const rootDirectory = await getRootDirectory();
const rootDirectory = await safeGetRootDirectory();

action({
rootDirectory,
Expand Down
31 changes: 31 additions & 0 deletions apps/commonality/test/integration/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,37 @@ const runTest = async ({
};

describe('init', () => {
it('shows an error if run outside a project', async () => {
const temporaryDirectoryPath = process.env['RUNNER_TEMP'] || os.tmpdir();
const temporaryPath = fs.mkdtempSync(temporaryDirectoryPath);

const initProcess = execa(
binPath,
['init', '--typescript', '--install-checks', '--verbose'],
{
cwd: temporaryPath,
stdout: 'pipe',
},
);

let initOutput = '';
initProcess.stdout?.on('data', (data) => {
console.log({ out: data.toString() });
initOutput += stripAnsi(data.toString());
});
initProcess.stderr?.on('data', (data) => {
console.log({ err: data.toString() });
initOutput += stripAnsi(data.toString());
});

await vi.waitFor(
() => {
expect(initOutput).toContain(`Unable to find a lockfile`);
},
{ timeout: 100_000 },
);
});

it(
'pnpm - Initializes Commonality in a new project',
() =>
Expand Down

0 comments on commit 00fa1ae

Please sign in to comment.