UNPKG

1.95 kBJavaScriptView Raw
1/**
2 * @file start commander, entry file
3 * @author zhangpeng53
4 */
5
6const wpconfig = require('../webpack.config');
7const webpack = require('webpack');
8const walkmd = require('./walkmd');
9const sh = require('child_process').execSync;
10const path = require('path');
11const fs = require('fs');
12
13const node_modules = path.resolve(__dirname, '../node_modules');
14const wpconfigPath = path.resolve(__dirname, '../webpack.config.js');
15
16
17function parseWPConfig(config, isProduction) {
18 let res = wpconfig(isProduction);
19 if (config.setWebpackConfig) {
20 res = config.setWebpackConfig(res);
21 }
22 return res;
23}
24
25function before(config) {
26
27 let src = path.resolve(process.cwd(), config.theme);
28 let dist = path.resolve(__dirname, '../theme');
29
30 // 如果是默认主题则使用theme_default
31 if (config.theme == 'default' || !config.theme) {
32 src = path.resolve(__dirname, '../theme_default');
33 }
34 sh(`rm -rf ${dist}`);
35 sh(`cp -R ${src} ${dist}`);
36 config.theme = dist;
37};
38
39exports.build = config => {
40 before(config);
41
42 walkmd(config, () => {
43 const site = path.resolve(process.cwd(), 'site');
44 sh(`rm -rf ${site}`);
45
46 const compiler = webpack(parseWPConfig(config, true));
47 compiler.run((err, stats) => {
48 // console.log(stats);
49 });
50 });
51}
52
53exports.dev = config => {
54 before(config);
55
56 walkmd(config, () => {
57
58 // const server = new webdevServer(compiler, wpconfig.devServer);
59 // server.listen(wpconfig.devServer.port, "localhost", ()=> {
60 // console.log('====start-dev-server====');
61 // });
62
63 // TODO
64 // const db = require("./tmp/__md__");
65
66 return;
67 const server = require('./server');
68 const wpConfig = parseWPConfig(config, false);
69 server(config, wpConfig);
70 // sh(`${node_modules}/.bin/webpack-dev-server --config ${wpconfigPath}`);
71 });
72}
\No newline at end of file