|
| 1 | +/** |
| 2 | + * macOSNotifJS: A simple Javascript plugin to create simulated macOS notifications on your website. |
| 3 | + * <https://github.com/MattIPv4/macOSNotifJS/> |
| 4 | + * Copyright (C) 2019 Matt Cowley (MattIPv4) (me@mattcowley.co.uk) |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as published |
| 8 | + * by the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with this program. If not, please see |
| 16 | + * <https://github.com/MattIPv4/macOSNotifJS/blob/master/LICENSE> or <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +const { name, version, description, browserslist } = require("../package.json"); |
| 20 | +const chalk = require("chalk"); |
| 21 | +const fs = require("fs"); |
| 22 | +const jsdoc = require("jsdoc-api"); |
| 23 | +const ejs = require("ejs"); |
| 24 | + |
| 25 | +const templateOptions = { |
| 26 | + name: "macOSNotifJS", |
| 27 | + packageName: name, |
| 28 | + version: version, |
| 29 | + dateOfBuild: new Date().toISOString().slice(0, 10), |
| 30 | + description: description, |
| 31 | + browsers: browserslist.map(x => { |
| 32 | + return [ |
| 33 | + "https://browserl.ist/?q=" + encodeURIComponent(x), |
| 34 | + x, |
| 35 | + ]; |
| 36 | + }), |
| 37 | +}; |
| 38 | + |
| 39 | +const buildJSDoc = async position => { |
| 40 | + // Remove previous docs contents |
| 41 | + try { |
| 42 | + jsdoc.renderSync({ |
| 43 | + files: [ |
| 44 | + "src/macOSNotif.js", |
| 45 | + ], |
| 46 | + package: "package.json", |
| 47 | + destination: "docs", |
| 48 | + }); |
| 49 | + } catch (err) { |
| 50 | + console.error(chalk.red.bold(`\n${position} Failed to build JSDoc.`)); |
| 51 | + throw err; |
| 52 | + } |
| 53 | + |
| 54 | + // Done |
| 55 | + console.log(chalk.greenBright.bold(`\n${position} Successfully built new JSDoc documentation.`)); |
| 56 | +}; |
| 57 | + |
| 58 | +const injectDocsHome = async position => { |
| 59 | + // Render the template |
| 60 | + let html; |
| 61 | + try { |
| 62 | + const templateFile = "build/templates/docs-home.ejs"; |
| 63 | + const template = fs.readFileSync(templateFile, "utf8"); |
| 64 | + html = ejs.render(template, { |
| 65 | + ...templateOptions, |
| 66 | + filename: templateFile, |
| 67 | + }); |
| 68 | + } catch (err) { |
| 69 | + console.error(chalk.red.bold(`\n${position} Failed to render custom homepage.`)); |
| 70 | + throw err; |
| 71 | + } |
| 72 | + |
| 73 | + // Inject into jsdoc |
| 74 | + try { |
| 75 | + const filePath = `docs/${name}/${version}/index.html`; |
| 76 | + let file = fs.readFileSync(filePath, "utf8"); |
| 77 | + file = file |
| 78 | + .replace(`<h3>${name} ${version}</h3>`, html) |
| 79 | + .replace("<title>", `<title>${name} - v${version} - `); |
| 80 | + fs.writeFileSync(filePath, file); |
| 81 | + } catch (err) { |
| 82 | + console.error(chalk.red.bold(`\n${position} Failed to inject custom homepage.`)); |
| 83 | + throw err; |
| 84 | + } |
| 85 | + |
| 86 | + // Done |
| 87 | + console.log(chalk.greenBright.bold(`\n${position} Custom JSDoc homepage successfully rendered & injected.`)); |
| 88 | +}; |
| 89 | + |
| 90 | +const createMainIndex = async position => { |
| 91 | + // Render the template |
| 92 | + try { |
| 93 | + const templateFile = "build/templates/main-index.ejs"; |
| 94 | + const template = fs.readFileSync(templateFile, "utf8"); |
| 95 | + const html = ejs.render(template, { |
| 96 | + ...templateOptions, |
| 97 | + filename: templateFile, |
| 98 | + }); |
| 99 | + fs.writeFileSync("index.html", html); |
| 100 | + } catch (err) { |
| 101 | + console.error(chalk.red.bold(`\n${position} Failed to render main index.`)); |
| 102 | + throw err; |
| 103 | + } |
| 104 | + |
| 105 | + // Done |
| 106 | + console.log(chalk.greenBright.bold(`\n${position} Successfully rendered main index file.`)); |
| 107 | +}; |
| 108 | + |
| 109 | +const processes = [ |
| 110 | + buildJSDoc, |
| 111 | + injectDocsHome, |
| 112 | + createMainIndex, |
| 113 | +]; |
| 114 | + |
| 115 | +const postFlight = async () => { |
| 116 | + // Hello! |
| 117 | + console.info(chalk.cyan.bold("Hello! Beginning the post-flight processes for macOSNotifJS!")); |
| 118 | + |
| 119 | + // Run |
| 120 | + processes.forEach(async (item, i) => { |
| 121 | + await item("(" + (i + 1) + "/" + processes.length + ")"); |
| 122 | + }); |
| 123 | + |
| 124 | + // Done! |
| 125 | + console.log(chalk.green.bold("\nAll done!\n")); |
| 126 | +}; |
| 127 | + |
| 128 | +// Run |
| 129 | +postFlight(); |
0 commit comments