浏览代码

feat: 优化add命令,支持国际化

suzigang 3 年之前
父节点
当前提交
61ae565b19
共有 3 个文件被更改,包括 88 次插入20 次删除
  1. 6 3
      jd/createComponentMode.js
  2. 64 3
      jd/demo.js
  3. 18 14
      jd/generate-taro-route.js

+ 6 - 3
jd/createComponentMode.js

@@ -100,8 +100,8 @@ const traverseAst = (ast, componentName, componentType) => {
     enter: (node) => {
       if (node.type === 'VariableDeclarator' && node.id.name === 'subpackages') {
         node.init.elements.forEach((item) => {
-          const itemKey = item.properties.find((value) => value.key.name === 'root').value.value;
-          const itemValue = item.properties.find((value) => value.key.name === 'pages').value.elements;
+          const itemKey = item.properties.find((value) => value.key.value === 'root').value.value;
+          const itemValue = item.properties.find((value) => value.key.value === 'pages').value.elements;
           const path = `pages/${componentName}/index`;
           if (itemKey === componentType && !itemValue.find((subItem) => subItem.value === path)) {
             itemValue.push({
@@ -149,12 +149,15 @@ const createDemo = (paths) => {
 };
 
 const createDoc = (paths) => {
-  /**生成doc文档 */
+  /**生成doc,中英文文档 */
   const sourcePath = paths.sourcePath;
   const name = sourcePath.substring(sourcePath.lastIndexOf('/') + 1);
   const doc = demoModel(name).doc;
+  const docEN = demoModel(name).docEN;
   const filePath = path.join(sourcePath, 'doc.md');
+  const filePathEN = path.join(sourcePath, 'doc.en-US.md');
   if (!fs.existsSync(filePath)) fs.writeFileSync(filePath, doc);
+  if (!fs.existsSync(filePathEN)) fs.writeFileSync(filePathEN, docEN);
 };
 
 const createScss = (paths) => {

+ 64 - 3
jd/demo.js

@@ -41,7 +41,7 @@ export default create({
 `,
     demo: `<template>
   <div class="demo">
-    <h2>基础用法</h2>
+    <h2>{{ translate('basic') }}</h2>
     <nut-cell>
       <nut-${nameLc}></nut-${nameLc}>
     </nut-cell>
@@ -49,11 +49,20 @@ export default create({
 </template>
 <script lang="ts">
 import { createComponent } from '@/packages/utils/create';
-const { createDemo } = createComponent('${nameLc}');
+const { createDemo, translate } = createComponent('${nameLc}');
+import { useTranslate } from '@/sites/assets/util/useTranslate';
+useTranslate({
+  'zh-CN': {
+    basic: '基本用法'
+  },
+  'en-US': {
+    basic: 'Basic Usage'
+  }
+})
 export default createDemo({
   props: {},
   setup() {
-    return {};
+    return { translate };
   }
 });
 </script>
@@ -133,6 +142,58 @@ app.use();
 | 事件名 | 说明           | 回调参数     |
 |--------|----------------|--------------|
 | click  | 点击图标时触发 | event: Event |
+`,
+    docEN: `# ${nameLc} 
+
+### Intro
+
+### Install
+
+\`\`\`javascript
+
+import { createApp } from 'vue';
+// vue
+import {  } from '@nutui/nutui';
+// taro
+import {  } from '@nutui/nutui-taro';
+
+const app = createApp();
+app.use();
+
+\`\`\`
+
+### Basic Usage
+
+:::demo
+
+\`\`\`html
+<template>
+  
+</template>
+<script lang="ts">
+  export default {
+    setup() {
+      return {  };
+    }
+  };
+</script>
+\`\`\`
+
+:::
+
+## API
+
+### Props
+
+| Attribute         | Description                             | Type   | Default           |
+|--------------|----------------------------------|--------|------------------|
+| name         | description               | String | -                |
+
+### Events
+
+| Event | Description           | Arguments     |
+|--------|----------------|--------------|
+| click  | description | event: Event |
 `
   };
 

+ 18 - 14
jd/generate-taro-route.js

@@ -28,20 +28,24 @@ const createConfig = async () => {
 };
 
 const create = async () => {
-  const configTemplate = {
-    pages: ['pages/index/index'],
-    subpackages: '',
-    window: {
-      backgroundTextStyle: 'light',
-      navigationBarBackgroundColor: '#fff',
-      navigationBarTitleText: 'NutUI',
-      navigationBarTextStyle: 'black'
-    }
-  };
-
-  configTemplate.subpackages = await createConfig();
-
-  fse.writeFileSync(taroConfig, `export default ${JSON.stringify(configTemplate)}`, 'utf8');
+  const subpackages = await createConfig();
+
+  fse.writeFileSync(
+    taroConfig,
+    `
+const subpackages = ${JSON.stringify(subpackages, null, 2)};\n
+export default {
+  pages: ['pages/index/index'],
+  subpackages,
+  window: {
+    backgroundTextStyle: 'light',
+    navigationBarBackgroundColor: '#fff',
+    navigationBarTitleText: 'NutUI',
+    navigationBarTextStyle: 'black'
+  }
+}`,
+    'utf8'
+  );
 };
 
 create();