wechat-devtool
Version:
Wechat Miniprogram Devtool CLI for NodeJS
194 lines • 8.87 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var os = require("os");
var path = require("path");
var fs = require("fs");
var iconv_lite_1 = __importDefault(require("iconv-lite"));
var promise_1 = require("./promise");
var DEFAULT_CLI = 'cli';
var cliPath;
/**
* 添加引号
* @param arg
*/
function quote(arg) {
return /\s/.test(arg) ? JSON.stringify(arg) : arg;
}
/**
* 获取 小程序CLI 路径
* refer https://github.com/egret-labs/egret-core/blob/master/tools/commands/run.ts
*/
function getCLIPath() {
return __awaiter(this, void 0, void 0, function () {
var wxPaths, _a, result, stdout, encoding, result2, stdout, stdoutArr, exePath, error_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (cliPath) {
return [2 /*return*/, cliPath];
}
wxPaths = [];
_a = os.platform();
switch (_a) {
case "darwin": return [3 /*break*/, 1];
case "win32": return [3 /*break*/, 3];
}
return [3 /*break*/, 7];
case 1: return [4 /*yield*/, promise_1.exec("defaults read com.tencent.webplusdevtools LastRunAppBundlePath")
.catch(function () { return promise_1.exec("defaults read com.tencent.wechat.devtools LastRunAppBundlePath"); }) // 旧版包名
.catch(function () { return undefined; })];
case 2:
result = _b.sent();
if (result && result.stdout) {
stdout = result.stdout.replace(/\n/g, "");
wxPaths.push(path.join(stdout, "/Contents/MacOS/cli"));
wxPaths.push(path.join(stdout, "/Contents/Resources/app.nw/bin/cli"));
}
// defaults read
wxPaths.push("/Applications/wechatwebdevtools.app/Contents/MacOS/cli");
wxPaths.push("/Applications/wechatwebdevtools.app/Contents/Resources/app.nw/bin/cli");
return [3 /*break*/, 7];
case 3:
_b.trys.push([3, 5, , 6]);
encoding = 'cp936';
return [4 /*yield*/, promise_1.exec('REG QUERY "HKLM\\SOFTWARE\\Wow6432Node\\Tencent\\微信web开发者工具"', { encoding: 'buffer' })];
case 4:
result2 = _b.sent();
stdout = iconv_lite_1.default.decode(result2.stdout, encoding);
if (stdout) {
stdoutArr = stdout.split("\r\n");
exePath = stdoutArr.find(function (p) { return p.indexOf(".exe") != -1; }) || "";
exePath = exePath.split(" ").find(function (path) { return path.indexOf(".exe") != -1; }) || "";
exePath = path.join(path.dirname(exePath), 'cli.bat');
wxPaths.unshift(exePath);
}
return [3 /*break*/, 6];
case 5:
error_1 = _b.sent();
return [3 /*break*/, 6];
case 6:
// defaults read
wxPaths.push("C:\\Program Files (x86)\\Tencent\\微信web开发者工具\\cli.bat", "C:\\Program Files\\Tencent\\微信web开发者工具\\cli.bat");
return [3 /*break*/, 7];
case 7: return [2 /*return*/, cliPath = wxPaths.find(function (wxpath) { return fs.existsSync(wxpath); })];
}
});
});
}
exports.getCLIPath = getCLIPath;
/**
* 获取CLI开启的端口号
*/
function getPort() {
var home = os.homedir();
var portPaths = process.platform === 'win32'
? [
path.join(home, '/AppData/Local/微信开发者工具/User Data/Default/.ide'),
path.join(home, '/AppData/Local/微信web开发者工具/User Data/Default/.ide')
]
: [
path.join(home, '/Library/Application Support/微信开发者工具/Default/.ide'),
path.join(home, '/Library/Application Support/微信开web发者工具/Default/.ide')
];
var portPath = portPaths.find(function (p) { return fs.existsSync(p); });
if (portPath) {
return promise_1.readFile(portPath).then(function (p) { return +p.toString(); });
}
else {
return Promise.reject(false);
}
}
exports.getPort = getPort;
/**
* 执行CLI命令
* @param args
*/
function cli() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return __awaiter(this, void 0, void 0, function () {
var cliPath;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, getCLIPath()];
case 1:
cliPath = _a.sent();
if (cliPath) {
return [2 /*return*/, promise_1.execFile(cliPath, args, { timeout: 120000 })];
}
else {
return [2 /*return*/, promise_1.exec('"' + DEFAULT_CLI + '" ' + args.map(quote).join(' '), { timeout: 120000 })];
}
return [2 /*return*/];
}
});
});
}
exports.cli = cli;
/**
* spwan 方式 调用 cli
* @param args
* @param options
*/
function cliSpawn(args, options) {
if (options === void 0) { options = {
stdio: 'inherit',
shell: true,
windowsHide: true,
windowsVerbatimArguments: true,
}; }
return __awaiter(this, void 0, void 0, function () {
var cliPath;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, getCLIPath()];
case 1:
cliPath = _a.sent();
return [2 /*return*/, promise_1.spawn('"' + cliPath + '"' || DEFAULT_CLI, args.map(quote), options)];
}
});
});
}
exports.cliSpawn = cliSpawn;
//# sourceMappingURL=cli.js.map