|
| 1 | +const fs = require('fs-extra') |
| 2 | +const path = require('path') |
| 3 | + |
| 4 | +const ProductionLine = require('productionline-web') |
| 5 | +const TaskRunner = require('shortbus') |
| 6 | +const Chassis = require('@chassis/core') |
| 7 | + |
| 8 | +class Builder extends ProductionLine { |
| 9 | + constructor (cfg) { |
| 10 | + super(cfg) |
| 11 | + |
| 12 | + this.devMode = false |
| 13 | + } |
| 14 | + |
| 15 | + copyComponent (cb) { |
| 16 | + fs.copySync(this.paths.component, this.outputDirectory('webcomponents')) |
| 17 | + |
| 18 | + cb() |
| 19 | + } |
| 20 | + |
| 21 | + copyCustomElements (cb) { |
| 22 | + this.walk(this.paths.authorElements).forEach(dir => { |
| 23 | + let filepath = path.join(dir, 'dist') |
| 24 | + this.walk(filepath).forEach(file => fs.copySync(filepath, this.outputDirectory('webcomponents'))) |
| 25 | + }) |
| 26 | + |
| 27 | + cb() |
| 28 | + } |
| 29 | + |
| 30 | + copyLibs (cb) { |
| 31 | + fs.copySync(this.paths.lib, this.outputDirectory('lib')) |
| 32 | + cb() |
| 33 | + } |
| 34 | + |
| 35 | + processCss (minify = true, cb) { |
| 36 | + let chassis = new Chassis({ |
| 37 | + minify, |
| 38 | + sourceMap: true, |
| 39 | + theme: path.join(this.SOURCE, 'css', 'main.theme'), |
| 40 | + layout: { |
| 41 | + minWidth: 320, |
| 42 | + maxWidth: 960 |
| 43 | + } |
| 44 | + }) |
| 45 | + |
| 46 | + let tasks = new TaskRunner() |
| 47 | + |
| 48 | + this.walk(this.paths.css).forEach(filepath => { |
| 49 | + tasks.add(`Process ${this.localDirectory(filepath)}`, cont => { |
| 50 | + chassis.process(filepath, (err, processed) => { |
| 51 | + if (err) { |
| 52 | + throw err |
| 53 | + } |
| 54 | + |
| 55 | + if (processed.sourceMap) { |
| 56 | + this.writeFileSync(`${this.outputDirectory(filepath)}.map`, processed.sourceMap) |
| 57 | + } |
| 58 | + |
| 59 | + this.writeFile(this.outputDirectory(filepath), this.applyHeader(processed.css, 'css'), cont) |
| 60 | + }) |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + tasks.on('complete', cb) |
| 65 | + tasks.run() |
| 66 | + } |
| 67 | + |
| 68 | + processJavascript (minify = true, cb) { |
| 69 | + let tasks = new TaskRunner() |
| 70 | + |
| 71 | + this.walk(this.paths.javascript).forEach(filepath => { |
| 72 | + tasks.add(`Process ${this.localDirectory(filepath)}`, cont => { |
| 73 | + let dir = path.dirname(filepath) |
| 74 | + let output = this.transpile(filepath) |
| 75 | + |
| 76 | + if (minify) { |
| 77 | + output = this.minify(output.code) |
| 78 | + } |
| 79 | + |
| 80 | + this.writeFile(this.outputDirectory(filepath), this.applyHeader(output.code, 'js'), cont) |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
| 84 | + tasks.on('complete', cb) |
| 85 | + tasks.run() |
| 86 | + } |
| 87 | + |
| 88 | + make (devMode = false) { |
| 89 | + this.clean() |
| 90 | + // this.copyAssets(true) |
| 91 | + this.addTask('Copy Libraries', next => this.copyLibs(next)) |
| 92 | + this.addTask('Copy Component', next => this.copyComponent(next)) |
| 93 | + this.addTask('Copy Custom Elements', next => this.copyCustomElements(next)) |
| 94 | + this.buildHTML() |
| 95 | + this.addTask('Build JavaScript', next => this.processJavascript(!devMode, next)) |
| 96 | + this.addTask('Build CSS', next => this.processCss(!devMode, next)) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +const builder = new Builder({ |
| 101 | + header: `Built at ${new Date().toTimeString()}\nCopyright (c) ${new Date().getFullYear()} Author.io`, |
| 102 | + |
| 103 | + commands: { |
| 104 | + '--prod' (cmd) { |
| 105 | + builder.make() |
| 106 | + }, |
| 107 | + |
| 108 | + '--dev' (cmd) { |
| 109 | + builder.make(true) |
| 110 | + |
| 111 | + builder.watch((action, filepath) => { |
| 112 | + if (action === 'create' || action === 'update') { |
| 113 | + builder.make(true) |
| 114 | + builder.run() |
| 115 | + } |
| 116 | + }) |
| 117 | + }, |
| 118 | + |
| 119 | + '--js-only' (cmd) { |
| 120 | + this.addTask('Build JavaScript', next => this.processJavascript(false, next)) |
| 121 | + } |
| 122 | + } |
| 123 | +}) |
| 124 | + |
| 125 | +builder.paths = { |
| 126 | + apps: path.join(builder.SOURCE, '/apps'), |
| 127 | + javascript: path.join(builder.SOURCE, 'js', '/**/*.js'), |
| 128 | + css: path.join(builder.SOURCE, 'css', '/**/*.css'), |
| 129 | + lib: path.join(builder.SOURCE, 'lib'), |
| 130 | + component: path.resolve('..', 'dist'), |
| 131 | + authorElements: './node_modules/@author.io/element-*' |
| 132 | +} |
| 133 | + |
| 134 | +builder.run() |
0 commit comments