UNPKG

2.68 kBJavaScriptView Raw
1/**
2 * @file start commander, entry file
3 * @author zhangpeng53
4 */
5require('babel-polyfill');
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 babelFiles = require('./babelFiles');
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
27 function before(config) {
28
29 let src;
30 let dist = path.resolve(__dirname, '../theme');
31
32 // 拷贝主题
33 // 如果是默认主题则使用theme_default
34 if (!config.theme || config.theme == 'default') {
35 src = path.resolve(__dirname, '../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 babelFiles(dist, dist);
44
45 //拷贝静态资源
46 const www = path.resolve(__dirname, '../www');
47 // console.log(path.join(dist, './static'));
48 sh(`rm -rf ${www}`);
49 sh(`mv ${path.join(dist, './static')} ${www}`);
50
51 //创建临时文件夹
52 const tmp = config.theme + '/tmp';
53 if (fs.existsSync(tmp)) sh(`rm -rf ${tmp}`);
54 fs.mkdirSync(tmp);
55
56 parseNav(config);
57};
58
59
60exports.dev = async config => {
61 before(config);
62
63 await walkmd(config);
64
65 const server = require('./server');
66 const wpConfig = parseWPConfig(config, false);
67 server(config, wpConfig);
68
69 // walkmd(config, () => {
70
71 // // const server = new webdevServer(compiler, wpconfig.devServer);
72 // // server.listen(wpconfig.devServer.port, "localhost", ()=> {
73 // // console.log('====start-dev-server====');
74 // // });
75
76 // // TODO
77 // // const db = require("./tmp/__md__");
78
79 // const server = require('./server');
80 // const wpConfig = parseWPConfig(config, false);
81 // server(config, wpConfig);
82 // // sh(`${node_modules}/.bin/webpack-dev-server --config ${wpconfigPath}`);
83 // });
84}
85
86exports.build = config => {
87 before(config);
88
89 walkmd(config, () => {
90 const site = path.resolve(process.cwd(), 'site');
91 sh(`rm -rf ${site}`);
92
93 const compiler = webpack(parseWPConfig(config, true));
94 compiler.run((err, stats) => {
95 // console.log(stats);
96 });
97 });
98}