@efebia/yaml-to-ts 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/generateDeclaration.d.ts +23 -0
- package/lib/generateDeclaration.js +78 -0
- package/lib/yaml-to-ts.d.ts +2 -0
- package/lib/yaml-to-ts.js +100 -0
- package/package.json +27 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as prettier from 'prettier';
|
|
2
|
+
export type Declaration = string | {
|
|
3
|
+
type: 'array';
|
|
4
|
+
value: Declaration;
|
|
5
|
+
} | {
|
|
6
|
+
[key: string]: any;
|
|
7
|
+
};
|
|
8
|
+
export declare const generateDeclaration: (currentValue: any) => Declaration;
|
|
9
|
+
export type StringifyKeyValue = {
|
|
10
|
+
k?: string;
|
|
11
|
+
v: string;
|
|
12
|
+
};
|
|
13
|
+
export type StringifyOptions = {
|
|
14
|
+
bracketsAround?: boolean;
|
|
15
|
+
};
|
|
16
|
+
type BuildConfig = {
|
|
17
|
+
prettierConfig: prettier.Config;
|
|
18
|
+
outFile: string;
|
|
19
|
+
interfaceName: string;
|
|
20
|
+
dryRun: boolean;
|
|
21
|
+
};
|
|
22
|
+
export declare const build: (jsonObj: object, configs: BuildConfig) => Promise<void>;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.build = exports.generateDeclaration = void 0;
|
|
27
|
+
const promises_1 = require("fs/promises");
|
|
28
|
+
const prettier = __importStar(require("prettier"));
|
|
29
|
+
const generateDeclaration = (currentValue) => {
|
|
30
|
+
if (typeof currentValue !== 'object')
|
|
31
|
+
return typeof currentValue;
|
|
32
|
+
if (Array.isArray(currentValue)) {
|
|
33
|
+
return {
|
|
34
|
+
type: 'array',
|
|
35
|
+
value: (0, exports.generateDeclaration)(currentValue[0]),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return Object.entries(currentValue).reduce((acc, [key, value]) => {
|
|
39
|
+
acc[key] = (0, exports.generateDeclaration)(value);
|
|
40
|
+
return acc;
|
|
41
|
+
}, {});
|
|
42
|
+
};
|
|
43
|
+
exports.generateDeclaration = generateDeclaration;
|
|
44
|
+
const stringify = (keyValue, opts = { bracketsAround: true }) => {
|
|
45
|
+
if (keyValue.k) {
|
|
46
|
+
if (opts.bracketsAround)
|
|
47
|
+
return `${keyValue.k}: {\n${keyValue.v}\n}`;
|
|
48
|
+
return `${keyValue.k}: ${keyValue.v}`;
|
|
49
|
+
}
|
|
50
|
+
if (opts.bracketsAround)
|
|
51
|
+
return `{\n${keyValue.v}\n}`;
|
|
52
|
+
return keyValue.v;
|
|
53
|
+
};
|
|
54
|
+
const generateStringDeclaration = (value, key) => {
|
|
55
|
+
if (typeof value !== 'object')
|
|
56
|
+
return stringify({ k: key, v: value.replace("'", '') }, { bracketsAround: false });
|
|
57
|
+
if (Object.hasOwn(value, 'type') && value['type'] === 'array') {
|
|
58
|
+
return stringify({ v: `${generateStringDeclaration(value['value'])}[]`, k: key }, { bracketsAround: false });
|
|
59
|
+
}
|
|
60
|
+
const insideKeys = Object.entries(value)
|
|
61
|
+
.map(([k, v]) => generateStringDeclaration(v, k))
|
|
62
|
+
.join(';\n');
|
|
63
|
+
return stringify({ k: key, v: insideKeys });
|
|
64
|
+
};
|
|
65
|
+
const build = async (jsonObj, configs) => {
|
|
66
|
+
const objectInterfaceDeclaration = (0, exports.generateDeclaration)(jsonObj);
|
|
67
|
+
const stringDeclaration = generateStringDeclaration(objectInterfaceDeclaration);
|
|
68
|
+
const typescriptDeclaration = `export interface ${configs.interfaceName} ${stringDeclaration}`;
|
|
69
|
+
const formatted = prettier.format(typescriptDeclaration, configs.prettierConfig);
|
|
70
|
+
if (configs.dryRun) {
|
|
71
|
+
console.log(formatted);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
await (0, promises_1.writeFile)(configs.outFile, prettier.format(typescriptDeclaration, configs.prettierConfig), {
|
|
75
|
+
encoding: 'utf-8',
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
exports.build = build;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
+
}) : function(o, v) {
|
|
17
|
+
o["default"] = v;
|
|
18
|
+
});
|
|
19
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
20
|
+
if (mod && mod.__esModule) return mod;
|
|
21
|
+
var result = {};
|
|
22
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
23
|
+
__setModuleDefault(result, mod);
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
26
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
27
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
28
|
+
};
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
const promises_1 = require("fs/promises");
|
|
31
|
+
const yaml = __importStar(require("js-yaml"));
|
|
32
|
+
const path = __importStar(require("path"));
|
|
33
|
+
const prettier_1 = __importDefault(require("prettier"));
|
|
34
|
+
const yargs_1 = __importDefault(require("yargs"));
|
|
35
|
+
const helpers_1 = require("yargs/helpers");
|
|
36
|
+
const generateDeclaration_js_1 = require("./generateDeclaration.js");
|
|
37
|
+
const parsedArgs = (0, helpers_1.hideBin)(process.argv);
|
|
38
|
+
(0, yargs_1.default)(parsedArgs)
|
|
39
|
+
.command('$0 <input-file> [--prettier-file prettier-file-URI] [--out-file out-file-URI] [--dry-run] [--interface-name name]', 'Build TS interface from YAML file', (args) => {
|
|
40
|
+
return args
|
|
41
|
+
.positional('input-file', {
|
|
42
|
+
alias: 'i',
|
|
43
|
+
type: 'string',
|
|
44
|
+
description: 'YAML Input file',
|
|
45
|
+
demandOption: true
|
|
46
|
+
})
|
|
47
|
+
.option('prettier-file', {
|
|
48
|
+
alias: 'p',
|
|
49
|
+
type: 'string',
|
|
50
|
+
description: 'Prettier configuration file',
|
|
51
|
+
})
|
|
52
|
+
.option('out-file', {
|
|
53
|
+
alias: 'o',
|
|
54
|
+
type: 'string',
|
|
55
|
+
description: 'Output file',
|
|
56
|
+
default: path.join(process.cwd(), 'declaration.d.ts'),
|
|
57
|
+
})
|
|
58
|
+
.option('dry-run', {
|
|
59
|
+
alias: 'd',
|
|
60
|
+
type: 'boolean',
|
|
61
|
+
description: 'Perform a dry run',
|
|
62
|
+
default: false,
|
|
63
|
+
})
|
|
64
|
+
.option('interface-name', {
|
|
65
|
+
alias: 'int',
|
|
66
|
+
type: 'string',
|
|
67
|
+
description: 'Define interface name',
|
|
68
|
+
default: 'Declaration',
|
|
69
|
+
})
|
|
70
|
+
.strict();
|
|
71
|
+
}, async (argv) => {
|
|
72
|
+
const fileVal = await (0, promises_1.readFile)(argv.inputFile, {
|
|
73
|
+
encoding: 'utf-8',
|
|
74
|
+
});
|
|
75
|
+
const parsedYaml = yaml.load(fileVal);
|
|
76
|
+
let prettierConfig = {
|
|
77
|
+
printWidth: 80,
|
|
78
|
+
tabWidth: 4,
|
|
79
|
+
semi: true,
|
|
80
|
+
singleQuote: true,
|
|
81
|
+
endOfLine: 'lf',
|
|
82
|
+
trailingComma: 'es5',
|
|
83
|
+
bracketSpacing: true,
|
|
84
|
+
arrowParens: 'always',
|
|
85
|
+
parser: 'typescript',
|
|
86
|
+
};
|
|
87
|
+
if (argv.prettierFile) {
|
|
88
|
+
const resolvedConfig = await prettier_1.default.resolveConfig(argv.prettierFile);
|
|
89
|
+
if (resolvedConfig) {
|
|
90
|
+
prettierConfig = resolvedConfig;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
await (0, generateDeclaration_js_1.build)(parsedYaml, {
|
|
94
|
+
interfaceName: argv.interfaceName,
|
|
95
|
+
outFile: argv.outFile,
|
|
96
|
+
prettierConfig,
|
|
97
|
+
dryRun: argv.dryRun,
|
|
98
|
+
});
|
|
99
|
+
})
|
|
100
|
+
.parse();
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@efebia/yaml-to-ts",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Build TS interface declaration from yml file",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/yaml-to-ts.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc"
|
|
9
|
+
},
|
|
10
|
+
"license": "ISC",
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"js-yaml": "^4.1.0",
|
|
13
|
+
"prettier": "^2.6.2",
|
|
14
|
+
"yargs": "^17.4.1"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@types/js-yaml": "^4.0.5",
|
|
18
|
+
"@types/node": "^17.0.23",
|
|
19
|
+
"@types/prettier": "^2.6.0",
|
|
20
|
+
"@types/yargs": "^17.0.10",
|
|
21
|
+
"typescript": "^4.6.3"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"/lib"
|
|
25
|
+
],
|
|
26
|
+
"bin": "lib/yaml-to-ts.js"
|
|
27
|
+
}
|