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