forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
185 lines (167 loc) · 5.5 KB
/
cli.js
File metadata and controls
185 lines (167 loc) · 5.5 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
// @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]');
}
/**
* 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',
},
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 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);
}
/** @type {string[]} */
const globalFiles = Array.isArray(options.values.global)
? options.values.global
: (options.values.global ? [options.values.global] : []);
const program = TypeProcessor.createProgram([filePath, ...globalFiles], configParseResult.options);
const diagnostics = program.getSemanticDiagnostics();
if (diagnostics.length > 0) {
diagnosticEngine.tsDiagnose(diagnostics);
process.exit(1);
}
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(Experimental) @_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;
const swiftOutput = hasAny ? prelude + bodies.join("\n\n") + "\n" : "";
if (options.values.output) {
if (hasAny) {
fs.mkdirSync(path.dirname(options.values.output), { recursive: true });
fs.writeFileSync(options.values.output, swiftOutput);
}
} else {
process.stdout.write(swiftOutput, "utf-8");
}
}