|
| 1 | +const fs = require('fs-extra'); |
| 2 | +const existsSync = require('exists-sync'); |
| 3 | +const path = require('path'); |
| 4 | +const Promise = require('ember-cli/lib/ext/promise'); |
| 5 | +const Task = require('ember-cli/lib/models/task'); |
| 6 | +const SilentError = require('silent-error'); |
| 7 | +const chalk = require('chalk'); |
| 8 | +const attemptNeverIndex = require('ember-cli/lib/utilities/attempt-never-index'); |
| 9 | +const findBuildFile = require('ember-cli/lib/utilities/find-build-file'); |
| 10 | +const viz = require('broccoli-viz'); |
| 11 | +const FSMonitor = require('fs-monitor-stack'); |
| 12 | +const Sync = require('tree-sync'); |
| 13 | +const mkdirp = require('mkdirp'); |
| 14 | + |
| 15 | +let resolve = null; |
| 16 | +let promise = new Promise((r) => resolve = r); |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +var signalsTrapped = false; |
| 22 | +var buildCount = 0; |
| 23 | + |
| 24 | +function outputViz(count, result, monitor) { |
| 25 | + var processed = viz.process(result.graph); |
| 26 | + |
| 27 | + processed.forEach(function(node) { |
| 28 | + node.stats.fs = monitor.statsFor(node); |
| 29 | + }); |
| 30 | + |
| 31 | + fs.writeFileSync('graph.' + count + '.dot', viz.dot(processed)); |
| 32 | + fs.writeFileSync('graph.' + count + '.json', JSON.stringify({ |
| 33 | + summary: { |
| 34 | + buildCount: count, |
| 35 | + output: result.directory, |
| 36 | + totalTime: result.totalTime, |
| 37 | + totalNodes: processed.length, |
| 38 | + stats: { |
| 39 | + fs: monitor.totalStats() |
| 40 | + } |
| 41 | + }, |
| 42 | + nodes: processed |
| 43 | + })); |
| 44 | +} |
| 45 | + |
| 46 | +module.exports = Task.extend({ |
| 47 | + setupBuilder: function() { |
| 48 | + this.environment = this.environment || 'development'; |
| 49 | + process.env.ANGULAR_ENV = process.env.ANGULAR_ENV || process.env.EMBER_CLI || this.environment; |
| 50 | + process.env.EMBER_ENV = process.env.ANGULAR_ENV; |
| 51 | + |
| 52 | + var buildFile = findBuildFile('angular-cli-build.js'); |
| 53 | + this.tree = buildFile({ project: this.project }); |
| 54 | + |
| 55 | + if (webpack) { |
| 56 | + console.log('webpack'); |
| 57 | + } else { |
| 58 | + var broccoli = require('ember-cli-broccoli'); |
| 59 | + this.builder = new broccoli.Builder(this.tree); |
| 60 | + } |
| 61 | + }, |
| 62 | + |
| 63 | + trapSignals: function() { |
| 64 | + if (!signalsTrapped) { |
| 65 | + process.on('SIGINT', this.onSIGINT.bind(this)); |
| 66 | + process.on('SIGTERM', this.onSIGTERM.bind(this)); |
| 67 | + process.on('message', this.onMessage.bind(this)); |
| 68 | + signalsTrapped = true; |
| 69 | + } |
| 70 | + }, |
| 71 | + |
| 72 | + init: function() { |
| 73 | + this.setupBuilder(); |
| 74 | + this.trapSignals(); |
| 75 | + }, |
| 76 | + |
| 77 | + /** |
| 78 | + Determine whether the output path is safe to delete. If the outputPath |
| 79 | + appears anywhere in the parents of the project root, the build would |
| 80 | + delete the project directory. In this case return `false`, otherwise |
| 81 | + return `true`. |
| 82 | + */ |
| 83 | + canDeleteOutputPath: function(outputPath) { |
| 84 | + var rootPathParents = [this.project.root]; |
| 85 | + var dir = path.dirname(this.project.root); |
| 86 | + rootPathParents.push(dir); |
| 87 | + while (dir !== path.dirname(dir)) { |
| 88 | + dir = path.dirname(dir); |
| 89 | + rootPathParents.push(dir); |
| 90 | + } |
| 91 | + return rootPathParents.indexOf(outputPath) === -1; |
| 92 | + }, |
| 93 | + |
| 94 | + copyToOutputPath: function(inputPath) { |
| 95 | + var outputPath = this.outputPath; |
| 96 | + |
| 97 | + mkdirp.sync(outputPath); |
| 98 | + |
| 99 | + if (!this.canDeleteOutputPath(outputPath)) { |
| 100 | + throw new SilentError('Using a build destination path of `' + outputPath + '` is not supported.'); |
| 101 | + } |
| 102 | + |
| 103 | + var sync = this._sync; |
| 104 | + if (sync === undefined) { |
| 105 | + this._sync = sync = new Sync(inputPath, path.resolve(this.outputPath)); |
| 106 | + } |
| 107 | + |
| 108 | + sync.sync(); |
| 109 | + }, |
| 110 | + |
| 111 | + build: function(...args: any[]) { |
| 112 | + attemptNeverIndex('tmp'); |
| 113 | + return promise; |
| 114 | + // return Promise.resolve(); |
| 115 | + // if (this.webpack) { |
| 116 | + // console.log(1, process.cwd()); |
| 117 | + // return new Promise((resolve, reject) => { |
| 118 | + // this.webpack.run((err, stats) => { |
| 119 | + // console.log(!!err, stats); |
| 120 | + // if (err) { |
| 121 | + // reject(err); |
| 122 | + // } |
| 123 | + // resolve(); |
| 124 | + // }); |
| 125 | + // }); |
| 126 | + // } |
| 127 | + // return this.builder.build(...args); |
| 128 | + }, |
| 129 | + |
| 130 | + cleanup: function() { |
| 131 | + var ui = this.ui; |
| 132 | + |
| 133 | + // if (this.webpack) { |
| 134 | + // this.webpack.cleanupAndExit(); |
| 135 | + return Promise.resolve(); |
| 136 | + // } else { |
| 137 | + // return this.builder.cleanup().catch(function (err) { |
| 138 | + // ui.writeLine(chalk.red('Cleanup error.')); |
| 139 | + // ui.writeError(err); |
| 140 | + // }); |
| 141 | + // } |
| 142 | + }, |
| 143 | + |
| 144 | + cleanupAndExit: function() { |
| 145 | + this.cleanup().finally(function() { |
| 146 | + process.exit(1); |
| 147 | + }); |
| 148 | + }, |
| 149 | + |
| 150 | + onSIGINT: function() { |
| 151 | + this.cleanupAndExit(); |
| 152 | + }, |
| 153 | + onSIGTERM: function() { |
| 154 | + this.cleanupAndExit(); |
| 155 | + }, |
| 156 | + onMessage: function(message) { |
| 157 | + if (message.kill) { |
| 158 | + this.cleanupAndExit(); |
| 159 | + } |
| 160 | + } |
| 161 | +}); |
0 commit comments