watcher.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.FileWatcher = void 0;
  7. const fs_extra_1 = __importDefault(require("fs-extra"));
  8. const path_1 = __importDefault(require("path"));
  9. const chokidar_1 = require("chokidar");
  10. const shared_1 = require("@vue/shared");
  11. const utils_1 = require("./utils");
  12. class FileWatcher {
  13. constructor({ src, dest, transform, verbose }) {
  14. this.src = !(0, shared_1.isArray)(src) ? [src] : src;
  15. this.dest = dest;
  16. this.transform = transform;
  17. this.verbose = verbose;
  18. }
  19. watch(watchOptions, onReady, onChange) {
  20. if (!this.watcher) {
  21. const copy = this.copy.bind(this);
  22. const remove = this.remove.bind(this);
  23. // escape chokidar cwd
  24. const src = this.src.map((src) => (0, utils_1.pathToGlob)(path_1.default.resolve(watchOptions.cwd), src));
  25. this.watcher = (0, chokidar_1.watch)(src, watchOptions)
  26. .on('add', copy)
  27. .on('addDir', copy)
  28. .on('change', copy)
  29. .on('unlink', remove)
  30. .on('unlinkDir', remove)
  31. .on('ready', () => {
  32. onReady && onReady(this.watcher);
  33. setTimeout(() => {
  34. this.onChange = onChange;
  35. }, 1000);
  36. })
  37. .on('error', (e) => console.error('watch', e));
  38. }
  39. return this.watcher;
  40. }
  41. add(paths) {
  42. this.info('add', paths);
  43. return this.watcher.add(paths);
  44. }
  45. unwatch(paths) {
  46. this.info('unwatch', paths);
  47. return this.watcher.unwatch(paths);
  48. }
  49. close() {
  50. this.info('close');
  51. return this.watcher.close();
  52. }
  53. copy(from) {
  54. const to = this.to(from);
  55. this.info('copy', from + '=>' + to);
  56. let content = '';
  57. if (this.transform) {
  58. const filename = this.from(from);
  59. content = this.transform(fs_extra_1.default.readFileSync(filename), filename);
  60. }
  61. if (content) {
  62. return fs_extra_1.default
  63. .outputFile(to, content)
  64. .catch(() => {
  65. // this.info('copy', e)
  66. })
  67. .then(() => this.onChange && this.onChange());
  68. }
  69. return fs_extra_1.default
  70. .copy(this.from(from), to, { overwrite: true })
  71. .catch(() => {
  72. // this.info('copy', e)
  73. })
  74. .then(() => this.onChange && this.onChange());
  75. }
  76. remove(from) {
  77. const to = this.to(from);
  78. this.info('remove', from + '=>' + to);
  79. return fs_extra_1.default
  80. .remove(to)
  81. .catch(() => {
  82. // this.info('remove', e)
  83. })
  84. .then(() => this.onChange && this.onChange());
  85. }
  86. info(type, msg) {
  87. this.verbose && console.log(type, msg);
  88. }
  89. from(from) {
  90. return path_1.default.join(this.watcher.options.cwd, from);
  91. }
  92. to(from) {
  93. return path_1.default.join(this.dest, from);
  94. }
  95. }
  96. exports.FileWatcher = FileWatcher;