-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.js
120 lines (110 loc) · 3.49 KB
/
library.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"use strict";
var Q = require('q');
var path = require('path');
var program = require('commander');
var _ = require('lodash');
module.exports = {
/**
* @param target
* @param includes
*/
addIncludes: function(target, includes) {
target.include = (target.include || (target.include = [])).concat(includes);
},
/**
* @param target
* @param options
*/
addOptions: function(target, options) {
_.merge(target.options || (target.options = {}), options, function(a, b) {
if (Array.isArray(a) || Array.isArray(b)) {
return (a || []).concat(b || []);
}
});
},
/**
* @param target
* @param basePath
* @param sources
*/
addSources: function(target, basePath, sources) {
if (!sources) {
sources = basePath;
basePath = null;
}
function getPath(path_) {
return path_[0] === '?' ? path_.substr(1) :
!basePath || path_[0] === '/' || path_.indexOf('!!') === 0 ?
path_ : '!!' + path.join(basePath, path_);
}
target.sources = (target.sources || (target.sources = [])).concat(sources.map(function(source) {
source = _.clone(source);
if (source.file) {
if (!Array.isArray(source.file)) {
source.file = [source.file];
}
source.file = source.file.map(getPath);
}
if (source.path) {
if (typeof source.path === 'string') {
source.path = getPath(source.path);
}
else {
source.path = _(source.path)
.map(function(value, key) { return [getPath(key), value]; })
.zipObject().value();
}
}
return source;
}));
},
/**
* @param target
* @param file
* @returns {string}
*/
finalizePath: function(target, file) {
if (!program.abspath && (target.site === '' || target.site)) {
file = this.normalizePath(null, path.relative(path.resolve(program.path, target.site), file));
if (target.siteAbsolute) {
file = '/' + file;
}
}
return file;
},
/**
* @param dest
* @param source
*/
merge: function(dest, source) {
if (!source) {
return dest;
}
return _.merge(dest, source, function(a, b) {
if (Array.isArray(a) || Array.isArray(b)) {
return (a || []).concat(b || []);
}
});
},
/**
* @param target
* @param path_
* @param [addSlash=false]
* @returns {string}
*/
normalizePath: function(target, path_, addSlash) {
if (path_.indexOf('!!') === 0) {
path_ = '/' + path.relative(target.root, path_.substr(2));
}
var root = target ? (path_[0] === '/' || path_[0] === '\\' ? target.root : target.js) : null;
if ((root === '' || root) && (path_[0] === '/' || path_[0] === '\\')) {
path_ = path_.substr(1);
}
var normalized = ((root === '' || root) ? path.resolve(root, path_) : path.normalize(path_))
.replace(/[\\\/]+/g, '/');
if (addSlash && normalized[normalized.length - 1] !== '/') {
normalized += '/';
}
return normalized;
}
};