Skip to content

Commit

Permalink
Use yargs to parse the arguments passed to the linter
Browse files Browse the repository at this point in the history
  This makes it possible to manually lint multiple files at once
without needing to run the linter multiple times.

  Also probably fixes the issue where an error in a single file would
cause all subsequent files to be printed at the end¹.

# References:
¹ https://travis-ci.org/mdn/browser-compat-data/builds/383532922#L7191
  • Loading branch information
ExE-Boss committed May 25, 2018
1 parent b8c7e1b commit 47a4bc2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"homepage": "https://github.com/mdn/browser-compat-data#readme",
"devDependencies": {
"ajv": "^5.0.1",
"mdn-confluence": "0.0.3"
"mdn-confluence": "0.0.3",
"yargs": "^11.1.0"
},
"scripts": {
"confluence": "node ./node_modules/mdn-confluence/main/generate.es6.js --output-dir=.",
Expand Down
40 changes: 30 additions & 10 deletions test/lint.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
var fs = require('fs');
var path = require('path');
var {testStyle} = require('./test-style');
var {testSchema} = require('./test-schema');
var {testVersions} = require('./test-versions');
var hasErrors, hasStyleErrors, hasSchemaErrors, hasVersionErrors = false;
var filesWithErrors = {};
const fs = require('fs');
const path = require('path');
const yargs = require('yargs');
const {testStyle} = require('./test-style');
const {testSchema} = require('./test-schema');
const {testVersions} = require('./test-versions');

const argv = yargs.alias('version','v')
.usage('$0 [[--] files...]', false, (yargs) => {
yargs.positional('files...', {
description: 'The files to lint',
type: 'string'
})
})
.help().alias('help','h').alias('help','?')
.parse(process.argv.slice(2));

let hasErrors = false;
let filesWithErrors = {};

function load(...files) {
if (files.length === 1 && files[0] instanceof Array) {
files = files[0];
}
for (let file of files) {
if (file.indexOf(__dirname) !== 0) {
file = path.resolve(__dirname, '..', file);
}

if (!fs.existsSync(file)) {
continue; // Ignore non-existent files
}

if (fs.statSync(file).isFile()) {
if (path.extname(file) === '.json') {
let hasStyleErrors, hasSchemaErrors, hasVersionErrors = false;
console.log(file.replace(path.resolve(__dirname, '..') + path.sep, ''));
if (file.indexOf('browsers' + path.sep) !== -1) {
hasSchemaErrors = testSchema(file, './../schemas/browsers.schema.json');
Expand All @@ -40,8 +60,8 @@ function load(...files) {
}
}

if (process.argv[2]) {
load(process.argv[2])
if (argv.files) {
load(argv.files)
} else {
load(
'api',
Expand All @@ -60,7 +80,7 @@ if (process.argv[2]) {

if (hasErrors) {
console.log("");
console.log(`Problems in ${Object.keys(filesWithErrors).length} files:`);
console.warn(`Problems in ${Object.keys(filesWithErrors).length} files:`);
for (let file in filesWithErrors) {
console.log(file);
testSchema(filesWithErrors[file]);
Expand Down

0 comments on commit 47a4bc2

Please sign in to comment.