-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
114 lines (88 loc) · 3.02 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
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
/**
* fis.baidu.com
*/
var pth = require('path');
function getServerInfo() {
var conf = pth.join(fis.project.getTempPath('server'), 'conf.json');
if (fis.util.isFile(conf)) {
return fis.util.readJSON(conf);
}
return {};
}
var serverRoot = (function () {
var key = 'FIS_SERVER_DOCUMENT_ROOT';
var serverInfo = getServerInfo();
if (process.env && process.env[key]) {
var path = process.env[key];
if (fis.util.exists(path) && !fis.util.isDir(path)) {
fis.log.error('invalid environment variable [' + key + '] of document root [' + path + ']');
}
return path;
} else if (serverInfo['root'] && fis.util.is(serverInfo['root'], 'String')) {
return serverInfo['root'];
} else {
return fis.project.getTempPath('www');
}
})();
var cwd = fis.processCWD || process.cwd();
function normalizePath(to, root) {
if (to[0] === '.') {
to = fis.util(cwd + '/' + to);
} else if (/^output\b/.test(to)) {
to = fis.util(root + '/' + to);
} else if (to === 'preview') {
to = serverRoot;
} else {
to = fis.util(to);
}
return to;
}
//处理相对路径,返回绝对路径
function dealPath(partPath) {
if (partPath[0] === '/') {
partPath = '.' + (partPath);
} else {
partPath = './' + (partPath);
}
partPath = normalizePath(partPath, fis.project.getProjectPath());
return partPath + '/';
}
function deliver(output, release, content, file, options) {
if (!release) {
fis.log.error('unable to get release path of file[' + file.realpath + ']: Maybe this file is neither in current project or releasable');
}
if (fis.util.exists(output) && !fis.util.isDir(output)) {
fis.log.error('unable to deliver file[' + file.realpath + '] to dir[' + output + ']: invalid output dir.');
}
var target;
target = fis.util(output, release);
fis.util.write(target, content);
//other 改写
if (options.other) {
for (var i = 0, len = options.other.length; i < len; i++) {
if ((options.other[i].filter).test(release) && file.fullname.indexOf(options.other[i].from) === 0) {
var realPath = options.other[i].to + file.fullname.substring(options.other[i].from.length);
fis.util.write(realPath, content);
}
}
}
fis.log.debug(
'release ' +
file.subpath.replace(/^\//, '') +
' >> '.yellow.bold +
target
);
}
module.exports = function (options, modified, total, next) {
var to = normalizePath(options.to || options.dest || 'preview', fis.project.getProjectPath());
if (options.other) {
for (var i = 0, len = options.other.length; i < len; i++) {
options.other[i].from = dealPath(options.other[i].from);
options.other[i].to = dealPath(options.other[i].to);
}
}
modified.forEach(function (file) {
deliver(to, file.getHashRelease(), file.getContent(), file, options);
});
next();
};