|
1 | 1 | "use strict" |
2 | 2 |
|
3 | | -//------------------------------------------------------------------------------ |
4 | | -// Requirements |
5 | | -//------------------------------------------------------------------------------ |
6 | | - |
7 | | -const sh = require("shelljs") |
| 3 | +const { spawn } = require("child_process") |
| 4 | +const path = require("path") |
| 5 | +const fs = require("fs-extra") |
8 | 6 | const version = require("../package.json").version |
9 | 7 | const ATOKEN = process.env.ATOKEN |
10 | | - |
11 | | -//------------------------------------------------------------------------------ |
12 | | -// Helpers |
13 | | -//------------------------------------------------------------------------------ |
14 | | - |
15 | | -/** |
16 | | - * Execute `cp` command. |
17 | | - * @returns {void} |
18 | | - */ |
19 | | -function cp(...args) { |
20 | | - sh.echo(`> cp ${args.join(" ")}`) |
21 | | - sh.cp(...args) |
22 | | -} |
23 | | - |
24 | | -/** |
25 | | - * Execute `rm` command. |
26 | | - * @returns {void} |
27 | | - */ |
28 | | -function rm(...args) { |
29 | | - sh.echo(`> rm ${args.join(" ")}`) |
30 | | - sh.rm(...args) |
31 | | -} |
| 8 | +const BUILD_ROOT = path.resolve(__dirname, "../dist") |
| 9 | +const DEPLOY_ROOT = path.resolve(__dirname, "..") |
32 | 10 |
|
33 | 11 | /** |
34 | 12 | * Execute a command. |
35 | 13 | * @param {string} command The command to execute. |
36 | 14 | * @returns {void} |
37 | 15 | */ |
38 | 16 | function exec(command) { |
39 | | - sh.echo(`> ${command}`) |
40 | | - const code = sh.exec(command, { stdio: "inherit" }).code |
41 | | - if (code) { |
42 | | - throw new Error(`Exited with ${code}.`) |
43 | | - } |
| 17 | + console.log(`> ${command}`) |
| 18 | + return new Promise((resolve, reject) => { |
| 19 | + const cp = spawn(command, [], { shell: true, stdio: "inherit" }) |
| 20 | + |
| 21 | + cp.on("close", code => { |
| 22 | + if (code) { |
| 23 | + reject(new Error(`Exited with ${code}.`)) |
| 24 | + } else { |
| 25 | + resolve() |
| 26 | + } |
| 27 | + }) |
| 28 | + }) |
44 | 29 | } |
45 | 30 |
|
46 | | -//------------------------------------------------------------------------------ |
47 | 31 | // Main |
48 | | -//------------------------------------------------------------------------------ |
| 32 | +;(async () => { |
| 33 | + console.log("BUILD") |
| 34 | + await exec(`git checkout v${version}`) |
| 35 | + await exec("npm run -s build") |
| 36 | + |
| 37 | + console.log("LOAD GH-PAGES") |
| 38 | + if (ATOKEN) { |
| 39 | + await exec( |
| 40 | + "git fetch --depth=1 https://github.com/mysticatea/vue-eslint-demo.git gh-pages:gh-pages", |
| 41 | + ) |
| 42 | + await exec("git checkout gh-pages") |
| 43 | + } else { |
| 44 | + await exec("git checkout gh-pages") |
| 45 | + await exec("git pull") |
| 46 | + } |
49 | 47 |
|
50 | | -// Build. |
51 | | -exec(`git checkout v${version}`) |
52 | | -exec("npm run build") |
| 48 | + console.log("CHECK VERSIONS") |
| 49 | + const oldVersions = await fs.readFile("versions.json", "utf8") |
| 50 | + const newVersions = await fs.readFile("dist/versions.json", "utf8") |
| 51 | + console.log(`OLD: ${oldVersions}`) |
| 52 | + console.log(`NEW: ${newVersions}`) |
53 | 53 |
|
54 | | -// Load gh-pages. |
55 | | -if (ATOKEN) { |
56 | | - exec( |
57 | | - "git fetch --depth=1 https://github.com/mysticatea/vue-eslint-demo.git gh-pages:gh-pages", |
58 | | - ) |
59 | | - exec("git checkout gh-pages") |
60 | | -} else { |
61 | | - exec("git checkout gh-pages") |
62 | | - exec("git pull") |
63 | | -} |
| 54 | + // Deploy. |
| 55 | + if (newVersions !== oldVersions) { |
| 56 | + console.log("CLEAN") |
| 57 | + for (const filename of await fs.readdir(DEPLOY_ROOT)) { |
| 58 | + const stat = await fs.stat(filename) |
| 59 | + if (!stat.isFile() || filename.startsWith(".")) { |
| 60 | + continue |
| 61 | + } |
64 | 62 |
|
65 | | -// Check versions. |
66 | | -const oldVersions = String(sh.cat("versions.json")) |
67 | | -const newVersions = String(sh.cat("dist/versions.json")) |
68 | | -sh.echo(`OLD: ${oldVersions}`) |
69 | | -sh.echo(`NEW: ${newVersions}`) |
| 63 | + console.log(`> rm ${filename}`) |
| 64 | + await fs.unlink(filename) |
| 65 | + } |
70 | 66 |
|
71 | | -// Deploy. |
72 | | -if (newVersions !== oldVersions) { |
73 | | - rm("-rf", "vs", "index.*") |
74 | | - cp("-r", "dist/*", ".") |
75 | | - exec("git add -A") |
76 | | - exec('git commit -m "Update: website"') |
77 | | - exec( |
78 | | - `git push${ |
79 | | - ATOKEN |
80 | | - ? ` https://mysticatea:${ATOKEN}@github.com/mysticatea/vue-eslint-demo.git gh-pages:gh-pages` |
81 | | - : "" |
82 | | - }`, |
83 | | - ) |
84 | | -} |
| 67 | + console.log("DEPLOY") |
| 68 | + for (const filename of await fs.readdir(BUILD_ROOT)) { |
| 69 | + console.log(`> mv dist/${filename} ${filename}`) |
| 70 | + await fs.rename( |
| 71 | + path.join(BUILD_ROOT, filename), |
| 72 | + path.join(DEPLOY_ROOT, filename), |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + await exec("git add -A") |
| 77 | + let updated = false |
| 78 | + try { |
| 79 | + await exec('git commit -m "Update: website"') |
| 80 | + updated = true |
| 81 | + } catch (_error) { |
| 82 | + console.log("NO UPDATE") |
| 83 | + } |
| 84 | + if (updated) { |
| 85 | + await exec( |
| 86 | + `git push${ |
| 87 | + ATOKEN |
| 88 | + ? ` https://mysticatea:${ATOKEN}@github.com/mysticatea/vue-eslint-demo.git gh-pages:gh-pages` |
| 89 | + : "" |
| 90 | + }`, |
| 91 | + ) |
| 92 | + } |
| 93 | + } else { |
| 94 | + console.log("NO UPDATE") |
| 95 | + } |
85 | 96 |
|
86 | | -// Back to master. |
87 | | -exec("git checkout master") |
| 97 | + // Back to master. |
| 98 | + await exec("git checkout master") |
| 99 | + console.log("COMPLETE") |
| 100 | +})().catch(error => { |
| 101 | + console.error(error.stack) |
| 102 | + process.exitCode = 1 |
| 103 | +}) |
0 commit comments