-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.ts
More file actions
79 lines (74 loc) · 1.95 KB
/
command.ts
File metadata and controls
79 lines (74 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { readFile } from "fs/promises";
import {
Format,
FORMATS,
getFileFormatFromName,
OFFICE_VERSIONS,
OfficeVersion,
ValidationResult,
} from "./index";
import validate from "./index";
import { ArgumentsCamelCase, Argv } from "yargs";
import chalk from "chalk";
function consolePrintErrors(errors: ValidationResult[]) {
if (errors.length > 0) {
for (const error of errors) {
console.log(chalk.blue(`.${error.path.partUri}/${error.path.xpath}`));
console.log(`└─ ${chalk.red(error.id)}: ${error.description}`);
}
console.log("");
}
console.log(
chalk[errors.length > 0 ? "red" : "green"](
`Found ${chalk.bold(errors.length)} errors`,
),
);
}
export const cmd = "$0 <ooxmlpath>";
export const desc = "validate docx files";
export const builder = (yargs: Argv) => {
yargs
.positional("ooxmlpath", {
describe: "filepath of OOXML file",
type: "string",
})
.option("office-version", {
alias: "ov",
describe: "office version used for validation",
choices: OFFICE_VERSIONS,
default: OFFICE_VERSIONS[0],
})
.option("output-format", {
alias: "of",
describe: "format of output",
choices: ["pretty", "json"],
default: "pretty",
})
.option("format", {
alias: "f",
describe: "document format (auto-detected from file extension)",
choices: FORMATS,
})
.demandOption(["ooxmlpath"]);
};
export async function handler({
ooxmlpath,
format,
officeVersion,
outputFormat,
}: ArgumentsCamelCase<{
ooxmlpath: string;
format: Format;
officeVersion: OfficeVersion;
outputFormat: string;
}>) {
const file = await readFile(ooxmlpath);
const parsedFormat = format ?? getFileFormatFromName(ooxmlpath);
const results = await validate(file, parsedFormat, officeVersion);
if (outputFormat === "json") {
console.log(JSON.stringify(results, null, 2));
} else {
consolePrintErrors(results);
}
process.exit(0);
}