copy-app-modules.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const fs = require('fs-extra');
  2. const path = require('path');
  3. const yargs = require('yargs')(process.argv.slice(2));
  4. const argv = yargs
  5. .usage('Usage: $0 -d <dist>')
  6. .help('help')
  7. .alias('version', 'v')
  8. .option('help', {
  9. alias: 'h'
  10. })
  11. .option('dist', {
  12. alias: 'd',
  13. type: 'string',
  14. requiresArg: true,
  15. describe: 'The directory where dependencies would be copies to.',
  16. })
  17. .option('dryrun', {
  18. type: 'boolean',
  19. describe: 'Find the dependencies and log to the screen only.',
  20. })
  21. .option('verbose', {
  22. alias: 'V',
  23. type: 'boolean',
  24. describe: 'Enable verbose log.',
  25. })
  26. .argv;
  27. const pkgfile = require('./package');
  28. function getDependencies(filePath) {
  29. let allDependencies = [];
  30. let cssDependencies = [];
  31. let jsDependencies = [];
  32. let fontDependencies = [];
  33. let fileDir = path.dirname(filePath);
  34. let fileContent = fs.readFileSync(filePath, 'utf8');
  35. let cssPattern = /<link rel="stylesheet" href="(\.\.\/node_modules\/.*\.css)"\/?>/g;
  36. let jsPattern = /<script src="(\.\.\/node_modules\/.*\.js)"><\/script>/g;
  37. let fontPattern = /url\('(\.\.\/fonts\/[a-zA-Z0-9\-]+\.[a-zA-z0-9]+)(\?[a-zA-Z0-9\-_=.#]+)?'\)/g;
  38. for (let match; (match = cssPattern.exec(fileContent)) !== null;) {
  39. let dependencyPath = path.join(fileDir, match[1]);
  40. if (argv.verbose) {
  41. console.log('find css dependency ' + dependencyPath);
  42. }
  43. cssDependencies.push(dependencyPath);
  44. }
  45. for (let i = 0; i < cssDependencies.length; i++) {
  46. let cssFilePath = cssDependencies[i];
  47. let cssFileDir = path.dirname(cssFilePath);
  48. let cssFileContent = fs.readFileSync(cssFilePath, 'utf8');
  49. for (let match; (match = fontPattern.exec(cssFileContent)) !== null;) {
  50. let dependencyPath = path.join(cssFileDir, match[1]);
  51. if (argv.verbose) {
  52. console.log('find font dependency (' + cssFilePath + ') ' + dependencyPath);
  53. }
  54. fontDependencies.push(dependencyPath);
  55. }
  56. }
  57. for (let match; (match = jsPattern.exec(fileContent)) !== null;) {
  58. let dependencyPath = path.join(fileDir, match[1]);
  59. if (argv.verbose) {
  60. console.log('find js dependency ' + dependencyPath);
  61. }
  62. jsDependencies.push(dependencyPath);
  63. }
  64. allDependencies.push(...cssDependencies);
  65. allDependencies.push(...fontDependencies);
  66. allDependencies.push(...jsDependencies);
  67. return allDependencies;
  68. }
  69. function copyDependencies(dependencies, dist) {
  70. for (let i = 0; i < dependencies.length; i++) {
  71. let filePath = dependencies[i];
  72. let srcFilePath = path.join(__dirname, filePath);
  73. let distFilePath = path.join(__dirname, dist, filePath);
  74. let distDir = path.dirname(distFilePath);
  75. if (!fs.existsSync(distDir)) {
  76. fs.mkdirpSync(distDir);
  77. }
  78. fs.copyFileSync(srcFilePath, distFilePath);
  79. }
  80. }
  81. if (!argv.dist || argv.help) {
  82. yargs.showHelp();
  83. } else {
  84. let dependencies = getDependencies(pkgfile.entry);
  85. if (!argv.dryrun) {
  86. copyDependencies(dependencies, argv.dist);
  87. }
  88. }