1 Star 0 Fork 0

codedigger_123/ioBroker

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
gulpfile.js 4.61 KB
一键复制 编辑 原始数据 按行查看 历史
'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`);
const diag = fs.readFileSync(`${dist}diag.sh`);
const nodeUpdate = fs.readFileSync(`${dist}node-update.sh`);
return uploadOneFile('/install.sh', install)
.then(() => uploadOneFile('/fix.sh', fix))
.then(() => uploadOneFile('/diag.sh', diag))
.then(() => uploadOneFile('/node-update.sh', nodeUpdate));
});
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');
const diag = fs.readFileSync(`${__dirname}/diag.sh`).toString('utf8');
const nodeUpdate = fs.readFileSync(`${__dirname}/node-update.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));
fs.writeFileSync(`${dist}diag.sh`, diag);
fs.writeFileSync(`${dist}node-update.sh`, nodeUpdate);
resolve();
});
});
gulp.task('fix', () => {
const pack = require('./package.json');
pack.name = '@iobroker/fix';
fs.writeFileSync(`${__dirname}/package.json`, JSON.stringify(pack, null, 2));
});
gulp.task('default', gulp.series('create', 'deploy'));
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/codedigger_123/ioBroker.git
git@gitee.com:codedigger_123/ioBroker.git
codedigger_123
ioBroker
ioBroker
master

搜索帮助