kordoc 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js ADDED
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ detectFormat,
4
+ parse
5
+ } from "./chunk-P2BZDRLZ.js";
6
+
7
+ // src/cli.ts
8
+ import { readFileSync, writeFileSync } from "fs";
9
+ import { basename, resolve } from "path";
10
+ import { Command } from "commander";
11
+ var program = new Command();
12
+ program.name("kordoc").description("\uBAA8\uB450 \uD30C\uC2F1\uD574\uBC84\uB9AC\uACA0\uB2E4 \u2014 HWP, HWPX, PDF \u2192 Markdown").version("0.1.0").argument("<files...>", "\uBCC0\uD658\uD560 \uD30C\uC77C \uACBD\uB85C (HWP, HWPX, PDF)").option("-o, --output <path>", "\uCD9C\uB825 \uD30C\uC77C \uACBD\uB85C (\uB2E8\uC77C \uD30C\uC77C \uC2DC)").option("-d, --out-dir <dir>", "\uCD9C\uB825 \uB514\uB809\uD1A0\uB9AC (\uB2E4\uC911 \uD30C\uC77C \uC2DC)").option("--format <type>", "\uCD9C\uB825 \uD615\uC2DD: markdown (\uAE30\uBCF8) \uB610\uB294 json", "markdown").option("--silent", "\uC9C4\uD589 \uBA54\uC2DC\uC9C0 \uC228\uAE30\uAE30").action(async (files, opts) => {
13
+ for (const filePath of files) {
14
+ const absPath = resolve(filePath);
15
+ const fileName = basename(absPath);
16
+ try {
17
+ const buffer = readFileSync(absPath);
18
+ const arrayBuffer = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
19
+ const format = detectFormat(arrayBuffer);
20
+ if (!opts.silent) {
21
+ process.stderr.write(`[kordoc] ${fileName} (${format}) ...`);
22
+ }
23
+ const result = await parse(arrayBuffer);
24
+ if (!result.success) {
25
+ process.stderr.write(` FAIL
26
+ `);
27
+ process.stderr.write(` \u2192 ${result.error}
28
+ `);
29
+ process.exitCode = 1;
30
+ continue;
31
+ }
32
+ if (!opts.silent) process.stderr.write(` OK
33
+ `);
34
+ const output = opts.format === "json" ? JSON.stringify(result, null, 2) : result.markdown || "";
35
+ if (opts.output && files.length === 1) {
36
+ writeFileSync(opts.output, output, "utf-8");
37
+ if (!opts.silent) process.stderr.write(` \u2192 ${opts.output}
38
+ `);
39
+ } else if (opts.outDir) {
40
+ const outPath = resolve(opts.outDir, fileName.replace(/\.[^.]+$/, ".md"));
41
+ writeFileSync(outPath, output, "utf-8");
42
+ if (!opts.silent) process.stderr.write(` \u2192 ${outPath}
43
+ `);
44
+ } else {
45
+ process.stdout.write(output + "\n");
46
+ }
47
+ } catch (err) {
48
+ process.stderr.write(`
49
+ [kordoc] ERROR: ${fileName} \u2014 ${err instanceof Error ? err.message : err}
50
+ `);
51
+ process.exitCode = 1;
52
+ }
53
+ }
54
+ });
55
+ program.parse();