finn-js-code-style
Advanced tools
Comparing version 4.1.2 to 4.2.0
@@ -5,10 +5,19 @@ var path = require('path'); | ||
var jshintStylish = require(require('jshint-stylish')); | ||
var JSHINTRC = '.jshintrc'; | ||
var NODE_MODULES = __dirname + '/../node_modules'; | ||
var USAGE = [ | ||
'Usage: finn-js-code-style <file | dir>...', | ||
'Use the .jshintrc file for jshint config (should \"extend\": \"./node_modules/finn-js-code-style/.jshintrc\")', | ||
'Use the .jshintignore to exclude files or folders' | ||
'Usage:', | ||
'\tfinn-js-code-style [options] <file | dir>...', | ||
'', | ||
'Use the .jshintrc file for jshint config (should "extends": "./node_modules/finn-js-code-style/.jshintrc")', | ||
'Use the .jshintignore to exclude files or folders', | ||
'', | ||
'Options:', | ||
'\t--fail : exits with code 1 when errors are generated', | ||
'\t--help : usage info' | ||
].join('\n'); | ||
var jshintRcPath = path.join(process.cwd(), JSHINTRC); | ||
function isProjectRoot () { | ||
@@ -18,13 +27,47 @@ return fs.existsSync( path.join(process.cwd(), 'package.json') ); | ||
module.exports = function (args) { | ||
var cmd, result, report; | ||
var currentDir = process.cwd(); | ||
var jshintRc = path.join(currentDir, JSHINTRC); | ||
var jsonReporter = '--reporter ' + path.join(NODE_MODULES, 'jshint-json', 'json.js'); | ||
function isRuntimeError(execResult) { | ||
return execResult.code === 1 && execResult.output; | ||
} | ||
if (!fs.existsSync(jshintRc) && isProjectRoot()) { | ||
function isStyleError(execResult) { | ||
return execResult.code === 2 && execResult.output; | ||
} | ||
function isError(execResult) { | ||
return execResult.code !== 0; | ||
} | ||
function excludeOpts(args) { | ||
return args.filter(function(arg) { | ||
return arg.indexOf('--') === -1; | ||
}); | ||
} | ||
function validateLocalJsHintRc() { | ||
if (!fs.existsSync(jshintRcPath) && isProjectRoot()) { | ||
console.log('No .jshintrc found. Creating...'); | ||
fs.writeFileSync(jshintRc, '{\n \"extends\": \"./node_modules/finn-js-code-style/.jshintrc\"\n}\n'); | ||
fs.writeFileSync(jshintRcPath, '{\n \"extends\": \"./node_modules/finn-js-code-style/.jshintrc\"\n}\n'); | ||
} | ||
} | ||
function displayResult(result, cmd) { | ||
var report; | ||
if (isRuntimeError(result)) { | ||
console.error('jshint command failed, runned with: ' + cmd); | ||
console.error(result.output); | ||
} else if (isStyleError(result)) { | ||
report = JSON.parse(result.output); | ||
jshintStylish.reporter(report.result, report.data); | ||
} | ||
} | ||
module.exports = function cli(args) { | ||
var cmd, result; | ||
var jsonReporter = '--reporter ' + path.join(NODE_MODULES, 'jshint-json', 'json.js'); | ||
var shouldFail = args[0] === '--fail'; | ||
var filesToHint = excludeOpts(args).join(' '); | ||
validateLocalJsHintRc(); | ||
if (args.length === 0 || args[0] === '--help') { | ||
@@ -35,12 +78,10 @@ console.log(USAGE); | ||
cmd = path.join(NODE_MODULES, '.bin', 'jshint') + ' ' + jsonReporter + ' ' + args.join(' '); | ||
cmd = path.join(NODE_MODULES, '.bin', 'jshint') + ' ' + jsonReporter + ' ' + filesToHint; | ||
result = exec(cmd, {silent: true}); | ||
if (result.code === 1 && result.output) { | ||
console.log('jshint command used: ' + cmd); | ||
console.log(result.output); | ||
} else if (result.code === 2 && result.output) { | ||
report = JSON.parse(result.output); | ||
jshintStylish.reporter(report.result, report.data); | ||
displayResult(result, cmd); | ||
if (isError(result) && shouldFail) { | ||
process.exit(1); | ||
} | ||
}; |
{ | ||
"name": "finn-js-code-style", | ||
"version": "4.1.2", | ||
"version": "4.2.0", | ||
"description": "Config files for FINN JavaScript style", | ||
@@ -5,0 +5,0 @@ "main": "lib/cli.js", |
@@ -18,4 +18,9 @@ # FINN.no JavaScript code style | ||
finn-js-code-style <file | dir>... | ||
finn-js-code-style [options] <file | dir>... | ||
### Options | ||
* `--help` Usage info | ||
* `--fail` Exit with code 1 when errors are generated | ||
## Config | ||
@@ -22,0 +27,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
13786
15
153
106