forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
154 lines (148 loc) · 6.28 KB
/
gulpfile.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
/**
* Copyright 2019 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.
*/
/* global require, process */
const argv = require('minimist')(process.argv.slice(2));
const gulp = require('gulp');
const {cyan, red} = require('kleur/colors');
const {isCiBuild} = require('./build-system/common/ci');
const {log} = require('./build-system/common/logging');
/**
* Helper that creates the tasks in AMP's toolchain based on what's being done:
* - If `gulp --tasks` has been invoked, eagerly load all task functions so we
* can print detailed information about their names, flags, usage, etc.
* - If a single gulp task has been invoked, create a wrapper that lazily loads
* individual task functions from their source files. After that, check the
* flag usage for the current task and load only those source file(s) used by
* the task that was invoked.
* @param {string} taskName
* @param {string} taskFuncName
* @param {string} taskSourceFileName
*/
function createTask(taskName, taskFuncName, taskSourceFileName) {
const isGulpTasks = argv._.length == 0 && argv.tasks == true; // gulp --tasks
const taskSourceFilePath = `./build-system/tasks/${taskSourceFileName}`;
if (isGulpTasks) {
const taskFunc = require(taskSourceFilePath)[taskFuncName];
gulp.task(taskName, taskFunc);
} else {
gulp.task(taskName, (callback) => {
const taskFunc = require(taskSourceFilePath)[taskFuncName];
checkFlags(taskName, taskFunc, callback);
return taskFunc(callback);
});
}
}
/**
* Checks if the flags passed in to a task are valid.
* @param {string} name
* @param {function} taskFunc
* @param {function} callback
*/
function checkFlags(name, taskFunc, callback) {
const validFlags = taskFunc.flags ? Object.keys(taskFunc.flags) : [];
if (isCiBuild()) {
validFlags.push('color'); // Used to enable log coloring during CI.
}
const usedFlags = Object.keys(argv).slice(1); // Skip the '_' argument
const invalidFlags = [];
usedFlags.forEach((flag) => {
if (!validFlags.includes(flag)) {
invalidFlags.push(`--${flag}`);
}
});
if (invalidFlags.length > 0) {
log(
red('ERROR:'),
'Found invalid flags for',
cyan(`gulp ${name}`) + ':',
cyan(invalidFlags.join(', '))
);
log('For detailed usage information, run', cyan('gulp --tasks') + '.');
if (validFlags.length > 0) {
log('Valid flags for', cyan(`gulp ${name}`) + ':');
validFlags.forEach((key) => {
log(cyan(`\t--${key}`) + `: ${taskFunc.flags[key]}`);
});
}
const reason = new Error('Found invalid flags');
reason.showStack = false;
callback(reason);
}
}
/**
* All the gulp tasks. Keep this list alphabetized.
*
* The three params used below are:
* 1. Name of the gulp task to be invoked E.g. gulp default
* 2. Name of the function in the source file. E.g. defaultTask()
* 3. Basename of the source file in build-system/tasks/. E.g. build-system/tasks/default-task.js
*/
createTask('analytics-vendor-configs','analyticsVendorConfigs','analytics-vendor-configs'); // prettier-ignore
createTask('ava', 'ava', 'ava');
createTask('babel-plugin-tests', 'babelPluginTests', 'babel-plugin-tests');
createTask('build', 'build', 'build');
createTask('bundle-size', 'bundleSize', 'bundle-size');
createTask('caches-json', 'cachesJson', 'caches-json');
createTask('check-exact-versions', 'checkExactVersions','check-exact-versions'); // prettier-ignore
createTask('check-links', 'checkLinks', 'check-links');
createTask('check-owners', 'checkOwners', 'check-owners');
createTask('check-renovate-config','checkRenovateConfig','check-renovate-config'); // prettier-ignore
createTask('check-sourcemaps', 'checkSourcemaps', 'check-sourcemaps');
createTask('check-types', 'checkTypes', 'check-types');
createTask('cherry-pick', 'cherryPick', 'cherry-pick');
createTask('clean', 'clean', 'clean');
createTask('codecov-upload', 'codecovUpload', 'codecov-upload');
createTask('compile-jison', 'compileJison', 'compile-jison');
createTask('coverage-map', 'coverageMap', 'coverage-map');
createTask('css', 'css', 'css');
createTask('default', 'defaultTask', 'default-task');
createTask('dep-check', 'depCheck', 'dep-check');
createTask('dev-dashboard-tests', 'devDashboardTests', 'dev-dashboard-tests');
createTask('dist', 'dist', 'dist');
createTask('e2e', 'e2e', 'e2e');
createTask('firebase', 'firebase', 'firebase');
createTask('get-zindex', 'getZindex', 'get-zindex');
createTask('integration', 'integration', 'integration');
createTask('lint', 'lint', 'lint');
createTask('make-extension', 'makeExtension', 'extension-generator');
createTask('markdown-toc', 'markdownToc', 'markdown-toc');
createTask('performance', 'performance', 'performance');
createTask('performance-urls', 'performanceUrls', 'performance-urls');
createTask('pr-check', 'prCheck', 'pr-check');
createTask('prepend-global', 'prependGlobal', 'prepend-global');
createTask('presubmit', 'presubmit', 'presubmit-checks');
createTask('prettify', 'prettify', 'prettify');
createTask('release', 'release', 'release');
createTask('serve', 'serve', 'serve');
createTask('server-tests', 'serverTests', 'server-tests');
createTask('storybook', 'storybook', 'storybook');
createTask('sweep-experiments', 'sweepExperiments', 'sweep-experiments');
createTask('test-report-upload', 'testReportUpload', 'test-report-upload');
createTask('unit', 'unit', 'unit');
createTask('update-packages', 'updatePackages', 'update-packages');
createTask('validator', 'validator', 'validator');
createTask('validator-webui', 'validatorWebui', 'validator');
createTask(
'check-video-interface-list',
'checkVideoInterfaceList',
'check-video-interface-list'
);
createTask(
'check-analytics-vendors-list',
'checkAnalyticsVendorsList',
'check-analytics-vendors-list'
);
createTask('visual-diff', 'visualDiff', 'visual-diff');