weapp-ide-cli
Version:
让微信开发者工具,用起来更加方便!
355 lines (335 loc) • 12.8 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
// src/cli.ts
var import_node_process6 = __toESM(require("process"), 1);
// src/logger.ts
var import_logger = __toESM(require("@weapp-core/logger"), 1);
var logger_default = import_logger.default;
// src/utils/argv.ts
var import_node_process2 = __toESM(require("process"), 1);
// src/utils/path.ts
var import_node_process = __toESM(require("process"), 1);
var import_pathe = __toESM(require("pathe"), 1);
function resolvePath(filePath) {
if (import_pathe.default.isAbsolute(filePath)) {
return filePath;
}
return import_pathe.default.resolve(import_node_process.default.cwd(), filePath);
}
// src/utils/argv.ts
function ensurePathArgument(argv2, optionIndex) {
const paramIdx = optionIndex + 1;
const param = argv2[paramIdx];
if (param && !param.startsWith("-")) {
argv2[paramIdx] = resolvePath(param);
} else {
argv2.splice(paramIdx, 0, import_node_process2.default.cwd());
}
return argv2;
}
function transformArgv(argv2, transforms) {
return transforms.reduce((current, transform) => transform(current), [
...argv2
]);
}
function createAlias(entry) {
return (input2) => {
const argv2 = [...input2];
let optionIndex = argv2.indexOf(entry.find);
if (optionIndex > -1) {
argv2.splice(optionIndex, 1, entry.replacement);
} else {
optionIndex = argv2.indexOf(entry.replacement);
}
if (optionIndex === -1) {
return argv2;
}
return ensurePathArgument(argv2, optionIndex);
};
}
function createPathCompat(option) {
return (input2) => {
const argv2 = [...input2];
const optionIndex = argv2.indexOf(option);
if (optionIndex === -1) {
return argv2;
}
return ensurePathArgument(argv2, optionIndex);
};
}
// src/utils/exec.ts
var import_node_process3 = __toESM(require("process"), 1);
async function execute(cliPath, argv2) {
const { execa } = await import("execa");
const task = execa(cliPath, argv2);
task?.stdout?.pipe(import_node_process3.default.stdout);
task?.stderr?.pipe(import_node_process3.default.stderr);
await task;
}
// src/cli/minidev.ts
var MINIDEV_COMMAND = "minidev";
function isCommandNotFound(error) {
return Boolean(
error && typeof error === "object" && "code" in error && error.code === "ENOENT"
);
}
async function runMinidev(argv2) {
try {
await execute(MINIDEV_COMMAND, [...argv2]);
} catch (error) {
if (isCommandNotFound(error)) {
logger_default.error("\u672A\u68C0\u6D4B\u5230\u652F\u4ED8\u5B9D\u5C0F\u7A0B\u5E8F CLI\uFF1Aminidev");
logger_default.log("\u8BF7\u5148\u5B89\u88C5 minidev\uFF0C\u53EF\u4F7F\u7528\u4EE5\u4E0B\u4EFB\u4E00\u547D\u4EE4\uFF1A");
logger_default.log("- pnpm add -g minidev");
logger_default.log("- npm install -g minidev");
logger_default.log("- yarn global add minidev");
return;
}
throw error;
}
}
// src/cli/prompt.ts
var import_node_process4 = require("process");
var import_promises = require("readline/promises");
var import_fs_extra2 = __toESM(require("fs-extra"), 1);
// src/config/custom.ts
var import_fs_extra = __toESM(require("fs-extra"), 1);
// src/config/paths.ts
var import_node_os = __toESM(require("os"), 1);
var import_pathe2 = __toESM(require("pathe"), 1);
var homedir = import_node_os.default.homedir();
var defaultCustomConfigDirPath = import_pathe2.default.join(homedir, ".weapp-ide-cli");
var defaultCustomConfigFilePath = import_pathe2.default.join(
defaultCustomConfigDirPath,
"config.json"
);
// src/config/custom.ts
var JSON_OPTIONS = {
encoding: "utf8",
spaces: 2
};
async function createCustomConfig(params) {
const trimmedCliPath = params.cliPath.trim();
if (!trimmedCliPath) {
throw new Error("cliPath cannot be empty");
}
const normalizedCliPath = resolvePath(trimmedCliPath);
await import_fs_extra.default.ensureDir(defaultCustomConfigDirPath);
await import_fs_extra.default.writeJSON(
defaultCustomConfigFilePath,
{
cliPath: normalizedCliPath
},
JSON_OPTIONS
);
return normalizedCliPath;
}
// src/cli/prompt.ts
async function promptForCliPath() {
const rl = (0, import_promises.createInterface)({ input: import_node_process4.stdin, output: import_node_process4.stdout });
try {
logger_default.log("\u8BF7\u8BBE\u7F6E\u5FAE\u4FE1web\u5F00\u53D1\u8005\u5DE5\u5177 CLI \u7684\u8DEF\u5F84");
logger_default.log("> \u63D0\u793A\uFF1A\u547D\u4EE4\u884C\u5DE5\u5177\u9ED8\u8BA4\u6240\u5728\u4F4D\u7F6E\uFF1A");
logger_default.log("- MacOS: <\u5B89\u88C5\u8DEF\u5F84>/Contents/MacOS/cli");
logger_default.log("- Windows: <\u5B89\u88C5\u8DEF\u5F84>/cli.bat");
logger_default.log("- Linux: <\u5B89\u88C5\u8DEF\u5F84>/files/bin/bin/wechat-devtools-cli");
const cliPath = (await rl.question("\u8BF7\u8F93\u5165\u5FAE\u4FE1web\u5F00\u53D1\u8005\u5DE5\u5177 CLI \u8DEF\u5F84\uFF1A")).trim();
if (!cliPath) {
logger_default.error("\u8DEF\u5F84\u4E0D\u80FD\u4E3A\u7A7A\uFF0C\u5DF2\u53D6\u6D88\u672C\u6B21\u914D\u7F6E\u3002");
return null;
}
try {
const normalizedPath = await createCustomConfig({ cliPath });
logger_default.log(`\u5168\u5C40\u914D\u7F6E\u5B58\u50A8\u4F4D\u7F6E\uFF1A${defaultCustomConfigFilePath}`);
if (!await import_fs_extra2.default.pathExists(normalizedPath)) {
logger_default.warn("\u5728\u5F53\u524D\u8DEF\u5F84\u672A\u627E\u5230\u5FAE\u4FE1web\u5F00\u53D1\u8005\u547D\u4EE4\u884C\u5DE5\u5177\uFF0C\u8BF7\u786E\u8BA4\u8DEF\u5F84\u662F\u5426\u6B63\u786E\u3002");
}
return normalizedPath;
} catch (error) {
const reason = error instanceof Error ? error.message : String(error);
logger_default.error(`\u4FDD\u5B58\u914D\u7F6E\u5931\u8D25\uFF1A${reason}`);
return null;
}
} finally {
rl.close();
}
}
// src/cli/resolver.ts
var import_fs_extra5 = __toESM(require("fs-extra"), 1);
// src/config/resolver.ts
var import_fs_extra4 = __toESM(require("fs-extra"), 1);
// src/runtime/platform.ts
var import_node_os2 = __toESM(require("os"), 1);
var import_node_process5 = __toESM(require("process"), 1);
var import_fs_extra3 = __toESM(require("fs-extra"), 1);
var import_pathe3 = __toESM(require("pathe"), 1);
var SupportedPlatformsMap = {
Windows_NT: "Windows_NT",
Darwin: "Darwin",
Linux: "Linux"
};
function isOperatingSystemSupported(osName = import_node_os2.default.type()) {
return osName === SupportedPlatformsMap.Windows_NT || osName === SupportedPlatformsMap.Darwin || osName === SupportedPlatformsMap.Linux;
}
var operatingSystemName = import_node_os2.default.type();
function createLinuxCliResolver() {
let resolvedPath;
let attempted = false;
let pending = null;
return async () => {
if (attempted) {
return resolvedPath;
}
if (!pending) {
pending = (async () => {
try {
const envPath = await getFirstBinaryPath("wechat-devtools-cli");
if (envPath) {
resolvedPath = envPath;
}
} catch (error) {
const reason = error instanceof Error ? error.message : String(error);
logger_default.warn(`\u83B7\u53D6 Linux wechat-devtools-cli \u8DEF\u5F84\u5931\u8D25\uFF1A${reason}`);
} finally {
attempted = true;
}
return resolvedPath;
})();
}
return pending;
};
}
var linuxCliResolver = createLinuxCliResolver();
var WINDOWS_DEFAULT_CLI = "C:\\Program Files (x86)\\Tencent\\\u5FAE\u4FE1web\u5F00\u53D1\u8005\u5DE5\u5177\\cli.bat";
var DARWIN_DEFAULT_CLI = "/Applications/wechatwebdevtools.app/Contents/MacOS/cli";
var cliPathResolvers = {
[SupportedPlatformsMap.Windows_NT]: async () => WINDOWS_DEFAULT_CLI,
[SupportedPlatformsMap.Darwin]: async () => DARWIN_DEFAULT_CLI,
[SupportedPlatformsMap.Linux]: linuxCliResolver
};
async function getDefaultCliPath(targetOs = operatingSystemName) {
if (!isOperatingSystemSupported(targetOs)) {
return void 0;
}
const resolver = cliPathResolvers[targetOs];
const resolvedPath = await resolver();
return resolvedPath;
}
async function getFirstBinaryPath(command) {
const envPath = import_node_process5.default.env.PATH || "";
const pathDirs = envPath.split(import_pathe3.default.delimiter);
for (const dir of pathDirs) {
const fullPath = import_pathe3.default.join(dir, command);
try {
await import_fs_extra3.default.access(fullPath, import_fs_extra3.default.constants.X_OK);
return fullPath;
} catch {
continue;
}
}
return void 0;
}
// src/config/resolver.ts
async function getConfig() {
if (await import_fs_extra4.default.pathExists(defaultCustomConfigFilePath)) {
try {
const config = await import_fs_extra4.default.readJSON(defaultCustomConfigFilePath);
const cliPath = typeof config.cliPath === "string" ? config.cliPath.trim() : "";
if (cliPath) {
logger_default.log("> \u5168\u5C40\u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84\uFF1A", defaultCustomConfigFilePath);
logger_default.log("> \u81EA\u5B9A\u4E49 CLI \u8DEF\u5F84\uFF1A", cliPath);
return {
cliPath,
source: "custom"
};
}
logger_default.warn("\u81EA\u5B9A\u4E49\u914D\u7F6E\u6587\u4EF6\u7F3A\u5C11\u6709\u6548\u7684 CLI \u8DEF\u5F84\uFF0C\u5C06\u5C1D\u8BD5\u4F7F\u7528\u9ED8\u8BA4\u8DEF\u5F84\u3002");
} catch (error) {
const reason = error instanceof Error ? error.message : String(error);
logger_default.warn(`\u89E3\u6790\u81EA\u5B9A\u4E49\u914D\u7F6E\u5931\u8D25\uFF0C\u5C06\u5C1D\u8BD5\u4F7F\u7528\u9ED8\u8BA4\u8DEF\u5F84\u3002\u539F\u56E0\uFF1A${reason}`);
}
}
const fallbackPath = await getDefaultCliPath();
if (fallbackPath) {
return {
cliPath: fallbackPath,
source: "default"
};
}
return {
cliPath: "",
source: "missing"
};
}
// src/cli/resolver.ts
async function resolveCliPath() {
const config = await getConfig();
if (!config.cliPath) {
return { cliPath: null, source: config.source };
}
const exists = await import_fs_extra5.default.pathExists(config.cliPath);
return {
cliPath: exists ? config.cliPath : null,
source: config.source
};
}
// src/cli/run.ts
var ARG_TRANSFORMS = [
createAlias({ find: "-p", replacement: "--project" }),
createPathCompat("--result-output"),
createPathCompat("-r"),
createPathCompat("--qr-output"),
createPathCompat("-o"),
createPathCompat("--info-output"),
createPathCompat("-i")
];
async function parse(argv2) {
const head = argv2[0];
if (head && ["alipay", "ali", "minidev"].includes(head)) {
await runMinidev(argv2.slice(1));
return;
}
if (!isOperatingSystemSupported(operatingSystemName)) {
logger_default.log(`\u5FAE\u4FE1web\u5F00\u53D1\u8005\u5DE5\u5177\u4E0D\u652F\u6301\u5F53\u524D\u5E73\u53F0\uFF1A${operatingSystemName} !`);
return;
}
if (head === "config") {
await promptForCliPath();
return;
}
const { cliPath, source } = await resolveCliPath();
if (!cliPath) {
const message = source === "custom" ? "\u5728\u5F53\u524D\u81EA\u5B9A\u4E49\u8DEF\u5F84\u4E2D\u672A\u627E\u5230\u5FAE\u4FE1web\u5F00\u53D1\u8005\u547D\u4EE4\u884C\u5DE5\u5177\uFF0C\u8BF7\u91CD\u65B0\u6307\u5B9A\u8DEF\u5F84\u3002" : "\u672A\u68C0\u6D4B\u5230\u5FAE\u4FE1web\u5F00\u53D1\u8005\u547D\u4EE4\u884C\u5DE5\u5177\uFF0C\u8BF7\u6267\u884C `weapp-ide-cli config` \u6307\u5B9A\u8DEF\u5F84\u3002";
logger_default.log(message);
await promptForCliPath();
return;
}
const formattedArgv = transformArgv(argv2, ARG_TRANSFORMS);
await execute(cliPath, formattedArgv);
}
// src/cli.ts
var argv = import_node_process6.default.argv.slice(2);
parse(argv).catch((err) => {
logger_default.error(err);
});