Skip to content

Commit dbc34fd

Browse files
committed
fix: remove @ts-nocheck and fix types for PG17 AST compatibility
- Remove @ts-nocheck from utils.ts, parse.ts, parser.ts, and cli.ts - Add proper TypeScript types and interfaces to all files - Fix AST construction to use correct PG17 format: - Use ast.* (unwrapped) inside A_Const fields (sval, ival, fval) - Use nodes.* (wrapped) for top-level nodes - Change numeric enum codes to PG17 string enums - Fix property names from str to sval - Wrap valuesLists items in List nodes - Fix OnConflictClause to use unwrapped InferClause - Update js-yaml import from safeLoad to load (v4 API) - Refactor CLI to use inquirerer's built-in CLI class - Add @types/js-yaml dev dependency - Update test snapshots with correct output
1 parent 06ebb37 commit dbc34fd

7 files changed

Lines changed: 327 additions & 205 deletions

File tree

packages/csv-to-pg/__tests__/__snapshots__/export.test.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`test case arrays 1`] = `
4-
"INSERT INTO ONLY (
4+
"INSERT INTO collections_public.field (
55
id,
66
schemas
77
) VALUES
88
('450e3b3b-b68d-4abc-990c-65cb8a1dcdb4', '{a,b}');"
99
`;
1010

1111
exports[`test case image/attachment 1`] = `
12-
"INSERT INTO ONLY (
12+
"INSERT INTO collections_public.field (
1313
id,
1414
name,
1515
image,
@@ -19,7 +19,7 @@ exports[`test case image/attachment 1`] = `
1919
`;
2020

2121
exports[`test case jsonb/json 1`] = `
22-
"INSERT INTO ONLY (
22+
"INSERT INTO collections_public.field (
2323
id,
2424
name,
2525
data
@@ -30,7 +30,7 @@ exports[`test case jsonb/json 1`] = `
3030
exports[`test case test case 1`] = `Promise {}`;
3131

3232
exports[`test case test case parser 1`] = `
33-
"INSERT INTO ONLY (
33+
"INSERT INTO collections_public.field (
3434
id,
3535
database_id,
3636
table_id,

packages/csv-to-pg/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
"test:watch": "jest --watch"
3333
},
3434
"devDependencies": {
35-
"makage": "^0.1.9"
35+
"@types/js-yaml": "^4.0.9",
36+
"makage": "^0.1.9",
37+
"pgsql-parser": "^17.9.2"
3638
},
3739
"keywords": [
3840
"csv",

packages/csv-to-pg/src/cli.ts

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,100 @@
11
#!/usr/bin/env node
2-
// @ts-nocheck
3-
import { Inquirerer } from 'inquirerer';
2+
import { CLI, type CommandHandler } from 'inquirerer';
43
import { readConfig } from './parse';
54
import { Parser } from './parser';
65
import { normalizePath } from './utils';
76
import { dirname } from 'path';
87
import { writeFileSync } from 'fs';
98

10-
const argv = process.argv.slice(2);
9+
interface ConfigFile {
10+
input?: string;
11+
output?: string;
12+
debug?: boolean;
13+
[key: string]: unknown;
14+
}
1115

12-
(async () => {
13-
const prompter = new Inquirerer();
16+
interface PromptResult {
17+
config?: string;
18+
input?: string;
19+
output?: string;
20+
}
1421

15-
let { config } = await prompter.prompt(
16-
{},
17-
[
22+
const handler: CommandHandler = async (argv, prompter) => {
23+
// Get config from positional argument or --config flag
24+
let configPath = argv._[0] as string | undefined;
25+
if (!configPath && argv.config) {
26+
configPath = argv.config as string;
27+
}
28+
29+
// If no config provided, prompt for it
30+
if (!configPath) {
31+
const result = await prompter.prompt<PromptResult>({}, [
1832
{
19-
_: true,
2033
name: 'config',
2134
type: 'text',
2235
message: 'Config file path',
2336
required: true
2437
}
25-
],
26-
argv
27-
);
38+
]);
39+
configPath = result.config;
40+
}
2841

29-
config = normalizePath(config);
30-
const dir = dirname(config);
31-
config = readConfig(config);
42+
const normalizedConfigPath = normalizePath(configPath!);
43+
const dir = dirname(normalizedConfigPath);
44+
const config = readConfig(normalizedConfigPath) as ConfigFile;
3245

33-
if (config.input) {
34-
if (!argv.includes('--input')) {
35-
argv.push('--input');
36-
config.input = normalizePath(config.input, dir);
37-
argv.push(config.input);
38-
}
46+
// Build initial values from parsed args and config
47+
const initialValues: PromptResult = {};
48+
49+
if (argv.input) {
50+
initialValues.input = argv.input as string;
51+
} else if (config.input) {
52+
initialValues.input = normalizePath(config.input, dir);
3953
}
40-
if (config.output) {
41-
if (!argv.includes('--output')) {
42-
argv.push('--output');
43-
config.output = normalizePath(config.output, dir);
44-
argv.push(config.output);
45-
}
54+
55+
if (argv.output) {
56+
initialValues.output = argv.output as string;
57+
} else if (config.output) {
58+
initialValues.output = normalizePath(config.output, dir);
4659
}
4760

48-
const results = await prompter.prompt(
49-
{},
50-
[
51-
{
52-
name: 'input',
53-
type: 'text',
54-
message: 'Input CSV file path',
55-
required: true
56-
},
57-
{
58-
name: 'output',
59-
type: 'text',
60-
message: 'Output SQL file path',
61-
required: true
62-
}
63-
],
64-
argv
65-
);
61+
const results = await prompter.prompt<PromptResult>(initialValues, [
62+
{
63+
name: 'input',
64+
type: 'text',
65+
message: 'Input CSV file path',
66+
required: true
67+
},
68+
{
69+
name: 'output',
70+
type: 'text',
71+
message: 'Output SQL file path',
72+
required: true
73+
}
74+
]);
6675

6776
config.input = results.input;
77+
config.output = results.output;
6878

69-
let outFile = results.output;
79+
let outFile = results.output!;
7080
if (!outFile.endsWith('.sql')) outFile = outFile + '.sql';
81+
config.output = outFile;
7182

72-
if (argv.includes('--debug')) {
83+
if (argv.debug) {
7384
config.debug = true;
7485
}
7586

76-
const parser = new Parser(config);
87+
// Cast config to the expected Parser config type
88+
const parserConfig = config as ConfigFile & { input: string; table: string; fields: Record<string, unknown> };
89+
const parser = new Parser(parserConfig);
7790
const sql = await parser.parse();
7891

79-
writeFileSync(config.output, sql);
80-
})();
92+
if (sql) {
93+
writeFileSync(config.output, sql);
94+
}
95+
96+
prompter.close();
97+
};
98+
99+
const cli = new CLI(handler, {});
100+
cli.run();

0 commit comments

Comments
 (0)