copymd.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const targetBaseUrl = `${process.cwd()}/site_docs`;
  2. const fse = require('fs-extra');
  3. const copyFile = (from, to) => {
  4. fse
  5. .copy(from, to)
  6. .then(() => {
  7. console.log('success!>', to);
  8. })
  9. .catch((err) => {
  10. console.error(err);
  11. });
  12. };
  13. const removeFile = async (url) => {
  14. return new Promise((res, rej) => {
  15. fse.remove(url, (err) => {
  16. if (err) {
  17. throw err;
  18. }
  19. res(true);
  20. });
  21. });
  22. };
  23. const copy = async () => {
  24. let configPath = `src/config.json`;
  25. let configPkgPath = `package.json`;
  26. let nutuiDocsConfigPath = `${targetBaseUrl}/config.json`;
  27. // 判断 site_docs 文件是否存在根路径中
  28. const existsRoot = await fse.pathExists(targetBaseUrl);
  29. if (existsRoot) await removeFile(targetBaseUrl);
  30. // 复制所有组件
  31. const fromConfig = await fse.readJson(configPath);
  32. fromConfig.nav.forEach(({ packages }) => {
  33. packages.forEach((item) => {
  34. if (item.show) {
  35. let cmpName = item.name.toLowerCase();
  36. let docpath = `src/packages/__VUE/${cmpName}/doc.md`;
  37. let docEnPath = `src/packages/__VUE/${cmpName}/doc.en-US.md`;
  38. let doctaropath = `src/packages/__VUE/${cmpName}/doc.taro.md`;
  39. fse.readFile(docpath, (err, data) => {
  40. if (!err) {
  41. copyFile(docpath, `${targetBaseUrl}/docs/${cmpName}/doc.md`);
  42. }
  43. });
  44. fse.readFile(docEnPath, (err, data) => {
  45. if (!err) {
  46. copyFile(docEnPath, `${targetBaseUrl}/docs/${cmpName}/doc.en-US.md`);
  47. }
  48. });
  49. fse.readFile(doctaropath, (err, data) => {
  50. if (!err) {
  51. copyFile(doctaropath, `${targetBaseUrl}/docs/${cmpName}/doc.taro.md`);
  52. }
  53. });
  54. }
  55. });
  56. });
  57. // 复制 config.json
  58. const fromPkgConfig = await fse.readJson(configPkgPath);
  59. const obj = {
  60. version: '',
  61. nav: [],
  62. docs: []
  63. };
  64. fse.outputJSON(nutuiDocsConfigPath, obj, () => {
  65. const docsConfig = fse.readJson(nutuiDocsConfigPath);
  66. docsConfig.version = fromPkgConfig.version;
  67. docsConfig.nav = fromConfig.nav;
  68. docsConfig.docs = fromConfig.docs;
  69. docsConfig.demoUrl = 'https://nutui.jd.com/3x/demo.html#';
  70. fse
  71. .writeJson(nutuiDocsConfigPath, docsConfig, {
  72. spaces: 2
  73. })
  74. .then(() => {
  75. console.log(`${fromPkgConfig.version} success!`);
  76. });
  77. });
  78. };
  79. // copy(reactBaseUrl, 'react');
  80. copy();