浏览代码

fix: add命令

guoxiaoxiao8 5 年之前
父节点
当前提交
3f59ac748f
共有 3 个文件被更改,包括 359 次插入49 次删除
  1. 242 48
      jd/createComponentMode.js
  2. 116 0
      src/config.js
  3. 1 1
      tsconfig.json

+ 242 - 48
jd/createComponentMode.js

@@ -3,9 +3,11 @@
 const inquirer = require('inquirer');
 // import {  ROOT_PACKAGE_PATH } from '../util/dic';
 //const conf = require(ROOT_PACKAGE_PATH('src/config.json'));
-import { nav } from '../src/config.ts';
-// const path = require('path');
-// const fs = require('fs');
+//import {a} from "./a.js"
+// import  {nav}  from '../src/config.js';
+// import { from } from 'core-js/fn/array';
+const path = require('path');
+const fs = require('fs');
 // const copy = require('copy');
 //const createPkgDeclare = require('./createPkgDeclare');
 // const t = require('@babel/types');
@@ -31,19 +33,19 @@ function init() {
         type: 'input',
         name: 'name',
         message: '组件英文名(每个单词的首字母都大写,如TextBox):',
-        validate(value: any) {
-          let repeat = false;
-          for (var i = 0; i < nav.length; i++) {
-            for (var j = 0; j < nav[i].packages.length; j++) {
-              if (nav[i].packages[j].name === value) {
-                repeat = true;
-              }
-            }
-          }
+        validate(value) {
+          //   let repeat = false;
+          //   for (var i = 0; i < nav.length; i++) {
+          //     for (var j = 0; j < nav[i].packages.length; j++) {
+          //       if (nav[i].packages[j].name === value) {
+          //         repeat = true;
+          //       }
+          //     }
+          //   }
 
-          if (repeat) {
-            return '该组件名已存在!';
-          }
+          //   if (repeat) {
+          //     return '该组件名已存在!';
+          //   }
           const pass = value && value.match(/^[A-Z]/);
           if (pass) {
             return true;
@@ -55,7 +57,7 @@ function init() {
         type: 'input',
         name: 'chnName',
         message: '组件中文名(十个字以内):',
-        validate(value: any) {
+        validate(value) {
           const pass = value && value.length <= 10;
           if (pass) {
             return true;
@@ -71,9 +73,9 @@ function init() {
       {
         type: 'rawlist',
         name: 'type',
-        message: '请选择组件类型(输入编号):',
+        message: '请选择组件类型(输入编号):目前只支持组建模板',
         choices: ['component', 'filter', 'directive', 'method'],
-        validate(value: any) {
+        validate(value) {
           const pass = value && /^[1-4]$/.test(value);
           if (pass) {
             return true;
@@ -81,45 +83,237 @@ function init() {
           return '输入有误!请输入选项前编号';
         }
       },
-      {
-        type: 'rawlist',
-        name: 'sort',
-        message: '请选择组件分类(输入编号):',
-        choices: sorts,
-        validate(value: any) {
-          const pass = /^[1-7]$/.test(value);
-          if (pass) {
-            return true;
-          }
-          return '输入有误!请输入选项前编号';
-        }
-      },
-      {
-        type: 'confirm',
-        name: 'showDemo',
-        message: '是否需要DEMO页面?',
-        default: true
-      },
-      {
-        type: 'confirm',
-        name: 'showTest',
-        message: '是否需要单元测试页面?',
-        default: true
-      },
+      //   {
+      //     type: 'rawlist',
+      //     name: 'sort',
+      //     message: '请选择组件分类(输入编号):',
+      //     // choices: sorts,
+      //     validate(value) {
+      //       const pass = /^[1-7]$/.test(value);
+      //       if (pass) {
+      //         return true;
+      //       }
+      //       return '输入有误!请输入选项前编号';
+      //     }
+      //   },
+      //   {
+      //     type: 'confirm',
+      //     name: 'showDemo',
+      //     message: '是否需要DEMO页面?',
+      //     default: true
+      //   },
+      //   {
+      //     type: 'confirm',
+      //     name: 'showTest',
+      //     message: '是否需要单元测试页面?',
+      //     default: true
+      //   },
       {
         type: 'input',
         name: 'author',
         message: '组件作者(可署化名):'
       }
     ])
-    .then(function(answers: any) {
+    .then(function(answers) {
       // answers.sort = String(sorts.indexOf(answers.sort));
-      // newCpt = Object.assign(newCpt, answers);
-      console.log(answers);
-      //createDir();
+      newCpt = Object.assign(newCpt, answers);
+      createNew();
     });
 }
+function createIndexJs() {
+  const nameLc = newCpt.name.toLowerCase();
+  const destPath = path.join('src/packages/' + nameLc);
+  if (!fs.existsSync(destPath)) {
+    fs.mkdirSync(destPath);
+  }
+  // copy(path.join(__dirname, './__template__/**.*'), destPath, function (err: any, file: any) {
+  // 	if (err) {
+  // 		console.log('拷贝__template__目录失败!');
+  // 	}
+  // 	createNew();
+  // });
+
+  if (newCpt.type == 'method') return;
+  return new Promise((resolve, reject) => {
+    //         let content = `import ${newCpt.name} from './src/${nameLc}.vue';
+    // ${newCpt.name}.install = function(Vue) {
+    //   Vue.${newCpt.type}(${newCpt.name}.name, ${newCpt.name});
+    // };
+    // export default ${newCpt.name}`;
+    //         let content2 = `${newCpt.name}.install = function(Vue) {
+    // Vue.${newCpt.type}(${newCpt.name}.name, ${newCpt.name});
+    // };
+    // export default ${newCpt.name}`;
 
-export default async function createComponent() {
+    // const dirPath = path.join(__dirname, `../src/packages/${nameLc}/`);
+
+    // const filePath = path.join(dirPath, `index.js`);
+    // if (!fs.existsSync(dirPath)) {
+    // 	fs.mkdirSync(filePath);
+    // }
+    // if (newCpt.type == 'filter' || newCpt.type == 'directive'){
+    //     content = content2;
+    // }
+    // fs.writeFile(filePath,  content, (err) => {
+    //     if (err) throw err;
+    resolve(`生成index.js文件成功`);
+    // });
+  });
+}
+
+function createVue() {
+  return new Promise((resolve, reject) => {
+    const nameLc = newCpt.name.toLowerCase();
+    let content = `<template>
+		<view :class="classes" @click="handleClick">
+		  <view>{{ name }}</view>
+		  <view>{{ txt }}</view>
+		</view>
+	  </template>
+	  <script lang="ts">
+	  import { toRefs } from 'vue';
+	  import { createComponent } from '@/utils/create';
+	  const { componentName, create } = createComponent('temp');
+	  
+	  export default create({
+		props: {
+		  name: {
+			type: String,
+			default: ''
+		  },
+		  txt: {
+			type: String,
+			default: ''
+		  }
+		},
+		components: {},
+		emits: ['click'],
+	  
+		setup(props, { emit }) {
+		  console.log('componentName', componentName);
+	  
+		  const { name, txt } = toRefs(props);
+	  
+		  const handleClick = (event: Event) => {
+			emit('click', event);
+		  };
+	  
+		  return { name, txt, handleClick };
+		}
+	  });
+	  </script>
+	  
+	  <style lang="scss">
+	  @import 'index.scss';
+	  </style>
+	  `;
+    const dirPath = path.join(__dirname, `../src/packages/${nameLc}/`);
+    const filePath = path.join(dirPath, `index.vue`);
+    if (!fs.existsSync(dirPath)) {
+      fs.mkdirSync(filePath);
+    }
+    fs.writeFile(filePath, content, err => {
+      if (err) throw err;
+      resolve(`生成${newCpt.name}.vue文件成功`);
+    });
+  });
+}
+
+function createDemo() {
+  return new Promise((resolve, reject) => {
+    const nameLc = newCpt.name.toLowerCase();
+    let content = `<template>
+    <div class="demo-list"></div>
+</template>
+<script>
+export default {
+    data() {
+        return {};
+    },
+    methods: {
+    }
+}
+</script>`;
+    const dirPath = path.join(__dirname, '../src/packages/' + nameLc);
+    const filePath = path.join(dirPath, `demo.vue`);
+    if (!fs.existsSync(dirPath)) {
+      fs.mkdirSync(filePath);
+    }
+    fs.writeFile(filePath, content, err => {
+      if (err) throw err;
+      resolve(`生成demo.vue文件成功`);
+    });
+  });
+}
+
+function addToPackageJson() {
+  // return new Promise((resolve, reject) => {
+  //     conf.packages.push(newCpt);
+  //     const dirPath = path.join(__dirname, `../`);
+  //     const filePath = path.join(dirPath, `config.json`);
+  //     fs.writeFile(filePath, JSON.stringify(conf, null, 2), (err) => {
+  //         if (err) throw err;
+  //         resolve(`修改config.json文件成功`);
+  //     });
+  // });
+}
+function createScss() {
+  return new Promise((resolve, reject) => {
+    const nameLc = newCpt.name.toLowerCase();
+    let content = `.nut-temp {}`;
+    const dirPath = path.join(__dirname, '../src/packages/' + nameLc);
+    const filePath = path.join(dirPath, `index.scss`);
+    if (!fs.existsSync(dirPath)) {
+      fs.mkdirSync(filePath);
+    }
+    fs.writeFile(filePath, content, err => {
+      if (err) throw err;
+      resolve(`index.scss文件成功`);
+    });
+  });
+}
+function createDoc() {
+  return new Promise((resolve, reject) => {
+    const nameLc = newCpt.name.toLowerCase();
+    let content = `组建使用说明文件`;
+    const dirPath = path.join(__dirname, '../src/packages/' + nameLc);
+    const filePath = path.join(dirPath, `doc.md`);
+    if (!fs.existsSync(dirPath)) {
+      fs.mkdirSync(filePath);
+    }
+    fs.writeFile(filePath, content, err => {
+      if (err) throw err;
+      resolve(`doc.md文件成功`);
+    });
+  });
+}
+function createNew() {
+  createIndexJs()
+    .then(() => {
+      if (newCpt.type == 'component' || newCpt.type == 'method') {
+        return createVue();
+      } else {
+        return;
+      }
+    })
+    .then(() => {
+      return createScss();
+    })
+    .then(() => {
+      return createDemo();
+    })
+    .then(() => {
+      return createDoc();
+    })
+    .then(() => {
+      // return addToPackageJson();
+    })
+    .then(() => {
+      console.log('组件模板生成完毕,请开始你的表演~');
+      process.exit();
+    });
+}
+function createComponent() {
   init();
 }
+createComponent();

+ 116 - 0
src/config.js

@@ -0,0 +1,116 @@
+export const versions = [
+  { name: '1.x', link: '/1x/' },
+  { name: '2.x', link: '/' },
+  { name: '3.x', link: '/3x/' }
+];
+
+export const docs = {
+  name: '指南',
+  packages: [
+    {
+      name: 'intro',
+      cName: '介绍',
+      show: true
+    },
+    {
+      name: 'start',
+      cName: '快速上手',
+      show: true
+    },
+    {
+      name: 'theme',
+      cName: '主题定制',
+      show: true
+    },
+    {
+      name: 'international',
+      cName: '国际化',
+      show: true
+    },
+    {
+      name: 'https://github.com/jdf2e/nutui/releases',
+      cName: '更新日志',
+      show: true,
+      isLink: true
+    }
+  ]
+};
+
+export const nav = [
+  {
+    name: '布局组件',
+    packages: [
+      {
+        name: 'Button',
+        sort: 1,
+        cName: '按钮组件',
+        type: 'component',
+        show: true,
+        desc: '按钮用于触发一个操作,如提交表单。',
+        author: 'richard1015'
+      }
+    ]
+  },
+  {
+    name: '操作反馈',
+    packages: []
+  },
+  {
+    name: '基础组件',
+    packages: [
+      {
+        name: 'Temp',
+        sort: 1,
+        cName: '模板组件',
+        type: 'component',
+        show: true,
+        desc: '组件模板示例',
+        author: 'richard1015'
+      },
+      {
+        name: 'Cell',
+        sort: 1,
+        cName: '单元格组件',
+        type: 'component',
+        show: true,
+        desc: '展示列表',
+        author: 'richard1015'
+      },
+      {
+        name: 'Uploader',
+        sort: 2,
+        cName: '上传组件',
+        type: 'component',
+        show: true,
+        desc: '上传文件、图片',
+        author: 'richard1015'
+      },
+      {
+        name: 'Icon',
+        sort: 3,
+        cName: '图标组件',
+        type: 'component',
+        show: true,
+        desc: '图标',
+        author: 'richard1015'
+      },
+      {
+        name: 'Price',
+        sort: 4,
+        cName: '价格组件',
+        type: 'component',
+        show: true,
+        desc: '价格组件',
+        author: 'ailululu'
+      }
+    ]
+  },
+  {
+    name: '导航组件',
+    packages: []
+  },
+  {
+    name: '业务组件',
+    packages: []
+  }
+];

+ 1 - 1
tsconfig.json

@@ -32,7 +32,7 @@
     "src/**/*.tsx",
     "src/**/*.vue",
     "tests/**/*.ts",
-    "tests/**/*.tsx"
+    "tests/**/*.tsx", "src/config.js"
   ],
   "exclude": [
     "node_modules"