copy-app-modules.js 3.0 KB

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