-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
187 lines (169 loc) · 5 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
const { src, watch, series, parallel } = require("gulp");
const fs = require("fs-extra");
const Bundler = require("parcel-bundler");
const { spawn } = require("child_process");
const util = require("util");
const exec = util.promisify(require("child_process").exec);
const packager = require("electron-packager");
const createDMG = require("electron-installer-dmg");
const createEXE = require("electron-installer-windows");
const path = require("path");
const debug = !!process.env.DEBUG_E;
const DARWIN_PACKAGER_OPTIONS = {
name: "Cloud Manager Desktop",
dir: "./",
platform: "darwin",
arch: "x64",
overwrite: true,
icon: "icon/icon",
ignore: [
// ignored from the produced electron app when packaging.
".cache",
".vscode",
"app",
"@adobe/coral-spectrum",
"@adobe/spectrum-css",
"vue",
"popper.js",
"core-js"
].map(toWildRegex)
};
const WIN_PACKAGER_OPTIONS = Object.assign({}, DARWIN_PACKAGER_OPTIONS, {
platform: "win32"
});
const PRETTIER_FILES = [
"main.js",
"preload.js",
"index.html",
"gulpfile.js",
"./app/**/*.js",
"./app/**/*.ts",
"./app/**/*.vue",
"!./app/client/typescript-axios/*"
];
// makes any passed string a wile regex. Example: test => /.*test.*/g
function toWildRegex(str) {
var escaped = str.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
return new RegExp(`.*${escaped}.*`, "g");
}
// spawn proccess and pipe output to stdout and stderr
function spawnAndLog(command, params) {
var env = Object.create(process.env);
const cp = spawn(command, params, { env: env, shell: process.platform == "win32" });
if (debug) {
cp.stdout.pipe(process.stdout);
}
cp.stderr.pipe(process.stderr);
return cp;
}
async function execAndLog(command) {
var env = Object.create(process.env);
const cp = await exec(command, { env: env });
if (debug) {
console.log(cp.stdout);
}
console.error(cp.stderr);
return cp;
}
function prettierTask(watchFiles) {
const prettify = filePath => {
if (!filePath) return;
execAndLog(`prettier --write ${filePath}`);
};
return (prettier = cb => {
if (watchFiles) {
watcher = watch(PRETTIER_FILES);
"change add unlink".split(" ").forEach(e => watcher.on(e, prettify));
} else {
src(PRETTIER_FILES).on("data", file => prettify(file.path));
}
cb();
});
}
function copyAssetsTask(cb) {
return fs.copy("node_modules/@adobe/coral-spectrum/dist/resources", "dist/resources");
}
function bundleTask(watch) {
const publicUrl = path.join(__dirname, "dist");
const bundler = new Bundler("./app/main.ts", {
watch: !!watch,
hmrHostname: "localhost",
publicUrl: publicUrl
});
return (bundle = () => bundler.bundle());
}
function electronTask(debug) {
return (electron = () => {
process.env.DEBUG_E = debug;
return spawnAndLog("electron", ["."]);
});
}
async function packageDarwinTask(cb) {
const appPaths = await packager(DARWIN_PACKAGER_OPTIONS);
console.log(`Electron app bundles created:\n${appPaths.join("\n")}`);
cb();
}
async function packageWinTask(cb) {
const appPaths = await packager(WIN_PACKAGER_OPTIONS);
console.log(`Electron app bundles created:\n${appPaths.join("\n")}`);
cb();
}
function dmgTask(options) {
return (dmg = cb => {
const appFolder = `${options.name}-${options.platform}-${options.arch}`;
const appName = `${options.name}.app`;
createDMG(
{
appPath: path.join(appFolder, appName),
out: appFolder,
title: options.name,
name: options.name,
icon: `${options.icon}.icns`,
overwrite: true
},
err => {
console.error(err);
console.log(`Electron DMG created`);
}
);
cb();
});
}
function exeTask(options) {
return (exe = cb => {
const appFolder = `${options.name}-${options.platform}-${options.arch}`;
const appName = `${options.name}.exe`;
createEXE(
{
src: appFolder,
dest: appFolder,
exe: appName,
title: options.name,
name: options.name,
icon: `${options.icon}.ico`,
overwrite: true
},
err => {
if (err) {
console.error(err);
}
console.log(`Electron EXE created`);
}
);
cb();
});
}
exports["prettier"] = prettierTask(false);
exports["prettier:watch"] = prettierTask(true);
exports["ui:build"] = series(this["prettier"], copyAssetsTask, bundleTask());
exports["ui:watch"] = series(this["prettier:watch"], copyAssetsTask, bundleTask(true));
exports["electron:watch"] = parallel(this["prettier:watch"], this["ui:watch"], electronTask(false));
exports["electron:watch:debug"] = parallel(
this["prettier:watch"],
this["ui:watch"],
electronTask(true)
);
exports["electron:package:darwin"] = series(this["ui:build"], packageDarwinTask);
exports["electron:package:win"] = series(this["ui:build"], packageWinTask);
exports["electron:dmg"] = series(this["electron:package:darwin"], dmgTask(DARWIN_PACKAGER_OPTIONS));
exports["electron:exe"] = series(this["electron:package:win"], exeTask(WIN_PACKAGER_OPTIONS));