forked from b0o/surfingkeys-conf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
290 lines (247 loc) · 8.96 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
const gulp = require("gulp")
const { task, src, dest } = gulp
const { parallel, series } = gulp
const parcel = require("gulp-parcel")
const replace = require("gulp-replace")
const rename = require("gulp-rename")
const eslint = require("gulp-eslint")
const file = require("gulp-file")
const path = require("path")
const del = require("del")
const platforms = require("platform-folders")
const fs = require("fs").promises
const fetch = require("node-fetch")
const http = require("http")
const gulpIf = require("gulp-if")
const { COPYFILE_EXCL } = require("fs").constants
const { URL } = require("url")
let srcFilesLoaded = false
let compl
let conf
let keys
let util
const requireSrcFiles = () => {
if (srcFilesLoaded) {
return
}
compl = require("./completions") // eslint-disable-line global-require
conf = require("./conf") // eslint-disable-line global-require
keys = require("./keys") // eslint-disable-line global-require
util = require("./util") // eslint-disable-line global-require
srcFilesLoaded = true
}
const paths = {
scripts: ["conf.priv.js", "completions.js", "conf.js", "actions.js", "help.js", "keys.js", "util.js"],
entry: "conf.js",
gulpfile: "gulpfile.js",
readme: "README.tmpl.md",
assets: "assets",
screenshots: "assets/screenshots",
favicons: "assets/favicons",
readmeOut: "README.md",
scriptOut: "surfingkeys.js",
installDir: platforms.getConfigHome(),
}
const servePort = 9919
// This notice will be injected into the generated README.md file
const disclaimer = `\
<!--
NOTICE:
This is an automatically generated file - Do not edit it directly.
The source file is ${paths.readme}
-->`
task("clean", () => del(["build", ".cache", ".tmp-gulp-compile-*"]))
task("clean-favicons", () => del([paths.favicons]))
const lint = (globs, opts = {}) => gulp.src(globs)
.pipe(eslint(opts))
.pipe(eslint.format())
task("lint", () => lint([...paths.scripts, paths.gulpfile]))
task("lint-fix", () => lint([...paths.scripts, paths.gulpfile], { fix: true })
.pipe(gulpIf(
(f) => f.eslint !== undefined && f.eslint.fixed === true,
gulp.dest((f) => f.base),
)))
task("check-priv", async () => {
try {
await fs.stat("./conf.priv.js")
} catch (e) {
// eslint-disable-next-line no-console
console.log("Notice: Initializing ./conf.priv.js - configure your API keys here.")
return fs.copyFile("./conf.priv.example.js", "./conf.priv.js", COPYFILE_EXCL)
}
return Promise.resolve()
})
task("docs", parallel(async () => {
requireSrcFiles()
const screens = {}
let screenshotList = ""
const screenshots = await fs.readdir(path.join(__dirname, paths.screenshots))
screenshots.forEach((s) => {
const name = path.basename(s, ".png").split("-")
const alias = name[0]
if (!screens[alias]) {
screens[alias] = []
}
screens[alias].push(path.join(paths.screenshots, path.basename(s)))
})
let complTable = Object.keys(compl).sort((a, b) => {
if (a < b) return -1
if (a > b) return 1
return 0
})
let keysTable = Object.keys(keys.maps).sort((a, b) => {
if (a === "global") return -1
if (b === "global") return 1
if (a < b) return -1
if (a > b) return 1
return 0
})
complTable = await complTable.reduce(async (acc1p, k) => {
const acc1 = await acc1p
const c = compl[k]
const u = new URL(c.domain ? `https://${c.domain}` : c.search)
const domain = u.hostname
let s = ""
if (screens[c.alias]) {
screens[c.alias].forEach((url, i) => {
const num = (i > 0) ? ` ${i + 1}` : ""
s += `<a href="#${c.name}${num.replace(" ", "-")}">:framed_picture:</a>`
screenshotList += `##### ${c.name}${num}\n`
screenshotList += `![${c.name} screenshot](./${url})\n\n`
})
}
const faviconExt = c.favicon ? path.extname(new URL(c.favicon).pathname) : ".ico"
const favicon = `<img src="./assets/favicons/${u.hostname}${faviconExt}" width="16px"> `
return `${acc1}
<tr>
<td><a href="${u.protocol}//${domain}">${favicon}</a></td>
<td><code>${c.alias}</code></td>
<td>${c.name}</td>
<td><a href="${u.protocol}//${domain}">${domain}</a></td>
<td>${s}</td>
</tr>`
}, Promise.resolve(""))
keysTable = await keysTable.reduce(async (acc1p, domain) => {
const acc1 = await acc1p
const header = "<tr><td><strong>Mapping</strong></td><td><strong>Description</strong></td></tr>"
const c = keys.maps[domain]
const maps = c.reduce((acc2, map) => {
let leader = ""
if (typeof map.leader !== "undefined") {
leader = map.leader // eslint-disable-line prefer-destructuring
} else if (domain === "global") {
leader = ""
} else {
leader = conf.siteleader
}
const mapStr = util.escape(`${leader}${map.alias}`.replace(" ", "<space>"))
return `${acc2}<tr><td><code>${mapStr}</code></td><td>${map.description}</td></tr>\n`
}, "")
let domainStr = "<strong>global</strong>"
const favicon = `<img src="./assets/favicons/${domain}.ico" width="16px"> `
if (domain !== "global") {
domainStr = `<a href="//${domain}">${favicon}${domain}</a>`
}
return `${acc1}<tr><th colspan="2">${domainStr}</th></tr>${header}\n${maps}`
}, Promise.resolve(""))
return src([paths.readme])
.pipe(replace("<!--{{DISCLAIMER}}-->", disclaimer))
.pipe(replace("<!--{{COMPL_COUNT}}-->", Object.keys(compl).length))
.pipe(replace("<!--{{COMPL_TABLE}}-->", complTable))
.pipe(replace("<!--{{KEYS_MAPS_COUNT}}-->", Object.keys(keys.maps).reduce((acc, m) => acc + m.length, 0)))
.pipe(replace("<!--{{KEYS_SITES_COUNT}}-->", Object.keys(keys.maps).length))
.pipe(replace("<!--{{KEYS_TABLE}}-->", keysTable))
.pipe(replace("<!--{{SCREENSHOTS}}-->", screenshotList))
.pipe(rename(paths.readmeOut))
.pipe(dest("."))
}))
const getFavicon = async ({ domain, favicon }, timeout = 5000) => {
const url = favicon
let data
const ext = path.extname(new URL(favicon).pathname)
try {
const res = await fetch(url, { timeout })
if (!res.ok) {
throw new Error(`request to ${url} failed with code ${res.status}`)
}
data = await res.buffer()
} catch (e) {
process.stdout.write(`no favicon found for ${url}: ${e}\n`)
// transparent pixel
data = Buffer.from(
"AAABAAEAAQEAAAEAIAAwAAAAFgAAACgAAAABAAAAAgAAAAEAIAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAA==",
"base64"
)
}
return {
name: `${domain}${ext}`,
source: data,
}
}
task("favicons", series("clean-favicons", async () => {
requireSrcFiles()
const sites = [].concat(
// search engine completions
Object.entries(compl)
.map(([, v]) => ({
domain: new URL(v.domain ? `https://${v.domain}` : v.search).hostname,
favicon: v.favicon ? v.favicon : `${new URL(v.domain ? `https://${v.domain}` : v.search).origin}/favicon.ico`,
})),
// site-specific keybindings
Object.keys(keys.maps)
.filter((k) => k !== "global")
.map((k) => ({
domain: k,
favicon: `${new URL(`https://${k}`).origin}/favicon.ico`,
})),
).filter((e, i, arr) => i === arr.indexOf(e)) // Keep only first occurrence of each element
const favicons = (await Promise.all(sites.map(async (site) => getFavicon(site))))
.filter((e) => e !== undefined)
return file(favicons, { src: true })
.pipe(dest(paths.favicons))
}))
task("docs-full", parallel("docs", "favicons"))
task("build",
series(
parallel(
"check-priv",
"clean",
),
parallel(
"lint",
() => src(paths.entry, { read: false })
.pipe(parcel())
.pipe(rename(paths.scriptOut))
.pipe(dest("build")),
)
))
task("dist", parallel("docs-full", "build"))
task("install", series("build", () => src(path.join("build", paths.scriptOut))
.pipe(dest(paths.installDir))))
const watch = (g, t) => () =>
gulp.watch(g, { ignoreInitial: false, usePolling: true }, t)
task("watch-build", watch(paths.scripts, series("build")))
task("watch-install", watch(paths.scripts, series("install")))
task("watch-lint", watch([...paths.scripts, paths.gulpfile], series("lint")))
task("watch-docs", watch([...paths.scripts, paths.readme], series("docs")))
task("watch-docs-full", watch([...paths.scripts, paths.readme], series("docs-full")))
const serve = (done) => {
const srv = http.createServer(async (req, res) => {
console.log(`${new Date().toISOString()} ${req.method} ${req.url}`) // eslint-disable-line no-console
res.writeHead(200, {
"Content-Type": "text/javascript; charset=UTF-8",
})
res.end(await fs.readFile(path.join("build", paths.scriptOut)))
})
srv.listen(servePort)
console.log(`web server is listening on port ${servePort}`) // eslint-disable-line no-console
srv.on("close", () => {
console.log("web server is closing...") // eslint-disable-line no-console
done()
})
}
task("serve-simple", serve)
task("serve-build", parallel("watch-build", "serve-simple"))
task("serve", series("serve-build"))
task("watch", series("watch-install"))
task("default", series("build"))