UNPKG

2.43 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');
12const parseNav = require('./parseNav');
13const babel = require('babel-core');
14
15const node_modules = path.resolve(__dirname, '../node_modules');
16const wpconfigPath = path.resolve(__dirname, '../webpack.config.js');
17
18
19function parseWPConfig(config, isProduction) {
20 let res = wpconfig(isProduction);
21 if (config.setWebpackConfig) {
22 res = config.setWebpackConfig(res);
23 }
24 return res;
25}
26
27function before(config) {
28
29 let src;
30 let dist = path.resolve(__dirname, '../theme');
31
32 // 拷贝主题
33 // 如果是默认主题则使用theme_default
34 if (config.theme == 'default' || !config.theme) {
35 src = path.resolve(__dirname, '../lib/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
44 //拷贝静态资源
45 const www = path.resolve(__dirname, '../www');
46 console.log(path.join(dist, './static'));
47 sh(`rm -rf ${www}`);
48 sh(`mv ${path.join(dist, './static')} ${www}`);
49
50 //创建临时文件夹
51 const tmp = config.theme + '/tmp';
52 if (fs.existsSync(tmp)) sh(`rm -rf ${tmp}`);
53 fs.mkdirSync(tmp);
54
55 parseNav(config);
56};
57
58exports.build = config => {
59 before(config);
60
61 walkmd(config, () => {
62 const site = path.resolve(process.cwd(), 'site');
63 sh(`rm -rf ${site}`);
64
65 const compiler = webpack(parseWPConfig(config, true));
66 compiler.run((err, stats) => {
67 // console.log(stats);
68 });
69 });
70}
71
72exports.dev = config => {
73 before(config);
74
75 walkmd(config, () => {
76
77 // const server = new webdevServer(compiler, wpconfig.devServer);
78 // server.listen(wpconfig.devServer.port, "localhost", ()=> {
79 // console.log('====start-dev-server====');
80 // });
81
82 // TODO
83 // const db = require("./tmp/__md__");
84
85 const server = require('./server');
86 const wpConfig = parseWPConfig(config, false);
87 server(config, wpConfig);
88 // sh(`${node_modules}/.bin/webpack-dev-server --config ${wpconfigPath}`);
89 });
90}
\No newline at end of file