class.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const tslib_1 = require("tslib");
  4. const debug_1 = tslib_1.__importDefault(require("debug"));
  5. const path_1 = tslib_1.__importDefault(require("path"));
  6. const utils = tslib_1.__importStar(require("../utils"));
  7. const debug = (0, debug_1.default)('egg-ts-helper#generators_class');
  8. function ClassGenerator(config, baseConfig) {
  9. const fileList = config.fileList;
  10. const dist = path_1.default.resolve(config.dtsDir, config.distName);
  11. debug('file list : %o', fileList);
  12. if (!fileList.length) {
  13. return { dist };
  14. }
  15. // using to compose import code
  16. let importStr = '';
  17. // using to create interface mapping
  18. const interfaceMap = {};
  19. fileList.forEach(f => {
  20. const { props, moduleName: sModuleName } = utils.getModuleObjByPath(f);
  21. const moduleName = `Export${sModuleName}`;
  22. const importContext = utils.getImportStr(config.dtsDir, path_1.default.join(config.dir, f), moduleName);
  23. importStr += `${importContext}\n`;
  24. // create mapping
  25. let collector = interfaceMap;
  26. while (props.length) {
  27. const name = utils.camelProp(props.shift(), config.caseStyle || baseConfig.caseStyle);
  28. if (!props.length) {
  29. collector[name] = moduleName;
  30. }
  31. else {
  32. collector = collector[name] = typeof collector[name] === 'object' ? collector[name] : Object.create(Object.prototype, {
  33. parentModuleName: {
  34. value: typeof collector[name] === 'string' ? collector[name] : undefined,
  35. },
  36. });
  37. }
  38. }
  39. });
  40. // interface name
  41. const interfaceName = config.interface || `T_${config.name.replace(/[\.\-]/g, '_')}`;
  42. // add mount interface
  43. let declareInterface;
  44. if (config.declareTo) {
  45. const interfaceList = config.declareTo.split('.');
  46. declareInterface = composeInterface(interfaceList.slice(1).concat(interfaceName), interfaceList[0], undefined, ' ');
  47. }
  48. return {
  49. dist,
  50. content: `${importStr}\n` +
  51. `declare module '${config.framework || baseConfig.framework}' {\n` +
  52. (declareInterface ? `${declareInterface}\n` : '') +
  53. composeInterface(interfaceMap, interfaceName, utils.strToFn(config.interfaceHandle), ' ') +
  54. '}\n',
  55. };
  56. }
  57. exports.default = ClassGenerator;
  58. ClassGenerator.defaultConfig = {
  59. distName: 'index.d.ts',
  60. };
  61. // composing all the interface
  62. function composeInterface(obj, wrapInterface, preHandle, indent) {
  63. let prev = '';
  64. let mid = '';
  65. let after = '';
  66. indent = indent || '';
  67. if (wrapInterface) {
  68. prev = `${indent}interface ${wrapInterface} {\n`;
  69. after = `${indent}}\n`;
  70. indent += ' ';
  71. }
  72. // compose array to object
  73. // ['abc', 'bbc', 'ccc'] => { abc: { bbc: 'ccc' } }
  74. if (Array.isArray(obj)) {
  75. let curr = obj.pop();
  76. while (obj.length) {
  77. curr = { [obj.pop()]: curr };
  78. }
  79. obj = curr;
  80. }
  81. Object.keys(obj).forEach(key => {
  82. const val = obj[key];
  83. if (typeof val === 'string') {
  84. mid += `${indent + key}: ${preHandle ? preHandle(val) : val};\n`;
  85. }
  86. else {
  87. const newVal = composeInterface(val, undefined, preHandle, indent + ' ');
  88. if (newVal) {
  89. mid += `${indent + key}: ${val.parentModuleName ? `${val.parentModuleName} & ` : ''}{\n${newVal + indent}}\n`;
  90. }
  91. }
  92. });
  93. return `${prev}${mid}${after}`;
  94. }