document-cli 0.0.0 → 1.0.1
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/README.md +186 -0
- package/dist/cli.js +615 -0
- package/dist/index.cjs +664 -0
- package/dist/index.d.cts +32 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +651 -0
- package/dist/io-Cl3MaB0L.js +81 -0
- package/dist/tui-CucYgqmL.js +5647 -0
- package/package.json +101 -2
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
2
|
+
import "documents.js";
|
|
3
|
+
import { basename, dirname, extname, join } from "node:path";
|
|
4
|
+
//#region src/format.ts
|
|
5
|
+
const EXTENSION_TO_FORMAT = {
|
|
6
|
+
docx: "docx",
|
|
7
|
+
pptx: "pptx",
|
|
8
|
+
xlsx: "xlsx",
|
|
9
|
+
odt: "odt",
|
|
10
|
+
odp: "odp",
|
|
11
|
+
ods: "ods",
|
|
12
|
+
odg: "odg",
|
|
13
|
+
odf: "odf",
|
|
14
|
+
pdf: "pdf"
|
|
15
|
+
};
|
|
16
|
+
const FORMAT_TO_EXTENSION = {
|
|
17
|
+
docx: "docx",
|
|
18
|
+
pptx: "pptx",
|
|
19
|
+
xlsx: "xlsx",
|
|
20
|
+
odt: "odt",
|
|
21
|
+
odp: "odp",
|
|
22
|
+
ods: "ods",
|
|
23
|
+
odg: "odg",
|
|
24
|
+
odf: "odf",
|
|
25
|
+
pdf: "pdf"
|
|
26
|
+
};
|
|
27
|
+
function isDocumentFormat(value) {
|
|
28
|
+
return value in FORMAT_TO_EXTENSION;
|
|
29
|
+
}
|
|
30
|
+
function inferFormatFromExtension(path) {
|
|
31
|
+
if (path === "-") return;
|
|
32
|
+
const lastSegment = path.split(/[/\\]/).pop() ?? path;
|
|
33
|
+
const dotIndex = lastSegment.lastIndexOf(".");
|
|
34
|
+
if (dotIndex <= 0) return;
|
|
35
|
+
const extension = lastSegment.slice(dotIndex + 1).toLowerCase();
|
|
36
|
+
return EXTENSION_TO_FORMAT[extension];
|
|
37
|
+
}
|
|
38
|
+
function formatToExtension(format) {
|
|
39
|
+
return FORMAT_TO_EXTENSION[format];
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/runtime/io.ts
|
|
43
|
+
function isUint8Array(value) {
|
|
44
|
+
return value instanceof Uint8Array;
|
|
45
|
+
}
|
|
46
|
+
async function readStdin(signal) {
|
|
47
|
+
const chunks = [];
|
|
48
|
+
for await (const chunk of process.stdin) {
|
|
49
|
+
if (signal?.aborted === true) throw new Error("Reading from stdin was aborted");
|
|
50
|
+
if (!isUint8Array(chunk)) throw new Error("Unexpected non-buffer chunk read from stdin");
|
|
51
|
+
chunks.push(chunk);
|
|
52
|
+
}
|
|
53
|
+
return new Uint8Array(Buffer.concat(chunks));
|
|
54
|
+
}
|
|
55
|
+
async function readInput(pathOrDash, options) {
|
|
56
|
+
if (pathOrDash === "-") return readStdin(options?.signal);
|
|
57
|
+
const buffer = await readFile(pathOrDash, { signal: options?.signal });
|
|
58
|
+
return new Uint8Array(buffer);
|
|
59
|
+
}
|
|
60
|
+
async function writeOutput(pathOrDash, bytes) {
|
|
61
|
+
if (pathOrDash === "-") {
|
|
62
|
+
await new Promise((resolve, reject) => {
|
|
63
|
+
process.stdout.write(bytes, (error) => {
|
|
64
|
+
if (error) {
|
|
65
|
+
reject(error);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
resolve();
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
await writeFile(pathOrDash, bytes);
|
|
74
|
+
}
|
|
75
|
+
function resolveDefaultOutputPath(inputPath, targetFormat) {
|
|
76
|
+
const directory = dirname(inputPath);
|
|
77
|
+
const stem = basename(inputPath, extname(inputPath));
|
|
78
|
+
return join(directory, `${stem}.${formatToExtension(targetFormat)}`);
|
|
79
|
+
}
|
|
80
|
+
//#endregion
|
|
81
|
+
export { inferFormatFromExtension as a, formatToExtension as i, resolveDefaultOutputPath as n, isDocumentFormat as o, writeOutput as r, readInput as t };
|