Skip to content

Commit 38eab65

Browse files
committed
Add init command support to CLI and enhance tests
- Introduced the `init` command in the CLI, routing it to a new `runInitImpl` function for initializing project structure. - Updated CLI usage messages to include the new `init` command for better user guidance. - Added comprehensive tests for the `init` command, ensuring proper functionality and integration with the CLI. - Created a new `init-command.ts` file to handle initialization logic, including file creation and package updates. These changes improve the CLI's usability and extend its functionality, providing users with a streamlined way to set up their projects.
1 parent 888d77c commit 38eab65

4 files changed

Lines changed: 1203 additions & 8 deletions

File tree

‎packages/dataqueue/src/cli.test.ts‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function makeDeps() {
2222
exit: vi.fn(),
2323
spawnSyncImpl: vi.fn(() => makeSpawnSyncReturns(0)),
2424
migrationsDir: '/migrations',
25+
runInitImpl: vi.fn(),
2526
} satisfies CliDeps;
2627
}
2728

@@ -34,20 +35,30 @@ describe('runCli', () => {
3435

3536
it('prints usage and exits with code 1 for no command', () => {
3637
runCli(['node', 'cli.js'], deps);
37-
expect(deps.log).toHaveBeenCalledWith(
38-
'Usage: dataqueue-cli migrate [--envPath <path>] [-s <schema> | --schema <schema>]',
39-
);
38+
expect(deps.log).toHaveBeenCalledWith('Usage:');
39+
expect(deps.log).toHaveBeenCalledWith(' dataqueue-cli init');
4040
expect(deps.exit).toHaveBeenCalledWith(1);
4141
});
4242

4343
it('prints usage and exits with code 1 for unknown command', () => {
4444
runCli(['node', 'cli.js', 'unknown'], deps);
45-
expect(deps.log).toHaveBeenCalledWith(
46-
'Usage: dataqueue-cli migrate [--envPath <path>] [-s <schema> | --schema <schema>]',
47-
);
45+
expect(deps.log).toHaveBeenCalledWith('Usage:');
46+
expect(deps.log).toHaveBeenCalledWith(' dataqueue-cli init');
4847
expect(deps.exit).toHaveBeenCalledWith(1);
4948
});
5049

50+
it('routes init command to runInitImpl', () => {
51+
runCli(['node', 'cli.js', 'init'], deps);
52+
expect(deps.runInitImpl).toHaveBeenCalledWith(
53+
expect.objectContaining({
54+
log: deps.log,
55+
error: deps.error,
56+
exit: deps.exit,
57+
}),
58+
);
59+
expect(deps.spawnSyncImpl).not.toHaveBeenCalled();
60+
});
61+
5162
it('calls spawnSyncImpl with correct args for migrate', () => {
5263
runCli(['node', 'cli.js', 'migrate'], deps);
5364
expect(deps.spawnSyncImpl).toHaveBeenCalledWith(

‎packages/dataqueue/src/cli.ts‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { spawnSync, SpawnSyncReturns } from 'child_process';
33
import path from 'path';
44
import { fileURLToPath } from 'url';
5+
import { InitDeps, runInit } from './init-command.js';
56

67
const __filename = fileURLToPath(import.meta.url);
78
const __dirname = path.dirname(__filename);
@@ -12,25 +13,35 @@ export interface CliDeps {
1213
exit?: (code: number) => void;
1314
spawnSyncImpl?: (...args: any[]) => SpawnSyncReturns<any>;
1415
migrationsDir?: string;
16+
initDeps?: InitDeps;
17+
runInitImpl?: (deps?: InitDeps) => void;
1518
}
1619

1720
export function runCli(
1821
argv: string[],
1922
{
2023
log = console.log,
24+
error = console.error,
2125
exit = (code: number) => process.exit(code),
2226
spawnSyncImpl = spawnSync,
2327
migrationsDir = path.join(__dirname, '../migrations'),
28+
initDeps,
29+
runInitImpl = runInit,
2430
}: CliDeps = {},
2531
): void {
2632
const [, , command, ...restArgs] = argv;
2733

34+
/**
35+
* Prints CLI usage and exits with non-zero code.
36+
*/
2837
function printUsage() {
38+
log('Usage:');
2939
log(
30-
'Usage: dataqueue-cli migrate [--envPath <path>] [-s <schema> | --schema <schema>]',
40+
' dataqueue-cli migrate [--envPath <path>] [-s <schema> | --schema <schema>]',
3141
);
42+
log(' dataqueue-cli init');
3243
log('');
33-
log('Options:');
44+
log('Options for migrate:');
3445
log(
3546
' --envPath <path> Path to a .env file to load environment variables (passed to node-pg-migrate)',
3647
);
@@ -48,6 +59,14 @@ export function runCli(
4859
log(
4960
' Example: PGSSLMODE=require NODE_EXTRA_CA_CERTS=/absolute/path/to/ca.crt PG_DATAQUEUE_DATABASE=... npx dataqueue-cli migrate',
5061
);
62+
log('');
63+
log('Notes for init:');
64+
log(
65+
' - Supports both Next.js App Router and Pages Router (prefers App Router if both exist).',
66+
);
67+
log(
68+
' - Scaffolds endpoint, cron.sh, queue placeholder, and package.json entries.',
69+
);
5170
exit(1);
5271
}
5372

@@ -89,6 +108,13 @@ export function runCli(
89108
{ stdio: 'inherit' },
90109
);
91110
exit(result.status ?? 1);
111+
} else if (command === 'init') {
112+
runInitImpl({
113+
log,
114+
error,
115+
exit,
116+
...initDeps,
117+
});
92118
} else {
93119
printUsage();
94120
}

0 commit comments

Comments
 (0)