mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-18 23:10:12 +01:00
Command-line interface for markdownlint
This commit is contained in:
parent
1e23d035ce
commit
4a4ac0d189
2 changed files with 77 additions and 2 deletions
67
bin/markdownlint-cli.js
Normal file
67
bin/markdownlint-cli.js
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
"use strict";
|
||||
|
||||
/* eslint no-console:0, no-process-exit:0 */
|
||||
|
||||
var pkg = require("../package");
|
||||
var program = require("commander");
|
||||
|
||||
function readConfiguration(args) {
|
||||
var rc = require("rc");
|
||||
var config = rc(pkg.name, {});
|
||||
if (args.config) {
|
||||
var fs = require("fs");
|
||||
try {
|
||||
var userConfig = JSON.parse(fs.readFileSync(args.config));
|
||||
var extend = require("deep-extend");
|
||||
config = extend(config, userConfig);
|
||||
} catch (e) {
|
||||
console.warn("Cannot read or parse config file", args.config);
|
||||
}
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
function lint(lintFiles, config) {
|
||||
var markdownlint = require("../" + pkg.main);
|
||||
var lintOptions = {
|
||||
"files": lintFiles,
|
||||
"config": config
|
||||
};
|
||||
return markdownlint.sync(lintOptions);
|
||||
}
|
||||
|
||||
function printResult(lintResult) {
|
||||
// var chalk = require("chalk");
|
||||
console.log(lintResult.toString());
|
||||
}
|
||||
|
||||
function isResultCorrect(lintResult) {
|
||||
var _ = require("lodash");
|
||||
function notEmptyObject(item) {
|
||||
return !_.isEqual(item, {});
|
||||
}
|
||||
return _.values(lintResult).some(notEmptyObject);
|
||||
}
|
||||
|
||||
program
|
||||
.version(pkg.version)
|
||||
.description(pkg.description)
|
||||
.usage("[options] <files>")
|
||||
.option("-c, --config [configFile]", "Configuration file");
|
||||
|
||||
program.parse(process.argv);
|
||||
|
||||
var files = program.args;
|
||||
|
||||
if (files && files.length > 0) {
|
||||
var config = readConfiguration(program);
|
||||
var lintResult = lint(files, config);
|
||||
printResult(lintResult);
|
||||
if (isResultCorrect(lintResult)) {
|
||||
process.exit(1);
|
||||
}
|
||||
} else {
|
||||
program.help();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue