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' ;
4
3
5
- import { Command , Option } from '@commander-js/extra-typings' ;
4
+ export * from './languages' ;
5
+ export * from './tokenizer' ;
6
6
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' ;
11
8
12
- function convertToLanguage ( language : string , token : Token ) {
9
+ export function convertToLanguage ( language : SupportedLanguage , token : Token ) {
13
10
switch ( language ) {
14
11
case 'typescript' :
15
12
return generateTypeScriptType ( token ) ;
@@ -25,53 +22,15 @@ function convertToLanguage(language: string, token: Token) {
25
22
}
26
23
}
27
24
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 ) ;
57
32
58
- const json = JSON . parse ( fileContent . toString ( ) ) ;
33
+ const tokens = tokenize ( parsedJson ) ;
59
34
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
+ }
0 commit comments