| 1 |
#! /usr/bin/env node |
| 2 |
|
| 3 |
/************************************************************* |
| 4 |
* |
| 5 |
* Copyright (c) 2018 The MathJax Consortium |
| 6 |
* |
| 7 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 |
* you may not use this file except in compliance with the License. |
| 9 |
* You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, software |
| 14 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 15 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 |
* See the License for the specific language governing permissions and |
| 17 |
* limitations under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
/** |
| 21 |
* @fileoverview Processes all the build and webpack files in a directory |
| 22 |
* or collection of directories |
| 23 |
* |
| 24 |
* @author [email protected] (Davide Cervone) |
| 25 |
*/ |
| 26 |
|
| 27 |
|
| 28 |
const fs = require('fs'); |
| 29 |
const path = require('path'); |
| 30 |
const {execSync} = require('child_process'); |
| 31 |
|
| 32 |
const options = { |
| 33 |
recursive: true |
| 34 |
}; |
| 35 |
|
| 36 |
/** |
| 37 |
* Get the directories to process and check for options |
| 38 |
*/ |
| 39 |
const dirs = process.argv.slice(2); |
| 40 |
|
| 41 |
if (dirs[0] === '--no-subdirs') { |
| 42 |
dirs.shift(); |
| 43 |
options.recursive = false; |
| 44 |
} |
| 45 |
|
| 46 |
if (dirs.length === 0) { |
| 47 |
dirs.push('.'); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* The commads to runb the bin/build scripts |
| 52 |
* (on Unix, could be done without the 'node ' prefix, but |
| 53 |
* for Windows, these are needed.) |
| 54 |
*/ |
| 55 |
const build = `node '${path.join(__dirname, 'build')}'`; |
| 56 |
const copy = `node '${path.join(__dirname, 'copy')}'`; |
| 57 |
const pack = `node '${path.join(__dirname, 'pack')}'`; |
| 58 |
|
| 59 |
/** |
| 60 |
* Regular expression for the components directory |
| 61 |
*/ |
| 62 |
const compRE = new RegExp(path.dirname(__dirname).replace(/([\\.{}[\]()?*^$])/g, '\\$1')); |
| 63 |
const dirRE = new RegExp(process.cwd().replace(/([\\.{}[\]()?*^$])/g, '\\$1')); |
| 64 |
|
| 65 |
/** |
| 66 |
* Process the contents of an array of directories |
| 67 |
* |
| 68 |
* @param {string} dirs The directories to process |
| 69 |
*/ |
| 70 |
function processList(dirs) { |
| 71 |
for (const dir of dirs) { |
| 72 |
const fulldir = path.resolve(dir); |
| 73 |
processDir(fulldir, buildLib); |
| 74 |
processDir(fulldir, webpackLib); |
| 75 |
processDir(fulldir, copyLib); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Run an action (build or webpack) on a directory and its subdirectories |
| 81 |
* |
| 82 |
* @param {string} dir The directory to process |
| 83 |
* @param {Function} action The action to take |
| 84 |
*/ |
| 85 |
function processDir(dir, action) { |
| 86 |
action(dir); |
| 87 |
if (options.recursive) { |
| 88 |
processSubdirs(dir, action); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Look for subdirectories and process them |
| 94 |
* |
| 95 |
* @param {string} dir The directory whose subdirectories are to be processed |
| 96 |
* @param {Function} action The action to take |
| 97 |
*/ |
| 98 |
function processSubdirs(dir, action) { |
| 99 |
for (const name of fs.readdirSync(dir)) { |
| 100 |
const file = path.join(dir, name); |
| 101 |
if (fs.lstatSync(file).isDirectory()) { |
| 102 |
processDir(file, action); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Run bin/build if there is a configuration file for it |
| 109 |
* |
| 110 |
* @param {string} dir The directory to check |
| 111 |
*/ |
| 112 |
function buildLib(dir) { |
| 113 |
const file = path.join(dir, 'build.json'); |
| 114 |
if (!fs.existsSync(file)) return; |
| 115 |
console.info('Building ' + dir.replace(compRE, '').replace(dirRE, '.')); |
| 116 |
const wd = process.cwd(); |
| 117 |
try { |
| 118 |
process.chdir(dir); |
| 119 |
const result = execSync(build); |
| 120 |
console.info(' ' + String(result).replace(/\n/g, '\n ')); |
| 121 |
} catch (err) { |
| 122 |
console.info(' ' + err.message); |
| 123 |
} |
| 124 |
process.chdir(wd); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Run webpack if there is a configuration file for it |
| 129 |
* |
| 130 |
* @param {string} dir The directory to check |
| 131 |
*/ |
| 132 |
function webpackLib(dir) { |
| 133 |
const file = path.join(dir, 'webpack.config.js'); |
| 134 |
if (!fs.existsSync(file)) return; |
| 135 |
console.info('Webpacking ' + dir.replace(compRE, '').replace(dirRE, '.')); |
| 136 |
const wd = process.cwd(); |
| 137 |
try { |
| 138 |
process.chdir(dir); |
| 139 |
const result = execSync(pack); |
| 140 |
console.info(' ' + String(result).replace(/\n/g, '\n ')); |
| 141 |
} catch (err) { |
| 142 |
console.info(' ' + err.message); |
| 143 |
} |
| 144 |
process.chdir(wd); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Copy the designated files if there is a configurtion file for it |
| 149 |
* |
| 150 |
* @param {string} dir The directory to check |
| 151 |
*/ |
| 152 |
function copyLib(dir) { |
| 153 |
const file = path.join(dir, 'copy.json'); |
| 154 |
if (!fs.existsSync(file)) return; |
| 155 |
console.info('Copying ' + dir.replace(compRE, '')); |
| 156 |
try { |
| 157 |
process.chdir(dir); |
| 158 |
const result = execSync(copy); |
| 159 |
console.info(' ' + String(result).replace(/\n/g, '\n ')); |
| 160 |
} catch (err) { |
| 161 |
console.info(' ' + err.message); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Process all the specified directories |
| 167 |
*/ |
| 168 |
processList(dirs); |