init.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const tslib_1 = require("tslib");
  4. const enquirer_1 = require("enquirer");
  5. const utils = tslib_1.__importStar(require("../utils"));
  6. const path_1 = tslib_1.__importDefault(require("path"));
  7. const fs_1 = tslib_1.__importDefault(require("fs"));
  8. const __1 = require("../");
  9. const TYPE_TS = 'typescript';
  10. const TYPE_JS = 'javascript';
  11. class InitCommand {
  12. constructor() {
  13. this.description = 'Init egg-ts-helper in your existing project';
  14. this.options = '<type>';
  15. }
  16. async run(_, { args, cwd }) {
  17. let type = args[1];
  18. const pkgInfo = utils.getPkgInfo(cwd);
  19. const typeList = [TYPE_TS, TYPE_JS];
  20. pkgInfo.egg = pkgInfo.egg || {};
  21. // verify type
  22. if (!typeList.includes(type)) {
  23. const result = await (0, enquirer_1.prompt)({
  24. type: 'autocomplete',
  25. name: 'type',
  26. message: 'Choose the type of your project',
  27. choices: utils.checkMaybeIsJsProj(cwd) ? typeList.reverse() : typeList,
  28. }).catch(() => {
  29. utils.log('cancel initialization');
  30. return { type: '' };
  31. });
  32. type = result.type;
  33. }
  34. if (type === TYPE_JS) {
  35. // create jsconfig.json
  36. const result = utils.writeJsConfig(cwd);
  37. if (result) {
  38. utils.log('create ' + result);
  39. }
  40. }
  41. else if (type === TYPE_TS) {
  42. pkgInfo.egg.typescript = true;
  43. // create tsconfig.json
  44. const result = utils.writeTsConfig(cwd);
  45. if (result) {
  46. utils.log('create ' + result);
  47. }
  48. }
  49. else {
  50. return;
  51. }
  52. // add egg-ts-helper/register to egg.require
  53. pkgInfo.egg.require = pkgInfo.egg.require || [];
  54. if (!pkgInfo.egg.require.includes('egg-ts-helper/register') && !pkgInfo.egg.declarations) {
  55. pkgInfo.egg.declarations = true;
  56. }
  57. // write package.json
  58. const pkgDist = path_1.default.resolve(cwd, './package.json');
  59. fs_1.default.writeFileSync(pkgDist, JSON.stringify(pkgInfo, null, 2));
  60. utils.log('change ' + pkgDist);
  61. // build once
  62. utils.log('create d.ts ...');
  63. (0, __1.createTsHelperInstance)({ cwd }).build();
  64. utils.log('complete initialization');
  65. }
  66. }
  67. exports.default = new InitCommand();