-
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathcli.js
More file actions
212 lines (193 loc) · 6.77 KB
/
cli.js
File metadata and controls
212 lines (193 loc) · 6.77 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
// @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: ts2swift <d.ts file path> -p <tsconfig.json path> [--global <d.ts>]... [-o output.swift]');
}
/**
* Run ts2swift for a single input file (programmatic API, no process I/O).
* @param {string} filePath - Path to the .d.ts file
* @param {{ tsconfigPath: string, logLevel?: string, globalFiles?: string[] }} options
* @returns {string} Generated Swift source
* @throws {Error} on parse/type-check errors (diagnostics are included in the message)
*/
export function run(filePath, options) {
const { tsconfigPath, logLevel = 'info', globalFiles: globalFilesOpt = [] } = options;
const globalFiles = Array.isArray(globalFilesOpt) ? globalFilesOpt : (globalFilesOpt ? [globalFilesOpt] : []);
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([filePath, ...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 [filePath, ...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',
},
},
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 logLevel = options.values["log-level"] || "info";
/** @type {string[]} */
const globalFiles = Array.isArray(options.values.global)
? options.values.global
: (options.values.global ? [options.values.global] : []);
const diagnosticEngine = new DiagnosticEngine(logLevel);
diagnosticEngine.print("verbose", `Processing ${filePath}...`);
let swiftOutput;
try {
swiftOutput = run(filePath, { tsconfigPath, logLevel, globalFiles });
} catch (err) {
console.error(err.message);
process.exit(1);
}
if (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");
}
}