x4js 2.0.15 → 2.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/x4.css +1 -1
- package/lib/cjs/x4.js +2 -2
- package/lib/esm/x4.css +1 -1
- package/lib/esm/x4.mjs +2 -2
- package/lib/src/components/components.ts +2 -1
- package/lib/src/core/component.ts +0 -9
- package/lib/src/x4.ts +4 -0
- package/lib/styles/x4.css +1 -1
- package/lib/types/x4js.d.ts +294 -193
- package/package.json +18 -28
- package/src/components/canvas/canvas.ts +1 -1
- package/src/components/components.ts +3 -1
- package/src/core/component.ts +0 -9
- package/src/x4.ts +4 -0
- package/.vscode/launch.json +0 -14
- package/demo/assets/house-light.svg +0 -1
- package/demo/assets/radio.svg +0 -4
- package/demo/index.html +0 -12
- package/demo/main.scss +0 -23
- package/demo/main.ts +0 -324
- package/demo/package.json +0 -26
- package/demo/scss.d.ts +0 -4
- package/demo/svg.d.ts +0 -1
- package/demo/tsconfig.json +0 -14
- package/scripts/build.mjs +0 -378
- package/scripts/prepack.mjs +0 -346
- package/tsconfig.json +0 -14
package/scripts/build.mjs
DELETED
|
@@ -1,378 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @file build.mjs
|
|
5
|
-
* @author Etienne Cochard
|
|
6
|
-
* @copyright (c) 2024 R-libre ingenierie, all rights reserved.
|
|
7
|
-
* @version 1.2
|
|
8
|
-
**/
|
|
9
|
-
|
|
10
|
-
import * as fs from "node:fs"
|
|
11
|
-
import * as path from "node:path"
|
|
12
|
-
import * as url from "node:url"
|
|
13
|
-
import * as http from "node:http"
|
|
14
|
-
|
|
15
|
-
import chalk from "chalk"
|
|
16
|
-
import esbuild from "esbuild"
|
|
17
|
-
import { WebSocketServer } from 'ws';
|
|
18
|
-
import Watcher from "watcher"
|
|
19
|
-
|
|
20
|
-
import { sassPlugin } from "esbuild-sass-plugin";
|
|
21
|
-
import { dtsPlugin } from "esbuild-plugin-d.ts";
|
|
22
|
-
//import { hostname } from 'node:os'
|
|
23
|
-
|
|
24
|
-
console.log( chalk.green("\n\n-- X4Build 1.2 -------------------") );
|
|
25
|
-
|
|
26
|
-
const releaseMode = process.argv.some(a => a == "--release")
|
|
27
|
-
const hmrMode = process.argv.some(a => a == "--hmr") // hot module replacement
|
|
28
|
-
const watchMode = process.argv.some(a => a == "--watch" ) // watch modifications
|
|
29
|
-
const serveMode = process.argv.some(a => a == "--serve") // watch modifications
|
|
30
|
-
const cjsMode = process.argv.some(a => a == "--cjs") // output mode
|
|
31
|
-
const dtsMode = process.argv.some(a => a == "--dts") // generate .d.ts
|
|
32
|
-
|
|
33
|
-
// forced outdir in command line
|
|
34
|
-
let outDir = process.argv.find( a => a.startsWith("--outdir=") );
|
|
35
|
-
if( outDir ) {
|
|
36
|
-
outDir = outDir.substring(9);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
console.log( "outdir =", outDir );
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
function readPackage() {
|
|
43
|
-
|
|
44
|
-
let cpath = process.cwd();
|
|
45
|
-
|
|
46
|
-
// check here
|
|
47
|
-
let pth = path.join(cpath, "package.json");
|
|
48
|
-
if (fs.existsSync(pth)) {
|
|
49
|
-
const raw = fs.readFileSync(pth, "utf-8");
|
|
50
|
-
return JSON.parse(raw);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
throw new Error("cannot find package.json");
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const def_settings = {
|
|
57
|
-
"outdir": "./bin",
|
|
58
|
-
"copy": [],
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
const pkg_settings = readPackage();
|
|
62
|
-
const settings = { ...def_settings, ...pkg_settings.x4build };
|
|
63
|
-
|
|
64
|
-
if( !settings.entryPoints ) {
|
|
65
|
-
settings.entryPoints = [pkg_settings.main];
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if( outDir ) {
|
|
69
|
-
settings.outdir = outDir;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
console.log( chalk.green(" release mode ..", releaseMode ) );
|
|
73
|
-
console.log( chalk.green(" hmr ...........", hmrMode ) );
|
|
74
|
-
console.log( chalk.green(" watching ......", watchMode ) );
|
|
75
|
-
console.log( chalk.green(" serve files ...", serveMode ) );
|
|
76
|
-
console.log( chalk.green(" output dir ....", settings.outdir ) );
|
|
77
|
-
console.log( chalk.green(" entry point ...", settings.entryPoints ) );
|
|
78
|
-
console.log( chalk.green(" generate .d.ts.", dtsMode ) );
|
|
79
|
-
console.log( chalk.green(" copying .......", settings.copy ) );
|
|
80
|
-
|
|
81
|
-
console.log( chalk.green("----------------------------------") );
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const js_hmr = ` // X4 Hot Module Replacement v1.1
|
|
87
|
-
{
|
|
88
|
-
const ws = new WebSocket( "ws://127.0.0.1:9998", "hmr" );
|
|
89
|
-
|
|
90
|
-
ws.onmessage = ( ev ) => {
|
|
91
|
-
if( ev.data=="reload-css" ) {
|
|
92
|
-
const gen_id = Date.now( );
|
|
93
|
-
document.querySelectorAll( "link[rel=stylesheet]").forEach( link => {
|
|
94
|
-
link.href = link.href.replace(/\\?.*|$/, "?" + gen_id)
|
|
95
|
-
} );
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
location.reload();
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}`
|
|
102
|
-
|
|
103
|
-
/** custom plugin to copy element after the build */
|
|
104
|
-
|
|
105
|
-
const post_plugin = {
|
|
106
|
-
name: 'post-cmd',
|
|
107
|
-
setup(build) {
|
|
108
|
-
|
|
109
|
-
build.onEnd(result => {
|
|
110
|
-
if (result.errors.length) {
|
|
111
|
-
console.error(`build ended with ${result.errors.length} errors`)
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
settings.copy?.forEach((desc) => {
|
|
115
|
-
const { from, to } = desc;
|
|
116
|
-
|
|
117
|
-
if (fs.existsSync(from)) {
|
|
118
|
-
fs.cpSync(from, path.join(settings.outdir, to), { recursive: true });
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
})
|
|
123
|
-
},
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
let buildcnt = 1;
|
|
127
|
-
|
|
128
|
-
const other_plugins = [];
|
|
129
|
-
|
|
130
|
-
if( dtsMode ) {
|
|
131
|
-
other_plugins.push( dtsPlugin( {
|
|
132
|
-
}) );
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
async function build() {
|
|
136
|
-
|
|
137
|
-
console.log(chalk.cyan(`building (${buildcnt++})...`));
|
|
138
|
-
|
|
139
|
-
try {
|
|
140
|
-
/** @type {esbuild.BuildOptions} */
|
|
141
|
-
const options = {
|
|
142
|
-
target: "node12",
|
|
143
|
-
logLevel: "error",
|
|
144
|
-
entryPoints: settings.entryPoints,
|
|
145
|
-
bundle: true,
|
|
146
|
-
charset: "utf8",
|
|
147
|
-
outdir: settings.outdir,
|
|
148
|
-
keepNames: true,
|
|
149
|
-
platform: "browser",
|
|
150
|
-
format: cjsMode ? "cjs" : "esm",
|
|
151
|
-
minify: releaseMode,
|
|
152
|
-
plugins: [
|
|
153
|
-
sassPlugin( {
|
|
154
|
-
type: "css",
|
|
155
|
-
filter: /\.scss$/,
|
|
156
|
-
}),
|
|
157
|
-
post_plugin,
|
|
158
|
-
...other_plugins,
|
|
159
|
-
],
|
|
160
|
-
assetNames: "assets/[name]-[hash]",
|
|
161
|
-
loader: {
|
|
162
|
-
".svg": "dataurl",
|
|
163
|
-
".jpg": "file",
|
|
164
|
-
".png": "file",
|
|
165
|
-
".woff": "file",
|
|
166
|
-
".woff2": "file",
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
if (!releaseMode) {
|
|
171
|
-
options.sourcemap = "inline";
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
if (hmrMode) {
|
|
175
|
-
options.banner = { js: js_hmr }
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
await esbuild.build(options);
|
|
179
|
-
}
|
|
180
|
-
catch (e) {
|
|
181
|
-
console.error(chalk.bgRed.white("build failure, waiting for correction"));
|
|
182
|
-
console.log(e.message);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// :: WEBSOCKET SERVER FOR HRM ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
|
187
|
-
|
|
188
|
-
if (watchMode) {
|
|
189
|
-
|
|
190
|
-
const wsServer = new WebSocketServer({
|
|
191
|
-
port: 9998
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
// all connected clients (1)
|
|
195
|
-
const clients = [];
|
|
196
|
-
|
|
197
|
-
wsServer.on('connection', (ws) => {
|
|
198
|
-
clients.push(ws);
|
|
199
|
-
|
|
200
|
-
wsServer.on('error', (err) => {
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
wsServer.on('message', function message(data) {
|
|
204
|
-
console.log('received: %s', data);
|
|
205
|
-
});
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
const sendClientMessage = (msg) => {
|
|
209
|
-
clients.forEach(ws => {
|
|
210
|
-
ws.send(msg);
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// file watcher ------------------------------------------
|
|
215
|
-
const selfPath = url.fileURLToPath(import.meta.url);
|
|
216
|
-
const cPath = process.cwd();
|
|
217
|
-
|
|
218
|
-
const watcher = new Watcher("./", {
|
|
219
|
-
recursive: true,
|
|
220
|
-
ignoreInitial: true,
|
|
221
|
-
ignore: (targetPath) => {
|
|
222
|
-
if (targetPath.startsWith(cPath)) {
|
|
223
|
-
targetPath = path.join(".", targetPath.substring(cPath.length));
|
|
224
|
-
targetPath = path.normalize(targetPath);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
if (targetPath.startsWith(settings.outdir) || targetPath.startsWith("node_modules") ) {
|
|
228
|
-
//console.log("skip watch", targetPath);
|
|
229
|
-
return true;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
return false;
|
|
233
|
-
}
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
watcher.on('all', async (event, targetPath, targetPathNext) => {
|
|
238
|
-
|
|
239
|
-
if (event == "change" || event == "add") {
|
|
240
|
-
// one of our copy path ?
|
|
241
|
-
if (targetPath.startsWith(cPath)) {
|
|
242
|
-
let pth = "." + targetPath.substring(cPath.length);
|
|
243
|
-
pth = path.normalize(pth);
|
|
244
|
-
|
|
245
|
-
//console.log( targetPath );
|
|
246
|
-
if (settings.copy?.some(desc => {
|
|
247
|
-
const from = path.normalize(desc.from);
|
|
248
|
-
if (pth == from || pth.startsWith(from)) {
|
|
249
|
-
return true;
|
|
250
|
-
}
|
|
251
|
-
})) {
|
|
252
|
-
await build();
|
|
253
|
-
return;
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
if (event == "change") {
|
|
259
|
-
// somebody changed this file, so leave
|
|
260
|
-
if (targetPath == selfPath) {
|
|
261
|
-
process.exit(0);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// depending of the extension, we send the correct type of refresh
|
|
265
|
-
switch (path.extname(targetPath)) {
|
|
266
|
-
case ".svg":
|
|
267
|
-
case ".css":
|
|
268
|
-
case ".scss": {
|
|
269
|
-
await build();
|
|
270
|
-
sendClientMessage("reload-css");
|
|
271
|
-
break;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
case ".html":
|
|
275
|
-
case ".ts":
|
|
276
|
-
case ".js":
|
|
277
|
-
case ".jsx":
|
|
278
|
-
case ".tsx": {
|
|
279
|
-
await build();
|
|
280
|
-
sendClientMessage("reload-js");
|
|
281
|
-
break;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
case ".json": {
|
|
285
|
-
if (path.basename(targetPath) == 'package.json') {
|
|
286
|
-
build();
|
|
287
|
-
}
|
|
288
|
-
break;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
//console.log ( event ); // => could be any target event: 'add', 'addDir', 'change', 'rename', 'renameDir', 'unlink' or 'unlinkDir'
|
|
293
|
-
//console.log ( targetPath ); // => the file system path where the event took place, this is always provided
|
|
294
|
-
}
|
|
295
|
-
});
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
// :: WEB SERVER ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
|
299
|
-
|
|
300
|
-
function serve() {
|
|
301
|
-
|
|
302
|
-
// maps file extention to MIME types
|
|
303
|
-
// full list can be found here: https://www.freeformatter.com/mime-types-list.html
|
|
304
|
-
const mimeType = {
|
|
305
|
-
'.ico': 'image/x-icon',
|
|
306
|
-
'.html': 'text/html',
|
|
307
|
-
'.js': 'text/javascript',
|
|
308
|
-
'.json': 'application/json',
|
|
309
|
-
'.css': 'text/css',
|
|
310
|
-
'.png': 'image/png',
|
|
311
|
-
'.jpg': 'image/jpeg',
|
|
312
|
-
'.wav': 'audio/wav',
|
|
313
|
-
'.mp3': 'audio/mpeg',
|
|
314
|
-
'.svg': 'image/svg+xml',
|
|
315
|
-
'.pdf': 'application/pdf',
|
|
316
|
-
'.zip': 'application/zip',
|
|
317
|
-
'.doc': 'application/msword',
|
|
318
|
-
'.eot': 'application/vnd.ms-fontobject',
|
|
319
|
-
'.ttf': 'application/x-font-ttf',
|
|
320
|
-
};
|
|
321
|
-
|
|
322
|
-
const server = http.createServer( {
|
|
323
|
-
hostname: "127.0.0.1"
|
|
324
|
-
});
|
|
325
|
-
|
|
326
|
-
server.on('request', (req, res) => {
|
|
327
|
-
//console.log(`${req.method} ${req.url}`);
|
|
328
|
-
|
|
329
|
-
// parse URL
|
|
330
|
-
const parsedUrl = url.parse(req.url);
|
|
331
|
-
|
|
332
|
-
// extract URL path
|
|
333
|
-
// Avoid https://en.wikipedia.org/wiki/Directory_traversal_attack
|
|
334
|
-
// e.g curl --path-as-is http://localhost:9000/../fileInDanger.txt
|
|
335
|
-
// by limiting the path to current directory only
|
|
336
|
-
const sanitizePath = path.normalize(parsedUrl.pathname).replace(/^(\.\.[\/\\])+/, '');
|
|
337
|
-
let pathname = path.join(settings.outdir, sanitizePath);
|
|
338
|
-
|
|
339
|
-
if( fs.existsSync(pathname) ) {
|
|
340
|
-
// if is a directory, then look for index.html
|
|
341
|
-
if (fs.statSync(pathname).isDirectory()) {
|
|
342
|
-
pathname = path.join( pathname, 'index.html' );
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
// read file from file system
|
|
346
|
-
fs.readFile(pathname, function (err, data) {
|
|
347
|
-
if (err) {
|
|
348
|
-
res.statusCode = 500;
|
|
349
|
-
res.end(`Error getting the file: ${err}.`);
|
|
350
|
-
}
|
|
351
|
-
else {
|
|
352
|
-
// based on the URL path, extract the file extention. e.g. .js, .doc, ...
|
|
353
|
-
const ext = path.parse(pathname).ext;
|
|
354
|
-
// if the file is found, set Content-type and send data
|
|
355
|
-
res.setHeader('Content-type', mimeType[ext] || 'text/plain');
|
|
356
|
-
res.end(data);
|
|
357
|
-
}
|
|
358
|
-
});
|
|
359
|
-
}
|
|
360
|
-
else {
|
|
361
|
-
// if the file is not found, return 404
|
|
362
|
-
res.statusCode = 404;
|
|
363
|
-
res.end(`File ${pathname} not found!`);
|
|
364
|
-
return;
|
|
365
|
-
}
|
|
366
|
-
});
|
|
367
|
-
|
|
368
|
-
server.listen(3000);
|
|
369
|
-
console.log( chalk.yellow("listening on http://127.0.0.1:3000") );
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
build();
|
|
373
|
-
|
|
374
|
-
if( serveMode ) {
|
|
375
|
-
serve();
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
|
package/scripts/prepack.mjs
DELETED
|
@@ -1,346 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import * as fs from 'node:fs';
|
|
4
|
-
import * as path from "node:path"
|
|
5
|
-
import chalk from "chalk"
|
|
6
|
-
import * as ts from 'typescript';
|
|
7
|
-
|
|
8
|
-
import { execSync } from "node:child_process";
|
|
9
|
-
|
|
10
|
-
function readJSON(fname) {
|
|
11
|
-
let cpath = process.cwd();
|
|
12
|
-
|
|
13
|
-
// check here
|
|
14
|
-
let pth = path.join(cpath, fname);
|
|
15
|
-
|
|
16
|
-
if (fs.existsSync(pth)) {
|
|
17
|
-
const raw = fs.readFileSync(pth, "utf-8");
|
|
18
|
-
return JSON.parse(raw);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
throw new Error(`cannot find ${fname}`);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function readPackage() {
|
|
25
|
-
return readJSON("package.json");
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
class DTSGenerator {
|
|
32
|
-
|
|
33
|
-
output = null;
|
|
34
|
-
|
|
35
|
-
emit( lvl, code ) {
|
|
36
|
-
this.output.write( '\t'.repeat(lvl)+code );
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
*
|
|
41
|
-
*/
|
|
42
|
-
|
|
43
|
-
genMethodSig(node, source, ctor) {
|
|
44
|
-
|
|
45
|
-
const name = ctor ? "constructor" : (node.name ? node.name.getText(source) : 'new');
|
|
46
|
-
|
|
47
|
-
const typeParameters = node.typeParameters
|
|
48
|
-
? `<${node.typeParameters.map(param => param.getText(source)).join(', ')}>`
|
|
49
|
-
: '';
|
|
50
|
-
|
|
51
|
-
const parameters = node.parameters?.map(param => {
|
|
52
|
-
const paramName = param.name.getText(source);
|
|
53
|
-
const paramType = param.type ? param.type.getText(source) : 'any';
|
|
54
|
-
return `${paramName}: ${paramType}`;
|
|
55
|
-
}).join(', ');
|
|
56
|
-
|
|
57
|
-
const returnType = ctor ? '' : `: ${node.type ? node.type.getText(source) : 'void'}`;
|
|
58
|
-
|
|
59
|
-
this.emit( 2, `${name}${typeParameters}( ${parameters} )${returnType};\n` );
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
*
|
|
64
|
-
*/
|
|
65
|
-
|
|
66
|
-
genPropSig( node, source ) {
|
|
67
|
-
const name = node.name ? node.name.getText(source) : 'unknown';
|
|
68
|
-
const type = node.type ? node.type.getText(source) : 'any';
|
|
69
|
-
const isOptional = node.questionToken ? '?' : '';
|
|
70
|
-
this.emit( 2, `${name}${isOptional}: ${type};\n` );
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
*
|
|
75
|
-
*/
|
|
76
|
-
|
|
77
|
-
genClassSig(node, source) {
|
|
78
|
-
|
|
79
|
-
const cname = node.name.getText(source);
|
|
80
|
-
const modifiers = node.modifiers ? node.modifiers.map(mod => mod.getText(source)).join(' ') : '';
|
|
81
|
-
|
|
82
|
-
const heritage = node.heritageClauses ? node.heritageClauses.map(clause => {
|
|
83
|
-
const typeText = clause.types.map(type => type.getText(source)).join(', ');
|
|
84
|
-
return clause.token === ts.SyntaxKind.ExtendsKeyword ? `extends ${typeText}` : `implements ${typeText}`;
|
|
85
|
-
}).join(' ') : '';
|
|
86
|
-
|
|
87
|
-
const typeParameters = node.typeParameters ? `<${node.typeParameters.map(param => param.getText(source)).join(', ')}>` : '';
|
|
88
|
-
|
|
89
|
-
this.emit( 1, `${modifiers} class ${cname}${typeParameters} ${heritage} {\n` );
|
|
90
|
-
|
|
91
|
-
ts.forEachChild(node, (x) => {
|
|
92
|
-
|
|
93
|
-
if (ts.isPropertyDeclaration(x)) {
|
|
94
|
-
const name = x.name ? x.name.getText(source) : 'unknown';
|
|
95
|
-
const type = x.type ? x.type.getText(source) : 'any';
|
|
96
|
-
const isOptional = x.questionToken ? '?' : '';
|
|
97
|
-
this.emit( 2, `${name}${isOptional}: ${type};\n` );
|
|
98
|
-
}
|
|
99
|
-
else if (ts.isMethodDeclaration(x)) {
|
|
100
|
-
this.genMethodSig(x, source, false);
|
|
101
|
-
}
|
|
102
|
-
else if (ts.isConstructorDeclaration(x)) {
|
|
103
|
-
this.genMethodSig(x, source, true);
|
|
104
|
-
}
|
|
105
|
-
else if (ts.isPropertySignature(x)) {
|
|
106
|
-
this.genPropSig( x, source );
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
//debugger;
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
this.emit( 1, "}\n\n" );
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
*
|
|
118
|
-
*/
|
|
119
|
-
|
|
120
|
-
genInterfaceSig(node, source) {
|
|
121
|
-
const name = node.name.getText(source);
|
|
122
|
-
const modifiers = node.modifiers ? node.modifiers.map(mod => mod.getText(source)).join(' ') + ' ' : '';
|
|
123
|
-
const heritage = node.heritageClauses ? node.heritageClauses.map(clause => {
|
|
124
|
-
const typeText = clause.types.map(type => type.getText(source)).join(', ');
|
|
125
|
-
return clause.token === ts.SyntaxKind.ExtendsKeyword ? `extends ${typeText}` : `implements ${typeText}`;
|
|
126
|
-
}).join(' ') : '';
|
|
127
|
-
|
|
128
|
-
const typeParameters = this.genTypeParams( node.typeParameters, source );
|
|
129
|
-
|
|
130
|
-
this.emit( 1, `${modifiers}interface ${name}${typeParameters} ${heritage} {\n` );
|
|
131
|
-
|
|
132
|
-
ts.forEachChild(node, (x) => {
|
|
133
|
-
|
|
134
|
-
if (ts.isPropertySignature(x)) {
|
|
135
|
-
const name = x.name ? x.name.getText(source) : 'unknown';
|
|
136
|
-
const type = x.type ? x.type.getText(source) : 'any';
|
|
137
|
-
const isOptional = x.questionToken ? '?' : '';
|
|
138
|
-
this.emit( 2, `${name}${isOptional}: ${type};\n` );
|
|
139
|
-
}
|
|
140
|
-
else if (ts.isMethodSignature(x)) {
|
|
141
|
-
this.genMethodSig(x, source, false);
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
this.emit( 1, "}\n\n" );
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
*
|
|
150
|
-
*/
|
|
151
|
-
|
|
152
|
-
genTypeParams( node, source ) {
|
|
153
|
-
return node ? `<${node.map(param => param.getText(source)).join(', ')}>` : '';
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
*
|
|
158
|
-
*/
|
|
159
|
-
|
|
160
|
-
genTypeDefinition(node, source) {
|
|
161
|
-
const typeName = node.name.getText(source);
|
|
162
|
-
const params = this.genTypeParams( node.typeParameters, source );
|
|
163
|
-
|
|
164
|
-
// pb if complex types
|
|
165
|
-
//const typeDefinition = node.type ? node.type.getText(source) : '';
|
|
166
|
-
|
|
167
|
-
this.emit( 1, `type ${typeName}${params} = ` );
|
|
168
|
-
|
|
169
|
-
if (ts.isTypeLiteralNode(node.type)) {
|
|
170
|
-
this.emit( 0, `{\n` );
|
|
171
|
-
|
|
172
|
-
node.type.members.forEach( member => {
|
|
173
|
-
if( ts.isMethodSignature(member) || ts.isConstructSignatureDeclaration(member) ) {
|
|
174
|
-
this.genMethodSig( member, source );
|
|
175
|
-
}
|
|
176
|
-
else if( ts.isPropertySignature(member) ) {
|
|
177
|
-
this.genPropSig( member, source );
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
this.emit( 1, `}\n\n` );
|
|
182
|
-
}
|
|
183
|
-
else if ( ts.isUnionTypeNode(node.type) || ts.isIntersectionTypeNode(node.type) ) {
|
|
184
|
-
const operator = ts.isUnionTypeNode(node.type) ? ' | ' : ' & ';
|
|
185
|
-
const typeDefinition = node.type.types.map(t => t.getText(source)).join(operator);
|
|
186
|
-
this.emit( 0, `${typeDefinition};\n\n` );
|
|
187
|
-
}
|
|
188
|
-
else if (ts.isArrayTypeNode(node.type)) {
|
|
189
|
-
// Gérer les tableaux
|
|
190
|
-
const typeDefinition = `${node.type.elementType.getText(source)}[]`;
|
|
191
|
-
debugger;
|
|
192
|
-
this.emit( 0, `${typeDefinition};\n\n` );
|
|
193
|
-
}
|
|
194
|
-
else {
|
|
195
|
-
// Pour les autres types (primitifs, tuples, etc.)
|
|
196
|
-
const typeDefinition = node.type.getText(source);
|
|
197
|
-
this.emit( 0, `${typeDefinition};\n\n` );
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
*
|
|
205
|
-
*/
|
|
206
|
-
|
|
207
|
-
genEnumDefinition(node,source) {
|
|
208
|
-
|
|
209
|
-
const ename = node.name.getText(source);
|
|
210
|
-
|
|
211
|
-
this.emit( 1, `enum ${ename} {\n`);
|
|
212
|
-
|
|
213
|
-
node.members.forEach( member => {
|
|
214
|
-
const memberName = member.name.getText(source);
|
|
215
|
-
const memberValue = member.initializer ? ` = ${member.initializer.getText(source)}` : '';
|
|
216
|
-
this.emit( 2, `${memberName}${memberValue},\n` );
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
// Assembler la définition complète
|
|
220
|
-
this.emit( 1, `}\n\n`);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
*
|
|
225
|
-
*/
|
|
226
|
-
|
|
227
|
-
visitNode(node, source) {
|
|
228
|
-
if (ts.isInterfaceDeclaration(node)) {
|
|
229
|
-
this.genInterfaceSig(node, source);
|
|
230
|
-
}
|
|
231
|
-
else if (ts.isClassDeclaration(node)) {
|
|
232
|
-
this.genClassSig(node, source);
|
|
233
|
-
}
|
|
234
|
-
else if (ts.isTypeAliasDeclaration(node)) {
|
|
235
|
-
this.genTypeDefinition(node, source );
|
|
236
|
-
}
|
|
237
|
-
else if (ts.isEnumDeclaration(node)) {
|
|
238
|
-
this.genEnumDefinition(node, source );
|
|
239
|
-
}
|
|
240
|
-
else {
|
|
241
|
-
ts.forEachChild(node, (x) => this.visitNode(x, source));
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* rz=eally quick and dirty dts generator
|
|
247
|
-
*/
|
|
248
|
-
|
|
249
|
-
async build( output_path ) {
|
|
250
|
-
|
|
251
|
-
this.output = fs.createWriteStream(output_path, {});
|
|
252
|
-
|
|
253
|
-
this.emit( 0, `/** \n`);
|
|
254
|
-
this.emit( 0, ` * ___ ___ __\n`);
|
|
255
|
-
this.emit( 0, ` * \\ \\/ / / _\n`);
|
|
256
|
-
this.emit( 0, ` * \\ / /_| |_\n`);
|
|
257
|
-
this.emit( 0, ` * / \\____ _| \n`);
|
|
258
|
-
this.emit( 0, ` * /__/\\__\\ |_|\n`);
|
|
259
|
-
this.emit( 0, ` * \n`);
|
|
260
|
-
this.emit( 0, ` * @author Etienne Cochard \n`);
|
|
261
|
-
this.emit( 0, ` * @copyright (c) 2024 R-libre ingenierie\n`);
|
|
262
|
-
this.emit( 0, ` *\n`);
|
|
263
|
-
this.emit( 0, ` * Use of this source code is governed by an MIT-style license \n`);
|
|
264
|
-
this.emit( 0, ` * that can be found in the LICENSE file or at https://opensource.org/licenses/MIT.\n`);
|
|
265
|
-
this.emit( 0, ` *\n` );
|
|
266
|
-
this.emit( 0, ` * AUTOGENERATED CODE, DO NOT MODIFY\n` );
|
|
267
|
-
this.emit( 0, ` * generated on ${new Date().toDateString()}\n` );
|
|
268
|
-
this.emit( 0, ` */\n\n\n` );
|
|
269
|
-
|
|
270
|
-
this.emit( 0, "declare module 'x4js' {\n\n");
|
|
271
|
-
|
|
272
|
-
const inputFiles = ["src/x4.ts", "src/custom.d.ts"];
|
|
273
|
-
const compilerOptions = readJSON("tsconfig.json");
|
|
274
|
-
|
|
275
|
-
compilerOptions.declaration = true;
|
|
276
|
-
|
|
277
|
-
const host = ts.createCompilerHost(compilerOptions);
|
|
278
|
-
const program = ts.createProgram(inputFiles, compilerOptions, host);
|
|
279
|
-
|
|
280
|
-
const cwd = process.cwd().replaceAll("\\", "/");
|
|
281
|
-
const mod_path = cwd + "/node_modules";
|
|
282
|
-
|
|
283
|
-
program.getSourceFiles().forEach((sourceFile) => {
|
|
284
|
-
|
|
285
|
-
let pth = sourceFile.fileName;
|
|
286
|
-
if (pth.startsWith(mod_path)) {
|
|
287
|
-
return;
|
|
288
|
-
}
|
|
289
|
-
else if( !pth.startsWith(cwd) ) {
|
|
290
|
-
return;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
pth = pth.substring(cwd.length);
|
|
294
|
-
|
|
295
|
-
this.emit(1,`// ---------------------------------------\n` );
|
|
296
|
-
this.emit(1,`// from ${pth}\n\n`);
|
|
297
|
-
|
|
298
|
-
this.visitNode(sourceFile, sourceFile);
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
this.emit(0,"}\n\n");
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
async function main() {
|
|
307
|
-
|
|
308
|
-
const pkg = readPackage();
|
|
309
|
-
|
|
310
|
-
console.log(chalk.yellow("-- prepack ------------------------"))
|
|
311
|
-
|
|
312
|
-
fs.mkdirSync('lib/src', { recursive: true });
|
|
313
|
-
fs.mkdirSync('lib/styles', { recursive: true });
|
|
314
|
-
fs.mkdirSync('lib/types', { recursive: true });
|
|
315
|
-
|
|
316
|
-
console.log(chalk.green("building esm..."));
|
|
317
|
-
execSync("node scripts/build.mjs --release --outdir=lib/esm");
|
|
318
|
-
|
|
319
|
-
console.log(chalk.green("building cjs..."));
|
|
320
|
-
execSync("node scripts/build.mjs --release --cjs --outdir=lib/cjs");
|
|
321
|
-
|
|
322
|
-
console.log(chalk.green("creating .d.ts..."));
|
|
323
|
-
|
|
324
|
-
const generator = new DTSGenerator( );
|
|
325
|
-
await generator.build( "lib/types/x4js.d.ts" );
|
|
326
|
-
|
|
327
|
-
console.log(chalk.green("copying dependencies..."));
|
|
328
|
-
|
|
329
|
-
fs.cpSync('lib/esm/x4.css', 'lib/styles/x4.css');
|
|
330
|
-
fs.renameSync('lib/esm/x4.js', 'lib/esm/x4.mjs');
|
|
331
|
-
|
|
332
|
-
fs.cpSync('src/', 'lib/src/', { recursive: true });
|
|
333
|
-
fs.cpSync('README.md', 'lib/README.txt');
|
|
334
|
-
|
|
335
|
-
console.log(chalk.yellow("-----------------------------------"))
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
main( );
|
|
339
|
-
|
|
340
|
-
//(async function tt( ) {
|
|
341
|
-
//const generator = new DTSGenerator( );
|
|
342
|
-
// await generator.build( "output.d.ts" );
|
|
343
|
-
//})( );
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|