-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
101 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,63 @@ | ||
import { generateJuliaStruct, generatePythonStruct, generateRustStruct, generateTypeScriptType } from './languages'; | ||
import { Token, tokenize } from './tokenizer'; | ||
#!/usr/bin/env node | ||
|
||
export * from './languages'; | ||
export * from './tokenizer'; | ||
import fs from 'fs/promises'; | ||
|
||
export type SupportedLanguage = 'typescript' | 'python' | 'julia' | 'rust'; | ||
import { Command, Option } from '@commander-js/extra-typings'; | ||
|
||
export function convertToLanguage(language: SupportedLanguage, token: Token) { | ||
switch (language) { | ||
case 'typescript': | ||
return generateTypeScriptType(token); | ||
import { convertToLanguage, SupportedLanguage } from './lib'; | ||
import { tokenize } from './tokenizer'; | ||
|
||
case 'python': | ||
return generatePythonStruct(token); | ||
export * from './lib'; | ||
|
||
case 'julia': | ||
return generateJuliaStruct(token); | ||
const program = new Command(); | ||
|
||
case 'rust': | ||
return generateRustStruct(token); | ||
program | ||
.name('json2struct') | ||
.description('Easily translate JSON into type definitions') | ||
.version('0.4.1') | ||
.configureOutput({ | ||
writeOut: (str) => process.stdout.write(`[OUT] ${str}`), | ||
writeErr: (str) => process.stdout.write(`[ERR] ${str}`), | ||
outputError: (str, write) => write(`\x1b[31m${str}\x1b[0m`), | ||
}); | ||
|
||
default: | ||
throw new Error(`${language} is not supported`); | ||
} | ||
} | ||
program | ||
.command('convert <input>', { isDefault: true }) | ||
.description('Convert JSON file to type file') | ||
.option('-o --output <output-file>') | ||
.option('--overwrite') | ||
.addOption( | ||
new Option('-l --language <output-language>') | ||
.choices<SupportedLanguage[]>(['typescript', 'python', 'julia', 'rust']) | ||
.default('typescript') | ||
) | ||
.action(async (inputPath, args) => { | ||
console.info(`\u001b[32mjson2struct: Converting ${inputPath} to ${args.language}:\u001b[0m`); | ||
|
||
/** | ||
* | ||
* @param language the language to translate to | ||
* @param json unparsed json string | ||
*/ | ||
export default function json2struct(language: string, json: string) { | ||
const parsedJson = JSON.parse(json); | ||
if (!args?.output?.length && args?.overwrite) { | ||
program.error('--overwrite options requires an output path'); | ||
return; | ||
} | ||
|
||
const tokens = tokenize(parsedJson); | ||
const fileContent = await fs.readFile(inputPath); | ||
|
||
return convertToLanguage(language as SupportedLanguage, tokens); | ||
} | ||
const json = JSON.parse(fileContent.toString()); | ||
|
||
const tokens = tokenize(json); | ||
|
||
const generatedStruct = convertToLanguage((args?.language as SupportedLanguage) ?? 'typescript', tokens); | ||
|
||
if (args.output?.length) { | ||
if (args?.overwrite) { | ||
await fs.writeFile(args.output, generatedStruct); | ||
} else { | ||
await fs.appendFile(args.output, generatedStruct); | ||
} | ||
} | ||
|
||
console.info(generatedStruct); | ||
}); | ||
|
||
program.addHelpCommand(); | ||
|
||
program.parse(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { generateJuliaStruct, generatePythonStruct, generateRustStruct, generateTypeScriptType } from './languages'; | ||
import { Token, tokenize } from './tokenizer'; | ||
|
||
export * from './languages'; | ||
export * from './tokenizer'; | ||
|
||
export type SupportedLanguage = 'typescript' | 'python' | 'julia' | 'rust'; | ||
|
||
export function convertToLanguage(language: SupportedLanguage, token: Token) { | ||
switch (language) { | ||
case 'typescript': | ||
return generateTypeScriptType(token); | ||
|
||
case 'python': | ||
return generatePythonStruct(token); | ||
|
||
case 'julia': | ||
return generateJuliaStruct(token); | ||
|
||
case 'rust': | ||
return generateRustStruct(token); | ||
|
||
default: | ||
throw new Error(`${language} is not supported`); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param language the language to translate to | ||
* @param json unparsed json string | ||
*/ | ||
export default function json2struct(language: string, json: string) { | ||
const parsedJson = JSON.parse(json); | ||
|
||
const tokens = tokenize(parsedJson); | ||
|
||
return convertToLanguage(language as SupportedLanguage, tokens); | ||
} |