-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinstall.js
More file actions
161 lines (132 loc) · 5.73 KB
/
Copy pathinstall.js
File metadata and controls
161 lines (132 loc) · 5.73 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var https = require('https');
var fs = require('fs');
var os = require('os');
var spawn = require('child_process').spawn;
var wget = require('wget');
var AdmZip = require("adm-zip");
var base_dir = __dirname;
var tmp_dir = base_dir + "/tmp";
var tools_dir = base_dir + "/tools";
var pngdefy_bin_dir = tools_dir + "/bin";
if (!fs.existsSync(tmp_dir)) {
fs.mkdirSync(tmp_dir, '755');
}
if (!fs.existsSync(tools_dir)) {
fs.mkdirSync(tools_dir, '755');
}
if (!fs.existsSync(pngdefy_bin_dir)) {
fs.mkdirSync(pngdefy_bin_dir, '755');
}
var url = "https://dl.dropboxusercontent.com/u/1983881/open_source_dependencies/pngdefry-master.zip";
var tempFile = tmp_dir + "/pngdefry-" + (new Date().getTime()) + ".zip";
function attemptDownload(attemptsLeft) {
var file = fs.createWriteStream(tempFile);
var request = https.get(url, function (response) {
response.pipe(file);
response.on('error', function (err) {
if (attemptsLeft === 0) {
throw err;
} else {
attemptDownload(attemptsLeft - 1);
return;
}
});
response.on('end', function () {
if (fs.existsSync(tempFile)) {
try {
var zip = new AdmZip(tempFile);
zip.extractAllTo(tmp_dir, true);
fs.unlink(tempFile);
var pngdefry_src_path = tmp_dir + "/pngdefry-master";
if (fs.existsSync(pngdefry_src_path)) {
//Everything seems ok.
//Rename pngdefry path
//Let's configure the tool
try {
process.chdir(pngdefry_src_path);
} catch (err) {
deleteFolderRecursive(tmp_dir);
process.exit();
}
var pngdefry_configure_file = "./configure";
var params = [pngdefry_configure_file, "--prefix", tools_dir];
var pngdefry_config = spawn('sh', params);
pngdefry_config.on('exit', function (code, signal) {
if (code != null) {
var pngdefry_make = spawn('make');
pngdefry_make.on('exit', function (code, signal) {
if (code != null) {
var pngdefry_make_install = spawn('make', ['install']);
pngdefry_make_install.on('exit', function (code, signal) {
if (code != null) {
// PNGdefy installed successfully
deleteFolderRecursive(tmp_dir);
} else {
attemptDownload(attemptsLeft - 1);
}
});
pngdefry_make_install.on("error", function (err) {
attemptDownload(attemptsLeft - 1);
});
}
pngdefry_make.on("error", function (err) {
attemptDownload(attemptsLeft - 1);
});
});
} else {
attemptDownload(attemptsLeft - 1);
return;
}
});
pngdefry_config.on("error", function (err) {
console.log(err);
});
// process.exit();
} else {
if (attemptsLeft === 0) {
throw new Error("Can find pngdefry directory, try running npm install again please.");
deleteFolderRecursive(tmp_dir);
process.exit();
} else {
attemptDownload(attemptsLeft - 1);
return;
}
}
} catch (e) {
fs.unlink(tempFile);
if (attemptsLeft === 0) {
throw new Error("Can extract pngdefry package, try running npm install again please.");
deleteFolderRecursive(tmp_dir);
process.exit();
} else {
attemptDownload(attemptsLeft - 1);
return;
}
}
} else {
if (attemptsLeft === 0) {
throw new Error("Can not find extracted directory , try running npm install again please.");
deleteFolderRecursive(tmp_dir);
process.exit();
} else {
attemptDownload(attemptsLeft - 1);
return;
}
}
});
});
}
var deleteFolderRecursive = function (path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function (file, index) {
var curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
attemptDownload(7);