1 | var _ = require('lodash');
|
2 | var async = require('async');
|
3 | var chalk = require('chalk');
|
4 | var glob = require('glob');
|
5 | var moment = require('moment');
|
6 | var mutil = require('miaow-util');
|
7 |
|
8 | var Cache = require('./cache');
|
9 | var Module = require('./module');
|
10 | var config = require('./config');
|
11 |
|
12 | moment.locale('zh-cn');
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | function compile(options, cb) {
|
21 | options = config(options);
|
22 |
|
23 | var cache = new Cache(options);
|
24 |
|
25 | var startTime = new Date().getTime();
|
26 | mutil.log('开始编译...');
|
27 |
|
28 | glob('**/*', {
|
29 | cwd: options.cwd,
|
30 | ignore: options.exclude || [],
|
31 | nodir: true
|
32 | }, function (err, srcPathList) {
|
33 | if (err) {
|
34 | return cb(err);
|
35 | }
|
36 |
|
37 | async.eachSeries(srcPathList, function (srcPath, cb) {
|
38 | var module = cache.get(srcPath);
|
39 |
|
40 | if (module) {
|
41 | return cb(null, module);
|
42 | } else {
|
43 | module = new Module(srcPath, options, cache);
|
44 | module.compile(cb);
|
45 | }
|
46 | }, function (err) {
|
47 | if (err) {
|
48 | return cb(err);
|
49 | }
|
50 |
|
51 | mutil.execPlugins(cache.modules, options.nextTasks || [], complete);
|
52 | });
|
53 | });
|
54 |
|
55 | function complete(err) {
|
56 | if (err) {
|
57 | return cb(err);
|
58 | }
|
59 |
|
60 | var endTime = new Date().getTime();
|
61 | mutil.log(
|
62 | '成功编译 ' +
|
63 | chalk.green.underline.bold(_.size(cache.modules)) +
|
64 | ' 个模块,耗时 ' +
|
65 | chalk.green.underline.bold(moment.duration(endTime - startTime).humanize())
|
66 | );
|
67 |
|
68 | cache.serialize();
|
69 | cb(null, cache);
|
70 | }
|
71 | }
|
72 |
|
73 | module.exports = compile;
|