Skip to content

Commit

Permalink
Accept a directory of md files
Browse files Browse the repository at this point in the history
  • Loading branch information
stasm committed Jul 31, 2019
1 parent 29899d9 commit d4015be
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
27 changes: 18 additions & 9 deletions format/lib/input.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from "fs";
import path from "path";
import readline from "readline";

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

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

if (typeof content === "string") {
callback(content);
}
});
export function* files(destination: string, ext: string) {
let files;
if (destination.endsWith(ext)) {
files = [destination];
} else {
files = fs
.readdirSync(destination)
.filter(filename => filename.endsWith(ext))
.map(filename => path.join(destination, filename));
}

for (let file of files) {
yield file;
}
}
10 changes: 4 additions & 6 deletions format/lib/validate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {deepStrictEqual} from "assert";
import {execSync} from "child_process";
import {Parser as MarkdownParser} from "commonmark";
import {diffString} from "json-diff";

export function validate(exe: string, spec: string, source: string) {
type formatFn = (stdin: string) => string;

export function validate(fn: formatFn, spec: string, source: string) {
// https://github.com/commonmark/commonmark.js/blob/master/README.md#usage
let reader = new MarkdownParser();
let parsed = reader.parse(source);
Expand All @@ -16,10 +17,7 @@ export function validate(exe: string, spec: string, source: string) {
let {entering, node} = current;
if (entering && node.type === "code_block" && node.literal !== null) {
if (node.info === "properties") {
actual = execSync(exe, {
input: node.literal,
encoding: "utf8",
});
actual = fn(node.literal);
} else if (node.info === "json" && actual) {
let expected = node.literal;

Expand Down
3 changes: 2 additions & 1 deletion format/tools/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const [filePath] = argv._;
if (filePath === "-") {
fromStdin(print);
} else if (filePath) {
fromFile(filePath, print);
let source = fromFile(filePath);
print(source);
} else {
exitHelp(1);
}
Expand Down
15 changes: 13 additions & 2 deletions format/tools/validate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {execSync} from "child_process";
import parseArgs from "minimist";
import {fromFile} from "../lib/input";
import {files, fromFile} from "../lib/input";
import {validate} from "../lib/validate";

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

let [exePath, mdPath] = argv._;
if (exePath && mdPath) {
fromFile(mdPath, source => validate(exePath, mdPath, source));
for (let file of files(mdPath, ".md")) {
let source = fromFile(file);
validate(format, file, source);
}
} else if (argv.help) {
exitHelp(0);
} else {
exitHelp(1);
}

function format(input: string) {
return execSync(exePath, {
input,
encoding: "utf8",
});
}

function exitHelp(exitCode: number) {
console.log(`
Usage: node validate.js <EXECUTABLE> <SPEC>
Expand Down

0 comments on commit d4015be

Please sign in to comment.