1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | const wpconfig = require('../webpack.config');
|
7 | const webpack = require('webpack');
|
8 | const walkmd = require('./walkmd');
|
9 | const sh = require('child_process').execSync;
|
10 | const path = require('path');
|
11 | const fs = require('fs');
|
12 | const parseNav = require('./parseNav');
|
13 | const babelFiles = require('./babelFiles');
|
14 |
|
15 | const node_modules = path.resolve(__dirname, '../node_modules');
|
16 | const wpconfigPath = path.resolve(__dirname, '../webpack.config.js');
|
17 |
|
18 |
|
19 | function parseWPConfig(config, isProduction) {
|
20 | let res = wpconfig(isProduction);
|
21 | if (config.setWebpackConfig) {
|
22 | res = config.setWebpackConfig(res);
|
23 | }
|
24 | return res;
|
25 | }
|
26 |
|
27 | function before(config) {
|
28 |
|
29 | let src;
|
30 | let dist = path.resolve(__dirname, '../theme');
|
31 |
|
32 |
|
33 |
|
34 | if (!config.theme || config.theme == 'default') {
|
35 | src = path.resolve(__dirname, '../theme_default');
|
36 | } else {
|
37 | src = path.resolve(process.cwd(), config.theme);
|
38 | }
|
39 | sh(`rm -rf ${dist}`);
|
40 | sh(`cp -R ${src} ${dist}`);
|
41 | config.theme = dist;
|
42 |
|
43 | babelFiles(dist, dist);
|
44 |
|
45 |
|
46 | const www = path.resolve(__dirname, '../www');
|
47 |
|
48 | sh(`rm -rf ${www}`);
|
49 | sh(`mv ${path.join(dist, './static')} ${www}`);
|
50 |
|
51 |
|
52 | const tmp = config.theme + '/tmp';
|
53 | if (fs.existsSync(tmp)) sh(`rm -rf ${tmp}`);
|
54 | fs.mkdirSync(tmp);
|
55 |
|
56 | parseNav(config);
|
57 | };
|
58 |
|
59 |
|
60 | exports.dev = async config => {
|
61 | before(config);
|
62 |
|
63 | await walkmd(config);
|
64 |
|
65 | const server = require('./server');
|
66 | const wpConfig = parseWPConfig(config, false);
|
67 | server(config, wpConfig);
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | }
|
85 |
|
86 | exports.build = config => {
|
87 | before(config);
|
88 |
|
89 | walkmd(config, () => {
|
90 | const site = path.resolve(process.cwd(), 'site');
|
91 | sh(`rm -rf ${site}`);
|
92 |
|
93 | const compiler = webpack(parseWPConfig(config, true));
|
94 | compiler.run((err, stats) => {
|
95 |
|
96 | });
|
97 | });
|
98 | }
|