@thingjs/cli
Version:
<div align="center"> <a href="https://www.thingjs.com/guide/"><img width="200" src="https://www.thingjs.com/static/images/avatar.png"/></a> </div>
145 lines (142 loc) • 4.11 kB
JavaScript
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import fs from 'node:fs';
import { Command } from 'commander';
import { prepareCheck, log } from '@thingjs/cli-common';
const name = "@thingjs/cli";
const version = "0.14.0";
const description = "";
const files = [
"dist"
];
const bin = {
thing: "./bin/thing.js"
};
const main$1 = "./dist/index.mjs";
const module = "./dist/index.mjs";
const types = "./dist/index.d.ts";
const type = "module";
const scripts = {
dev: "unbuild --stub",
build: "unbuild",
prepublishOnly: "npm run build"
};
const keywords = [
"uinosoft",
"uino",
"thing",
"thingjs",
"thing.js",
"cli"
];
const author = "";
const license = "BSD-3-Clause";
const dependencies = {
"@thingjs/cli-common": "workspace:^",
"@thingjs/cli-code-temp": "workspace:^",
commander: "^12.0.0",
execa: "^7.2.0",
inquirer: "^9.1.0"
};
const devDependencies = {
"@types/inquirer": "^9.0.1"
};
const engines = {
node: ">=18"
};
const pkg = {
name: name,
version: version,
description: description,
files: files,
bin: bin,
main: main$1,
module: module,
types: types,
type: type,
scripts: scripts,
keywords: keywords,
author: author,
license: license,
dependencies: dependencies,
devDependencies: devDependencies,
engines: engines
};
const program = new Command();
const PrefixPkgName = pkg.name.split("/")[0];
async function main() {
try {
listenGlobalError();
await prepareCheck({
nodeVersion: pkg.engines.node
});
registerCommand();
} catch (e) {
if (e instanceof Error) {
log.error("cli", e.message);
}
}
}
function listenGlobalError() {
process.on("unhandledRejection", (reason, p) => {
log.error("Unhandled Rejection at Promise", reason, p);
}).on("uncaughtException", (err) => {
log.error("Uncaught Exception thrown", err.message);
process.exit(1);
});
}
function registerCommand() {
program.command("create [projectName]").option("-f, --force", "Force a directory overwrite").option("-t, --template <templateType>", "Create an editor project").description("Create a new project powered by thing-cli").action(exec);
program.command("list").description(`list the thingjs versions`).alias("ls").action(exec);
program.command("install [thingjsVersion]").description(`install the thingjs version`).alias("i").action(exec);
program.commands.forEach((command) => {
command.option("-d, --debug", "debug");
});
program.helpOption("-h, --help", "display help for command").addHelpCommand(false);
program.version(pkg.version, "-v, --version", "check the version number");
program.on("command:*", (obj) => {
const availableCommands = program.commands.map((cmd) => cmd.name());
log.error("cli", `unknown command ${obj[0]}`);
if (availableCommands.length > 0) {
log.error("cli", `available command ${availableCommands.join(",")}`);
}
});
program.parse(process.argv);
}
function exec(...args) {
const command = args[args.length - 1];
const commandName = command.name() || "";
const commandOpts = command.opts() || {};
const commandArgs = command.args || [];
const pkgMap = {
create: `${PrefixPkgName}/cli-code-temp`,
list: `./commands/list`,
install: `./commands/install`
};
let execPkg = pkgMap[commandName];
if (execPkg) {
if (execPkg.startsWith("./")) {
const dirname = path.dirname(fileURLToPath(import.meta.url));
execPkg = !fs.existsSync(path.resolve(dirname, execPkg + ".mjs")) ? execPkg : execPkg + ".mjs";
execPkg = pathToFileURL(path.resolve(dirname, execPkg)).href;
}
import(execPkg).then((module = {}) => {
const commandFn = module.default;
if (typeof commandFn === "function") {
const context = {
command: {
name: commandName,
opts: commandOpts,
args: commandArgs
}
};
commandFn(context);
} else {
log.error("cli", `${execPkg} should export a default function`);
}
});
} else {
log.error("cli", `Cannot find command package ${commandName}`);
}
}
main();