Skip to content

Commit 3092c76

Browse files
committed
fix: rename cli.ts to index.ts
1 parent cd0c6b5 commit 3092c76

File tree

6 files changed

+101
-96
lines changed

6 files changed

+101
-96
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ bower_components
3636

3737
# Compiled binary addons (https://nodejs.org/api/addons.html)
3838
build/Release
39+
dist
3940

4041
# Dependency directories
4142
node_modules

package-lock.json

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

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
{
22
"name": "json2struct",
3-
"version": "0.4.0",
3+
"version": "0.4.1",
44
"description": "Easily translate JSON into type definitions",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"files": [
88
"dist"
99
],
10-
"bin": "./dist/cli.js",
10+
"bin": {
11+
"json2struct": "./dist/cli.js"
12+
},
1113
"scripts": {
1214
"start": "node dist/cli.js",
1315
"build": "tsc",
@@ -17,7 +19,7 @@
1719
"lint": "eslint --ext \".js,.mjs,.ts,.d.ts\" --ignore-path .gitignore .",
1820
"test": "vitest --run",
1921
"test:watch": "vitest",
20-
"local": "npm uninstall -g && npm install -g && json2struct",
22+
"local": "npm uninstall -g && npm install -g && json2struct",
2123
"example:typescript": "node dist/cli.js ./examples/example.json --output ./examples/example.d.ts --language typescript --overwrite && prettier --write ./examples/example.d.ts",
2224
"example:python": "node dist/cli.js ./examples/example.json --output ./examples/example.py --language python --overwrite",
2325
"example:julia": "node dist/cli.js ./examples/example.json --output ./examples/example.jl --language julia --overwrite",

src/cli.ts

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/index.ts

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,63 @@
1-
import { generateJuliaStruct, generatePythonStruct, generateRustStruct, generateTypeScriptType } from './languages';
2-
import { Token, tokenize } from './tokenizer';
1+
#!/usr/bin/env node
32

4-
export * from './languages';
5-
export * from './tokenizer';
3+
import fs from 'fs/promises';
64

7-
export type SupportedLanguage = 'typescript' | 'python' | 'julia' | 'rust';
5+
import { Command, Option } from '@commander-js/extra-typings';
86

9-
export function convertToLanguage(language: SupportedLanguage, token: Token) {
10-
switch (language) {
11-
case 'typescript':
12-
return generateTypeScriptType(token);
7+
import { convertToLanguage, SupportedLanguage } from './lib';
8+
import { tokenize } from './tokenizer';
139

14-
case 'python':
15-
return generatePythonStruct(token);
10+
export * from './lib';
1611

17-
case 'julia':
18-
return generateJuliaStruct(token);
12+
const program = new Command();
1913

20-
case 'rust':
21-
return generateRustStruct(token);
14+
program
15+
.name('json2struct')
16+
.description('Easily translate JSON into type definitions')
17+
.version('0.4.1')
18+
.configureOutput({
19+
writeOut: (str) => process.stdout.write(`[OUT] ${str}`),
20+
writeErr: (str) => process.stdout.write(`[ERR] ${str}`),
21+
outputError: (str, write) => write(`\x1b[31m${str}\x1b[0m`),
22+
});
2223

23-
default:
24-
throw new Error(`${language} is not supported`);
25-
}
26-
}
24+
program
25+
.command('convert <input>', { isDefault: true })
26+
.description('Convert JSON file to type file')
27+
.option('-o --output <output-file>')
28+
.option('--overwrite')
29+
.addOption(
30+
new Option('-l --language <output-language>')
31+
.choices<SupportedLanguage[]>(['typescript', 'python', 'julia', 'rust'])
32+
.default('typescript')
33+
)
34+
.action(async (inputPath, args) => {
35+
console.info(`\u001b[32mjson2struct: Converting ${inputPath} to ${args.language}:\u001b[0m`);
2736

28-
/**
29-
*
30-
* @param language the language to translate to
31-
* @param json unparsed json string
32-
*/
33-
export default function json2struct(language: string, json: string) {
34-
const parsedJson = JSON.parse(json);
37+
if (!args?.output?.length && args?.overwrite) {
38+
program.error('--overwrite options requires an output path');
39+
return;
40+
}
3541

36-
const tokens = tokenize(parsedJson);
42+
const fileContent = await fs.readFile(inputPath);
3743

38-
return convertToLanguage(language as SupportedLanguage, tokens);
39-
}
44+
const json = JSON.parse(fileContent.toString());
45+
46+
const tokens = tokenize(json);
47+
48+
const generatedStruct = convertToLanguage((args?.language as SupportedLanguage) ?? 'typescript', tokens);
49+
50+
if (args.output?.length) {
51+
if (args?.overwrite) {
52+
await fs.writeFile(args.output, generatedStruct);
53+
} else {
54+
await fs.appendFile(args.output, generatedStruct);
55+
}
56+
}
57+
58+
console.info(generatedStruct);
59+
});
60+
61+
program.addHelpCommand();
62+
63+
program.parse();

src/lib.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { generateJuliaStruct, generatePythonStruct, generateRustStruct, generateTypeScriptType } from './languages';
2+
import { Token, tokenize } from './tokenizer';
3+
4+
export * from './languages';
5+
export * from './tokenizer';
6+
7+
export type SupportedLanguage = 'typescript' | 'python' | 'julia' | 'rust';
8+
9+
export function convertToLanguage(language: SupportedLanguage, token: Token) {
10+
switch (language) {
11+
case 'typescript':
12+
return generateTypeScriptType(token);
13+
14+
case 'python':
15+
return generatePythonStruct(token);
16+
17+
case 'julia':
18+
return generateJuliaStruct(token);
19+
20+
case 'rust':
21+
return generateRustStruct(token);
22+
23+
default:
24+
throw new Error(`${language} is not supported`);
25+
}
26+
}
27+
28+
/**
29+
*
30+
* @param language the language to translate to
31+
* @param json unparsed json string
32+
*/
33+
export default function json2struct(language: string, json: string) {
34+
const parsedJson = JSON.parse(json);
35+
36+
const tokens = tokenize(parsedJson);
37+
38+
return convertToLanguage(language as SupportedLanguage, tokens);
39+
}

0 commit comments

Comments
 (0)