diff --git a/.gitignore b/.gitignore
index 071f556..338dc45 100644
--- a/.gitignore
+++ b/.gitignore
@@ -36,6 +36,7 @@ bower_components
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
+dist
# Dependency directories
node_modules
diff --git a/package-lock.json b/package-lock.json
index 93520ff..90e5fd6 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "json2struct",
- "version": "0.4.0",
+ "version": "0.4.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "json2struct",
- "version": "0.4.0",
+ "version": "0.4.1",
"license": "MIT",
"dependencies": {
"@commander-js/extra-typings": "^10.0.3",
@@ -17,7 +17,7 @@
},
"devDependencies": {
"@types/eslint": "^8.21.3",
- "@types/node": "^18.14.11",
+ "@types/node": "^18.15.11",
"@types/prettier": "^2.7.2",
"@typescript-eslint/eslint-plugin": "^5.55.0",
"@typescript-eslint/parser": "^5.55.0",
diff --git a/package.json b/package.json
index da91c8c..e179a25 100644
--- a/package.json
+++ b/package.json
@@ -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",
@@ -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",
diff --git a/src/cli.ts b/src/cli.ts
deleted file mode 100644
index bb258ce..0000000
--- a/src/cli.ts
+++ /dev/null
@@ -1,61 +0,0 @@
-#!/usr/bin/env node
-
-import fs from 'fs/promises';
-
-import { Command, Option } from '@commander-js/extra-typings';
-
-import { convertToLanguage, SupportedLanguage } from './';
-import { tokenize } from './tokenizer';
-
-const program = new Command();
-
-program
- .name('json2struct')
- .description('Easily translate JSON into type definitions')
- .version('0.4.0')
- .configureOutput({
- writeOut: (str) => process.stdout.write(`[OUT] ${str}`),
- writeErr: (str) => process.stdout.write(`[ERR] ${str}`),
- outputError: (str, write) => write(`\x1b[31m${str}\x1b[0m`),
- });
-
-program
- .command('convert ', { isDefault: true })
- .description('Convert JSON file to type file')
- .option('-o --output ')
- .option('--overwrite')
- .addOption(
- new Option('-l --language ')
- .choices(['typescript', 'python', 'julia', 'rust'])
- .default('typescript')
- )
- .action(async (inputPath, args) => {
- console.info(`\u001b[32mjson2struct: Converting ${inputPath} to ${args.language}:\u001b[0m`);
-
- if (!args?.output?.length && args?.overwrite) {
- program.error('--overwrite options requires an output path');
- return;
- }
-
- const fileContent = await fs.readFile(inputPath);
-
- 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();
diff --git a/src/index.ts b/src/index.ts
index 3c37fb8..6563c95 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -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 ', { isDefault: true })
+ .description('Convert JSON file to type file')
+ .option('-o --output ')
+ .option('--overwrite')
+ .addOption(
+ new Option('-l --language ')
+ .choices(['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();
diff --git a/src/lib.ts b/src/lib.ts
new file mode 100644
index 0000000..3c37fb8
--- /dev/null
+++ b/src/lib.ts
@@ -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);
+}