@nicnocquee/dataqueue 1.19.0 → 1.19.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cli.cjs CHANGED
@@ -1,70 +1,3 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const { spawnSync } = require('child_process');
4
- const path = require('path');
5
-
6
- function printUsage() {
7
- console.log(
8
- 'Usage: dataqueue-cli migrate [--envPath <path>] [-s <schema> | --schema <schema>]',
9
- );
10
- console.log('');
11
- console.log('Options:');
12
- console.log(
13
- ' --envPath <path> Path to a .env file to load environment variables (passed to node-pg-migrate)',
14
- );
15
- console.log(
16
- ' -s, --schema <schema> Set the schema to use (passed to node-pg-migrate)',
17
- );
18
- console.log('');
19
- console.log('Notes:');
20
- console.log(
21
- ' - The PG_DATAQUEUE_DATABASE environment variable must be set to your Postgres connection string.',
22
- );
23
- process.exit(1);
24
- }
25
-
26
- const [, , command, ...restArgs] = process.argv;
27
-
28
- if (command === 'migrate') {
29
- const migrationsDir = path.join(__dirname, 'migrations');
30
-
31
- // Support for -s or --schema argument
32
- let schemaArg = [];
33
- const sIndex = restArgs.indexOf('-s');
34
- const schemaIndex = restArgs.indexOf('--schema');
35
- if (sIndex !== -1 && restArgs[sIndex + 1]) {
36
- schemaArg = ['-s', restArgs[sIndex + 1], '--create-schema'];
37
- restArgs.splice(sIndex, 2);
38
- } else if (schemaIndex !== -1 && restArgs[schemaIndex + 1]) {
39
- schemaArg = ['-s', restArgs[schemaIndex + 1], '--create-schema'];
40
- restArgs.splice(schemaIndex, 2);
41
- }
42
-
43
- // Support for --envPath argument
44
- let envPathArg = [];
45
- const envPathIndex = restArgs.indexOf('--envPath');
46
- if (envPathIndex !== -1 && restArgs[envPathIndex + 1]) {
47
- envPathArg = ['--envPath', restArgs[envPathIndex + 1]];
48
- }
49
-
50
- const result = spawnSync(
51
- 'npx',
52
- [
53
- 'node-pg-migrate',
54
- 'up',
55
- '-t',
56
- 'dataqueuedev_migrations',
57
- '-d',
58
- 'PG_DATAQUEUE_DATABASE',
59
- '-m',
60
- migrationsDir,
61
- ...schemaArg,
62
- ...envPathArg,
63
- ...restArgs,
64
- ],
65
- { stdio: 'inherit' },
66
- );
67
- process.exit(result.status);
68
- } else {
69
- printUsage();
70
- }
3
+ require('./dist/src/cli.js').runCli(process.argv);
package/dist/cli.cjs ADDED
@@ -0,0 +1,78 @@
1
+ 'use strict';
2
+
3
+ var child_process = require('child_process');
4
+ var path = require('path');
5
+
6
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
7
+
8
+ var path__default = /*#__PURE__*/_interopDefault(path);
9
+
10
+ // src/cli.ts
11
+ function runCli(argv, {
12
+ log = console.log,
13
+ exit = (code) => process.exit(code),
14
+ spawnSyncImpl = child_process.spawnSync,
15
+ migrationsDir = path__default.default.join(__dirname, "../migrations")
16
+ } = {}) {
17
+ const [, , command, ...restArgs] = argv;
18
+ function printUsage() {
19
+ log(
20
+ "Usage: dataqueue-cli migrate [--envPath <path>] [-s <schema> | --schema <schema>]"
21
+ );
22
+ log("");
23
+ log("Options:");
24
+ log(
25
+ " --envPath <path> Path to a .env file to load environment variables (passed to node-pg-migrate)"
26
+ );
27
+ log(
28
+ " -s, --schema <schema> Set the schema to use (passed to node-pg-migrate)"
29
+ );
30
+ log("");
31
+ log("Notes:");
32
+ log(
33
+ " - The PG_DATAQUEUE_DATABASE environment variable must be set to your Postgres connection string."
34
+ );
35
+ exit(1);
36
+ }
37
+ if (command === "migrate") {
38
+ let schemaArg = [];
39
+ const sIndex = restArgs.indexOf("-s");
40
+ const schemaIndex = restArgs.indexOf("--schema");
41
+ if (sIndex !== -1 && restArgs[sIndex + 1]) {
42
+ schemaArg = ["-s", restArgs[sIndex + 1], "--create-schema"];
43
+ restArgs.splice(sIndex, 2);
44
+ } else if (schemaIndex !== -1 && restArgs[schemaIndex + 1]) {
45
+ schemaArg = ["-s", restArgs[schemaIndex + 1], "--create-schema"];
46
+ restArgs.splice(schemaIndex, 2);
47
+ }
48
+ let envPathArg = [];
49
+ const envPathIndex = restArgs.indexOf("--envPath");
50
+ if (envPathIndex !== -1 && restArgs[envPathIndex + 1]) {
51
+ envPathArg = ["--envPath", restArgs[envPathIndex + 1]];
52
+ }
53
+ const result = spawnSyncImpl(
54
+ "npx",
55
+ [
56
+ "node-pg-migrate",
57
+ "up",
58
+ "-t",
59
+ "dataqueuedev_migrations",
60
+ "-d",
61
+ "PG_DATAQUEUE_DATABASE",
62
+ "-m",
63
+ migrationsDir,
64
+ ...schemaArg,
65
+ ...envPathArg,
66
+ ...restArgs
67
+ ],
68
+ { stdio: "inherit" }
69
+ );
70
+ exit(result.status ?? 1);
71
+ } else {
72
+ printUsage();
73
+ }
74
+ }
75
+
76
+ exports.runCli = runCli;
77
+ //# sourceMappingURL=cli.cjs.map
78
+ //# sourceMappingURL=cli.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/cli.ts"],"names":["spawnSync","path"],"mappings":";;;;;;;;;;AAYO,SAAS,OACd,IAAA,EACA;AAAA,EACE,MAAM,OAAA,CAAQ,GAAA;AAAA,EACd,IAAA,GAAO,CAAC,IAAA,KAAiB,OAAA,CAAQ,KAAK,IAAI,CAAA;AAAA,EAC1C,aAAA,GAAgBA,uBAAA;AAAA,EAChB,aAAA,GAAgBC,qBAAA,CAAK,IAAA,CAAK,SAAA,EAAW,eAAe;AACtD,CAAA,GAAa,EAAC,EACR;AACN,EAAA,MAAM,KAAK,OAAA,EAAS,GAAG,QAAQ,CAAA,GAAI,IAAA;AAEnC,EAAA,SAAS,UAAA,GAAa;AACpB,IAAA,GAAA;AAAA,MACE;AAAA,KACF;AACA,IAAA,GAAA,CAAI,EAAE,CAAA;AACN,IAAA,GAAA,CAAI,UAAU,CAAA;AACd,IAAA,GAAA;AAAA,MACE;AAAA,KACF;AACA,IAAA,GAAA;AAAA,MACE;AAAA,KACF;AACA,IAAA,GAAA,CAAI,EAAE,CAAA;AACN,IAAA,GAAA,CAAI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A;AAAA,QACA,aAAA;AAAA,QACA,GAAG,SAAA;AAAA,QACH,GAAG,UAAA;AAAA,QACH,GAAG;AAAA,OACL;AAAA,MACA,EAAE,OAAO,SAAA;AAAU,KACrB;AACA,IAAA,IAAA,CAAK,MAAA,CAAO,UAAU,CAAC,CAAA;AAAA,GACzB,MAAO;AACL,IAAA,UAAA,EAAW;AAAA;AAEf","file":"cli.cjs","sourcesContent":["// Testable CLI logic for dataqueue\nimport { spawnSync, SpawnSyncReturns } from 'child_process';\nimport path from 'path';\n\nexport interface CliDeps {\n log?: (...args: any[]) => void;\n error?: (...args: any[]) => void;\n exit?: (code: number) => void;\n spawnSyncImpl?: (...args: any[]) => SpawnSyncReturns<any>;\n migrationsDir?: string;\n}\n\nexport function runCli(\n argv: string[],\n {\n log = console.log,\n exit = (code: number) => process.exit(code),\n spawnSyncImpl = spawnSync,\n migrationsDir = path.join(__dirname, '../migrations'),\n }: CliDeps = {},\n): void {\n const [, , command, ...restArgs] = argv;\n\n function printUsage() {\n log(\n 'Usage: dataqueue-cli migrate [--envPath <path>] [-s <schema> | --schema <schema>]',\n );\n log('');\n log('Options:');\n log(\n ' --envPath <path> Path to a .env file to load environment variables (passed to node-pg-migrate)',\n );\n log(\n ' -s, --schema <schema> Set the schema to use (passed to node-pg-migrate)',\n );\n log('');\n log('Notes:');\n log(\n ' - The PG_DATAQUEUE_DATABASE environment variable must be set to your Postgres connection string.',\n );\n exit(1);\n }\n\n if (command === 'migrate') {\n // Support for -s or --schema argument\n let schemaArg: string[] = [];\n const sIndex = restArgs.indexOf('-s');\n const schemaIndex = restArgs.indexOf('--schema');\n if (sIndex !== -1 && restArgs[sIndex + 1]) {\n schemaArg = ['-s', restArgs[sIndex + 1], '--create-schema'];\n restArgs.splice(sIndex, 2);\n } else if (schemaIndex !== -1 && restArgs[schemaIndex + 1]) {\n schemaArg = ['-s', restArgs[schemaIndex + 1], '--create-schema'];\n restArgs.splice(schemaIndex, 2);\n }\n\n // Support for --envPath argument\n let envPathArg: string[] = [];\n const envPathIndex = restArgs.indexOf('--envPath');\n if (envPathIndex !== -1 && restArgs[envPathIndex + 1]) {\n envPathArg = ['--envPath', restArgs[envPathIndex + 1]];\n }\n\n const result: SpawnSyncReturns<any> = spawnSyncImpl(\n 'npx',\n [\n 'node-pg-migrate',\n 'up',\n '-t',\n 'dataqueuedev_migrations',\n '-d',\n 'PG_DATAQUEUE_DATABASE',\n '-m',\n migrationsDir,\n ...schemaArg,\n ...envPathArg,\n ...restArgs,\n ],\n { stdio: 'inherit' },\n );\n exit(result.status ?? 1);\n } else {\n printUsage();\n }\n}\n"]}
package/dist/cli.d.cts ADDED
@@ -0,0 +1,12 @@
1
+ import { SpawnSyncReturns } from 'child_process';
2
+
3
+ interface CliDeps {
4
+ log?: (...args: any[]) => void;
5
+ error?: (...args: any[]) => void;
6
+ exit?: (code: number) => void;
7
+ spawnSyncImpl?: (...args: any[]) => SpawnSyncReturns<any>;
8
+ migrationsDir?: string;
9
+ }
10
+ declare function runCli(argv: string[], { log, exit, spawnSyncImpl, migrationsDir, }?: CliDeps): void;
11
+
12
+ export { type CliDeps, runCli };
package/dist/cli.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { SpawnSyncReturns } from 'child_process';
2
+
3
+ interface CliDeps {
4
+ log?: (...args: any[]) => void;
5
+ error?: (...args: any[]) => void;
6
+ exit?: (code: number) => void;
7
+ spawnSyncImpl?: (...args: any[]) => SpawnSyncReturns<any>;
8
+ migrationsDir?: string;
9
+ }
10
+ declare function runCli(argv: string[], { log, exit, spawnSyncImpl, migrationsDir, }?: CliDeps): void;
11
+
12
+ export { type CliDeps, runCli };
package/dist/cli.js ADDED
@@ -0,0 +1,72 @@
1
+ import { spawnSync } from 'child_process';
2
+ import path from 'path';
3
+
4
+ // src/cli.ts
5
+ function runCli(argv, {
6
+ log = console.log,
7
+ exit = (code) => process.exit(code),
8
+ spawnSyncImpl = spawnSync,
9
+ migrationsDir = path.join(__dirname, "../migrations")
10
+ } = {}) {
11
+ const [, , command, ...restArgs] = argv;
12
+ function printUsage() {
13
+ log(
14
+ "Usage: dataqueue-cli migrate [--envPath <path>] [-s <schema> | --schema <schema>]"
15
+ );
16
+ log("");
17
+ log("Options:");
18
+ log(
19
+ " --envPath <path> Path to a .env file to load environment variables (passed to node-pg-migrate)"
20
+ );
21
+ log(
22
+ " -s, --schema <schema> Set the schema to use (passed to node-pg-migrate)"
23
+ );
24
+ log("");
25
+ log("Notes:");
26
+ log(
27
+ " - The PG_DATAQUEUE_DATABASE environment variable must be set to your Postgres connection string."
28
+ );
29
+ exit(1);
30
+ }
31
+ if (command === "migrate") {
32
+ let schemaArg = [];
33
+ const sIndex = restArgs.indexOf("-s");
34
+ const schemaIndex = restArgs.indexOf("--schema");
35
+ if (sIndex !== -1 && restArgs[sIndex + 1]) {
36
+ schemaArg = ["-s", restArgs[sIndex + 1], "--create-schema"];
37
+ restArgs.splice(sIndex, 2);
38
+ } else if (schemaIndex !== -1 && restArgs[schemaIndex + 1]) {
39
+ schemaArg = ["-s", restArgs[schemaIndex + 1], "--create-schema"];
40
+ restArgs.splice(schemaIndex, 2);
41
+ }
42
+ let envPathArg = [];
43
+ const envPathIndex = restArgs.indexOf("--envPath");
44
+ if (envPathIndex !== -1 && restArgs[envPathIndex + 1]) {
45
+ envPathArg = ["--envPath", restArgs[envPathIndex + 1]];
46
+ }
47
+ const result = spawnSyncImpl(
48
+ "npx",
49
+ [
50
+ "node-pg-migrate",
51
+ "up",
52
+ "-t",
53
+ "dataqueuedev_migrations",
54
+ "-d",
55
+ "PG_DATAQUEUE_DATABASE",
56
+ "-m",
57
+ migrationsDir,
58
+ ...schemaArg,
59
+ ...envPathArg,
60
+ ...restArgs
61
+ ],
62
+ { stdio: "inherit" }
63
+ );
64
+ exit(result.status ?? 1);
65
+ } else {
66
+ printUsage();
67
+ }
68
+ }
69
+
70
+ export { runCli };
71
+ //# sourceMappingURL=cli.js.map
72
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1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AA,CAAS,WAAA,GAAc,CAAC,CAAA,EAAG;AAC1D,MAAA,SAAA,GAAY,CAAC,IAAA,EAAM,QAAA,CAAS,WAAA,GAAc,CAAC,GAAG,iBAAiB,CAAA;AAC/D,MAAA,QAAA,CAAS,MAAA,CAAO,aAAa,CAAC,CAAA;AAAA;AAIhC,IAAA,IAAI,aAAuB,EAAC;AAC5B,IAAA,MAAM,YAAA,GAAe,QAAA,CAAS,OAAA,CAAQ,WAAW,CAAA;AACjD,IAAA,IAAI,YAAA,KAAiB,EAAA,IAAM,QAAA,CAAS,YAAA,GAAe,CAAC,CAAA,EAAG;AACrD,MAAA,UAAA,GAAa,CAAC,WAAA,EAAa,QAAA,CAAS,YAAA,GAAe,CAAC,CAAC,CAAA;AAAA;AAGvD,IAAA,MAAM,MAAA,GAAgC,aAAA;AAAA,MACpC,KAAA;AAAA,MACA;AAAA,QACE,iBAAA;AAAA,QACA,IAAA;AAAA,QACA,IAAA;AAAA,QACA,yBAAA;AAAA,QACA,IAAA;AAAA,QACA,uBAAA;AAAA,QACA,IAAA;AAAA,QACA,aAAA;AAAA,QACA,GAAG,SAAA;AAAA,QACH,GAAG,UAAA;AAAA,QACH,GAAG;AAAA,OACL;AAAA,MACA,EAAE,OAAO,SAAA;AAAU,KACrB;AACA,IAAA,IAAA,CAAK,MAAA,CAAO,UAAU,CAAC,CAAA;AAAA,GACzB,MAAO;AACL,IAAA,UAAA,EAAW;AAAA;AAEf","file":"cli.js","sourcesContent":["// Testable CLI logic for dataqueue\nimport { spawnSync, SpawnSyncReturns } from 'child_process';\nimport path from 'path';\n\nexport interface CliDeps {\n log?: (...args: any[]) => void;\n error?: (...args: any[]) => void;\n exit?: (code: number) => void;\n spawnSyncImpl?: (...args: any[]) => SpawnSyncReturns<any>;\n migrationsDir?: string;\n}\n\nexport function runCli(\n argv: string[],\n {\n log = console.log,\n exit = (code: number) => process.exit(code),\n spawnSyncImpl = spawnSync,\n migrationsDir = path.join(__dirname, '../migrations'),\n }: CliDeps = {},\n): void {\n const [, , command, ...restArgs] = argv;\n\n function printUsage() {\n log(\n 'Usage: dataqueue-cli migrate [--envPath <path>] [-s <schema> | --schema <schema>]',\n );\n log('');\n log('Options:');\n log(\n ' --envPath <path> Path to a .env file to load environment variables (passed to node-pg-migrate)',\n );\n log(\n ' -s, --schema <schema> Set the schema to use (passed to node-pg-migrate)',\n );\n log('');\n log('Notes:');\n log(\n ' - The PG_DATAQUEUE_DATABASE environment variable must be set to your Postgres connection string.',\n );\n exit(1);\n }\n\n if (command === 'migrate') {\n // Support for -s or --schema argument\n let schemaArg: string[] = [];\n const sIndex = restArgs.indexOf('-s');\n const schemaIndex = restArgs.indexOf('--schema');\n if (sIndex !== -1 && restArgs[sIndex + 1]) {\n schemaArg = ['-s', restArgs[sIndex + 1], '--create-schema'];\n restArgs.splice(sIndex, 2);\n } else if (schemaIndex !== -1 && restArgs[schemaIndex + 1]) {\n schemaArg = ['-s', restArgs[schemaIndex + 1], '--create-schema'];\n restArgs.splice(schemaIndex, 2);\n }\n\n // Support for --envPath argument\n let envPathArg: string[] = [];\n const envPathIndex = restArgs.indexOf('--envPath');\n if (envPathIndex !== -1 && restArgs[envPathIndex + 1]) {\n envPathArg = ['--envPath', restArgs[envPathIndex + 1]];\n }\n\n const result: SpawnSyncReturns<any> = spawnSyncImpl(\n 'npx',\n [\n 'node-pg-migrate',\n 'up',\n '-t',\n 'dataqueuedev_migrations',\n '-d',\n 'PG_DATAQUEUE_DATABASE',\n '-m',\n migrationsDir,\n ...schemaArg,\n ...envPathArg,\n ...restArgs,\n ],\n { stdio: 'inherit' },\n );\n exit(result.status ?? 1);\n } else {\n printUsage();\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nicnocquee/dataqueue",
3
- "version": "1.19.0",
3
+ "version": "1.19.1",
4
4
  "description": "PostgreSQL-based job queue for Node.js applications with support for serverless environments",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -0,0 +1,130 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import { runCli, CliDeps } from './cli.js';
3
+ import type { SpawnSyncReturns } from 'child_process';
4
+
5
+ function makeSpawnSyncReturns(status: number): SpawnSyncReturns<string> {
6
+ // Provide all required properties for the mock
7
+ return {
8
+ pid: 123,
9
+ output: [],
10
+ stdout: '',
11
+ stderr: '',
12
+ status,
13
+ signal: null,
14
+ error: undefined,
15
+ };
16
+ }
17
+
18
+ function makeDeps() {
19
+ return {
20
+ log: vi.fn(),
21
+ error: vi.fn(),
22
+ exit: vi.fn(),
23
+ spawnSyncImpl: vi.fn(() => makeSpawnSyncReturns(0)),
24
+ migrationsDir: '/migrations',
25
+ } satisfies CliDeps;
26
+ }
27
+
28
+ describe('runCli', () => {
29
+ let deps: ReturnType<typeof makeDeps>;
30
+
31
+ beforeEach(() => {
32
+ deps = makeDeps();
33
+ });
34
+
35
+ it('prints usage and exits with code 1 for no command', () => {
36
+ runCli(['node', 'cli.js'], deps);
37
+ expect(deps.log).toHaveBeenCalledWith(
38
+ 'Usage: dataqueue-cli migrate [--envPath <path>] [-s <schema> | --schema <schema>]',
39
+ );
40
+ expect(deps.exit).toHaveBeenCalledWith(1);
41
+ });
42
+
43
+ it('prints usage and exits with code 1 for unknown command', () => {
44
+ runCli(['node', 'cli.js', 'unknown'], deps);
45
+ expect(deps.log).toHaveBeenCalledWith(
46
+ 'Usage: dataqueue-cli migrate [--envPath <path>] [-s <schema> | --schema <schema>]',
47
+ );
48
+ expect(deps.exit).toHaveBeenCalledWith(1);
49
+ });
50
+
51
+ it('calls spawnSyncImpl with correct args for migrate', () => {
52
+ runCli(['node', 'cli.js', 'migrate'], deps);
53
+ expect(deps.spawnSyncImpl).toHaveBeenCalledWith(
54
+ 'npx',
55
+ [
56
+ 'node-pg-migrate',
57
+ 'up',
58
+ '-t',
59
+ 'dataqueuedev_migrations',
60
+ '-d',
61
+ 'PG_DATAQUEUE_DATABASE',
62
+ '-m',
63
+ '/migrations',
64
+ ],
65
+ { stdio: 'inherit' },
66
+ );
67
+ expect(deps.exit).toHaveBeenCalledWith(0);
68
+ });
69
+
70
+ it('parses -s schema argument and passes to spawnSyncImpl', () => {
71
+ runCli(['node', 'cli.js', 'migrate', '-s', 'myschema'], deps);
72
+ expect(deps.spawnSyncImpl).toHaveBeenCalledWith(
73
+ 'npx',
74
+ expect.arrayContaining(['-s', 'myschema', '--create-schema']),
75
+ expect.anything(),
76
+ );
77
+ expect(deps.exit).toHaveBeenCalledWith(0);
78
+ });
79
+
80
+ it('parses --schema argument and passes to spawnSyncImpl', () => {
81
+ runCli(['node', 'cli.js', 'migrate', '--schema', 'myschema'], deps);
82
+ expect(deps.spawnSyncImpl).toHaveBeenCalledWith(
83
+ 'npx',
84
+ expect.arrayContaining(['-s', 'myschema', '--create-schema']),
85
+ expect.anything(),
86
+ );
87
+ expect(deps.exit).toHaveBeenCalledWith(0);
88
+ });
89
+
90
+ it('parses --envPath argument and passes to spawnSyncImpl', () => {
91
+ runCli(['node', 'cli.js', 'migrate', '--envPath', '.env.local'], deps);
92
+ expect(deps.spawnSyncImpl).toHaveBeenCalledWith(
93
+ 'npx',
94
+ expect.arrayContaining(['--envPath', '.env.local']),
95
+ expect.anything(),
96
+ );
97
+ expect(deps.exit).toHaveBeenCalledWith(0);
98
+ });
99
+
100
+ it('passes extra args to spawnSyncImpl', () => {
101
+ runCli(['node', 'cli.js', 'migrate', '--foo', 'bar'], deps);
102
+ expect(deps.spawnSyncImpl).toHaveBeenCalledWith(
103
+ 'npx',
104
+ expect.arrayContaining(['--foo', 'bar']),
105
+ expect.anything(),
106
+ );
107
+ expect(deps.exit).toHaveBeenCalledWith(0);
108
+ });
109
+
110
+ it('exits with nonzero code if spawnSyncImpl returns nonzero status', () => {
111
+ deps.spawnSyncImpl.mockReturnValueOnce(makeSpawnSyncReturns(2));
112
+ runCli(['node', 'cli.js', 'migrate'], deps);
113
+ expect(deps.exit).toHaveBeenCalledWith(2);
114
+ });
115
+
116
+ it('exits with code 1 if spawnSyncImpl returns undefined status', () => {
117
+ // Return an object with all required properties but status undefined
118
+ deps.spawnSyncImpl.mockReturnValueOnce({
119
+ pid: 123,
120
+ output: [],
121
+ stdout: '',
122
+ stderr: '',
123
+ status: null,
124
+ signal: null,
125
+ error: undefined,
126
+ });
127
+ runCli(['node', 'cli.js', 'migrate'], deps);
128
+ expect(deps.exit).toHaveBeenCalledWith(1);
129
+ });
130
+ });
package/src/cli.ts ADDED
@@ -0,0 +1,85 @@
1
+ // Testable CLI logic for dataqueue
2
+ import { spawnSync, SpawnSyncReturns } from 'child_process';
3
+ import path from 'path';
4
+
5
+ export interface CliDeps {
6
+ log?: (...args: any[]) => void;
7
+ error?: (...args: any[]) => void;
8
+ exit?: (code: number) => void;
9
+ spawnSyncImpl?: (...args: any[]) => SpawnSyncReturns<any>;
10
+ migrationsDir?: string;
11
+ }
12
+
13
+ export function runCli(
14
+ argv: string[],
15
+ {
16
+ log = console.log,
17
+ exit = (code: number) => process.exit(code),
18
+ spawnSyncImpl = spawnSync,
19
+ migrationsDir = path.join(__dirname, '../migrations'),
20
+ }: CliDeps = {},
21
+ ): void {
22
+ const [, , command, ...restArgs] = argv;
23
+
24
+ function printUsage() {
25
+ log(
26
+ 'Usage: dataqueue-cli migrate [--envPath <path>] [-s <schema> | --schema <schema>]',
27
+ );
28
+ log('');
29
+ log('Options:');
30
+ log(
31
+ ' --envPath <path> Path to a .env file to load environment variables (passed to node-pg-migrate)',
32
+ );
33
+ log(
34
+ ' -s, --schema <schema> Set the schema to use (passed to node-pg-migrate)',
35
+ );
36
+ log('');
37
+ log('Notes:');
38
+ log(
39
+ ' - The PG_DATAQUEUE_DATABASE environment variable must be set to your Postgres connection string.',
40
+ );
41
+ exit(1);
42
+ }
43
+
44
+ if (command === 'migrate') {
45
+ // Support for -s or --schema argument
46
+ let schemaArg: string[] = [];
47
+ const sIndex = restArgs.indexOf('-s');
48
+ const schemaIndex = restArgs.indexOf('--schema');
49
+ if (sIndex !== -1 && restArgs[sIndex + 1]) {
50
+ schemaArg = ['-s', restArgs[sIndex + 1], '--create-schema'];
51
+ restArgs.splice(sIndex, 2);
52
+ } else if (schemaIndex !== -1 && restArgs[schemaIndex + 1]) {
53
+ schemaArg = ['-s', restArgs[schemaIndex + 1], '--create-schema'];
54
+ restArgs.splice(schemaIndex, 2);
55
+ }
56
+
57
+ // Support for --envPath argument
58
+ let envPathArg: string[] = [];
59
+ const envPathIndex = restArgs.indexOf('--envPath');
60
+ if (envPathIndex !== -1 && restArgs[envPathIndex + 1]) {
61
+ envPathArg = ['--envPath', restArgs[envPathIndex + 1]];
62
+ }
63
+
64
+ const result: SpawnSyncReturns<any> = spawnSyncImpl(
65
+ 'npx',
66
+ [
67
+ 'node-pg-migrate',
68
+ 'up',
69
+ '-t',
70
+ 'dataqueuedev_migrations',
71
+ '-d',
72
+ 'PG_DATAQUEUE_DATABASE',
73
+ '-m',
74
+ migrationsDir,
75
+ ...schemaArg,
76
+ ...envPathArg,
77
+ ...restArgs,
78
+ ],
79
+ { stdio: 'inherit' },
80
+ );
81
+ exit(result.status ?? 1);
82
+ } else {
83
+ printUsage();
84
+ }
85
+ }