-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (62 loc) · 1.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict';
var readFile = require('fs').readFileSync;
var exists = require('fs').existsSync;
var join = require('path').join;
var spmrc = require('spmrc');
var extend = require('extend');
var mixarg = require('mixarg');
var defaults = {
// registry url of yuan server
registry: spmrc.get('registry'),
// global registry, others are private
global_registry: 'http://spmjs.io',
// an HTTP proxy, pass to request
proxy: spmrc.get('proxy'),
// the authKey that copied from spmjs accout page
auth: spmrc.get('auth'),
// the temp directory
temp: spmrc.get('user.temp'),
// cache directory
cache: join(spmrc.get('user.home'), '.spm', 'cache')
};
module.exports = function(cwd, opts) {
cwd = cwd || process.cwd();
opts = opts || {};
var pkgArgs = getPkgArgs(cwd, opts.pkg);
return extend.apply(null, [true, {}, defaults, pkgArgs].concat(opts.merge));
};
function getPkgArgs(cwd, pkg) {
if (!pkg) {
var pkgPath = join(cwd, 'package.json');
if (!exists(pkgPath)) return {};
pkg = JSON.parse(readFile(pkgPath));
}
if (!pkg.spm) return {};
var buildArgs = mixarg(pkg.spm.buildArgs);
buildArgs.global = getGlobal(buildArgs.global);
var ret = {
registry: pkg.spm.registry,
build: extend(buildArgs, pkg.spm.build),
server: pkg.spm.server || {},
scripts: pkg.spm.scripts || {}
};
// 兼容之前的 build 配置
[
'extractCSS', 'common', 'babel', 'uglify', 'hash', 'dest', 'less',
'autoprefixer', 'umd', 'pathmap'
].forEach(function(key) {
if (pkg.spm.hasOwnProperty(key)) {
ret.build[key] = pkg.spm[key];
}
});
return ret;
}
function getGlobal(str) {
if (typeof str !== 'string') return {};
var ret = {};
str.split(/\s*,\s*/).forEach(function(item) {
var m = item.split(':');
ret[m[0]] = m[1];
});
return ret;
}