Skip to content

Commit

Permalink
refactor(cli): remove bluebird, lodash, simplify arg parsing
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Repaces lodash with ferrum, removed bluebird, changes the meaning of `--schema-out` or `-x` to be no longer relative to output dir

The `--schema-out` or `-x` command line option is no longer relative to the output path (specified with `-o` or `--out`)
  • Loading branch information
trieloff committed Dec 2, 2019
1 parent a5c86af commit b6b1822
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1,461 deletions.
140 changes: 70 additions & 70 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,125 +12,125 @@
*/

const yargs = require('yargs');
const Promise = require('bluebird');
const path = require('path');
const _ = require('lodash');
const fs = Promise.promisifyAll(require('fs'));

const fs = require('fs');
const readdirp = require('readdirp');
const Ajv = require('ajv');
const i18n = require('i18n');
const logger = require('@adobe/helix-log');
const {
iter, pipe, filter, map, obj,
} = require('ferrum');

const { error, info } = logger;

const Schema = require('./lib/schema');
const readSchemaFile = require('./lib/readSchemaFile');

// init JSON Schema validator
const ajv = new Ajv({
allErrors: true, messages: true, schemaId: 'auto', logger,
});

// parse/process command line arguments
const { argv } = yargs
.usage('Generate Markdown documentation from JSON Schema.\n\nUsage: $0')
.demand('d')
.alias('d', 'input')
// TODO: is baseURL still a valid parameter?
.describe('d', 'path to directory containing all JSON Schemas or a single JSON Schema file. This will be considered as the baseURL. By default only files ending in .schema.json will be processed, unless the schema-extension is set with the -e flag.')
.coerce('d', (d) => {
const resolved = path.resolve(d);
if (fs.existsSync(resolved) && fs.lstatSync(d).isDirectory()) {
return resolved;
}
throw new Error(`Input file "${d}" is not a directory!`);
})

.alias('o', 'out')
.describe('o', 'path to output directory')
.default('o', path.resolve(path.join('.', 'out')))
.coerce('o', o => path.resolve(o))

.option('m', {
type: 'array',
})
.alias('m', 'meta')
.describe('m', 'add metadata elements to .md files Eg -m template=reference. Multiple values can be added by repeating the flag Eg: -m template=reference -m hide-nav=true')
.coerce('m', m => pipe(
// turn this into an object of key value pairs
iter(m),
map(i => i.split('=')),
obj,
))

.alias('s', 'metaSchema')
.describe('s', 'Custom meta schema path to validate schemas')
.coerce((s) => {
// eslint-disable-next-line import/no-dynamic-require, global-require
ajv.addMetaSchema(require(path.resolve(s)));
})

.alias('x', 'schema-out')
.describe('x', 'output JSON Schema files including description and validated examples in the _new folder at output directory, or suppress with -')
.describe('x', 'output JSON Schema files including description and validated examples in the specified folder, or suppress with -')
.default('o', path.resolve(path.join('.', 'out')))
.coerce(x => (x === '-' ? '' : path.resolve(x)))

.alias('e', 'schema-extension')
.describe('e', 'JSON Schema file extension eg. schema.json or json')
.default('e', 'schema.json')

.alias('n', 'no-readme')
.describe('n', 'Do not generate a README.md file in the output directory')
.default(false)

.describe('v', 'JSON Schema Draft version to use. Supported: 04, 06, 07 (default)')
.alias('v', 'draft')
.default('v', '07')
.describe('n', 'Do not generate a README.md file in the output directory')
.describe('link-*', 'Add this file as a link the explain the * attribute, e.g. --link-abstract=abstract.md')
.check((args) => {
if (!fs.existsSync(args.input)) {
throw new Error(`Input file "${args.input}" does not exist!`);
}
if (args.s && !fs.existsSync(args.s)) {
throw new Error(`Meta schema file "${args.s}" does not exist!`);
.coerce('v', (v) => {
if (v === '06' || v === 6) {
info('enabling draft-06 support');
// eslint-disable-next-line global-require
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'));
} else if (v === '04' || v === 4) {
// eslint-disable-next-line global-require
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
}
})

.describe('link-*', 'Add this file as a link the explain the * attribute, e.g. --link-abstract=abstract.md')

.alias('i', 'i18n')
.describe('i', 'path to a locales folder with an en.json file in it. This file will be used for all text parts in all templates')
.default('i', path.resolve(__dirname, 'lib', 'locales'))
.coerce('i', i => path.resolve(i))

.alias('p', 'properties')
.describe('p', 'A comma separated list with custom properties which should be also in the description of an element.')
.alias('h', 'header')
.describe('h', 'if the value is false the header will be skipped')
.default('h', true);

const docs = _.fromPairs(
_.toPairs(argv)
.filter(([key, _value]) => key.startsWith('link-'))
.map(([key, value]) => [key.substr(5), value]),
const docs = pipe(
iter(argv),
filter(([key, _value]) => key.startsWith('link-')),
map(([key, value]) => [key.substr(5), value]),
obj,
);

const ajv = new Ajv({
allErrors: true, messages: true, schemaId: 'auto', logger,
});
info(argv.v);
if (argv.v === '06' || argv.v === 6) {
info('enabling draft-06 support');
// eslint-disable-next-line global-require
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'));
} else if (argv.v === '04' || argv.v === 4) {
// eslint-disable-next-line global-require
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
}
const schemaPathMap = {};
const metaElements = {};
const schemaPath = path.resolve(argv.d);
const outDir = path.resolve(argv.o);
// eslint-disable-next-line no-nested-ternary
const schemaDir = argv.x === '-' ? '' : (argv.x ? path.resolve(argv.x) : outDir);
const metaElements = argv.m;
const schemaPath = argv.d;
const outDir = argv.o;
const schemaDir = argv.x;
const target = fs.statSync(schemaPath);
const readme = argv.n !== true;
const schemaExtension = argv.e || 'schema.json';
if (argv.s) {
// eslint-disable-next-line import/no-dynamic-require, global-require
ajv.addMetaSchema(require(path.resolve(argv.s)));
}

if (argv.m) {
if (_.isArray(argv.m)) {
_.each(argv.m, (item) => {
const [key, val] = item.split('=');
if (val !== undefined) {
metaElements[key] = val;
}
});
} else {
const [key, val] = (argv.m).split('=');
if (val !== undefined) {
metaElements[key] = val;
}
}
}
let i18nPath;
if (argv !== undefined && argv.i !== undefined) {
i18nPath = path.resolve(argv.i);
} else {
i18nPath = path.resolve(path.join(__dirname, 'lib/locales'));
}
i18n.configure({
// setup some locales - other locales default to en silently
locales: ['en'],
// where to store json files - defaults to './locales' relative to modules directory
directory: i18nPath,
defaultLocale: 'en',
});
const readme = !!argv.n;
const schemaExtension = argv.e;

info('output directory', outDir);
if (target.isDirectory()) {
// the ajv json validator will be passed into the main module to help with processing
const files = [];

readdirp(schemaPath, { root: schemaPath, fileFilter: `*.${schemaExtension}` })
.on('data', (entry) => {
files.push(entry.fullPath);
Expand Down
Loading

0 comments on commit b6b1822

Please sign in to comment.