|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const yaml = require('js-yaml'); |
| 6 | + |
| 7 | +/** |
| 8 | + * Returns a boolean indicating whether or not the given object is a literal object. |
| 9 | + * |
| 10 | + * @param {any} obj object who's type will be checked. |
| 11 | + * @returns {boolean} boolean indicating whether or not the given obj is a literal object. |
| 12 | + */ |
| 13 | +const isObjectLiteral = (obj) => |
| 14 | + obj != null && obj.constructor.name === 'Object'; |
| 15 | + |
| 16 | +/** |
| 17 | + * Attempts to require the project.emulsify.json file. |
| 18 | + * |
| 19 | + * @returns parsed project.emulsify.json file. |
| 20 | + */ |
| 21 | +const getEmulsifyConfig = () => { |
| 22 | + try { |
| 23 | + return require('../project.emulsify.json'); |
| 24 | + } catch (e) { |
| 25 | + throw new Error( |
| 26 | + `Unable to load an Emulsify project config file (project.emulsify.json): ${String( |
| 27 | + e, |
| 28 | + )}`, |
| 29 | + ); |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * Throws if the given emulsify config file is invalid. |
| 35 | + * |
| 36 | + * @param {*} config emulsify project config, as loaded from a project.emulsify.json file. |
| 37 | + */ |
| 38 | +const validateEmulsifyConfig = (config) => { |
| 39 | + const prefix = 'Invalid project.emulsify.json config file'; |
| 40 | + const example = JSON.stringify({ |
| 41 | + project: { |
| 42 | + name: 'Example Project', |
| 43 | + machineName: 'example-project', |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + if (!config) { |
| 48 | + throw new Error(`${prefix}.`); |
| 49 | + } |
| 50 | + |
| 51 | + if (!config.project || !isObjectLiteral(config.project)) { |
| 52 | + throw new Error( |
| 53 | + `${prefix}: Must contain a "project" key, with a name and machineName property. ${example}`, |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + if (typeof config.project.name !== 'string') { |
| 58 | + throw new Error( |
| 59 | + `${prefix}: the "project" object must contain a "name" key with a string value. ${example}`, |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + if (typeof config.project.machineName !== 'string') { |
| 64 | + throw new Error( |
| 65 | + `${prefix}: the "project" object must contain a "machineName" key with a string value. ${example}`, |
| 66 | + ); |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +/** |
| 71 | + * Takes an array of objects describing the origin and destination of a given file, |
| 72 | + * then moves each specified file according to it's to/from properties. |
| 73 | + * |
| 74 | + * @param {Array<{ to: string, from: string }>} files array of objects depicting the origin and destination of a given file. |
| 75 | + * @returns void. |
| 76 | + */ |
| 77 | +const renameFiles = (files) => |
| 78 | + files.map(({ from, to }) => |
| 79 | + fs.renameSync(path.join(__dirname, from), path.join(__dirname, to)), |
| 80 | + ); |
| 81 | + |
| 82 | +/** |
| 83 | + * Takes a machineName, and returns a fn that, when called with a str, |
| 84 | + * replaces all instances of `emulsify` with the given machineName. |
| 85 | + * |
| 86 | + * @param {string} machineName string that should replace emulsify. |
| 87 | + * @returns {function} fn that when called with a str, replaces all instances of `emulsify` with the given machineName. |
| 88 | + */ |
| 89 | +const strReplaceEmulsify = (machineName) => (str) => |
| 90 | + str.replace(/emulsify/g, machineName); |
| 91 | + |
| 92 | +/** |
| 93 | + * Loads a yml file at filePath, applies the functor to the contents of the file, and writes it. |
| 94 | + * |
| 95 | + * @param {string} filePath path to the file that should be loaded, modified, and re-saved. |
| 96 | + * @param {fn} functor fn that should return the new contents of the file, to be saved. |
| 97 | + * @returns void. |
| 98 | + */ |
| 99 | +const applyToYmlFile = (filePath, functor) => { |
| 100 | + if (!filePath || typeof filePath !== `string`) { |
| 101 | + throw new Error( |
| 102 | + `Cannot modify a file without knowing how to access it: ${filePath}`, |
| 103 | + ); |
| 104 | + } |
| 105 | + if (typeof functor !== 'function') { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + const file = yaml.load(fs.readFileSync(filePath, 'utf8')); |
| 110 | + fs.writeFileSync(filePath, yaml.dump(functor(file))); |
| 111 | +}; |
| 112 | + |
| 113 | +const main = () => { |
| 114 | + // Load up config file, throw if none exists. |
| 115 | + const config = getEmulsifyConfig(); |
| 116 | + |
| 117 | + // Validate config file, throw if it is missing |
| 118 | + //properties or is otherwise malformed. |
| 119 | + validateEmulsifyConfig(config); |
| 120 | + |
| 121 | + const { |
| 122 | + project: { machineName, name }, |
| 123 | + } = config; |
| 124 | + |
| 125 | + // Move all files to their correct location. |
| 126 | + renameFiles([ |
| 127 | + { |
| 128 | + from: '../emulsify.info.yml', |
| 129 | + to: `../${machineName}.info.yml`, |
| 130 | + }, |
| 131 | + { |
| 132 | + from: '../emulsify.theme', |
| 133 | + to: `../${machineName}.theme`, |
| 134 | + }, |
| 135 | + { |
| 136 | + from: '../emulsify.breakpoints.yml', |
| 137 | + to: `../${machineName}.breakpoints.yml`, |
| 138 | + }, |
| 139 | + { |
| 140 | + from: '../emulsify.libraries.yml', |
| 141 | + to: `../${machineName}.libraries.yml`, |
| 142 | + }, |
| 143 | + ]); |
| 144 | + |
| 145 | + // Update info.yml file. |
| 146 | + applyToYmlFile( |
| 147 | + path.join(__dirname, `../${machineName}.info.yml`), |
| 148 | + (info) => ({ |
| 149 | + ...info, |
| 150 | + name: machineName, |
| 151 | + libraries: info.libraries.map(strReplaceEmulsify(machineName)), |
| 152 | + }), |
| 153 | + ); |
| 154 | + |
| 155 | + // Update breakpoint.yml file. |
| 156 | + applyToYmlFile( |
| 157 | + path.join(__dirname, `../${machineName}.breakpoints.yml`), |
| 158 | + (breakpoints) => { |
| 159 | + const newBps = {}; |
| 160 | + for (const prop of Object.keys(breakpoints)) { |
| 161 | + newBps[strReplaceEmulsify(machineName)(prop)] = breakpoints[prop]; |
| 162 | + } |
| 163 | + return newBps; |
| 164 | + }, |
| 165 | + ); |
| 166 | +}; |
| 167 | + |
| 168 | +main(); |
0 commit comments