-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgulpfile.js
238 lines (193 loc) · 5.56 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
"use strict";
/* eslint-disable node/no-unpublished-require */
const fs = require("fs");
const gulp = require("gulp");
const gutil = require("gulp-util");
const del = require("del");
const NwBuilder = require("nw-builder");
const archiver = require("archiver");
const runSequence = require("run-sequence");
/* eslint-enable node/no-unpublished-require */
const platforms = {
win: ["win32", "win64"],
osx: ["osx32", "osx64"],
linux: ["linux32", "linux64"]
};
const platformsArray = (() => {
return Object.keys(platforms).reduce((prev, current) => {
return prev.concat(current);
}, []);
})();
let currentTarget;
switch (process.platform) {
case "win32":
currentTarget = "win";
break;
case "darwin":
currentTarget = "osx";
break;
case "linux":
currentTarget = "linux";
break;
default:
currentTarget = "";
break;
}
if (currentTarget) {
switch (process.arch) {
case "x64":
currentTarget += "64";
break;
case "ia32":
currentTarget += "32";
break;
default:
currentTarget = "";
break;
}
}
function readFile(file, encoding) {
return new Promise((resolve, reject) => {
encoding = encoding || "utf8";
fs.readFile(file, encoding, (err, data) => {
if (err) {
reject(err);
return;
}
resolve(data);
});
});
}
function writeFile(file, data, encoding) {
return new Promise((resolve, reject) => {
encoding = encoding || "utf8";
fs.writeFile(file, data, encoding, err => {
if (err) {
reject(err);
return;
}
resolve();
});
});
}
function readJSON(file, encoding) {
return readFile(file, encoding).then(data => JSON.parse(data));
}
function writeJSON(file, data, encoding) {
return writeFile(file, JSON.stringify(data, null, " ") + "\n", encoding);
}
function cleanList(target) {
const list = [];
const parent = "build/jikkyo";
if (target === "all") {
list.push(parent);
} else {
list.push(`${parent}/${target}`);
list.push(`${parent}/*-${target}.zip`);
}
return list;
}
function nw(targets) {
const nwb = new NwBuilder({
files: "src/**/*",
version: "0.12.3",
platforms: targets,
buildDir: "build",
cacheDir: "cache",
macCredits: "Credits.html",
macIcns: "src/images/jikkyo.icns",
winIco: "src/images/jikkyo.ico",
zip: false,
flavor: "normal"
});
nwb.on("log", msg => {
if (!/^Zipping/.test(msg))
gutil.log("nw-builder", msg);
});
return nwb.build();
}
function copyList(target) {
const files = ["README.md", "LICENSE"];
if (target.includes("win")) {
files.push("attachment/jikkyo_ct.cmd");
} else if (target.includes("osx")) {
files.push("attachment/jikkyo_ct.command");
} else if (target.includes("linux")) {
files.push("attachment/jikkyo_ct.sh");
}
return files;
}
function pack(targets) {
return readJSON("package.json").then(json => {
const version = json.version;
return Promise.all(targets.map(target => {
return new Promise((resolve, reject) => {
const dir = `build/jikkyo/${target}`;
const name = `jikkyo-v${version}-${target}`;
const out = `build/jikkyo/${name}.zip`;
const archive = archiver("zip");
const output = fs.createWriteStream(out);
output.on("close", resolve);
archive.on("error", reject);
archive.pipe(output);
archive.directory(dir, name)
.finalize();
});
}));
});
}
function release(target) {
return new Promise((resolve, reject) => {
runSequence(`clean:${target}`, `nw:${target}`, `copy:${target}`, `package:${target}`, err => {
if (err) {
reject(err);
return;
}
resolve();
});
});
}
gulp.task("sync", () => {
return Promise.all([
readJSON("package.json"),
readJSON("src/package.json")
]).then(jsons => {
const pkg = jsons[0];
const srcPkg = jsons[1];
srcPkg.name = pkg.name;
srcPkg.version = pkg.version;
srcPkg.description = pkg.description;
srcPkg.repository = pkg.repository;
srcPkg.homepage = pkg.homepage;
return writeJSON("src/package.json", srcPkg);
});
});
gulp.task("clean:all", () => del(cleanList("all")));
gulp.task("nw:all", () => nw(platformsArray));
gulp.task("copy:all", Object.keys(platforms).map(platform => `copy:${platform}`));
gulp.task("package:all", Object.keys(platforms).map(platform => `package:${platform}`));
gulp.task("release:all", () => release("all"));
Object.keys(platforms).forEach(platform => {
gulp.task(`clean:${platform}`, platforms[platform].map(target => `clean:${target}`));
gulp.task(`nw:${platform}`, () => nw(platforms[platform]));
gulp.task(`copy:${platform}`, platforms[platform].map(target => `copy:${target}`));
gulp.task(`package:${platform}`, platforms[platform].map(target => `package:${target}`));
gulp.task(`release:${platform}`, () => release(platform));
platforms[platform].forEach(target => {
gulp.task(`clean:${target}`, () => del(cleanList(target)));
gulp.task(`nw:${target}`, () => nw([target]));
gulp.task(`copy:${target}`, () => {
return gulp.src(copyList(target)).pipe(gulp.dest(`build/jikkyo/${target}`));
});
gulp.task(`package:${target}`, () => pack([target]));
gulp.task(`release:${target}`, () => release(target));
});
});
gulp.task("clean", ["clean:all"]);
if (currentTarget) {
gulp.task("nw", [`nw:${currentTarget}`]);
gulp.task("copy", [`copy:${currentTarget}`]);
gulp.task("package", [`package:${currentTarget}`]);
gulp.task("release", [`release:${currentTarget}`]);
}
gulp.task("default", ["nw"]);