-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
executable file
·509 lines (455 loc) · 15.3 KB
/
index.ts
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#!/usr/bin/env ts-node
import chalk from "chalk";
import { execSync } from "child_process";
import chokidar from "chokidar";
import { CleanWebpackPlugin } from "clean-webpack-plugin";
import crypto from "crypto";
import fs from "fs";
import got from "got"
import handler from "serve-handler";
import http from "http";
import path from "path";
import Progress from "progress";
import stream from "stream";
import tar from "tar";
import { promisify } from 'util';
import webpack from "webpack";
import webpackDevServer from "webpack-dev-server";
import WasmPackPlugin from "@wasm-tool/wasm-pack-plugin";
const HtmlPlugin = require("html-webpack-plugin");
const pipeline = promisify(stream.pipeline);
const HOME: string = `
import { Center, StatefulWidget, Text } from "calling-elvis";
export default class Index extends StatefulWidget {
render() {
return Center(
Text("Is anybody home?", {
bold: true,
italic: true,
size: 6,
})
);
}
}
`.slice(1);
enum Logger {
Done,
Error,
Info,
Wait,
}
interface IElvisPluginOptions {
home?: string;
pages?: string;
ssr?: boolean;
title?: string;
}
function log(text: string, ty?: Logger): void {
let status = "";
switch (ty) {
case Logger.Done:
status = chalk.greenBright("done");
break;
case Logger.Error:
status = chalk.red("error");
break;
case Logger.Wait:
status = chalk.cyan("wait");
break;
default:
status = chalk.dim(chalk.cyan("info"));
break;
}
console.log(`[ ${status} ] ${text}`);
}
function getPackageJson(): any {
let conf: any = {};
const pj = path.resolve(__dirname, "./package.json");
if (fs.existsSync(pj)) {
conf = require(pj);
} else {
conf = require(path.resolve(__dirname, "../package.json"));
}
return conf;
}
class ElvisPlugin {
public static autoOpts(root: string): IElvisPluginOptions {
let opts: IElvisPluginOptions = {
home: "index",
pages: "pages",
ssr: false,
title: "Calling Elvis!",
}
// locate pages entry
const pagesDir = path.resolve(root, opts.pages);
if (fs.existsSync(pagesDir)) {
if (!fs.statSync(pagesDir).isDirectory()) {
log(`${pagesDir} is not a directory`, Logger.Error);
process.exit(1);
}
} else {
fs.mkdirSync(pagesDir);
}
// generate home
const pages = ElvisPlugin.getPages(
fs.readdirSync(pagesDir)
).map((f) => f.slice(0, f.lastIndexOf(".")));
if (pages.indexOf("index") > -1) {
opts.home = "index";
} else if (pages.indexOf("home") > -1) {
opts.home = "home";
} else if (pages.length > 0) {
opts.home = pages[0];
} else {
log("write index to disk ...");
fs.writeFileSync(
path.resolve(pagesDir, "index.js"),
HOME,
);
}
return opts;
}
public static getPages(files?: string[]): string[] {
return files.filter((s) => {
return (s.indexOf(".elvis.js") < 0) && (s.endsWith(".js") || s.endsWith(".ts"));
});
}
public static locate(cur: string): string {
if (cur === "/") {
return "";
}
const files: string[] = fs.readdirSync(cur);
if (files.indexOf(".elvis.js") === -1) {
return ElvisPlugin.locate(path.resolve(cur, ".."));
}
return cur;
}
public static ref(path: string): string {
return crypto.createHmac(
"md5", "elvis-md5"
).update(
fs.readFileSync(path)
).digest("hex");
}
options: IElvisPluginOptions;
ptr: string;
root: string;
constructor(options?: IElvisPluginOptions) {
this.initOpts(options);
}
// apply plugin to webpack
public apply(compiler: webpack.Compiler): void {
compiler.hooks.afterPlugins.tap("elvis-webpack-plugin", (compiler) => {
this.patchOptsToCompiler(compiler);
});
compiler.hooks.done.tap("elvis-webpack-plugin", () => {
log("compiled successfully", Logger.Done);
});
}
private initOpts(options: IElvisPluginOptions): void {
const cwd = process.cwd();
// locate root path
let root = ElvisPlugin.locate(cwd);
if (root === "") {
root = cwd;
this.root = cwd;
} else {
this.root = root;
}
// init options from plugn entry
this.options = Object.assign(
ElvisPlugin.autoOpts(root),
options
);
// update local configurations
//
// wirite .elvis.js to disk if not exists
const conf = path.resolve(this.root, ".elvis.js");
if (fs.existsSync(conf)) {
this.options = Object.assign(this.options, import(conf));
} else {
const opts = JSON.stringify(this.options, null, 2);
fs.writeFileSync(conf, [
"/* elvis config file */",
`module.exports = ${opts}`,
].join("\n"));
}
}
private patchOptsToCompiler(compiler: webpack.Compiler): void {
// patch for HTMLWebpackPlugin
let plugins = compiler.options.plugins;
for (const i in plugins) {
if (plugins[i] instanceof HtmlPlugin) {
type HtmlWebpackPlugin = typeof HtmlPlugin;
let hp: HtmlWebpackPlugin = plugins[i];
hp.options.title = this.options.title;
}
}
// spa-or-ssr adapter
const calling = path.resolve(this.root, ".etc/calling.js");
const pagesDir = path.resolve(this.root, this.options.pages);
const home = this.options.home[0].toUpperCase() + this.options.home.slice(1);
if (!this.options.ssr) {
// update spa router
this.updateSPARouter(calling, home, pagesDir);
// check if is devServer
if (compiler.options.mode === "production") {
return;
}
// patch historyApiFallback
let webpackOpts = compiler.options;
if (!webpackOpts.devServer.historyApiFallback) {
webpackOpts.devServer.historyApiFallback = true;
}
// watch pages dir changes
chokidar.watch(pagesDir, {
ignored: (path: string, stats: fs.Stats) => {
if (path === pagesDir) {
return false;
} else if (stats != undefined && stats.isDirectory()) {
return true;
}
return /(?<!\.js|\.ts)$/.test(path);
},
ignoreInitial: true,
persistent: true,
})
.on("add", (file) => {
let name = path.basename(file).trim();
log(`add page ${chalk.cyan(name)}`, Logger.Info);
this.updateSPARouter(calling, home, pagesDir);
})
.on("unlink", (file) => {
let name = path.basename(file).trim();
log(`unlink page ${chalk.cyan(name)}`, Logger.Info);
this.updateSPARouter(calling, home, pagesDir);
});
} else {
// TODO: patch entry into ssr mode
log("not support ssr for now", Logger.Error);
process.exit(1);
}
}
// single page application handler
private updateSPARouter(
calling: string,
home: string,
pagesDir: string,
): void {
const pages = fs.readdirSync(pagesDir);
let lines = [
"/* elvis spa caller */",
`import { Router, Elvis } from "calling-elvis"`,
ElvisPlugin.getPages(pages).map((page) => {
let widget = page[0].toUpperCase() + page.slice(1);
let relative = path.relative(path.dirname(calling), `${pagesDir}/${page}`);
relative = relative.slice(0, relative.lastIndexOf("."));
return `import ${widget.slice(0, widget.lastIndexOf("."))} from "${relative}";`;
}).join("\n"),
"\nnew Elvis({",
` home: new ${home}(),`,
" router: new Router({",
ElvisPlugin.getPages(pages).map((page) => {
if (page.indexOf(home.toLowerCase()) < 0) {
page = page.slice(0, page.lastIndexOf("."));
let widget = page[0].toUpperCase() + page.slice(1);
return ` "${page}": new ${widget}(),`;
}
}).join("\n"),
" }),",
"}).calling();",
].join("\n");
if (!fs.existsSync(calling) || (crypto.createHmac(
"md5", "elvis-md5"
).update(lines).digest("hex") != ElvisPlugin.ref(calling))) {
fs.writeFileSync(calling, lines);
}
}
}
enum Mode {
Dev,
Ori,
Pro,
}
/* simple cli program */
class Program {
/* webpack configs */
static pack(mode: Mode): void {
let root = ElvisPlugin.locate(process.cwd());
if (root === "") {
root = process.cwd();
}
const etc = path.resolve(root, ".etc");
const bootstrap = path.resolve(etc, "bootstrap.js");
if (!fs.existsSync(bootstrap)) {
if (!fs.existsSync(etc)) {
fs.mkdirSync(etc);
}
log("bootstrap elvis ...");
fs.writeFileSync(bootstrap, `import("./calling");`);
}
let webpackMode = "development";
if (mode === Mode.Pro) {
webpackMode = "production";
}
let config: any = {
devServer: {
hot: true,
port: 1439,
noInfo: true,
},
devtool: "inline-source-map",
entry: bootstrap,
mode: webpackMode,
output: {
filename: "elvis.bundle.js",
path: path.resolve(process.cwd(), ".elvis"),
},
module: {
rules: [
{ test: /\.tsx?$/, loader: "ts-loader" }
]
},
plugins: [
new HtmlPlugin(),
new ElvisPlugin(),
new CleanWebpackPlugin(),
],
resolve: {
extensions: [".ts", ".js", ".wasm"],
},
watch: true,
};
// original mode
if (mode === Mode.Ori) {
config.plugins.push(new WasmPackPlugin({
crateDirectory: path.resolve(__dirname, "../../../elvis/web"),
}));
}
// webpack
const compiler = webpack(config);
// check mode
if (mode !== Mode.Pro) {
log("starting development server ...", Logger.Wait);
const server = new webpackDevServer(compiler, config.devServer);
server.listen(config.devServer.port, "127.0.0.1", (err) => {
if (err != null) {
log(`start server failed.`, Logger.Error);
process.exit(1);
}
log("waiting on http://localhost:1439 ...", Logger.Info);
});
} else {
let packageJson = getPackageJson();
log(`building ${packageJson.name} ...`, Logger.Wait);
compiler.run((err) => {
if (err != null) {
log(`compile failed.`, Logger.Error);
process.exit(1);
}
log(`${packageJson.name} has been packed at ${chalk.underline.cyan(config.output.path)}.`, Logger.Done);
});
}
}
static start(): void {
const http = require('http');
const dist = path.resolve(process.cwd(), ".elvis");
const conf = getPackageJson();
const server = http.createServer((request: http.IncomingMessage, response: http.ServerResponse) => {
return handler(request, response, {
"cleanUrls": true,
"public": dist,
});
})
server.listen(1439, () => {
log(`${conf.name} is firing at ${chalk.underline.cyan("http://localhost:1439")} ...`, Logger.Wait);
});
}
static async docs(): Promise<void> {
const home = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
const docsDir = path.resolve(home, ".elvis/docs");
if (fs.existsSync(path.resolve(docsDir, "index.html"))) {
try {
execSync(`open ${path.resolve(docsDir, "index.html")}`);
process.exit(0);
} catch (_) {
log("open docs filed", Logger.Error);
process.exit(1);
}
}
if (!fs.existsSync(path.resolve(home, ".elvis"))) {
fs.mkdirSync(path.resolve(home, ".elvis"));
} else if (fs.existsSync(docsDir)) {
fs.rmdirSync(docsDir);
}
fs.mkdirSync(docsDir);
let bar = new Progress(`[ ${chalk.cyan("wait")} ] [:bar] :rate/bps :etas`, {
complete: '=',
incomplete: ' ',
width: 42,
total: 1,
});
let prePercent = 0;
await pipeline(
got.stream("https://elvis-1253442844.cos.ap-hongkong.myqcloud.com/docs.tar.gz")
.on('request', (_) => {
log("downloading the elvis book ...", Logger.Wait);
})
.on("downloadProgress", (progress) => {
bar.tick(progress.percent - prePercent);
prePercent = progress.percent;
}),
tar.x({ cwd: docsDir, strip: 1 }),
).catch((e) => {
log(`download failed: ${e.toString()}`, Logger.Error);
process.exit(1);
});
execSync(`open ${path.resolve(docsDir, "index.html")}`)
}
static run(): void {
const argv = process.argv;
if (argv.length == 2) {
Program.help();
process.exit(0);
}
switch (argv[2].trim()) {
case "dev":
Program.pack(Mode.Dev);
break;
case "docs":
Program.docs();
break;
case "build":
Program.pack(Mode.Pro);
break;
case "ori":
Program.pack(Mode.Ori);
break;
case "start":
Program.start()
break;
default:
Program.help();
break;
}
}
static help() {
const conf: any = getPackageJson();
console.log([
`${conf.name} ${conf.version}\n`,
`${conf.description}\n\n`,
"USAGE: \n",
" elvis [SUBCOMMANND]\n\n",
"SUBCOMMANDS: \n",
" dev Calling Elvis!\n",
" docs Open The Elvis Book!\n",
" build Build your satellite!\n",
" start Launch your project to Mars!\n",
" help Prints this Message!"
].join(""));
}
}
// main
(function() {
Program.run();
})();