new.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 'use strict';
  2. console.log();
  3. process.on('exit', () => {
  4. console.log();
  5. });
  6. if (!process.argv[2]) {
  7. console.error('[组件名]必填 - Please enter new component name');
  8. process.exit(1);
  9. }
  10. const path = require('path');
  11. const fileSave = require('file-save');
  12. const uppercamelcase = require('uppercamelcase');
  13. const componentname = process.argv[2];
  14. const chineseName = process.argv[3] || componentname;
  15. const ComponentName = uppercamelcase(componentname);
  16. const PackagePath = path.resolve(__dirname, '../../packages', componentname);
  17. const Files = [
  18. {
  19. filename: 'index.js',
  20. content: `import ${ComponentName} from './src/main';
  21. /* istanbul ignore next */
  22. ${ComponentName}.install = function(Vue) {
  23. Vue.component(${ComponentName}.name, ${ComponentName});
  24. };
  25. export default ${ComponentName};`
  26. },
  27. {
  28. filename: 'src/main.vue',
  29. content: `<template>
  30. <div class="el-${componentname}"></div>
  31. </template>
  32. <script>
  33. export default {
  34. name: 'El${ComponentName}'
  35. };
  36. </script>`
  37. },
  38. {
  39. filename: path.join('../../examples/docs/zh-CN', `${componentname}.md`),
  40. content: `## ${ComponentName} ${chineseName}`
  41. },
  42. {
  43. filename: path.join('../../examples/docs/en-US', `${componentname}.md`),
  44. content: `## ${ComponentName}`
  45. },
  46. {
  47. filename: path.join('../../examples/docs/es', `${componentname}.md`),
  48. content: `## ${ComponentName}`
  49. },
  50. {
  51. filename: path.join('../../test/unit/specs', `${componentname}.spec.js`),
  52. content: `import { createTest, destroyVM } from '../util';
  53. import ${ComponentName} from 'packages/${componentname}';
  54. describe('${ComponentName}', () => {
  55. let vm;
  56. afterEach(() => {
  57. destroyVM(vm);
  58. });
  59. it('create', () => {
  60. vm = createTest(${ComponentName}, true);
  61. expect(vm.$el).to.exist;
  62. });
  63. });
  64. `
  65. },
  66. {
  67. filename: path.join('../../packages/theme-chalk/src', `${componentname}.scss`),
  68. content: `@import "mixins/mixins";
  69. @import "common/var";
  70. @include b(${componentname}) {
  71. }`
  72. },
  73. {
  74. filename: path.join('../../types', `${componentname}.d.ts`),
  75. content: `import { ElementUIComponent } from './component'
  76. /** ${ComponentName} Component */
  77. export declare class El${ComponentName} extends ElementUIComponent {
  78. }`
  79. }
  80. ];
  81. // 添加到 components.json
  82. const componentsFile = require('../../components.json');
  83. if (componentsFile[componentname]) {
  84. console.error(`${componentname} 已存在.`);
  85. process.exit(1);
  86. }
  87. componentsFile[componentname] = `./packages/${componentname}/index.js`;
  88. fileSave(path.join(__dirname, '../../components.json'))
  89. .write(JSON.stringify(componentsFile, null, ' '), 'utf8')
  90. .end('\n');
  91. // 创建 package
  92. Files.forEach(file => {
  93. fileSave(path.join(PackagePath, file.filename))
  94. .write(file.content, 'utf8')
  95. .end('\n');
  96. });
  97. // 添加到 nav.config.json
  98. const navConfigFile = require('../../examples/nav.config.json');
  99. Object.keys(navConfigFile).forEach(lang => {
  100. let groups = navConfigFile[lang][4].groups;
  101. groups[groups.length - 1].list.push({
  102. path: `/${componentname}`,
  103. title: lang === 'zh-CN' && componentname !== chineseName
  104. ? `${ComponentName} ${chineseName}`
  105. : ComponentName
  106. });
  107. });
  108. fileSave(path.join(__dirname, '../../examples/nav.config.json'))
  109. .write(JSON.stringify(navConfigFile, null, ' '), 'utf8')
  110. .end('\n');
  111. console.log('DONE!');