diff --git a/tools/fix-cli-node b/tools/fix-cli-node new file mode 100644 index 0000000..debc7d6 --- /dev/null +++ b/tools/fix-cli-node @@ -0,0 +1,20 @@ +#!/usr/bin/env node +const path = require("path"); +const fs = require("fs"); +const { spawn } = require("child_process"); + +console.info("Patching CLI command"); + +const rootDir = path.dirname(__dirname); + +let cli = fs.readFileSync(path.resolve(rootDir, "package.nw/js/common/cli/index.js"), "utf8"); + +cli = cli.replace(/USERPROFILE/g, "HOME"); +cli = cli.replace(/AppData\/Local\/\$\{global\.userDirName\}\/User Data/g, + ".config/${global.userDirName}"); +cli = cli.replace(/`\.\/\$\{global.appname\}\.exe`/g, + "require(\"path\").join(__dirname, \"../../../../bin/wechat-devtools\")"); +cli = cli.replace(/"\.\.\/\.\.\/\.\.\/\.\.\/resources_win\/nw\/x64\/nw.exe"/g, + "\"../../../../nwjs/nw\""); + +fs.writeFileSync(path.resolve(rootDir, "package.nw/js/common/cli/index.js"), cli); \ No newline at end of file diff --git a/tools/fix-package-name-node b/tools/fix-package-name-node new file mode 100644 index 0000000..4545834 --- /dev/null +++ b/tools/fix-package-name-node @@ -0,0 +1,23 @@ +#!/usr/bin/env node +const path = require("path"); +const fs = require("fs"); +const { spawn } = require("child_process"); + +const parseFile = function (path) { + + if (!fs.existsSync(path)) { + return; + } + + let content = JSON.parse(fs.readFileSync(path, "utf8")); + + content.name = "wechat_devtools"; + // 开启调试,更新参数 + content['chromium-args'] = content['chromium-args'].replace('--disable-devtools', '').replace('--ignore-gpu-blacklist', '--ignore-gpu-blocklist') + + fs.writeFileSync(path, JSON.stringify(content, null, 4)); + +}; + +parseFile(path.resolve(__dirname, "../package.nw/package.json")); +parseFile(path.resolve(__dirname, "../package.nw/package-lock.json")); diff --git a/tools/fix-selection-copy-node b/tools/fix-selection-copy-node new file mode 100644 index 0000000..dfeea81 --- /dev/null +++ b/tools/fix-selection-copy-node @@ -0,0 +1,23 @@ +#!/usr/bin/env node +const path = require("path"); +const os = require("os"); +const fs = require("fs"); +const { spawn, execSync } = require("child_process"); + +console.info("Patching editor selection copy configs"); + +let configPath = os.homedir() + "/.config/wechat_devtools/Default/Editor/User/settings.json"; +let config = undefined; +if (fs.existsSync(configPath)) { + // console.info(configPath) + config = JSON.parse(fs.readFileSync(configPath, "utf8")); +} else { + config = {}; +} + +config["editor.selectionClipboard"] = false; + +// nodejs只支持一级一级目录创建,效率低 +execSync(`mkdir -p ${path.dirname(configPath)}`) + +fs.writeFileSync(configPath, JSON.stringify(config, null, 4)); diff --git a/tools/install-desktop-icon-node b/tools/install-desktop-icon-node new file mode 100644 index 0000000..b691f92 --- /dev/null +++ b/tools/install-desktop-icon-node @@ -0,0 +1,43 @@ +#!/usr/bin/env node +const path = require("path"); +const fs = require("fs"); +const os = require("os"); +const { spawn } = require("child_process"); +const { info } = require("console"); + +for (let size of ["64", "128", "256", "512"]) { + let iconPath = + os.homedir() + + `/.local/share/icons/hicolor/${size}x${size}/wechat-devtools.png`; + try { + fs.mkdirSync(path.dirname(iconPath)); + } catch (error) {} + info(`Writing icon file ${iconPath}`); + fs.copyFileSync( + path.resolve(__dirname, "../res/icons", `wechat-devtools${size}.png`), + iconPath + ); +} + +let svgPath = + os.homedir() + "/.local/share/icons/hicolor/scalable/wechat-devtools.svg"; +try { + fs.mkdirSync(path.dirname(svgPath)); +} catch (error) {} +info(`Writing icon file ${svgPath}`); +fs.copyFileSync( + path.resolve(__dirname, "../res/icons/wechat-devtools.svg"), + svgPath +); + +let desktopCode = fs + .readFileSync(path.resolve(__dirname, "../res/template.desktop"), "utf8") + .replace("dir", path.resolve(__dirname, "..")); + +desktopPath = + os.homedir() + "/.local/share/applications/wechat-devtools.desktop"; +try { + fs.mkdirSync(path.dirname(desktopPath)); +} catch (error) {} +info(`Writing desktop file ${desktopPath}`); +fs.writeFileSync(desktopPath, desktopCode); diff --git a/tools/patch-wechat-devtools-node b/tools/patch-wechat-devtools-node new file mode 100644 index 0000000..cd2711a --- /dev/null +++ b/tools/patch-wechat-devtools-node @@ -0,0 +1,36 @@ +#!/usr/bin/env node +const path = require("path"); +const fs = require("fs"); +const { spawn } = require("child_process"); + +let code = fs.readFileSync(path.resolve(__dirname, "../package.nw/js/unpack/hackrequire/index.js"), {encoding:"utf8"}); + +let signatureBegin = "/* patch wechat devtools begin */\n"; +let signatureEnd = "/* patch wechat devtools end */\n"; + +let index = code.indexOf(signatureBegin); + +let patch = fs.readdirSync(path.resolve(__dirname, "../patch")).map((file) => { + console.log(file) + if ((file !== ".") && (file.endsWith(".js"))) { + return (`/* ${file} */\n` + + "(() => {\n\n" + + " try {\n\n" + + fs.readFileSync(path.resolve(__dirname, "../patch", file), {encoding:"utf8"}).trim().split("\n").map((line) => { + return " " + line; + }).join("\n") + "\n\n" + + " } catch (error) {\n" + + " process.stderr.write(error.message);\n" + + " process.stderr.write(error.stack);\n" + + " }\n\n" + + "})();"); + } + return ""; +}).join("\n").trim() + "\n"; + +if (code.indexOf(signatureBegin) !== -1) { + code = code.split(signatureEnd).slice(1).join(signatureEnd); +} + +fs.writeFileSync(path.resolve(__dirname, "../package.nw/js/unpack/hackrequire/index.js"), + signatureBegin + patch + signatureEnd + code); diff --git a/tools/setup-wechat-devtools b/tools/setup-wechat-devtools index 6abb3b8..cbc1817 100755 --- a/tools/setup-wechat-devtools +++ b/tools/setup-wechat-devtools @@ -18,7 +18,7 @@ this.next(); return; } - @.task.execute(@path(__dirname, "update-nwjs"), [], false, this.test); + @.task.execute(@path(__dirname, "update-nwjs-node"), [], false, this.test); }).then(function () { diff --git a/tools/setup-wechat-devtools-node b/tools/setup-wechat-devtools-node new file mode 100644 index 0000000..4f9b68a --- /dev/null +++ b/tools/setup-wechat-devtools-node @@ -0,0 +1,87 @@ +#!/usr/bin/env node +const path = require("path"); +const fs = require("fs"); +const { spawn } = require("child_process"); + +const init_node = function () { + console.info("Initializing node"); + return new Promise((resolve, reject) => { + if (fs.existsSync(path.resolve(__dirname, "../node"))) { + resolve(); + return; + } + + const updateNode = spawn( + path.resolve(__dirname, "update-node-node"), + [], + { + stdio: "inherit", + } + ); + updateNode.on("close", (code) => { + console.log(`Update Node code: ${code}`); + resolve(); + }); + }); +}; + +const init_nwjs = function () { + console.info("Initializing nwjs"); + + return new Promise((resolve, reject) => { + if (fs.existsSync(path.resolve(__dirname, "../nwjs"))) { + resolve(); + return; + } + + const updateNwjs = spawn( + path.resolve(__dirname, "update-nwjs-node"), + [], + { + stdio: "inherit", + } + ); + updateNwjs.on("close", (code) => { + console.log(`Update Nwjs result code: ${code}`); + resolve(); + }); + }); +}; +const init_wechat = function () { + console.info("Initializing wechat-devtools package"); + + return new Promise((resolve, reject) => { + if (fs.existsSync(path.resolve(__dirname, "../package.nw"))) { + resolve(); + return; + } + + const updateWechat = spawn( + path.resolve(__dirname, "update-wechat-devtools-node"), + [], + { + stdio: "inherit", + } + ); + updateWechat.on("close", (code) => { + console.log( + `Initializing wechat-devtools package result code:${code}` + ); + resolve(); + }); + }); +}; + +const start = async () => { + try { + await init_node(); + await init_nwjs(); + await init_wechat(); + + console.log(`Succeeded setting up wechat-devtools`); + } catch (error) { + console.error("异常", error); + } +}; + +start(); diff --git a/tools/test b/tools/test new file mode 100644 index 0000000..94cea15 --- /dev/null +++ b/tools/test @@ -0,0 +1,14 @@ +#!/usr/bin/env node + +const fs = require("fs"); +const path = require("path"); + +if (fs.existsSync(path.resolve(__dirname, "../nwjs"))) { + if (!fs.existsSync(path.resolve(__dirname, "../nwjs/package.nw"))) { + // 链接 + fs.symlinkSync( + path.resolve(__dirname, "../package.nw"), + path.resolve(__dirname, "../nwjs/package.nw") + ); + } +} diff --git a/tools/update-node-node b/tools/update-node-node new file mode 100644 index 0000000..196b419 --- /dev/null +++ b/tools/update-node-node @@ -0,0 +1,109 @@ +#!/usr/bin/env node +const path = require("path"); +const fs = require("fs"); +const { execSync, spawn } = require("child_process"); +const util = require("util"); + +const nodeConfig = require("../conf/node.json"); + +const download = function () { + return new Promise((resolve, reject) => { + try { + fs.mkdirSync(path.resolve(__dirname, "../cache")); + } catch (error) {} + + let url = nodeConfig.url + .replace("${version}", nodeConfig.version) + .replace("${version}", nodeConfig.version); + + let localPath = path.resolve( + __dirname, + "../cache", + url.split("?")[0].split("/").slice(-1)[0] + ); + + if (fs.existsSync(localPath)) { + console.info(`Cache available ${path.basename(localPath)}`); + resolve(localPath); + return; + } + + info(`Downloading ${url}`); + spawn("wget", ["c", url, "O", `${localPath}.tmp`], { + stdio: "inherit", + }).on("close", (code) => { + fs.renameSync(`${localPath}.tmp`, localPath); + resolve(localPath); + }); + }); +}; +const extract = function (localPath) { + console.info(`Extracting ${localPath}`); + + return new Promise((resolve, reject) => { + let extractPath = path.resolve( + __dirname, + `../tmp/${path.basename(localPath)}` + ); + + execSync(`rm -rf ${extractPath}`); + + fs.mkdirSync(extractPath); + + spawn("tar", ["xf", localPath, '-C', extractPath], { stdio: "inherit" }).on( + "close", + (code) => { + console.log(`Extracting result code: ${code}`) + resolve(extractPath); + } + ); + }); +}; +const upgrade = function (extractPath) { + console.info(`Upgrading ${path.basename(extractPath)}`); + + return new Promise((resolve, reject) => { + execSync(`rm -rf ${path.resolve(__dirname, "../node")}`); + + fs.renameSync( + path.resolve(extractPath, fs.readdirSync(extractPath)[0]), + path.resolve(__dirname, "../node") + ); + + execSync(`rm -rf ${extractPath}`); + + if (fs.existsSync(path.resolve(__dirname, "../nwjs"))) { + if (!fs.existsSync(path.resolve(__dirname, "../nwjs/node"))) { + fs.linkSync( + "../node/bin/node", + path.resolve(__dirname, "../nwjs/node") + ); + } + if (!fs.existsSync(path.resolve(__dirname, "../nwjs/node.exe"))) { + fs.linkSync( + "node", + path.resolve(__dirname, "../nwjs/node.exe") + ); + } + } + + resolve(); + }); +}; + +const start = async () => { + try { + const localPath = await download(); + const extractPath = await extract(localPath); + await upgrade(extractPath); + + console.log( + `Succeeded upgrading nodeConfig to version ${nodeConfig.version}` + ); + } catch (error) { + console.error("异常", error); + throw error + } +}; + +start(); diff --git a/tools/update-nwjs-node b/tools/update-nwjs-node index 7150ef3..583f649 100644 --- a/tools/update-nwjs-node +++ b/tools/update-nwjs-node @@ -4,6 +4,7 @@ const path = require('path') const net = require('net') const { spawn, execSync } = require('child_process'); const util = require('util'); +const { exit } = require('process'); const download = (nwjs) => { return new Promise((resolve, reject) => { @@ -92,7 +93,7 @@ const upgrade = function (extractPath) { fs.linkSync(path.resolve(__dirname, "../node/bin/node"), path.resolve(__dirname, "../nwjs/node")); fs.linkSync(path.resolve(__dirname, "../node/bin/node"), path.resolve(__dirname, "../nwjs/node.exe")); } - if (!fs.existsSync(path.resolve(__dirname, "../package.nw"))) { + if (fs.existsSync(path.resolve(__dirname, "../package.nw"))) { fs.linkSync(path.resolve(__dirname, "../package.nw"), path.resolve(__dirname, "../nwjs/package.nw")); } @@ -108,11 +109,12 @@ const init = async () => { const result = await upgrade(extractPath) console.info(`Succeeded upgrading nwjs to version ${nwjs.version}`); - } catch (error) { + } catch (err) { - if (error) { - console.error('error--->', error); + if (err) { + console.error('error--->', err); } + exit(-1) } } init() \ No newline at end of file diff --git a/tools/update-wechat-devtools-node b/tools/update-wechat-devtools-node new file mode 100644 index 0000000..8877ef0 --- /dev/null +++ b/tools/update-wechat-devtools-node @@ -0,0 +1,288 @@ +#!/usr/bin/env node + +const { execSync, exec, spawn } = require("child_process"); +const https = require("https"); +const fs = require("fs"); +const path = require("path"); +const { info } = require("console"); +const util = require("util"); + +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", +}; + +const args = process.argv.slice(2); +let branch = args[0]; +let ver = args[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"; + +const fetching = function () { + return new Promise((resolve, reject) => { + try { + fs.mkdirSync(path(__dirname, "../cache")); + } catch (error) {} + + info(`Fetching wechat-dev-tool info: ${url}`); + + if (ver) { + version = ver; + resolve( + `https://servicewechat.com/wxa-dev-logic/download_redirect?type=x64&from=mpwiki&download_version=${ver}&version_type=1` + ); + } else { + https + .get(url, (res) => { + let result = ""; + res.on("data", (data) => { + result += data; + }); + res.on("end", () => { + let links = {}; + result + .toString("utf8") + .split('
")[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]) { + console.error("No windows-64 dist found"); + exit(1); + } + resolve(links["windows-64"][0]); + }); + }) + .on("error", reject); + } + }); +}; + +const download = function (url) { + return new Promise((resolve, reject) => { + info(`Downloading ${url}`); + + version = url.match(/version=(\d+)&/)[1]; + let mainVer = version.substring(0, version.length - 9); + let subVer = version.substring(version.length - 9, version.length - 7); + let time = version.substring(version.length - 7); + version = [mainVer, subVer, time].join("."); + + let localPath = path.resolve( + __dirname, + util.format("../cache/wechat_devtools_%s_x64.exe", version) + ); + + if (fs.existsSync(localPath)) { + resolve(localPath); + return; + } + + // wget -c url -O out + const ls = spawn("wget", ["-c", url, "-O", `${localPath}.tmp`], { + stdio: "inherit", + }); + + ls.on("close", (code) => { + fs.renameSync(`${localPath}.tmp`, localPath); + resolve(localPath); + }); + }); +}; + +const extract = function (localPath) { + // 解压 + info(`Extracting ${localPath}`); + return new Promise((resolve, reject) => { + let extractPath = path.resolve( + __dirname, + `../tmp/${path.basename(localPath)}` + ); + + execSync(`rm -rf ${extractPath}`); + + try { + fs.mkdirSync(extractPath); + } catch (error) {} + + const ext = spawn( + "7z", + ["x", localPath, `-o${extractPath}`, packageDir], + { + stdio: "inherit", + } + ); + ext.on("close", (code) => { + info(`解压完毕:${code}`); + resolve(extractPath); + }); + }); +}; + +const upgrade = function (extractPath) { + info(`Upgrading ${path.basename(extractPath)}`); + + return new Promise((resolve, reject) => { + // 删除原文件 + execSync(`rm -rf ${path.resolve(__dirname, "../package.nw")}`); + + // 替换 + fs.renameSync( + path.resolve(extractPath, packageDir), + path.resolve(__dirname, "../package.nw") + ); + + // 删除临时 + execSync(`rm -rf ${extractPath}`); + + if (fs.existsSync(path.resolve(__dirname, "../nwjs"))) { + if (!fs.existsSync(path.resolve(__dirname, "../nwjs/package.nw"))) { + // 链接 + fs.symlinkSync( + path.resolve(__dirname, "../package.nw"), + path.resolve(__dirname, "../nwjs/package.nw") + ); + } + } + + resolve(); + }); +}; + +const patch_wechat_devtools_package_name = function () { + info("Patching wechat-devtools package name"); + + return new Promise((resolve, reject) => { + spawn(path.resolve(__dirname, "fix-package-name-node"), [], { + stdio: "inherit", + }).on("close", (code) => { + resolve(); + }); + }); +}; +const patch_wechat_devtools_editor_selection_autocopy = function () { + info("Patching wechat-devtools editor selection autocopy"); + + return new Promise((resolve, reject) => { + spawn(path.resolve(__dirname, "fix-selection-copy-node"), [], { + stdio: 'inherit', + }).on("close", (code) => { + resolve(); + }); + }); +}; +const patch_wechat_devtools_CLI = function () { + info("Patching wechat-devtools CLI supports"); + + return new Promise((resolve, reject) => { + spawn(path.resolve(__dirname, "fix-cli-node"), [], { stdio: "inherit" }).on( + "close", + (code) => { + resolve(); + } + ); + }); +}; +const rebuild_wechat_devtools_node_modules = function () { + info("Rebuilding wechat-devtools node modules"); + + return new Promise((resolve, reject) => { + spawn(path.resolve(__dirname, "rebuild-node-modules"), [], { + stdio: "inherit", + }).on("close", (code) => { + resolve(); + }); + }); +}; +const patch_wechat_devtools = function () { + info("Patching wechat-devtools"); + + return new Promise((resolve, reject) => { + const exec = spawn( + path.resolve(__dirname, "patch-wechat-devtools-node"), + [], + { + stdio: "inherit", + } + ); + exec.on("close", (code) => { + resolve(); + }); + }); +}; +const patch_wcc_wcsc = function () { + info("Patching wcc and wcsc"); + + return new Promise((resolve, reject) => { + fs.copyFileSync( + path.resolve(__dirname, "../wine/wcc"), + path.resolve(__dirname, "../package.nw/js/vendor/wcc") + ); + fs.copyFileSync( + path.resolve(__dirname, "../wine/wcsc"), + path.resolve(__dirname, "../package.nw/js/vendor/wcsc") + ); + + resolve(); + }); +}; + +const start = async () => { + try { + const url = await fetching(); + const localPath = await download(url); + const extractPath = await extract(localPath); + await upgrade(extractPath); + await patch_wechat_devtools_package_name(); + await patch_wechat_devtools_editor_selection_autocopy(); + await patch_wechat_devtools_CLI(); + await rebuild_wechat_devtools_node_modules(); + await patch_wechat_devtools(); + await patch_wcc_wcsc(); + console.log( + `Succeeded upgrading wechat-devtools to version ${version}` + ); + } catch (error) { + console.error("异常", error); + throw error + } +}; +start();