-
Notifications
You must be signed in to change notification settings - Fork 29
/
gulpfile.ts
241 lines (217 loc) · 7.04 KB
/
gulpfile.ts
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
239
240
241
/* eslint-disable jsdoc/require-returns */
import { ChildProcess, exec, spawn, SpawnOptions } from 'child_process'
import fkill from 'fkill'
import fs from 'fs'
import fse from 'fs-extra'
import { parallel, series } from 'gulp'
import path from 'path'
import { promisify } from 'util'
import xml2js from 'xml2js'
const asyncExec = promisify(exec)
const asyncWriteFile = promisify(fs.writeFile)
const BUILD_DIR = path.join(__dirname, 'build')
const APPX_ASSETS = path.join(BUILD_DIR, 'appx')
const SPACE_EYE_ICONS = path.join(__dirname, 'node_modules', 'space-eye-icons', 'dist')
const NODE_MODULES_BIN = path.join(__dirname, 'node_modules', '.bin')
const EXTENDED_PATH = NODE_MODULES_BIN + path.delimiter + (process.env.PATH ?? '')
const DIST = path.join(__dirname, 'dist')
const RELEASE = path.join(__dirname, 'release')
const MAIN_DIST = path.join(DIST, 'main.js')
const LEGAL_NOTICES = path.join(DIST, 'legal_notices.txt')
const BADGES_DIR = path.join(__dirname, 'docs', 'img', 'badges')
const defaultSpawnOptions: SpawnOptions = {
env: {
PATH: EXTENDED_PATH
},
stdio: 'inherit',
shell: true
}
const prodSpawnOptions: SpawnOptions = {
...defaultSpawnOptions,
env: {
...defaultSpawnOptions.env,
NODE_ENV: 'production'
}
}
/**
* Run Webpack with a config.
*
* @param config - Path to config to use
* @param production - Whether the node environment should be production
*/
function runWebpack(config: string, production = false): ChildProcess {
const options = production ? prodSpawnOptions : defaultSpawnOptions
return spawn('webpack', ['--config', config], options)
}
/**
* Build the main process code.
*/
function buildMain(): ChildProcess {
return runWebpack('webpack.main.prod.config.js', true)
}
/**
* Build the renderer process code.
*/
function buildRenderer(): ChildProcess {
return runWebpack('webpack.renderer.prod.config.js', true)
}
/**
* Generate the application license report from used packages.
*/
async function generateLicenseReport() {
const res = await asyncExec('yarn --silent licenses generate-disclaimer', {
maxBuffer: 1024 * 50000
})
await asyncWriteFile(LEGAL_NOTICES, res.stdout)
}
/**
* Copy icon asset files to the build dir for electron builder to access.
*/
async function copyIconAssets() {
// Delete old appx assets if they exist, then copy the new
await fse.remove(APPX_ASSETS)
await fse.copy(path.join(SPACE_EYE_ICONS, 'appx'), APPX_ASSETS)
}
/**
* Build for distribution with electron-builder.
*/
function buildDist() {
// If on Windows, clear out the old release dir (causes lock problems otherwise)
if (process.platform === 'win32') {
fse.emptyDir(RELEASE)
}
return spawn('electron-builder', defaultSpawnOptions)
}
/**
* Build main process code in dev mode.
*/
function buildMainDev(): ChildProcess {
return runWebpack('webpack.main.config.js')
}
/**
* Start running electron in dev mode.
*/
function startElectronDev(): ChildProcess {
return spawn('electron', [`"${MAIN_DIST}"`], defaultSpawnOptions)
}
/**
* Start the renderer process webpack dev server.
*/
function startRendererDevServer(): ChildProcess {
return spawn(
'webpack-dev-server',
['--config', 'webpack.renderer.dev.config.js'],
defaultSpawnOptions
)
}
/**
* Start live dev mode.
*
* @param done - Signal task is done
*/
function startDev(done: (error?: any) => void) {
// Start building main and start the renderer dev server
const rendererDevServer = startRendererDevServer()
const mainBuilder = buildMainDev()
// Wait until main finishes building
mainBuilder.on('close', code => {
// Stop the dev server and exit if main build didn't exit cleanly
if (code !== 0) {
fkill(rendererDevServer.pid, { force: process.platform === 'win32' })
done()
return
}
// Else, start electron dev
const electronDev = startElectronDev()
// When stopped, stop the renderer dev server
electronDev.on('close', () => {
fkill(rendererDevServer.pid, { force: process.platform === 'win32' })
done()
})
})
}
/**
* Create the dist dir if it doesn't exist.
*/
async function ensureDist() {
await fse.ensureDir(DIST)
}
/**
* Add the start-on-login extension to the APPX manifest after it is created.
*
* @param manifest - The parsed manifest
*/
function addAppxStartupExtension(manifest: any) {
// If an "Extensions" key doesn't exist, create it
const application = manifest.Package.Applications[0].Application[0]
if (!('Extensions' in application)) {
application.Extensions = [{}]
}
const extensions = application.Extensions[0]
// Add the startup task extension
extensions['desktop:Extension'] = [
{
$: {
Category: 'windows.startupTask',
Executable: application.$.Executable,
EntryPoint: 'Windows.FullTrustApplication'
},
'desktop:StartupTask': [
{
$: {
TaskId: 'SpaceEyeStartup',
Enabled: 'false',
DisplayName: 'SpaceEye'
}
}
]
}
]
}
export const appxManifestCreated = async function(): Promise<void> {
const manifest = process.argv[4]
// Read and parse the manifest
const content = await fse.readFile(manifest)
const xml = await xml2js.parseStringPromise(content.toString())
// Call modification functions
addAppxStartupExtension(xml)
// Build back to XML
const builder = new xml2js.Builder({ headless: true })
let rebuilt = builder.buildObject(xml)
// Add a custom header (need for suppression comment)
rebuilt = `<?xml version="1.0" encoding="utf-8"?>\n<!--suppress XmlUnusedNamespaceDeclaration -->\n${rebuilt}`
await fse.writeFile(manifest, rebuilt)
}
/**
* Download and save a file using curl.
*
* @param url - URL to download
* @param output - Output path to save file to
*/
async function curlFile(url: string, output: string) {
await asyncExec(`curl "${url}" -o "${output}"`)
}
/**
* Cache certain repo badges to reduce remote requests.
*/
export const cacheBadges = async function(): Promise<void> {
await Promise.all([
curlFile(
'https://img.shields.io/github/v/release/KYDronePilot/SpaceEye?label=latest%20release',
path.join(BADGES_DIR, 'latest-release.svg')
),
curlFile(
'https://img.shields.io/badge/platforms-macOS%20%7C%20Windows-lightgrey',
path.join(BADGES_DIR, 'supported-platforms.svg')
),
curlFile(
'https://img.shields.io/github/license/KYDronePilot/SpaceEye',
path.join(BADGES_DIR, 'license.svg')
)
])
}
export const build = parallel(buildMain, buildRenderer)
const buildCi = series(ensureDist, parallel(build, generateLicenseReport, copyIconAssets))
exports['build-ci'] = buildCi
exports['start-dev'] = startDev
exports.dist = series(buildCi, buildDist)