Skip to content

Commit

Permalink
Merge pull request #33 from Ubugeeei/5-cli-tools
Browse files Browse the repository at this point in the history
#5 cli tools
  • Loading branch information
ubugeeei authored Oct 15, 2023
2 parents 409536a + 7bbdccd commit 6fd61f3
Show file tree
Hide file tree
Showing 12 changed files with 503 additions and 15 deletions.
16 changes: 15 additions & 1 deletion cspell.config.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
module.exports = {
version: "0.2",
language: "en",
words: ["aran", "classfication", "dominik", "drawio", "hono", "outdir", "tegaki", "ubugeeei", "xmls"],
words: [
"aran",
"classfication",
"citty",
"consola",
"dominik",
"datagen",
"drawio",
"hono",
"outdir",
"tegaki",
"ubugeeei",
"unicodegen",
"xmls",
],
dictionaries: [
"softwareTerms",
"misc",
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,23 @@
"play": "cd examples/_basic && pnpm run dev"
},
"devDependencies": {
"@types/adm-zip": "^0.5.2",
"@types/node": "^20.8.6",
"cspell": "^7.3.8",
"esbuild": "^0.19.4",
"eslint": "^8.51.0",
"eslint-config-prettier": "^9.0.0",
"husky": "^8.0.3",
"prettier": "^3.0.3",
"rimraf": "^5.0.5",
"typescript": "^5.2.2"
},
"volta": {
"node": "18.18.2"
},
"dependencies": {
"adm-zip": "^0.5.10",
"citty": "^0.1.4",
"consola": "^3.2.3"
}
}
38 changes: 38 additions & 0 deletions packages/cli/bin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { defineCommand, runMain, runCommand } from "citty";
import { unicodegen } from "./cmd/unicodegen";
import { openJTegaki } from "./cmd/jTegaki";
import { datagen } from "./cmd/datagen";

const main = defineCommand({
meta: { name: "tegaki cli tools" },
async run({ rawArgs }) {
const [command] = rawArgs;
switch (command) {
case "unicodegen":
await runCommand(unicodegen, {
rawArgs: rawArgs.slice(1),
});
break;
case "jTegaki":
await runCommand(openJTegaki, {
rawArgs: rawArgs.slice(1),
});
break;
case "datagen":
await runCommand(datagen, {
rawArgs: rawArgs.slice(1),
});
break;
default: {
console.log(`Usage: tegaki <command> [options]
available commands:
- unicodegen
- jTegaki
- datagen`);
break;
}
}
},
});

runMain(main);
File renamed without changes.
64 changes: 64 additions & 0 deletions packages/cli/cmd/datagen/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { defineCommand } from "citty";
import consola from "consola";
import path from "node:path";

const PYTHON_SCRIPT = path.join(__dirname, "datagen.py");

export const datagen = defineCommand({
meta: {
name: "datagen",
description: "generate data from xml stroke data",
},
run: async () => {
try {
const py = await getPythonCmd();
if (!py) {
consola.error("python not found");
return;
}
const { spawn } = await import("child_process");
const child = spawn(py, [PYTHON_SCRIPT]);
child.stdout.on("data", data => {
console.log(data.toString());
});
child.stderr.on("data", data => {
console.log(data.toString());
});
child.on("close", code => {
console.log(`child process exited with code ${code}`);
});
} catch (e) {
process.exit(1);
}
},
});

const getPythonCmd = async (): Promise<"python" | "python3" | null> => {
let python = "python";
const { exec } = await import("child_process");

try {
const { stdout } = exec(`which ${python}`);
if (!stdout) throw new Error("command not found: python");

const { stdout: version } = exec(`${python} --version`);
if (!version?.toString().startsWith("Python 3")) {
throw new Error("python version is not 3");
}
consola.info(`Using ${version}`);
return "python";
} catch (err) {
try {
// pythonが見つからない場合、python3を探す
const { stdout } = exec(`which python3`);
if (stdout) {
return "python3";
} else {
consola.error("command not found: python or python3");
throw err;
}
} catch (err) {
return null;
}
}
};
36 changes: 36 additions & 0 deletions packages/cli/cmd/jTegaki/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import path from "node:path";
import AdmZip from "adm-zip";
import { defineCommand } from "citty";

const J_TEGAKI_ZIP_PATH = path.join(__dirname, "./jTegaki.zip");
const J_TEGAKI_DIR_PATH = path.join(__dirname, "./jTegaki.jar");

export const openJTegaki = defineCommand({
meta: { name: "jTegaki", description: "open jTegaki GUI Took" },
run: async () => {
await unzipJTegaki();
await openJar();
},
});

const openJar = async () => {
const { spawn } = await import("child_process");
const child = spawn("java", ["-jar", J_TEGAKI_DIR_PATH]);
child.stdout.on("data", data => {
console.log(data.toString());
});
child.stderr.on("data", data => {
console.log(data.toString());
});
child.on("close", code => {
console.log(`child process exited with code ${code}`);
});
child.on("error", err => {
console.error(err);
});
};

const unzipJTegaki = async () => {
const zip = new AdmZip(J_TEGAKI_ZIP_PATH);
zip.extractAllTo(path.join(__dirname, "."), true);
};
Binary file added packages/cli/cmd/jTegaki/jTegaki.zip
Binary file not shown.
47 changes: 47 additions & 0 deletions packages/cli/cmd/unicodegen/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { consola } from "consola";
import { defineCommand } from "citty";

export const unicodegen = defineCommand({
meta: { name: "unicodegen", description: "generate unicode" },
args: {
map: { type: "boolean", alias: ["m"], description: "print map char and unicode map", required: false },
separator: { type: "string", alias: ["s"], description: "separator", default: "\n" },
},
run: ({ args }) => {
const { map: isMap, separator, _: chars } = args;
let buffer = "";
if (isMap) {
buffer += genMap("char", "unicode");
buffer += "\n";
chars.forEach((char: string) => {
if (char.length !== 1) {
consola.error(`char length is not 1: ${char}`);
return;
}
buffer += genMap(char, getHexUnicode(char));
buffer += "\n";
});
} else {
chars.forEach((char: string) => {
if (char.length !== 1) {
consola.error(`char length is not 1: ${char}`);
return;
}
buffer += getHexUnicode(char);
buffer += separator;
});
}

console.log(buffer);
},
});

const getHexUnicode = (char: string) => {
const codePoint = char.codePointAt(0);
if (!codePoint) {
throw new Error(`codePoint is null: ${char}`);
}
return codePoint.toString(16).toUpperCase();
};

const genMap = (s1: string, s2: string) => `${s1}\t${s2}`;
12 changes: 0 additions & 12 deletions packages/cli/read_xml.py

This file was deleted.

38 changes: 38 additions & 0 deletions packages/cli/utils/ansi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export const ansi = {
/** font color */
c: {
black: (s: string) => `\x1b[30m${s}\x1b[0m`,
red: (s: string) => `\x1b[31m${s}\x1b[0m`,
green: (s: string) => `\x1b[32m${s}\x1b[0m`,
yellow: (s: string) => `\x1b[33m${s}\x1b[0m`,
blue: (s: string) => `\x1b[34m${s}\x1b[0m`,
magenta: (s: string) => `\x1b[35m${s}\x1b[0m`,
cyan: (s: string) => `\x1b[36m${s}\x1b[0m`,
white: (s: string) => `\x1b[37m${s}\x1b[0m`,
gray: (s: string) => `\x1b[90m${s}\x1b[0m`,
grey: (s: string) => `\x1b[90m${s}\x1b[0m`,
},

/** background color */
bgc: {
black: (s: string) => `\x1b[40m${s}\x1b[0m`,
red: (s: string) => `\x1b[41m${s}\x1b[0m`,
green: (s: string) => `\x1b[42m${s}\x1b[0m`,
yellow: (s: string) => `\x1b[43m${s}\x1b[0m`,
blue: (s: string) => `\x1b[44m${s}\x1b[0m`,
magenta: (s: string) => `\x1b[45m${s}\x1b[0m`,
cyan: (s: string) => `\x1b[46m${s}\x1b[0m`,
white: (s: string) => `\x1b[47m${s}\x1b[0m`,
},

// font styles
f: {
bold: (s: string) => `\x1b[1m${s}\x1b[0m`,
dim: (s: string) => `\x1b[2m${s}\x1b[0m`,
italic: (s: string) => `\x1b[3m${s}\x1b[0m`,
underline: (s: string) => `\x1b[4m${s}\x1b[0m`,
blink: (s: string) => `\x1b[5m${s}\x1b[0m`,
reverse: (s: string) => `\x1b[7m${s}\x1b[0m`,
hidden: (s: string) => `\x1b[8m${s}\x1b[0m`,
},
} as const;
Loading

0 comments on commit 6fd61f3

Please sign in to comment.