UNPKG

2.02 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 copyFile(src, dist) {
18 fs.writeFileSync(dist, fs.readFileSync(src));
19}
20
21const parseWPConfig = (config, isProduction) => {
22 let res = wpconfig(isProduction);
23 if (config.setWebpackConfig) {
24 res = config.setWebpackConfig(res);
25 }
26 return res;
27};
28
29const before = (config) => {
30
31 let src = path.resolve(process.cwd(), config.theme);
32 let dist = path.resolve(__dirname, '../theme');
33
34 // 如果是默认主题则使用theme_default
35 if (config.theme == 'default' || !config.theme) {
36 src = path.resolve(__dirname, '../theme_default');
37 }
38 sh(`cp -R ${src} ${dist}`);
39 config.theme = dist;
40};
41
42exports.build = config => {
43 before(config);
44
45 walkmd(config, () => {
46 const site = path.resolve(process.cwd(), 'site');
47 sh(`rm -rf ${site}`);
48
49 const compiler = webpack(parseWPConfig(config, true));
50 compiler.run((err, stats) => {
51 // console.log(stats);
52 });
53 });
54}
55
56exports.dev = config => {
57 before(config);
58
59 walkmd(config, () => {
60
61 // const server = new webdevServer(compiler, wpconfig.devServer);
62 // server.listen(wpconfig.devServer.port, "localhost", ()=> {
63 // console.log('====start-dev-server====');
64 // });
65
66 // TODO
67 // const db = require("./tmp/__md__");
68
69 return;
70 const server = require('./server');
71 const wpConfig = parseWPConfig(config, false);
72 server(config, wpConfig);
73 // sh(`${node_modules}/.bin/webpack-dev-server --config ${wpconfigPath}`);
74 });
75}
\No newline at end of file