-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathapp.js
More file actions
165 lines (141 loc) · 5.66 KB
/
app.js
File metadata and controls
165 lines (141 loc) · 5.66 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
// BridgeJS Playground Main Application
import { EditorSystem } from './editor.js';
import ts from 'typescript';
import { TypeProcessor } from './processor.js';
export class BridgeJSPlayground {
constructor() {
this.editorSystem = new EditorSystem();
this.playBridgeJS = null;
this.generateTimeout = null;
this.isInitialized = false;
// DOM Elements
this.errorDisplay = document.getElementById('errorDisplay');
this.errorMessage = document.getElementById('errorMessage');
}
// Initialize the application
async initialize() {
if (this.isInitialized) {
return;
}
try {
// Initialize editor system
await this.editorSystem.init();
// Initialize BridgeJS
await this.initializeBridgeJS();
// Set up event listeners
this.setupEventListeners();
// Load sample code
this.editorSystem.loadSampleCode();
this.isInitialized = true;
console.log('BridgeJS Playground initialized successfully');
} catch (error) {
console.error('Failed to initialize BridgeJS Playground:', error);
this.showError('Failed to initialize application: ' + error.message);
}
}
// Initialize BridgeJS
async initializeBridgeJS() {
try {
// Import the BridgeJS module
const { init } = await import("../../.build/plugins/PackageToJS/outputs/Package/index.js");
const { exports } = await init({
imports: {
createTS2Skeleton: this.createTS2Skeleton
}
});
this.playBridgeJS = new exports.PlayBridgeJS();
console.log('BridgeJS initialized successfully');
} catch (error) {
console.error('Failed to initialize BridgeJS:', error);
throw new Error('BridgeJS initialization failed: ' + error.message);
}
}
// Set up event listeners
setupEventListeners() {
// Add change listeners for real-time generation
this.editorSystem.addChangeListeners(() => {
// Debounce generation to avoid excessive calls
if (this.generateTimeout) {
clearTimeout(this.generateTimeout);
}
this.generateTimeout = setTimeout(() => this.generateCode(), 300);
});
}
createTS2Skeleton() {
return {
convert: (dtsCode) => {
const virtualFilePath = "bridge-js.d.ts"
const virtualHost = {
fileExists: fileName => fileName === virtualFilePath,
readFile: fileName => dtsCode,
getSourceFile: (fileName, languageVersion) => {
const sourceText = dtsCode;
if (sourceText === undefined) return undefined;
return ts.createSourceFile(fileName, sourceText, languageVersion);
},
getDefaultLibFileName: options => "lib.d.ts",
writeFile: (fileName, data) => {
console.log(`[emit] ${fileName}:\n${data}`);
},
getCurrentDirectory: () => "",
getDirectories: () => [],
getCanonicalFileName: fileName => fileName,
getNewLine: () => "\n",
useCaseSensitiveFileNames: () => true
}
// Create TypeScript program from d.ts content
const tsProgram = ts.createProgram({
rootNames: [virtualFilePath],
host: virtualHost,
options: {
noEmit: true,
declaration: true,
}
})
// Create diagnostic engine for error reporting
const diagnosticEngine = {
print: (level, message, node) => {
console.log(`[${level}] ${message}`);
if (level === 'error') {
this.showError(`TypeScript Error: ${message}`);
}
}
};
// Process the TypeScript definitions to generate skeleton
const processor = new TypeProcessor(tsProgram.getTypeChecker(), diagnosticEngine);
const skeleton = processor.processTypeDeclarations(tsProgram, virtualFilePath);
return JSON.stringify(skeleton);
}
}
}
// Generate code through BridgeJS
async generateCode() {
if (!this.playBridgeJS) {
this.showError('BridgeJS is not initialized');
return;
}
try {
this.hideError();
const inputs = this.editorSystem.getInputs();
const swiftCode = inputs.swift;
const dtsCode = inputs.dts;
// Process the code and get PlayBridgeJSOutput
const result = this.playBridgeJS.update(swiftCode, dtsCode);
// Update outputs using the PlayBridgeJSOutput object
this.editorSystem.updateOutputs(result);
console.log('Code generated successfully');
} catch (error) {
console.error('Error generating code:', error);
this.showError('Error generating code: ' + error.message);
}
}
// Show error message
showError(message) {
this.errorMessage.textContent = message;
this.errorDisplay.classList.add('show');
}
// Hide error message
hideError() {
this.errorDisplay.classList.remove('show');
}
}