UNPKG

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