Skip to content

Commit d4015be

Browse files
committed
Accept a directory of md files
1 parent 29899d9 commit d4015be

File tree

4 files changed

+37
-18
lines changed

4 files changed

+37
-18
lines changed

format/lib/input.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from "fs";
2+
import path from "path";
23
import readline from "readline";
34

45
export function fromStdin(callback: (value: string) => void) {
@@ -14,14 +15,22 @@ export function fromStdin(callback: (value: string) => void) {
1415
rl.on("close", () => callback(lines.join("\n") + "\n"));
1516
}
1617

17-
export function fromFile(filePath: string, callback: (value: string) => void) {
18-
fs.readFile(filePath, "utf8", (err: NodeJS.ErrnoException | null, content: string | Buffer) => {
19-
if (err) {
20-
throw err;
21-
}
18+
export function fromFile(filePath: string) {
19+
return fs.readFileSync(filePath, "utf8");
20+
}
2221

23-
if (typeof content === "string") {
24-
callback(content);
25-
}
26-
});
22+
export function* files(destination: string, ext: string) {
23+
let files;
24+
if (destination.endsWith(ext)) {
25+
files = [destination];
26+
} else {
27+
files = fs
28+
.readdirSync(destination)
29+
.filter(filename => filename.endsWith(ext))
30+
.map(filename => path.join(destination, filename));
31+
}
32+
33+
for (let file of files) {
34+
yield file;
35+
}
2736
}

format/lib/validate.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {deepStrictEqual} from "assert";
2-
import {execSync} from "child_process";
32
import {Parser as MarkdownParser} from "commonmark";
43
import {diffString} from "json-diff";
54

6-
export function validate(exe: string, spec: string, source: string) {
5+
type formatFn = (stdin: string) => string;
6+
7+
export function validate(fn: formatFn, spec: string, source: string) {
78
// https://github.com/commonmark/commonmark.js/blob/master/README.md#usage
89
let reader = new MarkdownParser();
910
let parsed = reader.parse(source);
@@ -16,10 +17,7 @@ export function validate(exe: string, spec: string, source: string) {
1617
let {entering, node} = current;
1718
if (entering && node.type === "code_block" && node.literal !== null) {
1819
if (node.info === "properties") {
19-
actual = execSync(exe, {
20-
input: node.literal,
21-
encoding: "utf8",
22-
});
20+
actual = fn(node.literal);
2321
} else if (node.info === "json" && actual) {
2422
let expected = node.literal;
2523

format/tools/format.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const [filePath] = argv._;
2121
if (filePath === "-") {
2222
fromStdin(print);
2323
} else if (filePath) {
24-
fromFile(filePath, print);
24+
let source = fromFile(filePath);
25+
print(source);
2526
} else {
2627
exitHelp(1);
2728
}

format/tools/validate.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import {execSync} from "child_process";
12
import parseArgs from "minimist";
2-
import {fromFile} from "../lib/input";
3+
import {files, fromFile} from "../lib/input";
34
import {validate} from "../lib/validate";
45

56
const argv = parseArgs(process.argv.slice(2), {
@@ -11,13 +12,23 @@ const argv = parseArgs(process.argv.slice(2), {
1112

1213
let [exePath, mdPath] = argv._;
1314
if (exePath && mdPath) {
14-
fromFile(mdPath, source => validate(exePath, mdPath, source));
15+
for (let file of files(mdPath, ".md")) {
16+
let source = fromFile(file);
17+
validate(format, file, source);
18+
}
1519
} else if (argv.help) {
1620
exitHelp(0);
1721
} else {
1822
exitHelp(1);
1923
}
2024

25+
function format(input: string) {
26+
return execSync(exePath, {
27+
input,
28+
encoding: "utf8",
29+
});
30+
}
31+
2132
function exitHelp(exitCode: number) {
2233
console.log(`
2334
Usage: node validate.js <EXECUTABLE> <SPEC>

0 commit comments

Comments
 (0)