UNPKG

2.73 kBJavaScriptView Raw
1/**
2 * @file start commander, entry file
3 * @author zhangpeng53
4 */
5require("babel-core/register");
6require('babel-polyfill');
7
8const wpconfig = require('../webpack.config');
9const webpack = require('webpack');
10const walkmd = require('./walkmd');
11const sh = require('child_process').execSync;
12const path = require('path');
13const fs = require('fs');
14const parseNav = require('./parseNav');
15const babelFiles = require('./babelFiles');
16
17const node_modules = path.resolve(__dirname, '../node_modules');
18const wpconfigPath = path.resolve(__dirname, '../webpack.config.js');
19
20
21function parseWPConfig(config, isProduction) {
22 let res = wpconfig(isProduction);
23 if (config.setWebpackConfig) {
24 res = config.setWebpackConfig(res);
25 }
26 return res;
27}
28
29 async function before(config) {
30
31 let src;
32 let dist = path.resolve(__dirname, '../theme');
33
34 // 拷贝主题
35 // 如果是默认主题则使用theme_default
36 if (!config.theme || config.theme == 'default') {
37 src = path.resolve(__dirname, '../theme_default');
38 } else {
39 src = path.resolve(process.cwd(), config.theme);
40 }
41 sh(`rm -rf ${dist}`);
42 sh(`cp -R ${src} ${dist}`);
43 config.theme = dist;
44
45
46 await babelFiles(dist, dist);
47
48 //拷贝静态资源
49 const www = path.resolve(__dirname, '../www');
50 // console.log(path.join(dist, './static'));
51 sh(`rm -rf ${www}`);
52 sh(`mv ${path.join(dist, './static')} ${www}`);
53
54 //创建临时文件夹
55 const tmp = config.theme + '/tmp';
56 if (fs.existsSync(tmp)) sh(`rm -rf ${tmp}`);
57 fs.mkdirSync(tmp);
58
59 parseNav(config);
60};
61
62
63exports.dev = async config => {
64 before(config);
65
66 await walkmd(config);
67
68 const server = require('./server');
69 const wpConfig = parseWPConfig(config, false);
70 server(config, wpConfig);
71
72 // walkmd(config, () => {
73
74 // // const server = new webdevServer(compiler, wpconfig.devServer);
75 // // server.listen(wpconfig.devServer.port, "localhost", ()=> {
76 // // console.log('====start-dev-server====');
77 // // });
78
79 // // TODO
80 // // const db = require("./tmp/__md__");
81
82 // const server = require('./server');
83 // const wpConfig = parseWPConfig(config, false);
84 // server(config, wpConfig);
85 // // sh(`${node_modules}/.bin/webpack-dev-server --config ${wpconfigPath}`);
86 // });
87}
88
89exports.build = config => {
90 before(config);
91
92 walkmd(config, () => {
93 const site = path.resolve(process.cwd(), 'site');
94 sh(`rm -rf ${site}`);
95
96 const compiler = webpack(parseWPConfig(config, true));
97 compiler.run((err, stats) => {
98 // console.log(stats);
99 });
100 });
101}