forked from cometchat/cometchat-uikit-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postinstall.js
70 lines (50 loc) · 1.76 KB
/
postinstall.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
const fs = require('fs-extra');
const request = require('request');
const extract = require('extract-zip')
const rimraf = require("rimraf");
const fileName = "cometchat-pro-react-ui-kit";
const filePath = __dirname + "/src/" + fileName;
const zipFileName = "cometchat-pro-react-ui-kit-master";
const zipName = zipFileName + ".zip";
const source = __dirname + "/" + zipFileName;
const destination = filePath;
const downloadUrl = "https://github.com/cometchat-pro/cometchat-pro-react-ui-kit/archive/master.zip";
const download = (uri, filename, callback) => {
request.head(uri, (err, res, body) => {
console.log('content-type:', res.headers['content-type']);
console.log('content-length:', res.headers['content-length']);
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};
const checkIfFolderExists = (path) => {
if (fs.existsSync(path)) {
console.log("File exists", path);
return true;
}
return false;
}
const deleteFileFolder = (target) => {
console.log("deleting file", target);
rimraf.sync(target);
}
if(checkIfFolderExists(filePath)) {
deleteFileFolder(filePath);
}
download(downloadUrl, zipName, (props) => {
try {
extract(zipName, {dir: __dirname}).then(response => {
fs.move(source, destination, error => {
if(error) {
return console.error('move file error!', error);
}
console.log('move file success!');
});
const zipFile = __dirname + "/" + zipName;
if(checkIfFolderExists(zipFile)) {
deleteFileFolder(zipFile);
}
});
} catch (error) {
console.log("File extraction error", zipName);
}
});