wechat-web-devtools-linux/tools/update-wechat-devtools
2022-01-23 12:01:05 +08:00

208 lines
6.4 KiB
Plaintext
Executable File

#!/usr/bin/env mew_js
const http = require("http");
const { info } = require("console");
const urls = {
"stable": "https://developers.weixin.qq.com/miniprogram/dev/devtools/stable.html",
"rc": "https://developers.weixin.qq.com/miniprogram/dev/devtools/rc.html",
"nightly": "https://developers.weixin.qq.com/miniprogram/dev/devtools/nightly.html"
};
let branch = @.process.switches().commands[0];
let ver = @.process.switches().commands[1];
if ((!branch) || (!urls[branch])) {
branch = "stable";
}
const url = urls[branch];
// "https://servicewechat.com/wxa-dev-logic/download_redirect?type=x64&from=mpwiki";
// const url = "https://servicewechat.com/wxa-dev-logic/download_redirect?type=x64&from=mpwiki&version_type=1";
// https://servicewechat.com/wxa-dev-logic/download_redirect?type=x64&from=mpwiki&download_version=1022001191&version_type=1
// https://servicewechat.com/wxa-dev-logic/download_redirect?type=x64&from=mpwiki&download_version=1021911180&version_type=1
// https://dldir1.qq.com/WechatWebDev/nightly/p-7aa88fbb60d64e4a96fac38999591e31/wechat_devtools_1.02.2001202_x64.exe
let version = undefined;
const packageDir = "code/package.nw";
let client = @.net.httpClient();
@.async(function () {
@.fs.makeDirs(@path(__dirname, "../cache"));
info(`Fetching wechat-dev-tool info: ${url}`);
if(ver){
version = ver
this.next(`https://servicewechat.com/wxa-dev-logic/download_redirect?type=x64&from=mpwiki&download_version=${ver}&version_type=1`);
}else
client.request(url, {
"onSuccess": (result) => {
let links = {};
result.toString("utf8").split("<div class=\"page-inner\"")[1].split("<a href=\"").slice(1).map((a) => {
return a.split("</a>")[0];
}).filter((link) => link[0] != "#").forEach((link) => {
let content = link.split(">").slice(1).join(">").replace(/<([^>]*)>/g, "").replace(/\s+/g, "-").toLowerCase();
if ((content === "windows-64") || (content === "windows-32") || (content === "macos")) {
if (!links[content]) {
links[content] = [];
}
let url = link.split("\"")[0];
links[content].push(url);
info(`Found ${content} link: ${url}`);
}
});
if (!links["windows-64"][0]) {
error("No windows-64 dist found");
process.exit(1);
}
this.next(links["windows-64"][0]);
},
"onError": this.reject
})
}).then(function (url) {
@info(`Downloading ${url}`);
let localPath = @path(__dirname, "../cache/wechat-devtools-x64.exe");
let lastSize = 0;
let lastProgress = 0;
let lastTime = Date.now();
let filename = "wechat-devtools-x64.exe";
@.fs.deleteFile.sync(localPath);
client.download(url, localPath, {
"redirects": Object.create(null),
"onSuccess": () => {
let newPath = @path(__dirname, "../cache", filename);
@.fs.moveFile(localPath, newPath);
this.next(newPath);
},
"onRedirect": (location) => {
@info(`Redirected to ${location}`);
filename = location.split("?")[0].split("/").slice(-1)[0];
version = filename.split("_").filter((x) => /^[0-9]+\.[0-9]+.[0-9]+$/.test(x))[0];
let newPath = @path(__dirname, "../cache", filename);
if (@.fs.exists(newPath)) {
this.next(newPath);
return true;
}
},
"onProgress": (size, total) => {
let progress = size / total * 100;
let now = Date.now();
if ((progress - lastProgress > 10) ||
(now - lastTime > 1000)) {
let speed = (size - lastSize) / (now - lastTime) * 1000 / 1024;
lastSize = size;
lastTime = now;
lastProgress = progress;
process.stdout.write(`Downloaded ${filename}: ${progress.toFixed(2)}%, speed ${speed.toFixed(2)} KiB/s\r`);
}
},
"onError": this.reject
});
}).then(function (localPath) {
// 解压
@info(`Extracting ${localPath}`);
let extractPath = @path(__dirname, `../tmp/${@.fs.basename(localPath)}`);
@.fs.deleteFile.sync(extractPath);
@.fs.makeDirs(extractPath);
@.task.execute("7z", ["x", localPath, `-o${extractPath}`, packageDir], extractPath, (error) => {
if (error) {
this.reject(error); return;
}
this.next(extractPath);
});
}).then(function (extractPath) {
@info(`Upgrading ${@.fs.filename(extractPath)}`);
// 删除元文件
@.fs.deleteFile.sync(@path(__dirname, "../package.nw"));
// 替换
@.fs.moveFile.sync(@path(extractPath, packageDir),
@path(__dirname, "../package.nw"));
// 删除临时
@.fs.deleteFile.sync(extractPath);
if (@.fs.exists(@path(__dirname, "../nwjs"))) {
if (!@.fs.exists(@path(__dirname, "../nwjs/package.nw"))) {
// 链接
@.fs.linkFile("../package.nw", @path(__dirname, "../nwjs/package.nw"));
}
}
this.next();
}).then(function () {
@info("Patching wechat-devtools package name");
@.task.execute(@path(__dirname, "fix-package-name"), [], false, this.test);
}).then(function () {
@info("Patching wechat-devtools editor selection autocopy");
@.task.execute(@path(__dirname, "fix-selection-copy"), [], false, this.test);
}).then(function () {
@info("Patching wechat-devtools CLI supports");
@.task.execute(@path(__dirname, "fix-cli"), [], false, this.test);
}).then(function () {
@info("Rebuilding wechat-devtools node modules");
@.task.execute(@path(__dirname, "rebuild-node-modules"), [], false, this.test);
}).then(function () {
@info("Patching wechat-devtools");
@.task.execute(@path(__dirname, "patch-wechat-devtools"), [], false, this.test);
}).then(function () {
@info("Patching wcc and wcsc");
@.fs.copyFile.sync(@path(__dirname, "../wine/wcc"), @path(__dirname, "../package.nw/js/vendor/wcc"));
@.fs.copyFile.sync(@path(__dirname, "../wine/wcsc"), @path(__dirname, "../package.nw/js/vendor/wcsc"));
this.next();
}).finished((error) => {
if (error) {
@error(error);
process.exit(1);
}
@celebr(`Succeeded upgrading wechat-devtools to version ${version}`);
process.exit(0);
});