-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
155 lines (145 loc) · 3.82 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
const { series, watch, parallel, src, dest } = require("gulp");
const electron = require("electron");
const proc = require("child_process");
const argv = require("yargs").argv;
const babel = require("gulp-babel");
const alias = require("gulp-path-alias");
const path = require("path");
const del = require("del");
const stream = require("stream");
const TARGET_DIR = path.join(__dirname, "target");
function createTransformTS(
glob = ["src/**/**.ts", "src/**/**.tsx"],
destDir = TARGET_DIR
) {
return function transfromTS(done) {
stream.pipeline(
src(glob),
alias({
paths: {
"@": path.join(__dirname, "./src"),
},
}),
babel({
presets: [
[
"@babel/preset-env",
{
targets: {
electron: "12",
},
},
],
"@babel/preset-typescript",
"@babel/preset-react",
],
comments: false,
}),
dest(destDir),
() => {
done();
}
);
};
}
const copy = parallel(
runner("copyProto", () => src("./src/**/**.proto").pipe(dest(TARGET_DIR))),
runner("copyHtml", () => src("./src/**/**.html").pipe(dest(TARGET_DIR))),
runner("copyCss", () => src("./src/**/**.css").pipe(dest(TARGET_DIR)))
);
function dev(doneDev) {
const instanceCount = parseInt(argv.instanceCount) || 1;
let children = [];
let watcher = null;
function clearChildren(done) {
if (children && children.length) {
children.forEach((child) => {
child.removeAllListeners();
child.kill();
});
children = [];
}
done();
}
function makeChildren() {
return parallel(
Array.from({ length: instanceCount }).map((_v, k) =>
createSpawnElectron(k)
)
);
}
function createSpawnElectron(alias) {
return function spawnElectron(done) {
child = proc.spawn(electron, [TARGET_DIR]);
child.stdout
.pipe(
new stream.Transform({
transform(chunk, enc, callback) {
this.push("instantce[" + alias + "]: " + String(chunk));
callback();
},
})
)
.pipe(process.stdout);
child.stderr
.pipe(
new stream.Transform({
transform(chunk, enc, callback) {
this.push("error:instantce[" + alias + "]: " + String(chunk));
callback();
},
})
)
.pipe(process.stderr);
children.push(child);
child.on("exit", (code) => {
const index = children.findIndex((one) => one === child);
children.splice(index, 1);
if (children && !children.length) {
doneDev && doneDev(code);
watcher && watcher.close();
process.exit(code);
}
});
done();
};
}
series(clean, series(parallel(copy, createTransformTS()), makeChildren()))();
watcher = watch(["./src"]);
watcher.on("change", function (filepath) {
console.log("[watch] file %s changed", filepath);
series(
/\.tsx?$/.test(filepath)
? createTransformTS(
filepath,
path.parse(
path.join(
TARGET_DIR,
path.relative(path.join(__dirname, "src"), filepath)
)
).dir
)
: copy,
clearChildren,
makeChildren()
)();
});
}
function clean(done) {
del.sync([TARGET_DIR], done());
}
function build(done) {
const child = proc.spawn("electron-builder", process.argv.slice(2));
child.stdout.pipe(process.stdout);
child.on("exit", (code) => {
done(code);
});
}
function runner(displayName, callback) {
callback.displayName = displayName;
return callback;
}
exports.dev = dev;
exports.compile = parallel(copy, createTransformTS());
exports.clean = clean;
exports.build = build;