build.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env node
  2. const fsExtra = require('fs-extra');
  3. const {resolve} = require('path');
  4. const config = require('./config.js');
  5. const commander = require('commander');
  6. const {build, watch} = require('./helper');
  7. const prePublish = require('./pre-publish');
  8. function run() {
  9. /**
  10. * Tips for `commander`:
  11. * (1) If arg xxx not specified, `commander.xxx` is undefined.
  12. * Otherwise:
  13. * If '-x, --xxx', `commander.xxx` can only be true/false, even if '--xxx yyy' input.
  14. * If '-x, --xxx <some>', the 'some' string is required, or otherwise error will be thrown.
  15. * If '-x, --xxx [some]', the 'some' string is optional, that is, `commander.xxx` can be boolean or string.
  16. * (2) `node ./build/build.js --help` will print helper info and exit.
  17. */
  18. commander
  19. .usage('[options]')
  20. .description('Build zrender and generate result files in directory `zrender/dist` ')
  21. .option(
  22. '--release',
  23. 'Build all for release'
  24. )
  25. .option(
  26. '--prepublish',
  27. 'Build all for release'
  28. )
  29. .option(
  30. '-w, --watch',
  31. 'Watch modifications of files and auto-compile to dist file (e.g., `zrender/dist/zrender.js`).'
  32. )
  33. .option(
  34. '--min',
  35. 'Whether to compress the output file.'
  36. )
  37. .parse(process.argv);
  38. let isWatch = !!commander.watch;
  39. let isRelease = !!commander.release;
  40. let isPrePublish = !!commander.prepublish;
  41. let min = !!commander.min;
  42. // Clear `echarts/dist`
  43. if (isRelease) {
  44. fsExtra.removeSync(getPath('./dist'));
  45. }
  46. if (isWatch) {
  47. watch(config.create());
  48. }
  49. else if (isPrePublish) {
  50. prePublish();
  51. }
  52. else if (isRelease) {
  53. build([
  54. config.create(false),
  55. config.create(true)
  56. ]).then(function () {
  57. prePublish();
  58. }).catch(handleBuildError);
  59. }
  60. else {
  61. build([config.create(min)]).catch(handleBuildError);
  62. }
  63. }
  64. function handleBuildError(err) {
  65. console.log(err);
  66. }
  67. /**
  68. * @param {string} relativePath Based on zrender directory.
  69. * @return {string} Absolute path.
  70. */
  71. function getPath(relativePath) {
  72. return resolve(__dirname, '../', relativePath);
  73. }
  74. run();