register.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Register = void 0;
  4. const tslib_1 = require("tslib");
  5. const cluster_1 = tslib_1.__importDefault(require("cluster"));
  6. const debug_1 = tslib_1.__importDefault(require("debug"));
  7. const core_1 = tslib_1.__importDefault(require("./core"));
  8. const util = tslib_1.__importStar(require("./utils"));
  9. const debug = (0, debug_1.default)('egg-ts-helper#register');
  10. class Register {
  11. constructor(options) {
  12. this.tsHelperClazz = (options === null || options === void 0 ? void 0 : options.tsHelperClazz) || core_1.default;
  13. }
  14. init(options) {
  15. /* istanbul ignore else */
  16. if (!cluster_1.default.isMaster)
  17. return;
  18. // make sure ets only run once
  19. const pid = process.env.ETS_REGISTER_PID;
  20. if (pid) {
  21. return debug('egg-ts-helper watcher has ran in %s', pid);
  22. }
  23. const watch = util.convertString(process.env.ETS_WATCH, process.env.NODE_ENV !== 'test');
  24. const clazz = this.tsHelperClazz;
  25. const cwd = (options === null || options === void 0 ? void 0 : options.cwd) || process.cwd();
  26. const instance = new clazz(Object.assign({ watch }, options));
  27. if (util.checkMaybeIsJsProj(cwd)) {
  28. // write jsconfig if the project is wrote by js
  29. util.writeJsConfig(cwd);
  30. }
  31. else {
  32. const tsNodeMode = process.env.EGG_TYPESCRIPT === 'true';
  33. // no need to clean in js project
  34. // clean local js file at first.
  35. // because egg-loader cannot load the same property name to egg.
  36. if (tsNodeMode && instance.config.autoRemoveJs) {
  37. util.cleanJs(cwd);
  38. }
  39. }
  40. // cache pid to env, prevent child process executing ets again
  41. process.env.ETS_REGISTER_PID = `${process.pid}`;
  42. // exec building
  43. instance.build();
  44. }
  45. }
  46. exports.default = Register;
  47. exports.Register = Register;