makeAll 4.4 KB

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