forked from wasabee-project/Wasabee-IITC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
217 lines (190 loc) · 4.93 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Requires
const fs = require("fs");
const path = require("path");
const gulp = require("gulp");
const injectfile = require("gulp-inject-file"); // https://www.npmjs.com/package/gulp-inject-file
const rename = require("gulp-rename"); // https://www.npmjs.com/package/gulp-rename
const contents = require("gulp-inject-string"); // https://www.npmjs.com/package/gulp-inject-string
const cfg = require("./plugin.config.json");
const trimlines = require("gulp-trimlines");
const eslint = require("gulp-eslint");
const del = require("del");
const webpack = require("webpack");
const PluginError = require("plugin-error");
const log = require("fancy-log");
const prettier = require("gulp-prettier");
const ensureDirectoryExistence = (filePath) => {
const dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) return;
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
};
// Config
var status = {
headers: null,
mode: null,
};
// status related tasks
gulp.task("set-mode-dev", (cb) => {
status.mode = "dev";
cb();
});
gulp.task("set-mode-prod", (cb) => {
status.mode = "prod";
cb();
});
gulp.task("clear", (cb) => {
status.headers = null;
status.mode = null;
cb();
});
// build tasks
gulp.task("buildheaders", (cb) => {
const content = fs.readFileSync(cfg.src.meta, "utf8");
let newContent = "";
for (const l of content.split("\n")) {
let newline = l;
for (const k of Object.keys(cfg.headers.common)) {
if (l.indexOf(`@${k} `) == 3) {
newline = `// @${k} ${cfg.headers.common[k]}`;
break;
}
}
for (const k of Object.keys(cfg.headers[status.mode])) {
if (l.indexOf(`@${k} `) == 3) {
newline = `// @${k} ${cfg.headers[status.mode][k]}`;
break;
}
}
newContent += newline + "\n";
}
// XXX just append to the version rather than overwriting a fixed string now
const gbd = () => {
const d = new Date();
let bd = d.getFullYear();
let t = ("0" + (d.getMonth() + 1)).substr(-2);
bd += t;
t = ("0" + d.getDate()).substr(-2);
bd += t;
t = ("0" + d.getHours()).substr(-2);
bd += t;
t = ("0" + d.getMinutes()).substr(-2);
bd += t;
t = ("0" + d.getSeconds()).substr(-2);
bd += t;
return bd;
};
newContent = newContent.replace("BUILDDATE", gbd());
status.headers = newContent;
cb();
});
gulp.task("webpack", (callback) => {
const webpackConfig = require("./webpack.config.js");
if (status.mode === "prod") {
webpackConfig.mode = "production";
webpackConfig.devtool = "nosources-source-map";
}
if (status.mode === "dev") {
webpackConfig.mode = "development";
webpackConfig.devtool = "eval-source-map";
// webpackConfig.optimization.minimize = true;
}
webpack(webpackConfig, function (err, stats) {
log(
"[webpack]",
stats.toString({
// output options
})
);
if (err) {
throw new PluginError("webpack", err);
}
callback();
});
});
gulp.task("buildplugin", (cb) => {
const destination = cfg.releaseFolder[status.mode];
gulp
.src(cfg.src.plugin)
// prepend headers
.pipe(contents.prepend(status.headers))
// inject files
.pipe(
injectfile({
pattern: "\\/\\*+\\s*inject:\\s*<filename>\\s*\\*+\\/",
})
)
// trim leading spaces
.pipe(trimlines({ leading: false }))
// rename and save
.pipe(rename(cfg.pluginName))
.pipe(gulp.dest(destination));
cb();
});
gulp.task("buildmeta", (cb) => {
const p = path.join(cfg.releaseFolder[status.mode], cfg.metaName);
ensureDirectoryExistence(p);
fs.writeFile(p, status.headers, (err) => {
cb(err);
});
});
// ESLint
gulp.task("eslint", (cb) => {
gulp
.src([
"**/*.js",
"!node_modules/**",
"!dist/**",
"!releases/**",
"!src/lib/**",
])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
cb();
});
gulp.task("eslint-fix", (cb) => {
gulp
.src([
"**/*.js",
"!node_modules/**",
"!dist/**",
"!releases/**",
"!src/lib/**",
])
.pipe(eslint({ fix: true }))
.pipe(eslint.format())
.pipe(eslint.failAfterError())
.pipe(gulp.dest("."));
cb();
});
gulp.task("prettier", () => {
return gulp
.src([
"**/*.js",
"!node_modules/**",
"!dist/**",
"!releases/**",
"!src/lib/**",
])
.pipe(prettier())
.pipe(gulp.dest("."));
});
// eslint at the end to catch unused variables, etc
gulp.task(
"build",
gulp.series(["buildheaders", "buildmeta", "webpack", "buildplugin", "eslint"])
);
// eslint-fix too
gulp.task("format", gulp.series(["prettier", "eslint-fix"]));
// gulp.task("format", gulp.series(["prettier"]));
gulp.task(
"build-dev",
gulp.series(["set-mode-dev", "format", "build", "clear"])
);
gulp.task(
"build-prod",
gulp.series(["set-mode-prod", "format", "build", "clear"])
);
gulp.task("default", gulp.series(["build-dev"]));
gulp.task("clean", () => del(["releases/*"]));