-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
74 lines (65 loc) · 2.16 KB
/
main.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
var refFiles = [], //读取的js文件
resHost = '', //服务器主机地址
resPath = ''; //项目路径
/*
example:
var refFiles = ['game-1.1.4.min.js', 'PxLoader.min.js', 'PxLoaderImage.min.js', 'tge-0.3.5.min.js', 'viewporter.min.js'],
resHost = 'static.tresensa.com', //服务器主机地址
resPath = '/vector-runner-remix/assets-1.1.4/'; //项目路径
*/
var http = require('http'),
fs = require('fs'),
mkdirp = require('mkdirp'),
sourceContents = [],
fileContent,
imgPaths = [],
imgPattern = /('|")(\w|\/)+\.(?:png|jpg|bmp|gif)('|")/gi,
matches,
imgPath;
//生成文件内容数组
for (var i =0; i < refFiles.length; i ++) {
fileContent = fs.readFileSync(refFiles[i]) ;
sourceContents.push(fileContent);
}
//生成图片路径数组
for (var i =0; i < sourceContents.length; i++) {
imgPath = '' + sourceContents[i];
matches = imgPath.match(imgPattern);
imgPaths = imgPaths.concat(matches);
};
//图片路径微调,下载图片
for(var j = 0; j < imgPaths.length; j++) {
imgPath = imgPaths[j];
if (imgPath) {
imgPath = imgPath.substr(1, imgPath.length - 2);
imgPath.replace('./','');
getResource(imgPath);
};
}
function getResource(path) {
var httpOptions = {
host: resHost,
port: 80
};
httpOptions.path = resPath + path;
//获取图片的路径
var index = path.lastIndexOf('/');
var folderPath = path.substr(0,index);
var fileName = path.substr(index + 1, path.length -1);
//检查图片文件夹是否存在,不存在则创建
mkdirp(folderPath, function(err) {
http.get(httpOptions, function(res){
var imagedata = '';
res.setEncoding('binary');
res.on('data', function(chunk){
imagedata += chunk
});
res.on('end', function(){
fs.writeFile(path, imagedata, 'binary', function(err){
if (err) throw err
console.log('File saved:' + path);
})
});
});
});
};