Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
31 changes: 31 additions & 0 deletions packages/wxt/e2e/tests/cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { mkdir, writeFile } from 'node:fs/promises';
import { dirname } from 'node:path';
import { x as spawn } from 'tinyexec';
import { describe, expect, it } from 'vitest';
import { TestProject, WXT_PACKAGE_DIR } from '../utils';

describe('CLI', () => {
it('should exit after prepare when an entrypoint creates active handles', async () => {
const project = new TestProject();
const backgroundPath = project.resolvePath('entrypoints/background.ts');
await mkdir(dirname(backgroundPath), { recursive: true });
await writeFile(
backgroundPath,
`new BroadcastChannel('wxt-prepare-test');

export default defineBackground(() => {});
`,
);

const result = await spawn(
'node',
['--import', 'tsx', 'src/cli/index.ts', 'prepare', project.root],
{
timeout: 5_000,
nodeOptions: { cwd: WXT_PACKAGE_DIR },
},
);

expect(result.exitCode).toBe(0);
});
});
3 changes: 3 additions & 0 deletions packages/wxt/src/cli/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('CLI', () => {
beforeEach(() => {
vi.resetModules();
process.argv = ogArgv;
vi.spyOn(process, 'exit').mockImplementation(() => undefined as never);
createServerMock.mockResolvedValue(mock());
});

Expand All @@ -53,6 +54,7 @@ describe('CLI', () => {
await importCli();

expect(createServerMock).toBeCalledWith({});
expect(process.exit).not.toBeCalled();
});

it('should respect passing a custom root', async () => {
Expand Down Expand Up @@ -399,6 +401,7 @@ describe('CLI', () => {
await importCli();

expect(prepareMock).toBeCalledWith({});
expect(process.exit).toBeCalledWith(0);
});

it('should respect passing a custom root', async () => {
Expand Down
14 changes: 9 additions & 5 deletions packages/wxt/src/cli/cli-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { x as spawn } from 'tinyexec';

/**
* Wrap an action handler to add a timer, error handling, and maybe enable debug
* mode.
* mode. Completed commands exit explicitly so handles created while loading
* user entrypoints cannot keep the CLI alive.
*/
export function wrapAction(
cb: (
Expand Down Expand Up @@ -38,10 +39,13 @@ export function wrapAction(

const status = await cb(...args);

if (!status?.isOngoing && !options?.disableFinishedLog)
consola.success(
`Finished in ${formatDuration(Date.now() - startTime)}`,
);
if (!status?.isOngoing) {
if (!options?.disableFinishedLog)
consola.success(
`Finished in ${formatDuration(Date.now() - startTime)}`,
);
process.exit(0);
}
} catch (err) {
consola.fail(
`Command failed after ${formatDuration(Date.now() - startTime)}`,
Expand Down