Skip to content

Commit

Permalink
Add filtering param for index.js when used as CLI (w3c#21)
Browse files Browse the repository at this point in the history
The index of specifications is mostly aimed at being imported in other modules,
but using it as a CLI is useful to visualize specs that the index contains.
This update adds the ability to pass an identifier argument on the command-line
that can either be:
- an integer, in which case it gets interpreted as the position of the spec of
interest in the list (it can be useful when a JSON schema validation error gets
reported as it often just points at a position in the list).
- a string, in which case the CLI returns all specs that have a URL, name,
shortname or composition level that matches the string.

The CLI outputs a formatted spec object if there's only one spec that matches
the filter argument, an array of spec objects if there's more than one.
  • Loading branch information
tidoust authored Mar 30, 2020
1 parent 4538631 commit 2c33292
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,38 @@ const specs = require("./specs.json")
computeShortname(spec.name || spec.url),
spec))

// Complete information with currentLevel property
// Complete information with currentLevel property and drop forceCurrent flags
// that no longer need to be exposed
.map((spec, _, list) => Object.assign(spec, computeCurrentLevel(spec, list)))
.map(spec => { delete spec.forceCurrent; return spec; })

// Complete information with previous/next level links
.map((spec, _, list) => Object.assign(spec, computePrevNext(spec, list)));

module.exports = { specs };

if (require.main === module) {
// Code used as command-line interface (CLI), output info about known specs.
// If a parameter was provided, use it to find the right spec(s):
// - if it's an integer, return the spec at that index in the list
// - if parameter is full or delta, return specs with same level composition
// - return specs that have the same URL, name or shortname otherwise
// Otherwise, return the whole list
const id = process.argv[2];
if (id) {
const res = id.match(/^\d+$/) ?
[specs[parseInt(id, 10)]] :
specs.filter(s =>
s.url === id ||
s.name === id ||
s.shortname === id ||
s.levelComposition === id);
console.log(JSON.stringify(res.length === 1 ? res[0] : res, null, 2));
}
else {
console.log(JSON.stringify(specs, null, 2));
}
}
else {
// Code referenced from another JS module, export
module.exports = { specs };
}

0 comments on commit 2c33292

Please sign in to comment.