1 | const path = require("path");
|
2 | const fs = require("fs-extra");
|
3 | const noox = require("noox");
|
4 | const parse = require("./parse/parse.js");
|
5 | const utils = require("./utils");
|
6 |
|
7 | const loadPlugins = require("./plugin.js").loadPlugins;
|
8 | const ydoc = require("./ydoc.js");
|
9 | const ydocPath = path.resolve(__dirname, "..");
|
10 | const loadMarkdownPlugins = require("./parse/markdown").loadMarkdownPlugins;
|
11 |
|
12 | function 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 |
|
24 | async function run(config) {
|
25 |
|
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);
|
39 | fs.ensureDirSync(themeDist);
|
40 |
|
41 | fs.copySync(root, dist);
|
42 | fs.copySync(themePath, themeDist);
|
43 | if (ydoc.config.theme && ydoc.config.theme !== "default") {
|
44 | handleTheme(ydoc.config.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
|
79 | }
|
80 |
|
81 | function handleTheme(theme) {
|
82 |
|
83 | let themePath = path.resolve(process.cwd(), "theme");
|
84 | let themeFile = path.resolve(themePath, "./ydoc-theme-" + theme);
|
85 | let themeModuleDir;
|
86 |
|
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 |
|
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 |
|
107 | module.exports = run; |
\ | No newline at end of file |