-
Notifications
You must be signed in to change notification settings - Fork 307
/
gulpfile.js
203 lines (176 loc) · 5.44 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
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
198
199
200
201
202
203
"use strict";
const Cesium = require("cesium");
const Promise = require("bluebird");
const child_process = require("child_process");
const fsExtra = require("fs-extra");
const gulp = require("gulp");
const Jasmine = require("jasmine");
const JasmineSpecReporter = require("jasmine-spec-reporter").SpecReporter;
const path = require("path");
const yargs = require("yargs");
const defaultValue = Cesium.defaultValue;
const defined = Cesium.defined;
const argv = yargs.argv;
// Add third-party node module binaries to the system path
// since some tasks need to call them directly.
const environmentSeparator = process.platform === "win32" ? ";" : ":";
const nodeBinaries = path.join(__dirname, "node_modules", ".bin");
process.env.PATH += environmentSeparator + nodeBinaries;
const specFiles = [
"**/*.js",
"!node_modules/**",
"!coverage/**",
"!doc/**",
"!bin/**",
];
module.exports = {
test: test,
"test-watch": testWatch,
coverage: coverage,
cloc: cloc,
"generate-third-party": generateThirdParty,
};
async function test() {
const jasmine = new Jasmine();
jasmine.loadConfigFile("specs/jasmine.json");
jasmine.exitOnCompletion = false;
jasmine.addReporter(
new JasmineSpecReporter({
displaySuccessfulSpec:
!defined(argv.suppressPassed) || !argv.suppressPassed,
}),
);
const results = await jasmine.execute();
if (argv.failTaskOnError && results.overallStatus === "failed") {
process.exitCode = 1;
}
}
function testWatch() {
return gulp.watch(specFiles).on("change", function () {
// We can't simply depend on the test task because Jasmine
// does not like being run multiple times in the same process.
try {
child_process.execSync("jasmine JASMINE_CONFIG_PATH=specs/jasmine.json", {
stdio: [process.stdin, process.stdout, process.stderr],
});
} catch (exception) {
console.log("Tests failed to execute.");
}
});
}
async function coverage() {
fsExtra.removeSync("coverage/server");
child_process.execSync(
"nyc" +
" --all" +
" --reporter=lcov" +
" --dir coverage" +
' -x "specs/**" -x "coverage/**" -x "doc/**" -x "bin/**" -x "index.js" -x "gulpfile.js"' +
" node_modules/jasmine/bin/jasmine.js" +
" JASMINE_CONFIG_PATH=specs/jasmine.json",
{
stdio: [process.stdin, process.stdout, process.stderr],
},
);
}
function cloc() {
let cmdLine;
const clocPath = path.join("node_modules", "cloc", "lib", "cloc");
//Run cloc on primary Source files only
const source = new Promise(function (resolve, reject) {
cmdLine = `perl ${clocPath} --quiet --progress-rate=0` + ` lib/ bin/`;
child_process.exec(cmdLine, function (error, stdout, stderr) {
if (error) {
console.log(stderr);
return reject(error);
}
console.log("Source:");
console.log(stdout);
resolve();
});
});
//If running cloc on source succeeded, also run it on the tests.
return source.then(function () {
return new Promise(function (resolve, reject) {
cmdLine = `perl ${clocPath} --quiet --progress-rate=0` + ` specs/lib/`;
child_process.exec(cmdLine, function (error, stdout, stderr) {
if (error) {
console.log(stderr);
return reject(error);
}
console.log("Specs:");
console.log(stdout);
resolve();
});
});
});
}
function getLicenseDataFromPackage(packageName, override) {
override = defaultValue(override, defaultValue.EMPTY_OBJECT);
const packagePath = path.join("node_modules", packageName, "package.json");
if (!fsExtra.existsSync(packagePath)) {
throw new Error(`Unable to find ${packageName} license information`);
}
const contents = fsExtra.readFileSync(packagePath);
const packageJson = JSON.parse(contents);
let licenseField = override.license;
if (!licenseField) {
licenseField = [packageJson.license];
}
if (!licenseField && packageJson.licenses) {
licenseField = packageJson.licenses;
}
if (!licenseField) {
console.log(`No license found for ${packageName}`);
licenseField = ["NONE"];
}
let version = packageJson.version;
if (!packageJson.version) {
console.log(`No version information found for ${packageName}`);
version = "NONE";
}
return {
name: packageName,
license: licenseField,
version: version,
url: `https://www.npmjs.com/package/${packageName}`,
notes: override.notes,
};
}
function readThirdPartyExtraJson() {
const path = "ThirdParty.extra.json";
if (fsExtra.existsSync(path)) {
const contents = fsExtra.readFileSync(path);
return JSON.parse(contents);
}
return [];
}
async function generateThirdParty() {
const packageJson = JSON.parse(fsExtra.readFileSync("package.json"));
const thirdPartyExtraJson = readThirdPartyExtraJson();
const thirdPartyJson = [];
const dependencies = packageJson.dependencies;
for (const packageName in dependencies) {
if (dependencies.hasOwnProperty(packageName)) {
const override = thirdPartyExtraJson.find(
(entry) => entry.name === packageName,
);
thirdPartyJson.push(getLicenseDataFromPackage(packageName, override));
}
}
thirdPartyJson.sort(function (a, b) {
const nameA = a.name.toLowerCase();
const nameB = b.name.toLowerCase();
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
return 0;
});
fsExtra.writeFileSync(
"ThirdParty.json",
JSON.stringify(thirdPartyJson, null, 2),
);
}