updates
This commit is contained in:
125
cmd-ts/main.ts
125
cmd-ts/main.ts
@@ -1,125 +0,0 @@
|
|||||||
#!/usr/bin/env runts -- --allow-env --allow-run
|
|
||||||
|
|
||||||
import { spawn } from "node:child_process";
|
|
||||||
import {
|
|
||||||
existsPath,
|
|
||||||
log,
|
|
||||||
readFileToString,
|
|
||||||
resolveFilename,
|
|
||||||
} from "https://global.hatter.ink/script/get/@12/deno-commons-mod.ts";
|
|
||||||
|
|
||||||
const CMD_CONFIG_FILE = "~/.config/cmd-ts.json";
|
|
||||||
|
|
||||||
interface CmdConfig {
|
|
||||||
cmds?: Map<string, Cmd>;
|
|
||||||
groups?: Map<string, CmdGroup>;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface CmdGroup {
|
|
||||||
cmds?: Map<string, Cmd>;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Cmd {
|
|
||||||
command: string[];
|
|
||||||
comment?: string;
|
|
||||||
alias?: string[];
|
|
||||||
}
|
|
||||||
|
|
||||||
async function loadConfig(): Promise<CmdConfig> {
|
|
||||||
const resolvedFilename = resolveFilename(CMD_CONFIG_FILE);
|
|
||||||
if (!await existsPath(resolvedFilename)) {
|
|
||||||
throw `Cmd config file not found: ${resolvedFilename}`;
|
|
||||||
}
|
|
||||||
const config = await readFileToString(resolvedFilename);
|
|
||||||
return JSON.parse(config) as CmdConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function showHelp(args: string[]) {
|
|
||||||
const cmdConfig = await loadConfig();
|
|
||||||
log.info(cmdConfig); // TODO ...
|
|
||||||
}
|
|
||||||
|
|
||||||
async function findCmd(
|
|
||||||
cmdConfig: CmdConfig,
|
|
||||||
c1: string,
|
|
||||||
c2: string | null,
|
|
||||||
): Cmd {
|
|
||||||
if (c2 === null) {
|
|
||||||
const cmds = [];
|
|
||||||
if (cmdConfig.cmds && cmdConfig.cmds[c1]) {
|
|
||||||
cmds.push(cmdConfig.cmds[c1]);
|
|
||||||
}
|
|
||||||
if (cmdConfig.groups && cmdConfig.groups[c1]) {
|
|
||||||
if (cmds.length === 0) {
|
|
||||||
await showHelp([c1]);
|
|
||||||
return null;
|
|
||||||
} else {
|
|
||||||
throw `Multiple cmd or group found: ${c1}`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (cmds.length === 0) {
|
|
||||||
throw `No cmd found: ${c1}`;
|
|
||||||
}
|
|
||||||
return cmds[0];
|
|
||||||
} else {
|
|
||||||
const cmds = cmdConfig.groups[c1];
|
|
||||||
if (!cmds) {
|
|
||||||
throw `No group found: ${c1}`;
|
|
||||||
}
|
|
||||||
const cmd = cmds.cmds[c2];
|
|
||||||
if (!cmd) {
|
|
||||||
throw `No group and cmd found: ${c1}:${c2}`;
|
|
||||||
}
|
|
||||||
return cmd;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function splitCmd(cmd: string): {
|
|
||||||
c1: string;
|
|
||||||
c2: string;
|
|
||||||
} {
|
|
||||||
const cmdCIndex = cmd.indexOf(":");
|
|
||||||
if (cmdCIndex >= 0) {
|
|
||||||
return {
|
|
||||||
c1: cmd.substring(0, cmdCIndex),
|
|
||||||
c2: cmd.substring(cmdCIndex + 1),
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
return {
|
|
||||||
c1: cmd,
|
|
||||||
c2: null,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function main() {
|
|
||||||
if (Deno.args.length === 0) {
|
|
||||||
await showHelp([]);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
log.info("Arguments", Deno.args);
|
|
||||||
const cmdConfig = await loadConfig();
|
|
||||||
const { c1, c2 } = splitCmd(Deno.args[0]);
|
|
||||||
const cmd = await findCmd(cmdConfig, c1, c2);
|
|
||||||
if (cmd === null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const command = cmd.command[0];
|
|
||||||
const args = [];
|
|
||||||
if (cmd.command.length > 1) {
|
|
||||||
for (let i = 1; i < cmd.command.length; i++) {
|
|
||||||
args.push(cmd.command[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (Deno.args.length > 1) {
|
|
||||||
for (let i = 1; i < Deno.args.length; i++) {
|
|
||||||
args.push(Deno.args[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
log.info("Execute command", command, args);
|
|
||||||
spawn(command, args, {
|
|
||||||
shell: true,
|
|
||||||
stdio: ["inherit", "inherit", "inherit"],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
await main();
|
|
||||||
@@ -43,14 +43,6 @@
|
|||||||
"publish_time": 1737306227061,
|
"publish_time": 1737306227061,
|
||||||
"update_time": 1770820648011
|
"update_time": 1770820648011
|
||||||
},
|
},
|
||||||
"cmd.ts": {
|
|
||||||
"script_name": "cmd.ts",
|
|
||||||
"script_length": 3076,
|
|
||||||
"script_sha256": "ca4a0193ca760b126e8471ccac1c73928b5dab79da5f89f198936e799032dab3",
|
|
||||||
"script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/cmd-ts/main.ts",
|
|
||||||
"publish_time": 1769009291639,
|
|
||||||
"update_time": 1769009291639
|
|
||||||
},
|
|
||||||
"commit-bun.ts": {
|
"commit-bun.ts": {
|
||||||
"script_name": "commit-bun.ts",
|
"script_name": "commit-bun.ts",
|
||||||
"script_length": 10363,
|
"script_length": 10363,
|
||||||
|
|||||||
@@ -4,11 +4,6 @@
|
|||||||
"script_length": 5917,
|
"script_length": 5917,
|
||||||
"script_sha256": "2b7efec1c473856e156bea9c8c9ce526c217138b8813b8706600808e895ce0f5"
|
"script_sha256": "2b7efec1c473856e156bea9c8c9ce526c217138b8813b8706600808e895ce0f5"
|
||||||
},
|
},
|
||||||
"cmd-ts": {
|
|
||||||
"script_name": "cmd-ts",
|
|
||||||
"script_length": 3076,
|
|
||||||
"script_sha256": "ca4a0193ca760b126e8471ccac1c73928b5dab79da5f89f198936e799032dab3"
|
|
||||||
},
|
|
||||||
"python-ts": {
|
"python-ts": {
|
||||||
"script_name": "python-ts",
|
"script_name": "python-ts",
|
||||||
"script_length": 16778,
|
"script_length": 16778,
|
||||||
|
|||||||
Reference in New Issue
Block a user