Skip to content

Commit edeae6d

Browse files
committed
accepts table and dataset from cli
1 parent 0ceed3b commit edeae6d

File tree

5 files changed

+45
-10
lines changed

5 files changed

+45
-10
lines changed

package-lock.json

+17-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"license": "ISC",
2424
"devDependencies": {
2525
"@types/jest": "^29.5.0",
26+
"@types/minimist": "^1.2.2",
2627
"@typescript-eslint/eslint-plugin": "^5.57.1",
2728
"@typescript-eslint/parser": "^5.57.1",
2829
"copyfiles": "^2.4.1",
@@ -36,5 +37,8 @@
3637
"ts-jest": "^29.1.0",
3738
"ts-node": "^10.9.1",
3839
"typescript": "^5.0.3"
40+
},
41+
"dependencies": {
42+
"minimist": "^1.2.8"
3943
}
4044
}

src/index.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ describe('main', () => {
55
it('should log error if no filepath is passed', () => {
66
process.argv = ['foo', 'bar']
77
console.log = jest.fn()
8-
main()
8+
const args = {_: []}
9+
main(args)
910
expect(console.log).toHaveBeenCalled()
1011
})
1112
})

src/index.ts

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
#!/usr/bin/env node
22

3+
import minimist from 'minimist'
34
import { readJsonFileContent, writeToStdOut } from "./io"
45
import { jsonToSqlView } from "./parser"
56

6-
export const main = () => {
7-
const args = process.argv.slice(2)
8-
const inputFilePath = args[0]
7+
export const main = (args: minimist.ParsedArgs) => {
8+
const inputFilePath = args._[0]
9+
10+
let table = '<table>'
11+
let dataset = '<dataset>'
912

1013
if(!inputFilePath) {
1114
printHelp();
1215
return
1316
}
1417

18+
if (args.table) {
19+
table = args.table
20+
}
21+
22+
if (args.dataset) {
23+
dataset = args.dataset
24+
}
25+
1526
try {
1627
const fileContent = readJsonFileContent(inputFilePath)
17-
const sqlOutput = jsonToSqlView(fileContent)
28+
const sqlOutput = jsonToSqlView(fileContent, {table, dataset})
1829
writeToStdOut(sqlOutput)
1930
} catch(err) {
2031
console.log(err)
@@ -26,5 +37,10 @@ function printHelp() {
2637
console.log(msg)
2738
}
2839

29-
main();
40+
const args = minimist(process.argv.slice(2), {
41+
stopEarly: true,
42+
boolean: true
43+
})
44+
45+
main(args);
3046

src/parser.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export function jsonToSqlView(json: any, options: Options = defaultOptions) {
6363
if (typeof json[k] === 'object' && isGeoJsonFeatureCollection(json[k])) {
6464
parentSql = `${parentSql}\n\t${commaIfNeeded(i)}TO_JSON_STRING(json_blob.${k}) as ${snakeCase(k)}`
6565
} else if (typeof json[k] === 'object' && !isGeoJsonFeatureCollection(json[k])){
66-
const query = parseNestedKey(json[k], k)
66+
const query = parseNestedKey(json[k], k, options)
6767
childQueries.push(query)
6868
} else {
6969
const type = datatype(json[k])

0 commit comments

Comments
 (0)