new.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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('../../test/unit/specs', `${componentname}.spec.js`),
  48. content: `import { createTest, destroyVM } from '../util';
  49. import ${ComponentName} from 'packages/${componentname}';
  50. describe('${ComponentName}', () => {
  51. let vm;
  52. afterEach(() => {
  53. destroyVM(vm);
  54. });
  55. it('create', () => {
  56. vm = createTest(${ComponentName}, true);
  57. expect(vm.$el).to.exist;
  58. });
  59. });
  60. `
  61. },
  62. {
  63. filename: path.join('../../packages/theme-chalk/src', `${componentname}.scss`),
  64. content: `@import "mixins/mixins";
  65. @import "common/var";
  66. @include b(${componentname}) {
  67. }`
  68. },
  69. {
  70. filename: path.join('../../types', `${componentname}.d.ts`),
  71. content: `import { ElementUIComponent } from './component'
  72. /** ${ComponentName} Component */
  73. export declare class El${ComponentName} extends ElementUIComponent {
  74. }`
  75. }
  76. ];
  77. // 添加到 components.json
  78. const componentsFile = require('../../components.json');
  79. if (componentsFile[componentname]) {
  80. console.error(`${componentname} 已存在.`);
  81. process.exit(1);
  82. }
  83. componentsFile[componentname] = `./packages/${componentname}/index.js`;
  84. fileSave(path.join(__dirname, '../../components.json'))
  85. .write(JSON.stringify(componentsFile, null, ' '), 'utf8')
  86. .end('\n');
  87. // 创建 package
  88. Files.forEach(file => {
  89. fileSave(path.join(PackagePath, file.filename))
  90. .write(file.content, 'utf8')
  91. .end('\n');
  92. });
  93. // 添加到 nav.config.json
  94. const navConfigFile = require('../../examples/nav.config.json');
  95. Object.keys(navConfigFile).forEach(lang => {
  96. let groups = navConfigFile[lang][4].groups;
  97. groups[groups.length - 1].list.push({
  98. path: `/${componentname}`,
  99. title: lang === 'zh-CN' && componentname !== chineseName
  100. ? `${ComponentName} ${chineseName}`
  101. : ComponentName
  102. });
  103. });
  104. fileSave(path.join(__dirname, '../../examples/nav.config.json'))
  105. .write(JSON.stringify(navConfigFile, null, ' '), 'utf8')
  106. .end('\n');
  107. console.log('DONE!');