2017-12-02 02:48:58 +00:00
|
|
|
const gulp = require("gulp")
|
2018-08-13 02:18:52 +00:00
|
|
|
const parcel = require("gulp-parcel")
|
2017-12-02 02:48:58 +00:00
|
|
|
const replace = require("gulp-replace")
|
|
|
|
const rename = require("gulp-rename")
|
|
|
|
const eslint = require("gulp-eslint")
|
|
|
|
const path = require("path")
|
|
|
|
const del = require("del")
|
|
|
|
const os = require("os")
|
|
|
|
const fs = require("fs")
|
|
|
|
const { spawn } = require("child_process")
|
|
|
|
const { URL } = require("url")
|
2017-08-28 03:24:24 +00:00
|
|
|
|
2018-11-10 01:47:39 +00:00
|
|
|
const compl = require("./completions")
|
|
|
|
const conf = require("./conf")
|
|
|
|
const keys = require("./keys")
|
|
|
|
const util = require("./util")
|
|
|
|
|
2017-12-02 02:48:58 +00:00
|
|
|
const paths = {
|
2018-11-03 00:21:44 +00:00
|
|
|
scripts: ["conf.priv.js", "completions.js", "conf.js", "actions.js", "help.js", "keys.js", "util.js"],
|
2018-08-13 02:18:52 +00:00
|
|
|
entry: "conf.js",
|
2017-12-02 02:48:58 +00:00
|
|
|
gulpfile: ["gulpfile.js"],
|
|
|
|
readme: ["README.tmpl.md"],
|
|
|
|
screenshots: "assets/screenshots",
|
|
|
|
}
|
2017-08-28 03:24:24 +00:00
|
|
|
|
2017-10-31 07:27:41 +00:00
|
|
|
// This notice will be injected into the generated README.md file
|
2017-10-31 05:39:21 +00:00
|
|
|
const disclaimer = `\
|
|
|
|
<!--
|
|
|
|
|
|
|
|
NOTICE:
|
|
|
|
This is an automatically generated file - Do not edit it directly.
|
|
|
|
The source file is README.tmpl.md
|
|
|
|
|
2017-12-02 02:48:58 +00:00
|
|
|
-->`
|
2017-10-31 05:39:21 +00:00
|
|
|
|
2017-12-02 02:48:58 +00:00
|
|
|
gulp.task("gulp-autoreload", () => {
|
|
|
|
let p
|
|
|
|
const spawnChildren = function spawnChildren() {
|
|
|
|
if (p) p.kill()
|
|
|
|
p = spawn("gulp", ["lint-gulpfile", "install", "watch-nogulpfile"], { stdio: "inherit" })
|
|
|
|
}
|
|
|
|
gulp.watch("gulpfile.js", spawnChildren)
|
|
|
|
spawnChildren()
|
|
|
|
})
|
2017-11-17 14:29:09 +00:00
|
|
|
|
2018-08-13 02:18:52 +00:00
|
|
|
gulp.task("clean", () => del(["build", ".cache", ".tmp-gulp-compile-*"]))
|
2017-08-28 03:24:24 +00:00
|
|
|
|
2017-12-02 02:48:58 +00:00
|
|
|
gulp.task("lint", () =>
|
|
|
|
gulp
|
|
|
|
.src([].concat(paths.scripts, paths.gulpfile))
|
|
|
|
.pipe(eslint())
|
|
|
|
.pipe(eslint.format()))
|
2017-09-17 23:25:38 +00:00
|
|
|
|
2017-12-02 02:48:58 +00:00
|
|
|
gulp.task("lint", () =>
|
|
|
|
gulp
|
|
|
|
.src(paths.gulpfile)
|
|
|
|
.pipe(eslint())
|
|
|
|
.pipe(eslint.format()))
|
2017-11-17 14:29:09 +00:00
|
|
|
|
2018-08-23 10:47:38 +00:00
|
|
|
gulp.task("check-priv", () => {
|
|
|
|
try {
|
|
|
|
fs.statSync("./conf.priv.js")
|
|
|
|
} catch (e) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log("Creating ./conf.priv.js based on ./conf.priv.example.js")
|
|
|
|
fs.copyFileSync("./conf.priv.example.js", "./conf.priv.js", fs.constants.COPYFILE_EXCL)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
gulp.task("build", ["check-priv", "clean", "lint", "readme"], () => gulp.src(paths.entry, { read: false })
|
2018-08-13 02:18:52 +00:00
|
|
|
.pipe(parcel())
|
|
|
|
.pipe(rename(".surfingkeys"))
|
2017-12-02 02:48:58 +00:00
|
|
|
.pipe(gulp.dest("build")))
|
2017-08-28 03:24:24 +00:00
|
|
|
|
2017-12-02 02:48:58 +00:00
|
|
|
gulp.task("install", ["build"], () => gulp.src("build/.surfingkeys")
|
|
|
|
.pipe(gulp.dest(os.homedir())))
|
2017-08-28 03:24:24 +00:00
|
|
|
|
2017-12-02 02:48:58 +00:00
|
|
|
gulp.task("watch", () => {
|
|
|
|
gulp.watch([].concat(paths.scripts, paths.gulpfile), ["readme", "install"])
|
|
|
|
gulp.watch(paths.readme, ["readme"])
|
|
|
|
})
|
2017-11-17 14:29:09 +00:00
|
|
|
|
2017-12-02 02:48:58 +00:00
|
|
|
gulp.task("watch-nogulpfile", () => {
|
|
|
|
gulp.watch([].concat(paths.scripts), ["readme", "install"])
|
|
|
|
gulp.watch(paths.readme, ["readme"])
|
|
|
|
})
|
2017-08-28 03:24:24 +00:00
|
|
|
|
2017-12-02 02:48:58 +00:00
|
|
|
gulp.task("readme", () => {
|
|
|
|
const screens = {}
|
|
|
|
let screenshotList = ""
|
|
|
|
fs.readdirSync(path.join(__dirname, paths.screenshots)).forEach((s) => {
|
|
|
|
const file = path.basename(s, ".png").split("-")
|
|
|
|
const alias = file[0]
|
|
|
|
if (!screens[alias]) {
|
|
|
|
screens[alias] = []
|
|
|
|
}
|
|
|
|
screens[alias].push(path.join(paths.screenshots, path.basename(s)))
|
|
|
|
})
|
2018-11-10 01:47:39 +00:00
|
|
|
|
|
|
|
const complTable = Object.keys(compl).sort((a, b) => {
|
2017-12-02 02:48:58 +00:00
|
|
|
if (a < b) return -1
|
|
|
|
if (a > b) return 1
|
|
|
|
return 0
|
|
|
|
}).reduce((a, k) => {
|
|
|
|
const c = compl[k]
|
|
|
|
const u = new URL(c.search)
|
|
|
|
const domain = (u.hostname === "cse.google.com") ? "Google Custom Search" : u.hostname
|
|
|
|
let s = ""
|
|
|
|
if (screens[c.alias]) {
|
|
|
|
screens[c.alias].forEach((url, i) => {
|
|
|
|
const num = (i > 0) ? ` ${i + 1}` : ""
|
2017-12-02 03:02:57 +00:00
|
|
|
s += `[:framed_picture:](#${c.name}${num.replace(" ", "-")}) `
|
2017-12-02 02:48:58 +00:00
|
|
|
screenshotList += `##### ${c.name}${num}\n`
|
|
|
|
screenshotList += `![${c.name} screenshot](./${url})\n\n`
|
|
|
|
})
|
|
|
|
}
|
2018-11-10 01:47:39 +00:00
|
|
|
return `${a} | \`${c.alias}\` | \`${c.name}\` | \`${domain}\` | ${s} |\n`
|
|
|
|
}, "")
|
|
|
|
|
|
|
|
const 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
|
|
|
|
}).reduce((acc1, domain) => {
|
|
|
|
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) => {
|
|
|
|
const leader = typeof map.leader !== "undefined" ? map.leader : domain === "global" ? "" : 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`
|
|
|
|
}, "")
|
|
|
|
const domainStr = domain === "global" ? "<strong>global</strong>" : `<a href="//${domain}">${domain}</a>`
|
|
|
|
return `${acc1}<tr><th colspan="2">${domainStr}</th></tr>${header}\n${maps}`
|
2017-12-02 02:48:58 +00:00
|
|
|
}, "")
|
2018-11-10 01:47:39 +00:00
|
|
|
|
2017-12-02 02:48:58 +00:00
|
|
|
return gulp.src(["./README.tmpl.md"])
|
2017-12-02 03:10:34 +00:00
|
|
|
.pipe(replace("<!--{{DISCLAIMER}}-->", disclaimer))
|
|
|
|
.pipe(replace("<!--{{COMPL_COUNT}}-->", Object.keys(compl).length))
|
2018-11-10 01:47:39 +00:00
|
|
|
.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))
|
2017-12-02 03:10:34 +00:00
|
|
|
.pipe(replace("<!--{{SCREENSHOTS}}-->", screenshotList))
|
2017-12-02 02:48:58 +00:00
|
|
|
.pipe(rename("README.md"))
|
|
|
|
.pipe(gulp.dest("."))
|
|
|
|
})
|
2017-10-31 05:39:21 +00:00
|
|
|
|
2017-12-02 02:48:58 +00:00
|
|
|
gulp.task("default", ["build"])
|