UNPKG

2.62 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 const www = path.resolve(__dirname, '../www');
45 console.log(path.join(dist, './static'));
46 sh(`rm -rf ${www}`);
47 sh(`mv ${path.join(dist, './static')} ${www}`);
48
49 //创建临时文件夹
50 const tmp = config.theme + '/tmp';
51 if (fs.existsSync(tmp)) sh(`rm -rf ${tmp}`);
52 fs.mkdirSync(tmp);
53
54 parseNav(config);
55};
56
57exports.build = config => {
58 before(config);
59
60 walkmd(config, () => {
61 const site = path.resolve(process.cwd(), 'site');
62 sh(`rm -rf ${site}`);
63
64 const compiler = webpack(parseWPConfig(config, true));
65 compiler.run((err, stats) => {
66 // console.log(stats);
67 });
68 });
69}
70
71exports.dev = async config => {
72 // before(config);
73
74 await walkmd(config);
75
76 const server = require('./server');
77 const wpConfig = parseWPConfig(config, false);
78 server(config, wpConfig);
79
80 // walkmd(config, () => {
81
82 // // const server = new webdevServer(compiler, wpconfig.devServer);
83 // // server.listen(wpconfig.devServer.port, "localhost", ()=> {
84 // // console.log('====start-dev-server====');
85 // // });
86
87 // // TODO
88 // // const db = require("./tmp/__md__");
89
90 // const server = require('./server');
91 // const wpConfig = parseWPConfig(config, false);
92 // server(config, wpConfig);
93 // // sh(`${node_modules}/.bin/webpack-dev-server --config ${wpconfigPath}`);
94 // });
95}
\No newline at end of file