Skip to content

Commit 2999eb7

Browse files
authored
fix: move cli tool to seperate file (#24)
* fix: move cli tool to seperate file * fix: skip lib check
1 parent 970938a commit 2999eb7

File tree

6 files changed

+91
-64
lines changed

6 files changed

+91
-64
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
"version": "0.4.0",
44
"description": "Easily translate JSON into type definitions",
55
"main": "dist/index.js",
6-
"bin": {
7-
"json2struct": "./dist/index.js"
8-
},
6+
"types": "dist/index.d.ts",
7+
"files": [
8+
"dist"
9+
],
10+
"bin": "./dist/cli.js",
911
"scripts": {
10-
"start": "node dist/index.js",
12+
"start": "node dist/cli.js",
1113
"build": "tsc",
14+
"build:clean": "tsc --build --clean && npm run build",
1215
"format": "prettier --ignore-path .gitignore --write .",
1316
"format:check": "prettier --check --ignore-path .gitignore --write .",
1417
"lint": "eslint --ext \".js,.mjs,.ts,.d.ts\" --ignore-path .gitignore .",

src/cli.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env node
2+
3+
import fs from 'fs/promises';
4+
5+
import { Command, Option } from '@commander-js/extra-typings';
6+
7+
import { convertToLanguage, SupportedLanguage } from './';
8+
import { tokenize } from './tokenizer';
9+
10+
const program = new Command();
11+
12+
program
13+
.name('json2struct')
14+
.description('Easily translate JSON into type definitions')
15+
.version('0.4.0')
16+
.configureOutput({
17+
writeOut: (str) => process.stdout.write(`[OUT] ${str}`),
18+
writeErr: (str) => process.stdout.write(`[ERR] ${str}`),
19+
outputError: (str, write) => write(`\x1b[31m${str}\x1b[0m`),
20+
});
21+
22+
program
23+
.command('convert <input>', { isDefault: true })
24+
.description('Convert JSON file to type file')
25+
.option('-o --output <output-file>')
26+
.option('--overwrite')
27+
.addOption(
28+
new Option('-l --language <output-language>')
29+
.choices<SupportedLanguage[]>(['typescript', 'python', 'julia'])
30+
.default('typescript')
31+
)
32+
.action(async (inputPath, args) => {
33+
console.info(`\u001b[32mjson2struct: Converting ${inputPath} to ${args.language}:\u001b[0m`);
34+
35+
if (!args?.output?.length && args?.overwrite) {
36+
program.error('--overwrite options requires an output path');
37+
return;
38+
}
39+
40+
const fileContent = await fs.readFile(inputPath);
41+
42+
const json = JSON.parse(fileContent.toString());
43+
44+
const tokens = tokenize(json);
45+
46+
const generatedStruct = convertToLanguage((args?.language as SupportedLanguage) ?? 'typescript', tokens);
47+
48+
if (args.output?.length) {
49+
if (args?.overwrite) {
50+
await fs.writeFile(args.output, generatedStruct);
51+
} else {
52+
await fs.appendFile(args.output, generatedStruct);
53+
}
54+
}
55+
56+
console.info(generatedStruct);
57+
});
58+
59+
program.addHelpCommand();
60+
61+
program.parse();

src/index.ts

Lines changed: 16 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
#!/usr/bin/env node
2-
3-
import fs from 'fs/promises';
1+
import { generateJuliaStruct, generatePythonStruct, generateTypeScriptType } from './languages';
2+
import { Token, tokenize } from './tokenizer';
43

5-
import { Command, Option } from '@commander-js/extra-typings';
4+
export * from './languages';
5+
export * from './tokenizer';
66

7-
import { generateJuliaStruct } from './languages/julia';
8-
import { generatePythonStruct } from './languages/python';
9-
import { generateTypeScriptType } from './languages/typescript';
10-
import { Token, tokenize } from './tokenizer';
7+
export type SupportedLanguage = 'typescript' | 'python' | 'julia';
118

12-
function convertToLanguage(language: string, token: Token) {
9+
export function convertToLanguage(language: SupportedLanguage, token: Token) {
1310
switch (language) {
1411
case 'typescript':
1512
return generateTypeScriptType(token);
@@ -25,53 +22,15 @@ function convertToLanguage(language: string, token: Token) {
2522
}
2623
}
2724

28-
const program = new Command();
29-
30-
program
31-
.name('json2struct')
32-
.description('Easily translate JSON into type definitions')
33-
.version('0.4.0')
34-
.configureOutput({
35-
writeOut: (str) => process.stdout.write(`[OUT] ${str}`),
36-
writeErr: (str) => process.stdout.write(`[ERR] ${str}`),
37-
outputError: (str, write) => write(`\x1b[31m${str}\x1b[0m`),
38-
});
39-
40-
program
41-
.command('convert <input>', { isDefault: true })
42-
.description('Convert JSON file to type file')
43-
.option('-o --output <output-file>')
44-
.option('--overwrite')
45-
.addOption(
46-
new Option('-l --language <output-language>').choices(['typescript', 'python', 'julia']).default('typescript')
47-
)
48-
.action(async (inputPath, args) => {
49-
console.info(`\u001b[32mjson2struct: Converting ${inputPath} to ${args.language}:\u001b[0m`);
50-
51-
if (!args?.output?.length && args?.overwrite) {
52-
program.error('--overwrite options requires an output path');
53-
return;
54-
}
55-
56-
const fileContent = await fs.readFile(inputPath);
25+
/**
26+
*
27+
* @param language the language to translate to
28+
* @param json unparsed json string
29+
*/
30+
export default function json2struct(language: string, json: string) {
31+
const parsedJson = JSON.parse(json);
5732

58-
const json = JSON.parse(fileContent.toString());
33+
const tokens = tokenize(parsedJson);
5934

60-
const tokens = tokenize(json);
61-
62-
const generatedStruct = convertToLanguage(args?.language ?? 'typescript', tokens);
63-
64-
if (args.output?.length) {
65-
if (args?.overwrite) {
66-
await fs.writeFile(args.output, generatedStruct);
67-
} else {
68-
await fs.appendFile(args.output, generatedStruct);
69-
}
70-
}
71-
72-
console.info(generatedStruct);
73-
});
74-
75-
program.addHelpCommand();
76-
77-
program.parse();
35+
return convertToLanguage(language as SupportedLanguage, tokens);
36+
}

src/languages/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { generateTypeScriptType } from './typescript';
2+
export { generatePythonStruct } from './python';
3+
export { generateJuliaStruct } from './julia';

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"declaration": true,
1111
"declarationMap": true,
1212
"lib": ["es6", "es2015"],
13-
"resolveJsonModule": true
13+
"resolveJsonModule": true,
14+
"skipLibCheck": true
1415
},
1516
"include": ["src/"],
1617
"exclude": ["src/__tests__/**", "**/**/*.test.ts"]

0 commit comments

Comments
 (0)