-
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathcli.js
More file actions
151 lines (136 loc) · 4.34 KB
/
cli.js
File metadata and controls
151 lines (136 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// @ts-check
import * as fs from 'fs';
import { TypeProcessor } from './processor.js';
import { parseArgs } from 'util';
import ts from 'typescript';
import path from 'path';
class DiagnosticEngine {
/**
* @param {string} level
*/
constructor(level) {
const levelInfo = DiagnosticEngine.LEVELS[level];
if (!levelInfo) {
throw new Error(`Invalid log level: ${level}`);
}
this.minLevel = levelInfo.level;
/** @type {ts.FormatDiagnosticsHost} */
this.formattHost = {
getCanonicalFileName: (fileName) => fileName,
getNewLine: () => ts.sys.newLine,
getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
};
}
/**
* @param {readonly ts.Diagnostic[]} diagnostics
*/
tsDiagnose(diagnostics) {
const message = ts.formatDiagnosticsWithColorAndContext(diagnostics, this.formattHost);
process.stderr.write(message, "utf-8");
}
static LEVELS = {
"verbose": {
color: '\x1b[34m',
level: 0,
},
"info": {
color: '\x1b[32m',
level: 1,
},
"warning": {
color: '\x1b[33m',
level: 2,
},
"error": {
color: '\x1b[31m',
level: 3,
},
}
/**
* @param {keyof typeof DiagnosticEngine.LEVELS} level
* @param {string} message
* @param {ts.Node | undefined} node
*/
print(level, message, node = undefined) {
const levelInfo = DiagnosticEngine.LEVELS[level];
if (levelInfo.level < this.minLevel) {
return;
}
const color = levelInfo.color;
if (node) {
const sourceFile = node.getSourceFile();
const { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart());
const location = sourceFile.fileName + ":" + (line + 1) + ":" + (character);
process.stderr.write(`${location}: ${color}${level}\x1b[0m: ${message}\n`);
} else {
process.stderr.write(`${color}${level}\x1b[0m: ${message}\n`);
}
}
}
function printUsage() {
console.error('Usage: ts2skeleton <d.ts file path> -p <tsconfig.json path> [-o output.json]');
}
/**
* Main function to run the CLI
* @param {string[]} args - Command-line arguments
* @returns {void}
*/
export function main(args) {
// Parse command line arguments
const options = parseArgs({
args,
options: {
output: {
type: 'string',
short: 'o',
},
project: {
type: 'string',
short: 'p',
},
"log-level": {
type: 'string',
default: 'info',
},
},
allowPositionals: true
})
if (options.positionals.length !== 1) {
printUsage();
process.exit(1);
}
const tsconfigPath = options.values.project;
if (!tsconfigPath) {
printUsage();
process.exit(1);
}
const filePath = options.positionals[0];
const diagnosticEngine = new DiagnosticEngine(options.values["log-level"] || "info");
diagnosticEngine.print("verbose", `Processing ${filePath}...`);
// Create TypeScript program and process declarations
const configFile = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
const configParseResult = ts.parseJsonConfigFileContent(
configFile.config,
ts.sys,
path.dirname(path.resolve(tsconfigPath))
);
if (configParseResult.errors.length > 0) {
diagnosticEngine.tsDiagnose(configParseResult.errors);
process.exit(1);
}
const program = TypeProcessor.createProgram(filePath, configParseResult.options);
const diagnostics = program.getSemanticDiagnostics();
if (diagnostics.length > 0) {
diagnosticEngine.tsDiagnose(diagnostics);
process.exit(1);
}
const processor = new TypeProcessor(program.getTypeChecker(), diagnosticEngine);
const results = processor.processTypeDeclarations(program, filePath);
// Write results to file or stdout
const jsonOutput = JSON.stringify(results, null, 2);
if (options.values.output) {
fs.writeFileSync(options.values.output, jsonOutput);
} else {
process.stdout.write(jsonOutput, "utf-8");
}
}