|
3 | 3 |
|
4 | 4 | const fs = require('fs');
|
5 | 5 | const fmt = require('util').format;
|
6 |
| -const uniq = require('ember-cli-lodash-subset').uniq; |
7 | 6 | const merge = require('ember-cli-lodash-subset').merge;
|
8 | 7 | const md5Hex = require('md5-hex');
|
9 | 8 | const path = require('path');
|
10 | 9 | const Plugin = require('broccoli-plugin');
|
| 10 | +const child_process = require('child_process'); |
| 11 | +const rimraf = require('rimraf'); |
11 | 12 |
|
12 | 13 | const stringify = require('json-stable-stringify');
|
13 | 14 |
|
@@ -47,13 +48,15 @@ module.exports = class FastBootConfig extends Plugin {
|
47 | 48 | * and write it to `package.json`.
|
48 | 49 | */
|
49 | 50 | build() {
|
50 |
| - this.buildConfig(); |
51 |
| - this.buildDependencies(); |
52 |
| - this.buildManifest(); |
53 |
| - this.buildHostWhitelist(); |
54 |
| - |
55 |
| - let outputPath = path.join(this.outputPath, 'package.json'); |
56 |
| - this.writeFileIfContentChanged(outputPath, this.toJSONString()); |
| 51 | + return Promise.all([ |
| 52 | + this.buildConfig(), |
| 53 | + this.buildDependencies(), |
| 54 | + this.buildManifest(), |
| 55 | + this.buildHostWhitelist() |
| 56 | + ]).then(() => { |
| 57 | + let packageOutputPath = path.join(this.outputPath, 'package.json'); |
| 58 | + this.writeFileIfContentChanged(packageOutputPath, this.toJSONString()); |
| 59 | + }); |
57 | 60 | }
|
58 | 61 |
|
59 | 62 | writeFileIfContentChanged(outputPath, content) {
|
@@ -124,8 +127,86 @@ module.exports = class FastBootConfig extends Plugin {
|
124 | 127 | });
|
125 | 128 | }
|
126 | 129 |
|
127 |
| - this.dependencies = dependencies; |
128 |
| - this.moduleWhitelist = uniq(moduleWhitelist); |
| 130 | + return this.updateDependencies(dependencies, moduleWhitelist); |
| 131 | + } |
| 132 | + |
| 133 | + updateDependencies(dependencies, moduleWhitelist) { |
| 134 | + if (this.dependenciesChanged(dependencies)) { |
| 135 | + return this.getPackageLock(dependencies).then(() => { |
| 136 | + this.dependencies = dependencies; |
| 137 | + this.moduleWhitelist = moduleWhitelist; |
| 138 | + }); |
| 139 | + } else { |
| 140 | + this.dependencies = dependencies; |
| 141 | + this.moduleWhitelist = moduleWhitelist; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + dependenciesChanged(dependencies) { |
| 146 | + return stringify(dependencies) !== stringify(this.dependencies); |
| 147 | + } |
| 148 | + |
| 149 | + getPackageLock(dependencies) { |
| 150 | + let packagePath = path.join(this.project.root, 'package.json'); |
| 151 | + let lockfilePath = path.join(this.project.root, 'package-lock.json'); |
| 152 | + let tmpFolder = path.join(this.outputPath, 'package-lock-generator'); |
| 153 | + |
| 154 | + fs.mkdirSync(tmpFolder); |
| 155 | + fs.symlinkSync(packagePath, path.join(tmpFolder, 'package.json')); |
| 156 | + fs.symlinkSync(lockfilePath, path.join(tmpFolder, 'package-lock.json')); |
| 157 | + |
| 158 | + const firstInstall = new Promise((resolve, reject) => { |
| 159 | + this.ui.writeLine('FastBoot: Building node module cache.'); |
| 160 | + child_process.exec('npm install --cache=tmp', { cwd: tmpFolder }, (error) => { |
| 161 | + if (error) { |
| 162 | + reject(error); |
| 163 | + } else { |
| 164 | + resolve(); |
| 165 | + } |
| 166 | + }); |
| 167 | + }) |
| 168 | + |
| 169 | + return firstInstall |
| 170 | + .then(() => { |
| 171 | + this.ui.writeLine('FastBoot: Generating lockfile.'); |
| 172 | + |
| 173 | + fs.unlinkSync(path.join(tmpFolder, 'package.json')); |
| 174 | + fs.unlinkSync(path.join(tmpFolder, 'package-lock.json')); |
| 175 | + |
| 176 | + fs.writeFileSync(path.join(tmpFolder, 'package.json'), stringify({ dependencies })); |
| 177 | + |
| 178 | + const secondInstall = new Promise((resolve, reject) => { |
| 179 | + child_process.exec('npm install --cache=tmp --offline', { cwd: tmpFolder }, (error) => { |
| 180 | + if (error) { |
| 181 | + reject(error); |
| 182 | + } else { |
| 183 | + resolve(); |
| 184 | + } |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + return secondInstall; |
| 189 | + }) |
| 190 | + .then(() => { |
| 191 | + this.ui.writeLine('FastBoot: Removing node module cache.'); |
| 192 | + |
| 193 | + fs.renameSync( |
| 194 | + path.join(tmpFolder, 'package-lock.json'), |
| 195 | + path.join(this.outputPath, 'package-lock.json') |
| 196 | + ); |
| 197 | + |
| 198 | + const removeTmpFolder = new Promise((resolve, reject) => { |
| 199 | + rimraf(tmpFolder, (error) => { |
| 200 | + if (error) { |
| 201 | + reject(error); |
| 202 | + } else { |
| 203 | + resolve(); |
| 204 | + } |
| 205 | + }); |
| 206 | + }); |
| 207 | + |
| 208 | + return removeTmpFolder; |
| 209 | + }); |
129 | 210 | }
|
130 | 211 |
|
131 | 212 | updateFastBootManifest(manifest) {
|
|
0 commit comments