forked from ioBroker/ioBroker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
135 lines (112 loc) · 3.91 KB
/
gulpfile.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict';
const gulp = require('gulp');
const fs = require('fs');
const Stream = require('stream');
const Client = require('ssh2').Client;
const dist = __dirname + '/dist/';
const SFTP_HOST = process.env.SFTP_HOST;
const SFTP_PORT = process.env.SFTP_PORT;
const SFTP_USER = process.env.SFTP_USER;
const SFTP_PASS = process.env.SFTP_PASS;
const DEBUG = process.env.DEBUG === 'true' || process.env.DEBUG === true;
const FAST_TEST = process.env.FAST_TEST === 'true' || process.env.FAST_TEST === true;
const SFTP_CONFIG = {
host: SFTP_HOST,
port: parseInt(SFTP_PORT, 10),
username: SFTP_USER,
password: SFTP_PASS,
};
function writeSftp(sftp, fileName, data, cb) {
const readStream = new Stream.PassThrough();
readStream.end(Buffer.from(data));
const writeStream = sftp.createWriteStream(fileName);
writeStream.on('close', () => {
DEBUG && console.log(`${new Date().toISOString()} ${fileName} - file transferred successfully`);
readStream.end();
if (cb) {
cb();
cb = null;
}
});
writeStream.on('end', () => {
DEBUG && console.log('sftp connection closed');
readStream.close();
if (cb) {
cb();
cb = null;
}
});
// initiate transfer of file
readStream.pipe(writeStream);
}
function uploadOneFile(fileName, data) {
return new Promise((resolve, reject) => {
const conn = new Client();
conn.on('ready', () =>
conn.sftp((err, sftp) => {
if (err) {
return reject(err);
}
if (FAST_TEST) {
console.log('Simulate upload of ' + fileName);
return resolve();
}
// file must be deleted, because of the new file smaller, the rest of old file will stay.
checkAndDeleteIfExist(sftp, fileName, () =>
writeSftp(sftp, fileName, data, () => {
sftp.end();
conn.end();
resolve();
}));
}))
.connect(SFTP_CONFIG);
});
}
function checkAndDeleteIfExist(sftp, fileName, cb) {
sftp.exists(fileName, doExist => {
if (doExist) {
sftp.unlink(fileName, cb);
} else {
cb();
}
});
}
function replaceLib(text, lib) {
const lines = text.split('\n');
const newLines = [];
let ignore = false;
for (let i = 0; i < lines.length; i++) {
if (lines[i].includes('# get and load the LIB => START')) {
ignore = true;
newLines.push(lib);
} else if (lines[i].includes('# get and load the LIB => END')) {
ignore = false;
} else if (!ignore) {
newLines.push(lines[i]);
}
}
return newLines.join('\n');
}
gulp.task('deploy', () => {
const install = fs.readFileSync(dist + 'install.sh');
const fix = fs.readFileSync(dist + 'fix.sh');
return uploadOneFile('/install.sh', install)
.then(() => uploadOneFile('/fix.sh', fix));
});
gulp.task('create', () => {
return new Promise(resolve => {
if (!fs.existsSync(dist)) {
fs.mkdirSync(dist);
}
const install = fs.readFileSync(__dirname + '/installer.sh').toString('utf8');
const fix = fs.readFileSync(__dirname + '/fix_installation.sh').toString('utf8');
const lib = fs.readFileSync(__dirname + '/installer_library.sh').toString('utf8');
// replace
// LIB_NAME="installer_library.sh"
// LIB_URL="https://raw.githubusercontent.com/ioBroker/ioBroker/stable-installer/$LIB_NAME"
fs.writeFileSync(dist + 'install.sh', replaceLib(install, lib));
fs.writeFileSync(dist + 'fix.sh', replaceLib(fix, lib));
resolve();
});
});
gulp.task('default', gulp.series('create', 'deploy'));