generate-types.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const config = require('../src/config.json');
  2. const path = require('path');
  3. const fs = require('fs-extra');
  4. let importStr = `import { App } from 'vue';
  5. declare class UIComponent {
  6. static install(vue: App): void;
  7. }\n`;
  8. const packages = [];
  9. config.nav.map((item) => {
  10. item.packages.forEach((element) => {
  11. let { name, show, exportEmpty, type } = element;
  12. if (show || exportEmpty) {
  13. importStr +=
  14. type == 'methods'
  15. ? `declare function ${name}(options: any): void\n`
  16. : `declare class ${name} extends UIComponent {}\n`;
  17. packages.push(name);
  18. }
  19. });
  20. });
  21. let installFunction = `
  22. export interface InstallationOptions {
  23. locale?: any;
  24. lang?: any;
  25. }
  26. declare function install(app: App, options?: InstallationOptions): void;
  27. export { ${packages.join(',')},install };
  28. declare const _default: {
  29. install: typeof install;
  30. version: string;
  31. };
  32. export default _default;`;
  33. let fileStr = importStr + installFunction;
  34. fs.outputFile(path.resolve(__dirname, '../dist/nutui.d.ts'), fileStr, 'utf8', (error) => {
  35. // logger.success(`${package_config_path} 文件写入成功`);
  36. });
  37. fs.outputFile(
  38. path.resolve(__dirname, '../dist/index.d.ts'),
  39. `import * as NutUI from './nutui';
  40. export default NutUI;
  41. export * from './nutui';`,
  42. 'utf8',
  43. (error) => {
  44. // logger.success(`${package_config_path} 文件写入成功`);
  45. }
  46. );