forked from microsoft/vscode-arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
152 lines (133 loc) · 5.33 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
const gulp = require("gulp");
const eslint = require('gulp-eslint');
const tslint = require("gulp-tslint");
const PluginError = require('plugin-error');
const log = require('fancy-log');
const ts = require("gulp-typescript");
const sourcemaps = require("gulp-sourcemaps");
const webpack = require("webpack");
const del = require('del');
const fs = require("fs");
const path = require("path");
const childProcess = require("child_process");
const argv = require('minimist')(process.argv.slice(2));
gulp.task("tslint", () => {
return gulp.src(["**/*.ts", "**/*.tsx", "!**/*.d.ts", "!./vendor/**", "!node_modules/**", "!./src/views/node_modules/**", "!out/**"])
.pipe(tslint())
.pipe(tslint.report());
});
gulp.task("eslint", () => {
return gulp.src(["!**/node_modules/**"])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task("html-webpack", (done) => {
const config = require("./src/views/webpack.config.js");
config.context = `${__dirname}/src/views`;
config.mode = argv.mode ? argv.mode : 'production';
return webpack(config, (err, stats) => {
const statsJson = stats.toJson();
if (err || (statsJson.errors && statsJson.errors.length)) {
statsJson.errors.forEach(webpackError => {
log.error(`Error (webpack): ${webpackError}`);
});
throw new PluginError('webpack', JSON.stringify(err || statsJson.errors));
}
log('[webpack]', stats.toString());
done();
});
});
gulp.task("node_modules-webpack", (done) => {
const config = require("./webpack.config.js");
config.context = `${__dirname}`;
config.mode = argv.mode ? argv.mode : 'production';
return webpack(config, (err, stats) => {
const statsJson = stats.toJson();
if (err || (statsJson.errors && statsJson.errors.length)) {
statsJson.errors.forEach(webpackError => {
log.error(`Error (webpack): ${webpackError}`);
});
throw new PluginError('webpack', JSON.stringify(err || statsJson.errors));
}
log('[webpack]', stats.toString());
done();
});
});
gulp.task("ts-compile", () => {
const tsProject = ts.createProject("./tsconfig.json");
return tsProject.src()
.pipe(sourcemaps.init())
.pipe(tsProject())
.pipe(sourcemaps.write(".", {
mapSources: (sourcePath, file) => {
// Correct source map path.
const relativeSourcePath = path.relative(path.dirname(file.path), path.join(file.base, sourcePath));
return relativeSourcePath;
}
}))
.pipe(gulp.dest("out"));
});
gulp.task("clean", (done) => {
return del('out', done);
});
gulp.task("genAikey", (done) => {
if (process.env.TRAVIS_TAG) {
const ISPROD = /^v?[0-9]+\.[0-9]+\.[0-9]+$/.test(process.env.TRAVIS_TAG || "");
const packageJson = JSON.parse(fs.readFileSync("package.json"));
if (ISPROD) {
packageJson.aiKey = process.env["PROD_AIKEY"];
} else {
packageJson.aiKey = process.env["INT_AIKEY"] || packageJson.aiKey;
}
fs.writeFileSync("package.json", JSON.stringify(packageJson, null, 2) + "\n");
done();
} else {
log("Skipping genAiKey");
done();
}
});
gulp.task("test", (done) => {
function removeExtensionDependencies() {
const packageJson = JSON.parse(fs.readFileSync("package.json"));
packageJson.extensionDependencies = [];
fs.writeFileSync("package.json", JSON.stringify(packageJson, null, 2) + "\n");
}
function restoreExtensionDependencies() {
const packageJson = JSON.parse(fs.readFileSync("package.json"));
packageJson.extensionDependencies = ["ms-vscode.cpptools"];
fs.writeFileSync("package.json", JSON.stringify(packageJson, null, 2) + "\n");
}
// When using cli command "npm test" to exec test, the depended extensions (cpptools) are not available so that
// the extension cannot be activated. As a workaround, remove extensionDependencies from package.json before running test
// and restore extensionDependencies after test exited.
removeExtensionDependencies();
const child = childProcess.spawn("node", ["./out/test/runTest"], {
cwd: __dirname,
env: Object.assign({}, process.env),
});
child.stdout.on("data", (data) => {
log(data.toString().trim());
});
child.stderr.on("data", (data) => {
log.error(data.toString().trim());
});
child.on("error", (error) => {
log.error(error);
});
child.on("exit", (code) => {
restoreExtensionDependencies();
if (code === 0) {
done();
} else {
log.error("exit code: " + code);
done(code);
}
});
});
gulp.task("build", gulp.series("clean", "ts-compile", "html-webpack", "node_modules-webpack"));
gulp.task("build_without_view", gulp.series("clean", "ts-compile"));
gulp.task("watch", () => {
gulp.watch(["./src/**/*", "./test/**/*", "!./src/views/**/*"], ["ts-compile"]);
gulp.watch(["./src/views/**/*", "!./src/views/node_modules/**"], ["html-webpack"]);
});