UNPKG

3.3 kBJavaScriptView Raw
1const path = require("path");
2const fs = require("fs-extra");
3const noox = require("noox");
4const parse = require("./parse/parse.js");
5const utils = require("./utils");
6
7const loadPlugins = require("./plugin.js").loadPlugins;
8const ydoc = require("./ydoc.js");
9const ydocPath = path.resolve(__dirname, "..");
10const loadMarkdownPlugins = require("./parse/markdown").loadMarkdownPlugins;
11
12function initConfig(config){
13 const projectPath = utils.projectPath;
14 if(!config){
15 const configFilepath = utils.getConfigPath(projectPath);
16 config = utils.getConfig(configFilepath);
17 }
18
19 utils.extend(ydoc.config, config);
20 ydoc.config.dist = path.resolve(projectPath, ydoc.config.dist);
21 ydoc.config.root = path.resolve(projectPath, ydoc.config.root);
22}
23
24async function run(config) {
25 // init Resources path
26 initConfig(config)
27 const dist = ydoc.config.dist;
28 const root = ydoc.config.root;
29 const themePath = path.resolve(ydocPath, "theme");
30 const customerComponentsPath = path.resolve(root, "_components");
31
32 const themeDist = path.resolve(dist, "_theme");
33 const componentsDist = path.resolve(themeDist, "components");
34
35 if(process.env.NODE_ENV === 'production'){
36 fs.removeSync(dist);
37 }
38 fs.ensureDirSync(dist); // 新建 my-project/_site
39 fs.ensureDirSync(themeDist); // 新建 my-project/_site/_theme
40
41 fs.copySync(root, dist); // docs => _site
42 fs.copySync(themePath, themeDist); // ydoc/theme => my-project/_site/_theme
43 if (ydoc.config.theme && ydoc.config.theme !== "default") {
44 handleTheme(ydoc.config.theme); // load custom theme
45 }
46
47 fs.copySync(
48 path.resolve(themeDist, "style.css"),
49 path.resolve(dist, "ydoc/styles", "style.css")
50 );
51 fs.copySync(
52 path.resolve(themeDist, "images"),
53 path.resolve(dist, "ydoc/images")
54 );
55 fs.copySync(
56 path.resolve(themeDist, "scripts"),
57 path.resolve(dist, "ydoc/scripts")
58 );
59
60 if(utils.dirExist(customerComponentsPath)){
61 utils.mergeCopyFiles(customerComponentsPath, componentsDist);
62 }
63
64
65 loadPlugins();
66
67 utils.noox = new noox(componentsDist, {
68 relePath: ydoc.relePath,
69 hook: ydoc.hook
70 });
71
72 loadMarkdownPlugins(ydoc.config.markdownIt);
73
74 await parse.parseSite(dist);
75 fs.removeSync(themeDist);
76
77 return {
78 code: 0 //0 代表成功
79 }
80
81 function handleTheme(theme) {
82 // 如果theme的文件夹中存在对应的theme,则使用对应的theme,没有的话使用node_modules
83 let themePath = path.resolve(process.cwd(), "theme");
84 let themeFile = path.resolve(themePath, "./ydoc-theme-" + theme);
85 let themeModuleDir;
86 // console.log('-----themeFile',themeFile)
87 if(fs.existsSync(themeFile)) {
88 themeModuleDir = themeFile;
89 } else {
90 let modules = path.resolve(process.cwd(), "node_modules");
91 let themeFile = path.resolve(modules, "./ydoc-theme-" + theme);
92 themeModuleDir = path.resolve(themeFile, "./theme");
93 }
94 // console.log('=======themeModuleDir', themeModuleDir)
95 try {
96 utils.mergeCopyFiles(themeModuleDir, themeDist);
97 } catch (err) {
98 err.message =
99 "Load " +
100 path.resolve(modules, "./ydoc-theme-" + theme) +
101 `theme failed, Please run "npm install ydoc-theme-${theme}" install the theme.`;
102 throw err;
103 }
104 }
105}
106
107module.exports = run;
\No newline at end of file