Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command line interface #8

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ironic that this file is missing a trailing newline...right after insert_final_newline = true 😉

67 changes: 67 additions & 0 deletions bin/markdownlint-cli.js
Original file line number Diff line number Diff line change
@@ -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();
}
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,24 @@
"url": "https://github.com/DavidAnson/markdownlint.git"
},
"bugs": "https://github.com/DavidAnson/markdownlint/issues",
"bin": {
"markdownlint": "./bin/markdownlint-cli.js"
},
"scripts": {
"start": "node ./bin/markdownlint-cli.js",
"test": "nodeunit",
"test-cover": "istanbul cover node_modules/nodeunit/bin/nodeunit",
"debug": "node debug node_modules/nodeunit/bin/nodeunit",
"lint": "eslint lib test && eslint --env browser --global markdownit --global markdownlint --rule \"no-unused-vars: 0, no-extend-native: 0, max-statements: 0, no-console: 0\" demo && eslint --rule \"no-console: 0, no-shadow: 0\" example",
"lint": "eslint lib test bin && eslint --env browser --global markdownit --global markdownlint --rule \"no-unused-vars: 0, no-extend-native: 0, max-statements: 0, no-console: 0\" demo && eslint --rule \"no-console: 0, no-shadow: 0\" example",
"build-demo": "cpy node_modules/markdown-it/dist/markdown-it.min.js demo && cd demo && rimraf markdownlint-browser.* && cpy file-header.js . --rename=markdownlint-browser.js && browserify browser-polyfills.js ../lib/markdownlint.js --standalone markdownlint >> markdownlint-browser.js && uglifyjs markdownlint-browser.js --compress --mangle --comments --output markdownlint-browser.min.js",
"example": "npm install through2 && cd example && node standalone.js && grunt markdownlint --force && gulp markdownlint"
},
"dependencies": {
"markdown-it": "^5.1.0"
"commander": "~2.9.0",
"deep-extend": "~0.4.1",
"lodash": "~3.10.1",
"markdown-it": "^5.1.0",
"rc": "~1.1.6"
},
"devDependencies": {
"browserify": "^13.0.0",
Expand Down