forked from GoogleChrome/developer.chrome.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-external.js
More file actions
86 lines (71 loc) · 2.8 KB
/
build-external.js
File metadata and controls
86 lines (71 loc) · 2.8 KB
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
/**
* @fileoverview Builds all external sources. This clears the data/ folder here
* and runs all scripts in build/, which can write new files there.
*
* This is intended for use by Cloud Build, or by site devs doing local work.
*/
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const childProcess = require('child_process');
const crypto = require('crypto');
const syncTestdata = require('./lib/sync-testdata');
async function run() {
let errors = 0;
const scripts = glob.sync('build/*.js', {cwd: __dirname});
scripts.sort(); // run in alphabetical order
const projectRoot = path.join(__dirname, '..');
const dataTarget = path.join(__dirname, 'data');
fs.rmSync(dataTarget, {recursive: true, force: true});
fs.mkdirSync(dataTarget, {recursive: true});
// If this is a CI build, we start with everything found in "fallback/". It won't win, but it
// will be used in cases where credentials and such aren't available.
if (process.env.CI) {
const all = await syncTestdata();
console.info('! Using fallback before build in CI, copied:', all);
}
/** @type {childProcess.ExecFileSyncOptions} */
const options = {cwd: projectRoot, stdio: 'inherit'};
for (const script of scripts) {
const r = path.join(__dirname, script);
console.info('> Running', r);
try {
childProcess.execFileSync('node', [r], options);
} catch (e) {
const withStatus = /** @type {{status: any}} */ (e);
// We don't log the error here, as we're already getting STDERR piped above.
console.warn(`! Failed to execute "${script}" (${withStatus.status})`);
++errors;
}
}
// Determine the hash for everything in data/.
const hash = crypto.createHash('sha256');
const allFiles = glob.sync('data/**/*', {cwd: __dirname});
if (!allFiles.length) {
throw new Error('no files generated, cowardly refusing to hash');
}
// Sort allFiles, in case glob.sync is inconsistent.
allFiles.sort();
for (const f of allFiles) {
const p = path.join(__dirname, f);
const bytes = fs.readFileSync(p);
hash.update(bytes);
}
const digest = hash.digest('hex');
console.info(
`@ Generated digest=${digest} for ${allFiles.length} files:`,
allFiles
);
fs.writeFileSync(path.join(__dirname, 'data/.hash'), digest);
// If there were any errors, return with a non-zero status code anyway.
if (errors) {
// eslint-disable-next-line no-process-exit
process.exit(1);
}
// Mark this local environment as being build-only, so it won't automatically sync.
const payload =
'// This file blocks synchronizing local data, because you ran `npm run build-external`.\n' +
'// Delete it to bring back automatic sync when you run `npm run dev`.';
fs.writeFileSync(path.join(__dirname, 'local-build-flag'), payload);
}
run();