|
| 1 | +"use strict" |
| 2 | + |
| 3 | +const { spawn } = require("child_process") |
| 4 | +const path = require("path") |
| 5 | +const fs = require("fs-extra") |
| 6 | +const GH_TOKEN = process.argv[2] |
| 7 | +const BUILD_ROOT = path.resolve(__dirname, "../docs/.vuepress/dist") |
| 8 | +const DEPLOY_ROOT = path.resolve(__dirname, "..") |
| 9 | + |
| 10 | +/** |
| 11 | + * Execute a command. |
| 12 | + * @param {string} command The command to execute. |
| 13 | + * @returns {void} |
| 14 | + */ |
| 15 | +function exec(command) { |
| 16 | + console.log(`> ${command.replace(GH_TOKEN, "****")}`) |
| 17 | + return new Promise((resolve, reject) => { |
| 18 | + const cp = spawn(command, [], { shell: true, stdio: "inherit" }) |
| 19 | + |
| 20 | + cp.on("close", code => { |
| 21 | + if (code) { |
| 22 | + reject(new Error(`Exited with ${code}.`)) |
| 23 | + } else { |
| 24 | + resolve() |
| 25 | + } |
| 26 | + }) |
| 27 | + }) |
| 28 | +} |
| 29 | + |
| 30 | +//eslint-disable-next-line @mysticatea/node/no-unsupported-features/es-syntax |
| 31 | +;(async () => { |
| 32 | + // Checkout. |
| 33 | + await exec("git checkout gh-pages") |
| 34 | + |
| 35 | + // Clean. |
| 36 | + for (const filename of await fs.readdir(DEPLOY_ROOT)) { |
| 37 | + const stat = await fs.stat(filename) |
| 38 | + if (!stat.isFile() || filename.startsWith(".")) { |
| 39 | + continue |
| 40 | + } |
| 41 | + |
| 42 | + console.log(`> rm ${filename}`) |
| 43 | + await fs.unlink(filename) |
| 44 | + } |
| 45 | + |
| 46 | + // Move. |
| 47 | + for (const filename of await fs.readdir(BUILD_ROOT)) { |
| 48 | + console.log(`> mv docs/.vuepress/dist/${filename} ${filename}`) |
| 49 | + await fs.rename( |
| 50 | + path.join(BUILD_ROOT, filename), |
| 51 | + path.join(DEPLOY_ROOT, filename) |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + // Commit. |
| 56 | + await exec("git add -A") |
| 57 | + let updated = false |
| 58 | + try { |
| 59 | + await exec('git commit -m "Update: website"') |
| 60 | + updated = true |
| 61 | + } catch (_error) { |
| 62 | + console.log("NO UPDATE") |
| 63 | + } |
| 64 | + |
| 65 | + // Push. |
| 66 | + if (updated) { |
| 67 | + await exec( |
| 68 | + `git push https://mysticatea:${GH_TOKEN}@github.com/mysticatea/eslint-plugin-eslint-comments.git gh-pages:gh-pages` |
| 69 | + ) |
| 70 | + } |
| 71 | +})().catch(error => { |
| 72 | + console.error(error.stack) |
| 73 | + process.exitCode = 1 |
| 74 | +}) |
0 commit comments