Skip to content

Commit cad0387

Browse files
committed
feat: option to ignore schemas not valid JSON
1 parent 0570f54 commit cad0387

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@mikaello/avrodoc-plus",
33
"description": "Documentation tool for Avro schemas. Forked from https://github.com/leosilvadev/avrodoc-plus.",
4-
"version": "1.0.2",
4+
"version": "1.1.0",
55
"author": "mikaello https://github.com/mikaello",
66
"type": "module",
77
"bin": "./bin/avrodoc-plus.js",

src/avrodoc.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,23 @@ const avrodocDebug = debug("avrodoc:avrodoc");
1616
* @param {Array<string>} inputfiles an array with resolved filenames to be read and parsed and eventually added to the avrodoc
1717
* @param {string} outputfile the html file that should be written
1818
*/
19-
async function createAvroDoc(extra_less_files, inputfiles, outputfile) {
19+
async function createAvroDoc(
20+
extra_less_files,
21+
inputfiles,
22+
outputfile,
23+
ignoreInvalid
24+
) {
2025
avrodocDebug(`Creating ${outputfile} from `, inputfiles);
21-
let schemata = inputfiles.map(function (filename) {
22-
return {
23-
json: readJSON(filename),
24-
filename: filename,
25-
};
26-
});
26+
let schemata = inputfiles
27+
.map((filename) => {
28+
const json = readJSON(filename, ignoreInvalid);
29+
return json != null ? { json, filename } : null;
30+
})
31+
.filter((s) => s != null);
32+
2733
const html = await topLevelHTML(extra_less_files, {
2834
inline: true,
29-
schemata: schemata,
35+
schemata,
3036
});
3137
return await writeAvroDoc(outputfile, html);
3238
}
@@ -50,16 +56,20 @@ async function writeAvroDoc(output, html) {
5056
/**
5157
* Reads in the given file and parses as json
5258
* @param {string} filename to be read
59+
* @param {boolean} ignoreInvalid should we ignore invalid JSON files
5360
* @returns {object} with parsed AVRO
5461
*/
55-
function readJSON(filename) {
62+
function readJSON(filename, ignoreInvalid) {
5663
let json, parsed;
5764
avrodocDebug("Parsing ", filename);
5865
json = fs.readFileSync(path.resolve(process.cwd(), filename), "utf-8");
5966
try {
6067
parsed = JSON.parse(json);
6168
} catch (e) {
62-
console.error("Not a valid json file: " + filename);
69+
console.error("Not a valid JSON file: " + filename);
70+
if (ignoreInvalid) {
71+
return;
72+
}
6373
process.exit(1);
6474
}
6575
return parsed;

src/cli.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ const argv = arg({
2525

2626
"--style": String,
2727
"-s": "--style",
28+
29+
"--ignore-invalid": Boolean,
2830
});
2931

3032
let inputFiles = null;
3133
let outputFile = null;
34+
let ignoreInvalidSchemas = false;
3235

3336
// Determine list of input files file1.avsc file2.avsc
3437
if (argv["--input"]) {
@@ -46,14 +49,18 @@ if (argv["--output"]) {
4649

4750
const extra_less_files = argv["--style"] ? [argv["--style"]] : [];
4851

52+
if (argv["--ignore-invalid"]) {
53+
ignoreInvalidSchemas = true;
54+
}
55+
4956
//valid input?
5057
if (!inputFiles || inputFiles.length === 0 || outputFile === null) {
5158
console.error(
52-
"Usage: avrodoc [-i rootfolder] [my-schema.avsc [another-schema.avsc...]] [-o my-documentation.html] [-s my-style.less]"
59+
"Usage: avrodoc [-i rootfolder] [my-schema.avsc [another-schema.avsc...]] [-o my-documentation.html] [-s my-style.less] [--ignore-invalid]"
5360
);
5461
process.exit(1);
5562
}
56-
createAvroDoc(extra_less_files, inputFiles, outputFile);
63+
createAvroDoc(extra_less_files, inputFiles, outputFile, ignoreInvalidSchemas);
5764

5865
//private stuff
5966

0 commit comments

Comments
 (0)