2020-10-27 01:05:37 +08:00
|
|
|
|
// node module
|
2021-04-26 16:26:49 +08:00
|
|
|
|
const fs = require('fs');
|
|
|
|
|
const https = require('https');
|
|
|
|
|
const resolve = require('path').resolve;
|
|
|
|
|
const join = require('path').resolve;
|
|
|
|
|
const extract = require('extract-zip');
|
2020-10-27 01:05:37 +08:00
|
|
|
|
|
|
|
|
|
// 函数参数
|
2021-04-26 16:26:49 +08:00
|
|
|
|
const dest = resolve(__dirname, '../');
|
|
|
|
|
const fileName = 'NeteaseCloudMusicApi-master.zip';
|
2020-10-27 01:05:37 +08:00
|
|
|
|
const options = {
|
2021-04-26 16:26:49 +08:00
|
|
|
|
hostname: 'github.91chifun.workers.dev',
|
2020-10-27 01:05:37 +08:00
|
|
|
|
path: `//https://github.com/Binaryify/NeteaseCloudMusicApi/archive/master.zip`,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 完整的流程控制
|
|
|
|
|
/**
|
|
|
|
|
* 1. 检查本地文件是否已有
|
|
|
|
|
* 2. 下载默认/指定版本的 zip 压缩包,等待下载
|
|
|
|
|
* 3. 解压缩
|
|
|
|
|
* 4. 进入目录安装依赖 npm install
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
function fix2(number) {
|
|
|
|
|
return number.toFixed(2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function download(options, fileName, callback) {
|
|
|
|
|
return await new Promise((resolve, reject) => {
|
2021-04-26 16:26:49 +08:00
|
|
|
|
const destPath = join(__dirname, '../' + fileName);
|
2020-10-27 01:05:37 +08:00
|
|
|
|
// Check if exist
|
|
|
|
|
if (fs.existsSync(destPath)) return resolve(destPath);
|
|
|
|
|
|
|
|
|
|
const file = fs.createWriteStream(destPath);
|
2021-04-26 16:26:49 +08:00
|
|
|
|
const request = https.get(options, res => {
|
|
|
|
|
let len = res.headers && parseInt(res.headers['content-length'], 10);
|
2020-10-27 01:05:37 +08:00
|
|
|
|
let cur = 0;
|
|
|
|
|
// 1048576 - bytes in 1Megabyte
|
|
|
|
|
const MEGA = 1048576;
|
|
|
|
|
let total = 0;
|
|
|
|
|
if (len) {
|
|
|
|
|
total = len / MEGA;
|
|
|
|
|
}
|
|
|
|
|
if (!len) {
|
|
|
|
|
console.log(
|
2021-04-26 16:26:49 +08:00
|
|
|
|
'Downloading, but can not get content-length, please be patient.'
|
2020-10-27 01:05:37 +08:00
|
|
|
|
);
|
|
|
|
|
}
|
2021-04-26 16:26:49 +08:00
|
|
|
|
res.on('data', chunk => {
|
2020-10-27 01:05:37 +08:00
|
|
|
|
if (len) {
|
|
|
|
|
cur += chunk.length;
|
2020-10-27 01:42:49 +08:00
|
|
|
|
console.log(
|
|
|
|
|
`Downloading ${fix2((100.0 * cur) / len)}% ${fix2(
|
|
|
|
|
cur / MEGA
|
|
|
|
|
)}/${fix2(total)}mb`
|
|
|
|
|
);
|
2020-10-27 01:05:37 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2021-04-26 16:26:49 +08:00
|
|
|
|
res.on('end', () => {
|
|
|
|
|
callback('Downloading complete!');
|
2020-10-27 01:05:37 +08:00
|
|
|
|
});
|
|
|
|
|
res.pipe(file);
|
2021-04-26 16:26:49 +08:00
|
|
|
|
file.on('finish', () => {
|
2020-10-27 01:05:37 +08:00
|
|
|
|
file.close(() => {
|
2021-04-26 16:26:49 +08:00
|
|
|
|
callback('File wrote complete!');
|
2020-10-27 01:05:37 +08:00
|
|
|
|
resolve(destPath);
|
|
|
|
|
});
|
|
|
|
|
});
|
2021-04-26 16:26:49 +08:00
|
|
|
|
file.on('error', err => {
|
2020-10-27 01:05:37 +08:00
|
|
|
|
fs.unlink(destPath);
|
|
|
|
|
reject(err);
|
|
|
|
|
});
|
2021-04-26 16:26:49 +08:00
|
|
|
|
request.on('error', err => {
|
|
|
|
|
console.log('Error: ' + err.message);
|
2020-10-27 01:05:37 +08:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function unzip(source, target) {
|
|
|
|
|
try {
|
|
|
|
|
await extract(source, {
|
|
|
|
|
dir: target,
|
|
|
|
|
});
|
2021-04-26 16:26:49 +08:00
|
|
|
|
console.log('Extraction complete');
|
2020-10-27 01:05:37 +08:00
|
|
|
|
return true;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// handle any errors
|
2021-04-26 16:26:49 +08:00
|
|
|
|
if (err.message === 'end of central directory record signature not found') {
|
|
|
|
|
console.log('Not a full_downloaded zip file, removed!');
|
2020-10-27 01:05:37 +08:00
|
|
|
|
fs.unlinkSync(source);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Download process
|
2021-04-26 16:26:49 +08:00
|
|
|
|
download(options, fileName, text => {
|
2020-10-27 01:05:37 +08:00
|
|
|
|
console.log(text);
|
2021-04-26 16:26:49 +08:00
|
|
|
|
}).then(path => {
|
2020-10-27 01:42:49 +08:00
|
|
|
|
console.log(path);
|
2020-10-27 01:05:37 +08:00
|
|
|
|
// Unzip process
|
|
|
|
|
return unzip(path, dest);
|
|
|
|
|
});
|