diff --git a/package.json b/package.json index 5d7a57c..5d97fd2 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,11 @@ "electron:serve": "vue-cli-service electron:serve", "postinstall": "electron-builder install-app-deps", "postuninstall": "electron-builder install-app-deps", - "prettier": "npx prettier --write ./src" + "prettier": "npx prettier --write ./src", + "napi_run": "cd ./NeteaseCloudMusicApi-master && npm run start", + "napi_pull": "node script/setup.js v3.45.1", + "napi_install": "cd ./NeteaseCloudMusicApi-master && npm install", + "setup": "npm run napi_pull && npm run napi_install" }, "main": "background.js", "dependencies": { @@ -24,6 +28,7 @@ "electron-is-dev": "^1.2.0", "electron-log": "^4.2.4", "electron-updater": "^4.3.5", + "extract-zip": "^2.0.1", "howler": "^2.2.0", "js-cookie": "^2.2.1", "nprogress": "^0.2.0", diff --git a/script/setup.js b/script/setup.js new file mode 100644 index 0000000..8576895 --- /dev/null +++ b/script/setup.js @@ -0,0 +1,99 @@ +// node module +const fs = require("fs"); +const https = require("https"); +const resolve = require("path").resolve; +const join = require("path").resolve; +const extract = require("extract-zip"); + +// 函数参数 +const dest = resolve(__dirname, "../"); +const fileName = "NeteaseCloudMusicApi-master.zip"; +const options = { + hostname: "github.91chifun.workers.dev", + 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) => { + const destPath = join(__dirname, "../" + fileName); + // Check if exist + if (fs.existsSync(destPath)) return resolve(destPath); + + const file = fs.createWriteStream(destPath); + const request = https.get(options, (res) => { + let len = res.headers && parseInt(res.headers["content-length"], 10); + let cur = 0; + // 1048576 - bytes in 1Megabyte + const MEGA = 1048576; + let total = 0; + if (len) { + total = len / MEGA; + } + if (!len) { + console.log( + "Downloading, but can not get content-length, please be patient." + ); + } + res.on("data", (chunk) => { + if (len) { + cur += chunk.length; + console.log(`Downloading ${fix2((100.0 * cur) / len)}% ${fix2(cur / MEGA)}/${fix2(total)}mb`); + } + }); + res.on("end", () => { + callback("Downloading complete!"); + }); + res.pipe(file); + file.on("finish", () => { + file.close(() => { + callback("File wrote complete!"); + resolve(destPath); + }); + }); + file.on("error", (err) => { + fs.unlink(destPath); + reject(err); + }); + request.on("error", (err) => { + console.log("Error: " + err.message); + }); + }); + }); +} + +async function unzip(source, target) { + try { + await extract(source, { + dir: target, + }); + console.log("Extraction complete"); + return true; + } catch (err) { + // handle any errors + if (err.message === "end of central directory record signature not found") { + console.log("Not a full_downloaded zip file, removed!"); + fs.unlinkSync(source); + } + return false; + } +} +// Download process +download(options, fileName, (text) => { + console.log(text); +}).then((path) => { + console.log(path) + // Unzip process + return unzip(path, dest); +}); diff --git a/yarn.lock b/yarn.lock index e859293..590482f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1274,6 +1274,13 @@ dependencies: "@types/yargs-parser" "*" +"@types/yauzl@^2.9.1": + version "2.9.1" + resolved "https://registry.npm.taobao.org/@types/yauzl/download/@types/yauzl-2.9.1.tgz?cache=0&sync_timestamp=1596841684525&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fyauzl%2Fdownload%2F%40types%2Fyauzl-2.9.1.tgz#d10f69f9f522eef3cf98e30afb684a1e1ec923af" + integrity sha1-0Q9p+fUi7vPPmOMK+2hKHh7JI68= + dependencies: + "@types/node" "*" + "@vue/babel-helper-vue-jsx-merge-props@^1.0.0": version "1.0.0" resolved "https://registry.npm.taobao.org/@vue/babel-helper-vue-jsx-merge-props/download/@vue/babel-helper-vue-jsx-merge-props-1.0.0.tgz?cache=0&sync_timestamp=1596768129236&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40vue%2Fbabel-helper-vue-jsx-merge-props%2Fdownload%2F%40vue%2Fbabel-helper-vue-jsx-merge-props-1.0.0.tgz#048fe579958da408fb7a8b2a3ec050b50a661040" @@ -4446,6 +4453,17 @@ extract-zip@^1.0.3: mkdirp "^0.5.4" yauzl "^2.10.0" +extract-zip@^2.0.1: + version "2.0.1" + resolved "https://registry.npm.taobao.org/extract-zip/download/extract-zip-2.0.1.tgz?cache=0&sync_timestamp=1591773082587&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fextract-zip%2Fdownload%2Fextract-zip-2.0.1.tgz#663dca56fe46df890d5f131ef4a06d22bb8ba13a" + integrity sha1-Zj3KVv5G34kNXxMe9KBtIruLoTo= + dependencies: + debug "^4.1.1" + get-stream "^5.1.0" + yauzl "^2.10.0" + optionalDependencies: + "@types/yauzl" "^2.9.1" + extsprintf@1.3.0: version "1.3.0" resolved "https://registry.npm.taobao.org/extsprintf/download/extsprintf-1.3.0.tgz#96918440e3041a7a414f8c52e3c574eb3c3e1e05"