Skip to content

Commit

Permalink
docs: improve documentation and input validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaello committed Apr 2, 2022
1 parent 8d9e7a0 commit 4024e76
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 38 deletions.
39 changes: 25 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,31 @@ Or without installation:
npx @mikaello/avrodoc-plus -i source -o out.html
```

### Options

- -i _sourcefolder_
Pass in a source folder that will recursively parsed and crawled for avsc files
- -o _outputfile_
The file where the generated doc should be written to
- --title _Avrodoc for ACME_
The title that will be used in the generated HTML page, deafults to _Avrodoc_.
- -s _external stylesheet less file_
Your own less file, used to override specific style of your generated page
- --ignore-invalid
Ignore avsc files that can not be parsed as JSON (instead of quiting)

### Enhancements
## Options

```text
USAGE:
avrodoc [FLAGS] [OPTIONS] [AVRO FILES...]
FLAGS:
--ignore-invalid Ignore avsc files that can not be parsed as JSON (instead of quiting)
OPTIONS:
-i, --input <folder> Pass in a source folder that will recursively parsed and crawled for avsc files
-o, --output <file> The file where the generated doc should be written to
--title <title> The title that will be used in the generated HTML page, deafults to "Avrodoc".
-s, --style <file> Your own less file, used to override specific style of your generated page
ARGS:
<AVRO FILES>... If not --input is given, you can specify individual AVRO files here
EXAMPLES:
avrodoc --ignore-invalid --input ./schemas --output avrodoc.html --title "My First Avrodoc"
avrodoc --output avro.html --style my-styles.less avro_schema1.avsc avro_schema2.avsc avro_schema3.avsc
```

## Enhancements

- support for input folders
- support of schema/type search (search by namespace and/or schema/type)
22 changes: 17 additions & 5 deletions public/js/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,13 @@ AvroDoc.Schema = function (avrodoc, shared_types, schema_json, filename) {
}
}

// Check type of object. Get all additional arbitrary attributes on the object
// for inclusion in the decorated schema object.
/**
* Check type of object. Get all additional arbitrary attributes on the object
* for inclusion in the decorated schema object.
*
* @param {object} schema
* @return {object} schema
*/
function decorateCustomAttributes(schema) {
if (schema.annotations !== undefined && schema.annotations !== null) {
return schema;
Expand Down Expand Up @@ -182,12 +187,19 @@ AvroDoc.Schema = function (avrodoc, shared_types, schema_json, filename) {
return schema;
}

// Takes a node in the schema tree (a JS object) and adds some fields that are useful for
// template rendering.
/**
* Takes a node in the schema tree (a JS object) and adds some fields that are
* useful for template rendering.
*
* @param {object} schema
* @return {object} schema
*/
function decorate(schema) {
schema.filename = filename;
schema["is_" + schema.type] = true;
if (schema.is_error) schema.is_record = true;
if (schema.is_error) {
schema.is_record = true;
}
schema.qualified_name = qualifiedName(schema);
schema = decorateCustomAttributes(schema);

Expand Down
67 changes: 48 additions & 19 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import arg from "arg";
const debug = debugFn("avrodoc:cli");

const argv = arg({
"--help": Boolean,
"-h": "--help",

"--output": String,
"-o": "--output",

Expand All @@ -31,42 +34,68 @@ const argv = arg({
"--ignore-invalid": Boolean,
});

let inputFiles = null;
let outputFile = null;
let ignoreInvalidSchemas = false;
const usage = `USAGE:
avrodoc [FLAGS] [OPTIONS] [AVRO FILES...]
FLAGS:
--ignore-invalid Ignore avsc files that can not be parsed as JSON (instead of quiting)
OPTIONS:
-i, --input <folder> Pass in a source folder that will recursively parsed and crawled for avsc files
-o, --output <file> The file where the generated doc should be written to
--title <title> The title that will be used in the generated HTML page, deafults to "Avrodoc".
-s, --style <file> Your own less file, used to override specific style of your generated page
ARGS:
<AVRO FILES>... If not --input is given, you can specify individual AVRO files here
EXAMPLES:
avrodoc --ignore-invalid --input ./schemas --output avrodoc.html --title "My First Avrodoc"
avrodoc --output avro.html --style my-styles.less avro_schema1.avsc avro_schema2.avsc avro_schema3.avsc
`;

if (
argv["--help"] ||
(argv._.length === 0 && Object.entries(argv).length === 1) // no params
) {
console.log(usage);
process.exit(0);
}

let inputFiles = [];

// Determine list of input files file1.avsc file2.avsc
if (argv["--input"]) {
debug("Collecting all avsc files from root folder ", argv["--input"]);
inputFiles = collectInputFiles(argv["--input"]);
} else if (argv._.length > 0) {
debug("Using passed arguments as inputfiles...");
inputFiles = argv._;
inputFiles = inputFiles.concat(collectInputFiles(argv["--input"]));
}

// Determine whether an output file is specified
if (argv["--output"]) {
outputFile = argv["--output"];
if (argv._.length > 0) {
debug("Using passed arguments as inputfiles...");
inputFiles = inputFiles.concat(argv._);
}

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

if (argv["--ignore-invalid"]) {
ignoreInvalidSchemas = true;
}
const outputFile = argv["--output"];
const pageTitle = argv["--title"];
const extraLessFile = argv["--style"];
const ignoreInvalidSchemas = Boolean(argv["--ignore-invalid"]);

//valid input?
if (!inputFiles || inputFiles.length === 0 || outputFile === null) {
if (inputFiles.length === 0) {
console.error(
'Missing input schemata, either specify a folder with "--input" or individual AVRO files. Specify "--help" to see usage.'
);
process.exit(1);
} else if (outputFile == null) {
console.error(
"Usage: avrodoc [-i rootfolder] [my-schema.avsc [another-schema.avsc...]] [-o my-documentation.html] [-s my-style.less] [--ignore-invalid]"
'Missing output file, specify with "--output" where the resulting HTML should go. Specify "--help" to see usage.'
);
process.exit(1);
}

createAvroDoc(
pageTitle,
extra_less_files,
extraLessFile ? [extraLessFile] : [],
inputFiles,
outputFile,
ignoreInvalidSchemas
Expand Down

0 comments on commit 4024e76

Please sign in to comment.