copy 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 Copies specified files to the distribution directory
  20. *
  21. * @author dpvc@mathjax.org (Davide Cervone)
  22. */
  23. const fs = require('fs');
  24. const path = require('path');
  25. /**
  26. * The amount of space for each level of indentation
  27. */
  28. const INDENT = ' ';
  29. /**
  30. * The configuration data for the copy operation
  31. */
  32. const config = JSON.parse(fs.readFileSync(process.argv[2] || 'copy.json'));
  33. /**
  34. * Get the directory for node modules (either the parent of the MathJax directory,
  35. * or the MathJax node_modules directory, if it exists).
  36. */
  37. const parent = path.resolve(__dirname, '..', '..');
  38. const nodeDir = (dir => (fs.existsSync(dir) ? dir : path.resolve(parent, '..')))(path.join(parent, 'node_modules'));
  39. /**
  40. * Copy a file or directory tree
  41. *
  42. * @param {string} from The directory to copy from
  43. * @param {string} to The directory to copy to
  44. * @param {string} name The name of the file or directory to copy
  45. * @param {string} space The indentation for output
  46. */
  47. function copyFile(from, to, name, space) {
  48. !fs.existsSync(to) && fs.mkdirSync(to, {recursive: true});
  49. const copy = path.resolve(from, name);
  50. const dest = path.resolve(to, name);
  51. if (fs.lstatSync(copy).isDirectory()) {
  52. console.info(space + name + '/');
  53. for (const file of fs.readdirSync(copy)) {
  54. copyFile(copy, dest, file, space + INDENT);
  55. }
  56. } else {
  57. console.info(space + name);
  58. fs.copyFileSync(copy, dest);
  59. }
  60. }
  61. /**
  62. * Copy the given files
  63. */
  64. const wd = process.cwd();
  65. const to = path.resolve(wd, config.to);
  66. const from = path.resolve(wd, config.from.replace(/\[node\]/, nodeDir));
  67. for (const name of config.copy) {
  68. copyFile(from, to, name, '');
  69. }