1 | var _ = require('lodash');
|
2 | var fs = require('graceful-fs');
|
3 | var path = require('path');
|
4 | var console = require('miaow-util').console;
|
5 |
|
6 | var processCWD = process.cwd();
|
7 | var defaultOptions = {
|
8 |
|
9 | context: process.cwd(),
|
10 |
|
11 |
|
12 | exclude: [],
|
13 |
|
14 |
|
15 | output: 'build',
|
16 |
|
17 |
|
18 | cache: 'cache',
|
19 |
|
20 |
|
21 | hashLength: 10,
|
22 |
|
23 |
|
24 | hashConnector: '.',
|
25 |
|
26 |
|
27 | domain: '',
|
28 |
|
29 |
|
30 | debug: true,
|
31 |
|
32 |
|
33 | plugins: [],
|
34 |
|
35 |
|
36 | modules: [],
|
37 |
|
38 |
|
39 | resolve: {
|
40 | moduleDirectory: ['node_modules', 'bower_components'],
|
41 | extensions: ['.js'],
|
42 | extensionAlias: {
|
43 | '.css': ['.less']
|
44 | }
|
45 | }
|
46 | };
|
47 |
|
48 |
|
49 | function getConfigPath(configPath, context) {
|
50 |
|
51 | var configPathList = [];
|
52 |
|
53 | if (configPath) {
|
54 | configPathList.push(path.resolve(processCWD, configPath));
|
55 | }
|
56 |
|
57 | if (context) {
|
58 | configPathList.push(path.resolve(context, 'miaow.config.js'));
|
59 | }
|
60 |
|
61 | configPathList.push(path.resolve(processCWD, 'miaow.config.js'));
|
62 |
|
63 | return _.find(configPathList, fs.existsSync);
|
64 | }
|
65 |
|
66 | module.exports = function(argv) {
|
67 | var options = {};
|
68 |
|
69 | if (argv.silent) {
|
70 | console.log = console.warn = _.noop;
|
71 | }
|
72 |
|
73 | var configPath = getConfigPath(argv.configPath, argv.context);
|
74 | if (configPath) {
|
75 | console.log('使用配置:' + configPath);
|
76 |
|
77 | options = require(configPath);
|
78 |
|
79 | if (_.isArray(options)) {
|
80 | if (argv.environment) {
|
81 | options = _.find(options, {environment: argv.environment});
|
82 | if (!options) {
|
83 | throw new Error('查不到运行环境为 ' + argv.environment + ' 的配置信息。');
|
84 | }
|
85 | } else {
|
86 | options = options[0];
|
87 | }
|
88 | }
|
89 | }
|
90 |
|
91 | options = _.assign({}, defaultOptions, options, argv);
|
92 |
|
93 |
|
94 | options.modules = (options.modules.concat({test: '**/*'})).map(function(module) {
|
95 | module.tasks = (module.tasks || []).map(function(task) {
|
96 | if (_.isFunction(task)) {
|
97 | task = {
|
98 | task: task,
|
99 | options: {}
|
100 | };
|
101 | }
|
102 |
|
103 | return task;
|
104 | });
|
105 |
|
106 | return _.assign(
|
107 | {},
|
108 | _.pick(options, ['hashLength', 'hashConnector', 'domain', 'debug']),
|
109 | module,
|
110 | _.pick(options, ['context', 'output'])
|
111 | );
|
112 | });
|
113 |
|
114 | return options;
|
115 | };
|