Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
139 lines (122 loc) · 3.91 KB

File metadata and controls

139 lines (122 loc) · 3.91 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// @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 {
constructor() {
/** @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);
console.log(message);
}
/**
* @param {string} message
* @param {ts.Node | undefined} node
*/
info(message, node = undefined) {
this.printLog("info", '\x1b[32m', message, node);
}
/**
* @param {string} message
* @param {ts.Node | undefined} node
*/
warn(message, node = undefined) {
this.printLog("warning", '\x1b[33m', message, node);
}
/**
* @param {string} message
*/
error(message) {
this.printLog("error", '\x1b[31m', message);
}
/**
* @param {string} level
* @param {string} color
* @param {string} message
* @param {ts.Node | undefined} node
*/
printLog(level, color, message, node = undefined) {
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',
}
},
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();
diagnosticEngine.info(`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");
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.