UNPKG

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