|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const { resolve } = require("path"); |
| 4 | +const fs = require('fs'); |
| 5 | +const { readdir, rename, unlink, readFile, writeFile, access } = fs.promises; |
| 6 | +const { promisify } = require('util'); |
| 7 | +const exec = promisify(require('child_process').exec); |
| 8 | + |
| 9 | +const vPrev = require("../assets/version.json").version; |
| 10 | +const vNext = require("../package.json").version; |
| 11 | + |
| 12 | +const ENC = "utf-8"; |
| 13 | + |
| 14 | +const FILES = [ |
| 15 | + "./jekyll-theme-hydejack.gemspec", |
| 16 | + "./_includes/body/scripts.html", |
| 17 | + "./_includes/body/footer.html", |
| 18 | + "./_includes/head/meta-static.html", |
| 19 | + "./_includes/head/links-static.html", |
| 20 | + "./_includes/head/styles-inline.html", |
| 21 | + "./_includes/head/styles-no-inline.html", |
| 22 | + "./_includes/header.txt", |
| 23 | + "./_includes/js/service-worker.js", |
| 24 | + "./_layouts/compress.html", |
| 25 | + "./_js/lib/version.js", |
| 26 | +].map(f => resolve(f)); |
| 27 | + |
| 28 | +/** |
| 29 | + * @param {string} dir |
| 30 | + * @returns {Promise<string[]>} |
| 31 | + * @see https://stackoverflow.com/a/45130990/870615 |
| 32 | + */ |
| 33 | +async function getFiles(dir) { |
| 34 | + const dirents = await readdir(dir, { withFileTypes: true }); |
| 35 | + const files = await Promise.all(dirents.map((dirent) => { |
| 36 | + const res = resolve(dir, dirent.name); |
| 37 | + return dirent.isDirectory() ? getFiles(res) : [res]; |
| 38 | + })); |
| 39 | + return Array.prototype.concat(...files); |
| 40 | +} |
| 41 | + |
| 42 | +(async function main() { |
| 43 | + try { |
| 44 | + const prev = vPrev.replace(/\./g, "\\."); |
| 45 | + const prevRegExp = new RegExp(prev, "g"); |
| 46 | + |
| 47 | + // const args = await Promise.all([ |
| 48 | + // getFiles("./hyde/_posts"), |
| 49 | + // getFiles("./hydejack/_posts"), |
| 50 | + // getFiles("./_projects"), |
| 51 | + // getFiles("./docs"), |
| 52 | + // ]); |
| 53 | + const args = []; |
| 54 | + |
| 55 | + const files = Array.prototype.concat.call(FILES, ...args); |
| 56 | + |
| 57 | + const pFiles = Promise.all( |
| 58 | + files |
| 59 | + .filter(([f]) => !f.startsWith(".")) |
| 60 | + .map(f => [f, readFile(f, ENC)]) |
| 61 | + .map(async ([f, p]) => { |
| 62 | + const content = await p; |
| 63 | + |
| 64 | + // if (f.includes("CHANGELOG")) { |
| 65 | + // const pattern = new RegExp(`([^v])${prev}`, "g"); |
| 66 | + // return [f, content.replace(pattern, `$1${vNext}`)]; |
| 67 | + // } |
| 68 | + |
| 69 | + return [f, content.replace(prevRegExp, vNext)]; |
| 70 | + }) |
| 71 | + .map(async p => { |
| 72 | + const [f, content] = await p; |
| 73 | + return writeFile(f, content, ENC); |
| 74 | + }) |
| 75 | + ); |
| 76 | + |
| 77 | + const pUnlink = Promise.all( |
| 78 | + (await getFiles('./assets/js')) |
| 79 | + .filter(f => f.match(/assets\/js\/*hydejack-*/i)) |
| 80 | + .map(unlink) |
| 81 | + ); |
| 82 | + |
| 83 | + const pJSCSS = rename( |
| 84 | + resolve(`./assets/css/hydejack-${vPrev}.css`), |
| 85 | + resolve(`./assets/css/hydejack-${vNext}.css`) |
| 86 | + ); |
| 87 | + |
| 88 | + const pSearchW = rename( |
| 89 | + resolve(`./assets/js/search-worker-${vPrev}.js`), |
| 90 | + resolve(`./assets/js/search-worker-${vNext}.js`) |
| 91 | + ); |
| 92 | + |
| 93 | + await Promise.all([pUnlink, pFiles, pJSCSS, pSearchW]); |
| 94 | + |
| 95 | + await writeFile('./assets/version.json', JSON.stringify({ version: vNext, prevVersion: vPrev }, null, 2)); |
| 96 | + |
| 97 | + try { |
| 98 | + await access('../.scripts/version.js', fs.constants.X_OK); |
| 99 | + await exec('../.scripts/version.js'); |
| 100 | + } catch (e) { |
| 101 | + console.warn(e) |
| 102 | + } |
| 103 | + |
| 104 | + process.exit(0); |
| 105 | + } catch (e) { |
| 106 | + console.error(e); // eslint-disable-line |
| 107 | + process.exit(1); |
| 108 | + } |
| 109 | +})(); |
0 commit comments