-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathcli.js
More file actions
256 lines (234 loc) · 8.46 KB
/
cli.js
File metadata and controls
256 lines (234 loc) · 8.46 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// @ts-check
import * as fs from 'fs';
import os from 'os';
import path from 'path';
import { parseArgs } from 'util';
import ts from 'typescript';
import { TypeProcessor } from './processor.js';
class DiagnosticEngine {
/**
* @param {keyof typeof DiagnosticEngine.LEVELS} 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: ts2swift <input> [options]
<input> Path to a .d.ts file, or "-" to read from stdin
Options:
-o, --output <path> Write Swift to <path>. Use "-" for stdout (default).
-p, --project <path> Path to tsconfig.json (default: tsconfig.json).
--global <path> Add a .d.ts as a global declaration file (repeatable).
--log-level <level> One of: verbose, info, warning, error (default: info).
-h, --help Show this help.
Examples:
ts2swift lib.d.ts
ts2swift lib.d.ts -o Generated.swift
ts2swift lib.d.ts -p ./tsconfig.build.json -o Sources/Bridge/API.swift
cat lib.d.ts | ts2swift - -o Generated.swift
ts2swift lib.d.ts --global dom.d.ts --global lib.d.ts
`);
}
/**
* Run ts2swift for a single input file (programmatic API, no process I/O).
* @param {string[]} filePaths - Paths to the .d.ts files
* @param {{ tsconfigPath: string, logLevel?: keyof typeof DiagnosticEngine.LEVELS, globalFiles?: string[] }} options
* @returns {string} Generated Swift source
* @throws {Error} on parse/type-check errors (diagnostics are included in the message)
*/
export function run(filePaths, options) {
const { tsconfigPath, logLevel = 'info', globalFiles = [] } = options;
const diagnosticEngine = new DiagnosticEngine(logLevel);
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) {
const message = ts.formatDiagnosticsWithColorAndContext(configParseResult.errors, {
getCanonicalFileName: (fileName) => fileName,
getNewLine: () => ts.sys.newLine,
getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
});
throw new Error(`TypeScript config/parse errors:\n${message}`);
}
const program = TypeProcessor.createProgram([...filePaths, ...globalFiles], configParseResult.options);
const diagnostics = program.getSemanticDiagnostics();
if (diagnostics.length > 0) {
const message = ts.formatDiagnosticsWithColorAndContext(diagnostics, {
getCanonicalFileName: (fileName) => fileName,
getNewLine: () => ts.sys.newLine,
getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
});
throw new Error(`TypeScript semantic errors:\n${message}`);
}
const prelude = [
"// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,",
"// DO NOT EDIT.",
"//",
"// To update this file, just rebuild your project or run",
"// `swift package bridge-js`.",
"",
"@_spi(BridgeJS) import JavaScriptKit",
"",
"",
].join("\n");
/** @type {string[]} */
const bodies = [];
const globalFileSet = new Set(globalFiles);
for (const inputPath of [...filePaths, ...globalFiles]) {
const processor = new TypeProcessor(program.getTypeChecker(), diagnosticEngine, {
defaultImportFromGlobal: globalFileSet.has(inputPath),
});
const result = processor.processTypeDeclarations(program, inputPath);
const body = result.content.trim();
if (body.length > 0) bodies.push(body);
}
const hasAny = bodies.length > 0;
return hasAny ? prelude + bodies.join("\n\n") + "\n" : "";
}
/**
* Main function to run the CLI
* @param {string[]} args - Command-line arguments
* @returns {void}
*/
export function main(args) {
const options = parseArgs({
args,
options: {
output: {
type: 'string',
short: 'o',
},
project: {
type: 'string',
short: 'p',
},
global: {
type: 'string',
multiple: true,
},
"log-level": {
type: 'string',
default: 'info',
},
help: {
type: 'boolean',
short: 'h',
},
},
allowPositionals: true
})
if (options.values.help) {
printUsage();
process.exit(0);
}
if (options.positionals.length !== 1) {
printUsage();
process.exit(1);
}
/** @type {string[]} */
let filePaths = options.positionals;
/** @type {(() => void)[]} cleanup functions to run after completion */
const cleanups = [];
if (filePaths[0] === '-') {
const content = fs.readFileSync(0, 'utf-8');
const stdinTempPath = path.join(os.tmpdir(), `ts2swift-stdin-${process.pid}-${Date.now()}.d.ts`);
fs.writeFileSync(stdinTempPath, content);
cleanups.push(() => fs.unlinkSync(stdinTempPath));
filePaths = [stdinTempPath];
}
const logLevel = /** @type {keyof typeof DiagnosticEngine.LEVELS} */ ((() => {
const logLevel = options.values["log-level"] || "info";
if (!Object.keys(DiagnosticEngine.LEVELS).includes(logLevel)) {
console.error(`Invalid log level: ${logLevel}. Valid levels are: ${Object.keys(DiagnosticEngine.LEVELS).join(", ")}`);
process.exit(1);
}
return logLevel;
})());
const globalFiles = options.values.global || [];
const tsconfigPath = options.values.project || "tsconfig.json";
const diagnosticEngine = new DiagnosticEngine(logLevel);
diagnosticEngine.print("verbose", `Processing ${filePaths.join(", ")}`);
let swiftOutput;
try {
swiftOutput = run(filePaths, { tsconfigPath, logLevel, globalFiles });
} catch (/** @type {unknown} */ err) {
if (err instanceof Error) {
diagnosticEngine.print("error", err.message);
} else {
diagnosticEngine.print("error", String(err));
}
process.exit(1);
} finally {
for (const cleanup of cleanups) {
cleanup();
}
}
// Write to file or stdout
if (options.values.output && options.values.output !== "-") {
if (swiftOutput.length > 0) {
fs.mkdirSync(path.dirname(options.values.output), { recursive: true });
fs.writeFileSync(options.values.output, swiftOutput);
}
} else {
process.stdout.write(swiftOutput, "utf-8");
}
}