atool-doc
Version:
Static demo site generator based on atool-build & dora.
132 lines (106 loc) • 4.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
exports.default = function (options) {
var source = options.source,
dest = options.dest,
cwd = options.cwd,
tpl = options.tpl,
config = options.config,
port = options.port,
asset = options.asset,
doraPlugins = options.doraPlugins;
var plugins = doraPlugins ? doraPlugins.split(',') : [];
if (plugins.indexOf('webpack') !== -1) {
console.warn('no need to add dora-plugin-webpack, ignore it');
}
var tplDefault = (0, _path.join)(root, _constant.tplSet.github);
var tplPath = void 0;
if (tpl) {
if (_constant.tplSet[tpl]) {
tplPath = (0, _path.join)(root, _constant.tplSet[tpl]);
} else {
tplPath = (0, _path.join)(cwd, tpl);
if (!(0, _fs.existsSync)(tplPath)) {
console.warn('There\'s no file in ' + tpl + ', creating one for you...');
(0, _fs.writeFileSync)(tplPath, (0, _fs.readFileSync)(tplDefault, 'utf-8'), 'utf-8');
}
}
} else {
tplPath = tplDefault;
}
var webpackConfig = void 0;
if (options.build) {
webpackConfig = (0, _getWebpackConfig2.default)(source, asset, dest, cwd, tplPath, config);
var compiler = (0, _webpack2.default)(webpackConfig);
if (options.watch) {
compiler.watch(200, function (err) {
if (err) {
console.error(err);
}
});
} else {
compiler.run(function (err) {
if (err) {
console.error(err);
}
});
}
} else {
(0, _dora2.default)({
port: port,
resolveDir: (0, _path.join)(root, 'node_modules'),
plugins: [{
'middleware.before': function middlewareBefore() {
webpackConfig = (0, _getWebpackConfig2.default)(source, asset, dest, cwd, tplPath, config);
},
'middleware': function middleware() {
var compiler = (0, _webpack2.default)(webpackConfig);
this.set('compiler', compiler);
compiler.plugin('done', function (stats) {
if (stats.hasErrors()) {
console.log(stats.toString({ colors: true }));
}
});
return require('koa-webpack-dev-middleware')(compiler, _extends({
publicPath: '/',
quiet: true
}, this.query));
},
'server.after': function serverAfter() {
_chokidar2.default.watch([source + '/**/*.md', source + '/**/*.js', source + '/**/*.jsx'], {
ignored: /node_modules/,
ignoreInitial: true
}).on('add', function (path) {
console.log();
console.log('atool-doc: add ' + path + ', restaring...');
process.send('restart');
}).on('unlink', function (path) {
console.log();
console.log('atool-doc: remove ' + path + ', restaring...');
process.send('restart');
});
}
}].concat(_toConsumableArray(plugins.filter(function (item) {
return item !== 'webpack' && item !== 'dora-plugin-webpack';
})))
});
}
};
var _path = require('path');
var _fs = require('fs');
var _webpack = require('atool-build/lib/webpack');
var _webpack2 = _interopRequireDefault(_webpack);
var _dora = require('dora');
var _dora2 = _interopRequireDefault(_dora);
var _getWebpackConfig = require('./getWebpackConfig');
var _getWebpackConfig2 = _interopRequireDefault(_getWebpackConfig);
var _constant = require('./constant');
var _chokidar = require('chokidar');
var _chokidar2 = _interopRequireDefault(_chokidar);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var root = (0, _path.join)(__dirname, '..');
module.exports = exports['default'];