-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
update-packages.js
197 lines (182 loc) · 6.39 KB
/
update-packages.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
/**
* Copyright 2017 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const colors = require('ansi-colors');
const fs = require('fs-extra');
const log = require('fancy-log');
const {exec, execOrDie, getStderr} = require('../exec');
const {isTravisBuild} = require('../travis');
const argv = require('minimist')(process.argv.slice(2));
const yarnExecutable = 'npx yarn';
/**
* Writes the given contents to the patched file if updated
* @param {string} patchedName Name of patched file
* @param {string} file Contents to write
*/
function writeIfUpdated(patchedName, file) {
if (!fs.existsSync(patchedName) || fs.readFileSync(patchedName) != file) {
fs.writeFileSync(patchedName, file);
if (!isTravisBuild()) {
log(colors.green('Patched'), colors.cyan(patchedName));
}
}
}
/**
* @param {string} filePath
* @param {string} newFilePath
* @param {...any} args Search and replace string pairs.
*/
function replaceInFile(filePath, newFilePath, ...args) {
let file = fs.readFileSync(filePath, 'utf8');
for (let i = 0; i < args.length; i += 2) {
const searchValue = args[i];
const replaceValue = args[i + 1];
if (!file.includes(searchValue)) {
throw new Error(`Expected "${searchValue}" to appear in ${filePath}.`);
}
file = file.replace(searchValue, replaceValue);
}
writeIfUpdated(newFilePath, file);
}
/**
* Patches Web Animations API by wrapping its body into `install` function.
* This gives us an option to call polyfill directly on the main window
* or a friendly iframe.
*/
function patchWebAnimations() {
// Copies web-animations-js into a new file that has an export.
const patchedName =
'node_modules/web-animations-js/web-animations.install.js';
let file = fs
.readFileSync('node_modules/web-animations-js/web-animations.min.js')
.toString();
// Replace |requestAnimationFrame| with |window|.
file = file.replace(/requestAnimationFrame/g, function(a, b) {
if (file.charAt(b - 1) == '.') {
return a;
}
return 'window.' + a;
});
// Fix web-animations-js code that violates strict mode.
// See https://github.com/ampproject/amphtml/issues/18612 and
// https://github.com/web-animations/web-animations-js/issues/46
file = file.replace(/b.true=a/g, 'b?b.true=a:true');
// Fix web-animations-js code that attempts to write a read-only property.
// See https://github.com/ampproject/amphtml/issues/19783 and
// https://github.com/web-animations/web-animations-js/issues/160
file = file.replace(/this\._isFinished\s*=\s*\!0,/, '');
// Wrap the contents inside the install function.
file =
'export function installWebAnimations(window) {\n' +
'var document = window.document;\n' +
file +
'\n' +
'}\n';
writeIfUpdated(patchedName, file);
}
/**
* Creates a version of document-register-element that can be installed
* without side effects.
*/
function patchRegisterElement() {
// Copies document-register-element into a new file that has an export.
// This works around a bug in closure compiler, where without the
// export this module does not generate a goog.provide which fails
// compilation: https://github.com/google/closure-compiler/issues/1831
const dir = 'node_modules/document-register-element/build/';
replaceInFile(
dir + 'document-register-element.node.js',
dir + 'document-register-element.patched.js',
// Elimate the immediate side effect.
'installCustomElements(global);',
'',
// Replace CJS export with ES6 export.
'module.exports = installCustomElements;',
'export {installCustomElements};'
);
}
/**
* Installs custom lint rules from build-system/eslint-rules to node_modules.
*/
function installCustomEslintRules() {
const customRuleDir = 'build-system/eslint-rules';
const customRuleName = 'eslint-plugin-amphtml-internal';
exec(yarnExecutable + ' unlink', {'stdio': 'ignore', 'cwd': customRuleDir});
exec(yarnExecutable + ' link', {'stdio': 'ignore', 'cwd': customRuleDir});
exec(yarnExecutable + ' unlink ' + customRuleName, {'stdio': 'ignore'});
exec(yarnExecutable + ' link ' + customRuleName, {'stdio': 'ignore'});
if (!isTravisBuild()) {
log(colors.green('Installed lint rules from'), colors.cyan(customRuleDir));
}
}
/**
* Does a yarn check on node_modules, and if it is outdated, runs yarn.
*/
function runYarnCheck() {
const integrityCmd = yarnExecutable + ' check --integrity';
if (getStderr(integrityCmd).trim() != '') {
log(
colors.yellow('WARNING:'),
'The packages in',
colors.cyan('node_modules'),
'do not match',
colors.cyan('package.json.')
);
const verifyTreeCmd = yarnExecutable + ' check --verify-tree';
exec(verifyTreeCmd);
log('Running', colors.cyan('yarn'), 'to update packages...');
/**
* NOTE: executing yarn with --production=false prevents having
* NODE_ENV=production variable set which forces yarn to not install
* devDependencies. This usually breaks gulp for example.
*/
execOrDie(`${yarnExecutable} install --production=false`); // Stop execution when Ctrl + C is detected.
} else {
log(
colors.green('All packages in'),
colors.cyan('node_modules'),
colors.green('are up to date.')
);
}
}
/**
* Used as a pre-requisite by several gulp tasks.
*/
function maybeUpdatePackages() {
if (!isTravisBuild()) {
updatePackages();
}
}
/**
* Installs custom lint rules, updates node_modules (for local dev), and patches
* web-animations-js and document-register-element if necessary.
*/
async function updatePackages() {
if (isTravisBuild() || argv._.includes('lint')) {
installCustomEslintRules();
}
if (!isTravisBuild()) {
runYarnCheck();
}
patchWebAnimations();
patchRegisterElement();
}
module.exports = {
maybeUpdatePackages,
updatePackages,
};
updatePackages.description =
'Runs yarn if node_modules is out of date, and applies custom patches';