forked from asdfjkl/kanjicanvas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from Ubugeeei/5-cli-tools
#5 cli tools
- Loading branch information
Showing
12 changed files
with
503 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.