-
Notifications
You must be signed in to change notification settings - Fork 0
/
gemstone-tool-frontend.js
484 lines (452 loc) · 21.9 KB
/
gemstone-tool-frontend.js
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
/*
** GemstoneJS -- Gemstone JavaScript Technology Stack
** Copyright (c) 2016-2019 Gemstone Project <http://gemstonejs.com>
** Licensed under Apache License 2.0 <https://spdx.org/licenses/Apache-2.0>
*/
/* external requirements */
const fs = require("mz/fs")
const path = require("path")
const gemstoneConfig = require("gemstone-config")
const spawn = require("child_process").spawn
const glob = require("glob-promise")
const chalk = require("chalk")
const table = require("table")
const { codeFrameColumns } = require("@babel/code-frame")
const pkg = require("./package.json")
const gemstoneLinterJS = require("gemstone-linter-js")
const gemstoneLinterTS = require("gemstone-linter-ts")
const gemstoneLinterHTML = require("gemstone-linter-html")
const gemstoneLinterCSS = require("gemstone-linter-css")
const gemstoneLinterYAML = require("gemstone-linter-yaml")
const gemstoneLinterJSON = require("gemstone-linter-json")
const Progress = require("progress")
const Chokidar = require("chokidar")
const beep = require("beepbeep")
const notifier = require("node-notifier")
/* generate a table */
const mktable = (data, config = {}) => {
const cfg = Object.assign({}, {
border: table.getBorderCharacters("void"),
columnDefault: { paddingLeft: 0, paddingRight: 1 },
drawJoin: () => false,
drawHorizontalLine: () => false
}, config)
return table.table(data, cfg)
.replace(/^/, " ")
.replace(/\n(.)/g, "\n $1")
}
/* export the Gemstone Tool plugin API */
module.exports = function () {
this.register({
name: "frontend-build",
desc: "Build Gemstone Frontend Application",
opts: [
{ name: "cwd", type: "string", def: ".",
desc: "Change working directory to given path" },
{ name: "debug", type: "boolean", def: false,
desc: "Enable debugging mode" },
{ name: "verbose", type: "boolean", def: false,
desc: "Enable verbose output mode" },
{ name: "watch", type: "boolean", def: false,
desc: "Enable filesystem watching mode" },
{ name: "beep", type: "boolean", def: false,
desc: "Beep terminal after build" },
{ name: "notify", type: "boolean", def: false,
desc: "Notify after error build" },
{ name: "server", type: "boolean", def: false,
desc: "Enable HTTP server mode" },
{ name: "env", type: "string", def: "development",
desc: "Build for target environment (\"production\" or \"development\")" },
{ name: "tag", type: "string", def: "",
desc: "Build for tagged environment (\"\")" }
],
args: [
],
func: async function (opts /*, ...args */) {
/* sanity check options */
if (!opts.env.match(/^(?:production|development)$/))
throw new Error(`invalid environment "${opts.env}"`)
/* display header */
const header = `${chalk.bold("** Gemstone Frontend Build Tool " + pkg.version)}\n` +
"** Copyright (c) 2016-2019 Gemstone Project <http://gemstonejs.com>\n" +
"** Licensed under Apache License 2.0 <https://spdx.org/licenses/Apache-2.0>\n" +
"\n"
process.stderr.write(header)
/* change working directory */
if (opts.cwd !== ".")
process.chdir(opts.cwd)
/* locate Node executable */
const nodeExe = process.execPath
/* locate Webpack CLI */
const webpackCli = require.resolve("webpack-cli/bin/cli.js")
/* locate Webpack configuration generator */
const gwcFile = require.resolve("gemstone-config-webpack")
/* determine Gemstone configuration */
const cfg = gemstoneConfig()
/*
* PASS 1: Linting
*/
const Pass1 = async () => {
process.stderr.write(chalk.bold("++ PASS 1: LINTING\n"))
process.stderr.write(`-- using configuration for ${chalk.bold.green(opts.env)} environment\n`)
process.stderr.write("-- executing linters with Gemstone configuration\n")
/* lint source files */
let progressCur = 0.0
const progressBar = new Progress(` linting: [${chalk.green(":bar")}] ${chalk.bold(":percent")} (elapsed: :elapseds) :msg `, {
complete: "#",
incomplete: "=",
width: 20,
total: 6.0,
stream: process.stderr
})
let rounds = 0
const options = {
verbose: opts.verbose,
env: opts.env,
colors: process.stderr.isTTY,
rules: {},
progress: (fraction, msg) => {
if (msg.length > 40)
msg = msg.substr(0, 40) + "..."
const delta = ((rounds * 1.0) + fraction) - progressCur
progressBar.tick(delta, { msg })
if (progressBar.complete)
process.stderr.write("\n")
progressCur += delta
}
}
const report = {
sources: {},
findings: []
}
let passed = true
const filenames = {}
const doLint = async (ctx, linter, proc, ext) => {
filenames[ctx] = await glob(path.join(cfg.path.source, "**", ext))
passed &= await linter(filenames[ctx],
Object.assign({}, options,
cfg.linting && cfg.linting[proc] ? { rules: cfg.linting[proc] } : {}), report)
rounds++
}
await doLint("JS", gemstoneLinterJS, "eslint", "*.js")
await doLint("TS", gemstoneLinterTS, "tslint", "*.ts")
await doLint("HTML", gemstoneLinterHTML, "htmlhint", "*.html")
await doLint("CSS", gemstoneLinterCSS, "stylelint", "*.css")
await doLint("YAML", gemstoneLinterYAML, "jsyaml", "*.yaml")
await doLint("JSON", gemstoneLinterJSON, "jsonlint", "*.json")
/* report linting results */
const data = [ [
chalk.underline("Source Type"),
chalk.underline("Files"),
chalk.underline("Findings")
] ]
const mkstat = (ctx) => {
const files = filenames[ctx].length
let findings = report.findings
.filter((finding) => finding.ctx === ctx)
.length
if (findings > 0) {
ctx = chalk.bold.red(ctx)
findings = chalk.red(findings)
}
else {
ctx = chalk.bold(ctx)
findings = chalk.green(findings)
}
data.push([ ctx, files, findings ])
}
mkstat("JS")
mkstat("TS")
mkstat("HTML")
mkstat("CSS")
mkstat("YAML")
mkstat("JSON")
process.stderr.write(mktable(data))
process.stderr.write("\n")
/* report linting details */
report.findings
.sort((a, b) => {
let diff = a.filename.localeCompare(b.filename)
if (diff === 0) {
diff = a.line - b.line
if (diff === 0)
diff = a.col - b.col
}
return diff
})
.forEach((finding) => {
const ctx = `[${finding.ctx}]`
let dirname = path.dirname(finding.filename)
if (dirname !== "")
dirname += "/"
const basename = path.basename(finding.filename)
const line = finding.line
const column = finding.column
const message = finding.message
const origin = `[${finding.ruleProc}: ${finding.ruleId}]`
let frame = codeFrameColumns(
report.sources[finding.filename] || "",
{ start: { line: finding.line, column: finding.column } },
{ linesAbove: 2, linesBelow: 2 }
)
frame = frame
.replace(/^(\s*\d+\s+\|)/, (_, m1) =>
chalk.grey(m1))
.replace(/(\n)(\s*\d+\s+\|)/g, (_, m1, m2) =>
m1 + chalk.grey(m2))
.replace(/(\|\s*)(\^)/, (_, m1, m2) =>
chalk.grey(m1) + chalk.bold.red(m2))
.replace(/(\n)(>)(\s*\d+\s+\|)(.*)/, (_, m1, m2, m3, m4) =>
m1 + chalk.bold.red(m2) + chalk.grey(m3) + chalk.green(m4))
const output =
`${chalk.bold.red("ERROR:")} ` +
`${chalk.red("file")} ${chalk.red(dirname)}${chalk.red.bold(basename)}` +
`${chalk.red(", line ")}${chalk.red.bold(line)}${chalk.red(", column ")}${chalk.red.bold(column)}${chalk.red("")} ` +
`${chalk.grey(ctx)}\n` +
` ${chalk.bold(message)} ${chalk.grey(origin)}\n` +
`${frame}\n` +
"\n"
process.stderr.write(output)
})
return passed
}
/*
* PASS 2: Bundling
*/
const Pass2 = async () => {
process.stderr.write(chalk.bold("++ PASS 2: COMPILING\n"))
process.stderr.write(`-- using configuration for ${chalk.bold.green(opts.env)} environment\n`)
/* generate temporary Webpack configuration stub */
const wpcFile = ".gemstone.webpack.js"
const gwcFileEsc = gwcFile.replace(/\\/g, "\\\\")
const wpcData = `module.exports = require("${gwcFileEsc}")({\n` +
` verbose: ${opts.verbose},\n` +
` env: "${opts.env}",\n` +
` tag: "${opts.tag}"\n` +
"})\n"
await fs.writeFile(wpcFile, wpcData, { encoding: "utf8" })
/* spawn Webpack command-line interface */
const stats = await new Promise((resolve, reject) => {
process.stderr.write("-- executing bundler with Gemstone configuration\n")
const wpOpts = [ webpackCli, "--config", wpcFile, "--bail", "--json" ]
if (opts.debug)
wpOpts.push("--debug")
const child = spawn(nodeExe, wpOpts, { stdio: [ "inherit", "pipe", "inherit" ] })
let stdout = ""
child.stdout.on("data", async (data) => {
stdout += data.toString()
})
child.on("close", async (code) => {
await fs.unlink(wpcFile)
let stats
try {
stats = JSON.parse(stdout)
}
catch (ex) {
process.stderr.write(`ERROR: failed to parse JSON output of Webpack:\n${stdout}`)
reject(new Error(`failed to parse JSON output of Webpack:\n${stdout}`))
}
if (code === 0)
resolve(stats)
else
resolve(stats) /* no need to reject */
})
})
process.stderr.write("\n")
/* report Webpack on entries */
if (stats.errors.length === 0) {
const data = [ [
chalk.underline("Entry"),
chalk.underline("Chunks"),
chalk.underline("Assets")
] ]
Object.keys(stats.entrypoints).forEach((name) => {
const entry = stats.entrypoints[name]
data.push([
chalk.bold(name),
entry.chunks.map((chunk) => chalk.green(chunk)).join(", "),
entry.assets.join(", ")
])
})
process.stderr.write(mktable(data))
process.stderr.write("\n")
}
/* report on Webpack chunks */
if (stats.errors.length === 0) {
const data = [ [
chalk.underline("Chunk"),
chalk.underline("Parents"),
chalk.underline("Names"),
chalk.underline("Size"),
chalk.underline("Modules")
] ]
Object.keys(stats.chunks).forEach((name) => {
const chunk = stats.chunks[name]
data.push([
chalk.green(chunk.id),
chunk.parents.map((chunk) => chalk.green(chunk)).join(", "),
chunk.names.join(", "),
chunk.size,
chunk.modules.length
])
})
process.stderr.write(mktable(data))
process.stderr.write("\n")
}
/* report on Webpack modules */
if (stats.errors.length === 0) {
const data = [ [
chalk.underline("Module"),
chalk.underline("Size"),
chalk.underline("Chunks"),
chalk.underline("Depth")
] ]
stats.modules.map((module) => {
let name = module.identifier
name = name.replace(/^.*!/, "")
name = path.relative(process.cwd(), name)
return { name, size: module.size, chunks: module.chunks, depth: module.depth }
}).sort((a, b) => {
if (a.depth !== b.depth)
return a.depth - b.depth
else
a.name.localeCompare(b.name)
}).forEach((module) => {
let name = module.name
if (!opts.verbose && name.match(/^(?:node_modules|bower_components)\//))
return
if (module.errors > 0)
name = chalk.red(name)
else if (module.warnings > 0)
name = chalk.yellow(name)
data.push([
name,
module.size,
module.chunks.map((chunk) => chalk.green(chunk)).join(", "),
module.depth
])
})
process.stderr.write(mktable(data))
process.stderr.write("\n")
}
/* report Webpack errors */
if (stats.errors.length > 0) {
process.stderr.write(`${chalk.bold.red("** ERROR: Webpack reported the following errors:")}\n`)
process.stderr.write(stats.errors.join("\n"))
process.stderr.write("\n")
}
/* report Webpack warnings */
if (stats.warnings.length > 0) {
process.stderr.write(`${chalk.bold.yellow("** WARNING: Webpack reported the following warnings")}:\n`)
process.stderr.write(stats.warnings.join("\n"))
process.stderr.write("\n")
}
return (stats.errors.length === 0 && stats.warnings.length === 0)
}
/*
* MAIN
*/
/* execute passes */
const singleRun = async () => {
let passed = await Pass1()
if (passed)
passed = await Pass2()
if (opts.beep) {
/* beep notification */
if (passed)
beep(1)
else
beep([ 0, 100, 500 ])
}
if (opts.notify) {
/* message notification */
if (passed)
notifier.notify({
title: "Gemstone Build: OK",
message: "Please reload the application in your browser.",
wait: false
})
else
notifier.notify({
title: "Gemstone Build: ERROR",
message: "Please check the Gemstone error output in your terminal.",
wait: false
})
}
}
/* distinguish between continuous and on-time execution */
if (opts.watch) {
/* continuous execution */
return new Promise((/* resolve, reject */) => {
/* internal state */
let first = true /* is this the first call after last watching? */
let ready = false /* is the filesyste watching already ready? */
let need = false /* is there a need for running the passes? */
let changed = {} /* the paths which have changed */
let timer = null /* the timer for deferred handling */
let running = false /* are we currently running the handler */
/* deferred handler */
const handler = async () => {
running = true
if (!first) {
process.stderr.write("\r \r")
process.stderr.write(`-- files changed: ${chalk.bold.green(Object.keys(changed).length)}\n\n`)
Object.keys(changed).forEach((filename) => {
filename = path.relative(process.cwd(), filename)
process.stderr.write(` ${chalk.green(filename)}\n`)
})
process.stderr.write("\n")
process.stderr.write(`${chalk.grey("== ========================================================================= ==")}\n`)
process.stderr.write("\n")
}
first = false
need = false
changed = {}
await singleRun()
process.stderr.write(`## ${chalk.bold("IDLE: WATCHER")}\n` +
` files changed: ${chalk.bold.yellow("[WAITING FOR FILESYSTEM CHANGES] ")}`)
running = false
/* is there ne need in the meantime? */
if (need) {
if (timer !== null)
clearTimeout(timer)
timer = setTimeout(handler, 0.0 * 1000)
}
}
/* watch filesystem */
const watcher = Chokidar.watch(cfg.path.source, {
ignored: /(?:[/\\]\.|\.(sw[px])$|~$|\.subl.*?\.tmp|___jb_tmp___$)/,
ignorePermissionErrors: true,
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 1.5 * 1000,
pollInterval: 100
}
})
watcher.on("ready", (/* ev, path */) => {
/* filesysten watching is ready */
timer = setTimeout(handler, 0.0 * 1000)
ready = true
})
watcher.on("all", (ev, path) => {
/* filesysten has changed */
if (ready) {
need = true
changed[path] = true
if (!running) {
if (timer !== null)
clearTimeout(timer)
timer = setTimeout(handler, 1.0 * 1000)
}
}
})
})
}
else {
/* one-time execution */
await singleRun()
return ""
}
}
})
}