Skip to content

Commit

Permalink
fix: rename cli.ts to index.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Apr 3, 2023
1 parent cd0c6b5 commit 3092c76
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 96 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ bower_components

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
dist

# Dependency directories
node_modules
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"name": "json2struct",
"version": "0.4.0",
"version": "0.4.1",
"description": "Easily translate JSON into type definitions",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"bin": "./dist/cli.js",
"bin": {
"json2struct": "./dist/cli.js"
},
"scripts": {
"start": "node dist/cli.js",
"build": "tsc",
Expand All @@ -17,7 +19,7 @@
"lint": "eslint --ext \".js,.mjs,.ts,.d.ts\" --ignore-path .gitignore .",
"test": "vitest --run",
"test:watch": "vitest",
"local": "npm uninstall -g && npm install -g && json2struct",
"local": "npm uninstall -g && npm install -g && json2struct",
"example:typescript": "node dist/cli.js ./examples/example.json --output ./examples/example.d.ts --language typescript --overwrite && prettier --write ./examples/example.d.ts",
"example:python": "node dist/cli.js ./examples/example.json --output ./examples/example.py --language python --overwrite",
"example:julia": "node dist/cli.js ./examples/example.json --output ./examples/example.jl --language julia --overwrite",
Expand Down
61 changes: 0 additions & 61 deletions src/cli.ts

This file was deleted.

82 changes: 53 additions & 29 deletions src/index.ts
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();
39 changes: 39 additions & 0 deletions src/lib.ts
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);
}

0 comments on commit 3092c76

Please sign in to comment.