Browse Source

feat: sync-docs 内容修改

yangxiaolu3 4 years ago
parent
commit
9d193231d7
1 changed files with 47 additions and 28 deletions
  1. 47 28
      jd/copymd.js

+ 47 - 28
jd/copymd.js

@@ -1,4 +1,4 @@
-const targetBaseUrl = 'site_docs';
+const targetBaseUrl = `${process.cwd()}/site_docs`;
 const fse = require('fs-extra');
 const copyFile = (from, to) => {
   fse
@@ -10,20 +10,58 @@ const copyFile = (from, to) => {
       console.error(err);
     });
 };
+const removeFile = async (url) => {
+  return new Promise((res, rej) => {
+    fse.remove(url, (err) => {
+      if (err) {
+        throw err;
+      }
+      res(true);
+    });
+  });
+};
+
 const copy = async () => {
   let configPath = `src/config.json`;
   let configPkgPath = `package.json`;
   let nutuiDocsConfigPath = `${targetBaseUrl}/config.json`;
 
-  const exists = await fse.pathExists(configPath);
-
-  if (exists) {
-    const fromConfig = await fse.readJson(configPath);
+  // 判断 site_docs 文件是否存在根路径中
+  const existsRoot = await fse.pathExists(targetBaseUrl);
 
-    const fromPkgConfig = await fse.readJson(configPkgPath);
+  if (existsRoot) await removeFile(targetBaseUrl);
+  // 复制所有组件
+  const fromConfig = await fse.readJson(configPath);
+  fromConfig.nav.forEach(({ packages }) => {
+    packages.forEach((item) => {
+      if (item.show) {
+        let cmpName = item.name.toLowerCase();
+        let docpath = `src/packages/__VUE/${cmpName}/doc.md`;
+        let doctaropath = `src/packages/__VUE/${cmpName}/doc.taro.md`;
+        fse.readFile(docpath, (err, data) => {
+          if (!err) {
+            copyFile(docpath, `${targetBaseUrl}/docs/${cmpName}/doc.md`);
+          }
+        });
+        fse.readFile(doctaropath, (err, data) => {
+          if (!err) {
+            copyFile(doctaropath, `${targetBaseUrl}/docs/${cmpName}/doc.taro.md`);
+          }
+        });
+      }
+    });
+  });
 
-    const docsConfig = await fse.readJson(nutuiDocsConfigPath);
+  // 复制 config.json
+  const fromPkgConfig = await fse.readJson(configPkgPath);
 
+  const obj = {
+    version: '',
+    nav: [],
+    docs: []
+  };
+  fse.outputJSON(nutuiDocsConfigPath, obj, () => {
+    const docsConfig = fse.readJson(nutuiDocsConfigPath);
     docsConfig.version = fromPkgConfig.version;
     docsConfig.nav = fromConfig.nav;
     docsConfig.docs = fromConfig.docs;
@@ -33,28 +71,9 @@ const copy = async () => {
         spaces: 2
       })
       .then(() => {
-        console.log(`${docsConfig.version} success!`);
-      });
-    fromConfig.nav.forEach(({ packages }) => {
-      packages.forEach((item) => {
-        if (item.show) {
-          let cmpName = item.name.toLowerCase();
-          let docpath = `src/packages/__VUE/${cmpName}/doc.md`;
-          let doctaropath = `src/packages/__VUE/${cmpName}/doc.taro.md`;
-          fse.readFile(docpath, (err, data) => {
-            if (!err) {
-              copyFile(docpath, `${targetBaseUrl}/docs/${cmpName}/doc.md`);
-            }
-          });
-          fse.readFile(doctaropath, (err, data) => {
-            if (!err) {
-              copyFile(doctaropath, `${targetBaseUrl}/docs/${cmpName}/doc.taro.md`);
-            }
-          });
-        }
+        console.log(`${fromPkgConfig.version} success!`);
       });
-    });
-  }
+  });
 };
 // copy(reactBaseUrl, 'react');
 copy();