|
| 1 | +#!/usr/bin/env node |
| 2 | +const { spawn, spawnSync } = require("child_process"); |
| 3 | +const path = require("path"); |
| 4 | +const args = process.argv.slice(2); |
| 5 | +const wcscPath = path.resolve(__dirname, "./wcsc.bin") |
| 6 | +function encode1(s) { |
| 7 | + return encodeURI(s).replace( |
| 8 | + /%(u[0-9A-F]{4})|(%[0-9A-F]{2})/gm, |
| 9 | + function ($0, $1, $2) { |
| 10 | + return ($1 && "\\" + $1.toLowerCase()) || decodeURI($2); |
| 11 | + } |
| 12 | + ); |
| 13 | +} |
| 14 | +if (args.includes("-ll")) { |
| 15 | + const wcsc = spawn(wcscPath, args, { |
| 16 | + cwd: process.cwd(), |
| 17 | + }); |
| 18 | + const spwanData = [], |
| 19 | + errData = []; |
| 20 | + wcsc.stdout.on("data", (e) => { |
| 21 | + spwanData.push(e); |
| 22 | + }); |
| 23 | + wcsc.stderr.on("data", (e) => { |
| 24 | + errData.push(e); |
| 25 | + }); |
| 26 | + wcsc.on("close", (n) => { |
| 27 | + // console.log('close', new Date().getTime()/1000) |
| 28 | + if (0 === n) { |
| 29 | + const str = Buffer.concat(spwanData).toString().replace(/\\\\/g, '\\\\u005c'); |
| 30 | + const resultSplit = encode1(str).split("="); |
| 31 | + const tempObj = {}; |
| 32 | + for ( |
| 33 | + let i = 0, total = resultSplit.length; |
| 34 | + i < total && resultSplit[i + 1]; |
| 35 | + i += 2 |
| 36 | + ) { |
| 37 | + // a=b ---> a: b |
| 38 | + const key = resultSplit[i]; |
| 39 | + if (key === "version") continue; |
| 40 | + tempObj[key] = resultSplit[i + 1].replace( |
| 41 | + /((\\x[\da-f]{2}|\\u[\da-f]{4})){1,}/gi, |
| 42 | + function ($0, $1, $2) { |
| 43 | + return eval('"' + $0 + '"'); |
| 44 | + // return dict[$0] ? dict[$0] : eval('"' + $0 + '"') |
| 45 | + } |
| 46 | + ); |
| 47 | + } |
| 48 | + const resultObj = { |
| 49 | + common: tempObj.comm, //.replace(/\\n/g, '\\u000a'), |
| 50 | + pageWxss: {}, |
| 51 | + }; |
| 52 | + // console.log(resultObj.common) |
| 53 | + // console.log('for key', new Date().getTime()/1000) |
| 54 | + for (const key in tempObj) { |
| 55 | + if (key.endsWith(".wxss")) { |
| 56 | + resultObj.pageWxss[key] = tempObj[key]; |
| 57 | + } |
| 58 | + } |
| 59 | + let result = JSON.stringify(resultObj); |
| 60 | + String.prototype.splice = function (start, newStr) { |
| 61 | + return this.slice(0, start) + newStr + this.slice(start + 1); |
| 62 | + }; |
| 63 | + result = result.replace(/\\\\/g, "\\"); |
| 64 | + // console.log('main replace', new Date().getTime()/1000) |
| 65 | + // for (let i = 0; i < result.length; i++) { |
| 66 | + // if (result[i] === "\\") { |
| 67 | + // // && result[i + 1] !== 'u' |
| 68 | + // const c = result[i + 1] === "n" ? "\n" : result[i + 1]; |
| 69 | + // result = result.splice( |
| 70 | + // i + 1, |
| 71 | + // "u" + c.charCodeAt(0).toString(16).padStart(4, "0") |
| 72 | + // ); |
| 73 | + // } |
| 74 | + // } |
| 75 | + result = result.replace(/\\[\s\S]{1}/gi, function ($0, $1, $2) { |
| 76 | + // console.log($0, $1) |
| 77 | + const c = $0 === "\\n" ? "\n" : $0[1]; |
| 78 | + return "\\u" + c.charCodeAt(0).toString(16).padStart(4, "0") |
| 79 | + }) |
| 80 | + // console.log('main replace end', new Date().getTime()/1000) |
| 81 | + // result = result.replace(/u005c"/g, 'u005cx22') |
| 82 | + // console.log('done', new Date().getTime()/1000) |
| 83 | + process.stdout.write(result); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + // const wcsc = spawnSync(path.resolve(__dirname, "../wcsc"), args, { |
| 88 | + // cwd: process.cwd(), |
| 89 | + // }); |
| 90 | + // if (wcsc.status !== 0) { |
| 91 | + // console.error(wcsc.error); |
| 92 | + // process.exit(wcsc.status); |
| 93 | + // } |
| 94 | + // const str = wcsc.stdout.toString(); |
| 95 | + // // console.log(str) |
| 96 | + // const resultSplit = encode1(str).split("="); |
| 97 | + // const tempObj = {}; |
| 98 | + // for ( |
| 99 | + // let i = 0, total = resultSplit.length; |
| 100 | + // i < total && resultSplit[i + 1]; |
| 101 | + // i += 2 |
| 102 | + // ) { |
| 103 | + // // a=b ---> a: b |
| 104 | + // const key = resultSplit[i]; |
| 105 | + // if (key === "version") continue; |
| 106 | + // tempObj[key] = resultSplit[i + 1].replace( |
| 107 | + // /((\\x[\da-f]{2}|\\u[\da-f]{4})){1,}/gi, |
| 108 | + // function ($0, $1, $2) { |
| 109 | + // return eval('"' + $0 + '"'); |
| 110 | + // // return dict[$0] ? dict[$0] : eval('"' + $0 + '"') |
| 111 | + // } |
| 112 | + // ); |
| 113 | + // } |
| 114 | + // const resultObj = { |
| 115 | + // common: tempObj.comm, //.replace(/\\n/g, '\\u000a'), |
| 116 | + // pageWxss: {}, |
| 117 | + // }; |
| 118 | + // // console.log(resultObj.common) |
| 119 | + // for (const key in tempObj) { |
| 120 | + // if (key.endsWith(".wxss")) { |
| 121 | + // resultObj.pageWxss[key] = tempObj[key]; |
| 122 | + // } |
| 123 | + // } |
| 124 | + // let result = JSON.stringify(resultObj); |
| 125 | + // String.prototype.splice = function (start, newStr) { |
| 126 | + // return this.slice(0, start) + newStr + this.slice(start + 1); |
| 127 | + // }; |
| 128 | + // result = result.replace(/\\\\/g, "\\"); |
| 129 | + // for (let i = 0; i < result.length; i++) { |
| 130 | + // if (result[i] === "\\") { |
| 131 | + // // && result[i + 1] !== 'u' |
| 132 | + // const c = result[i + 1] === "n" ? "\n" : result[i + 1]; |
| 133 | + // result = result.splice( |
| 134 | + // i + 1, |
| 135 | + // "u" + c.charCodeAt(0).toString(16).padStart(4, "0") |
| 136 | + // ); |
| 137 | + // } |
| 138 | + // } |
| 139 | + // process.stdout.write(result); |
| 140 | +} else { |
| 141 | + spawn(wcscPath, args, { |
| 142 | + cwd: process.cwd(), |
| 143 | + stdio: "inherit", |
| 144 | + }); |
| 145 | +} |
0 commit comments