Skip to content

Commit

Permalink
chore(lint): added standard linting
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Dec 8, 2015
1 parent 9034a27 commit 635937d
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 210 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[![dependencies][ban-sensitive-files-dependencies-image] ][ban-sensitive-files-dependencies-url]
[![devdependencies][ban-sensitive-files-devdependencies-image] ][ban-sensitive-files-devdependencies-url]
[![semantic-release][semantic-image] ][semantic-url]
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)

Note: the source file with rules was taken from file
[git-deny-patterns.json](https://github.com/jandre/safe-commit-hook/blob/master/git-deny-patterns.json)
Expand Down Expand Up @@ -56,6 +57,12 @@ Add to your project `npm install --save-dev ban-sensitive-files`
}
```

## Advanced

To figure out what the script is doing, enable debug logging

DEBUG=ban npm run ban

### Small print

Author: Gleb Bahmutov © 2015
Expand Down
48 changes: 24 additions & 24 deletions bin/ban.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
#!/usr/bin/env node
#!/usr/bin/env node --harmony

require('update-notifier')({
pkg: require('../package.json')
}).notify();
}).notify()

const log = require('debug')('ban');
const log = require('debug')('ban')

function getProperty(prop) {
function getProperty (prop) {
return function (obj) {
return obj[prop];
};
return obj[prop]
}
}

const isBanned = require('..');
const ggit = require('ggit');
const getName = getProperty('name');
const isBanned = require('..')
const ggit = require('ggit')
const getName = getProperty('name')

function collectFilenames(info) {
var filenames = [];
function collectFilenames (info) {
var filenames = []
if (info.A) {
filenames = filenames.concat(info.A.map(getName));
filenames = filenames.concat(info.A.map(getName))
}
if (info.M) {
filenames = filenames.concat(info.M.map(getName));
filenames = filenames.concat(info.M.map(getName))
}
if (info.C) {
filenames = filenames.concat(info.C.map(getName));
filenames = filenames.concat(info.C.map(getName))
}
return filenames;
return filenames
}

function printFilenames(names) {
log('%d changed filenames', names.length);
log(names);
return names;
function printFilenames (names) {
log('%d changed filenames', names.length)
log(names)
return names
}

ggit.changedFiles()
.then(collectFilenames)
.then(printFilenames)
.then(isBanned)
.then(function (foundBannedFilenames) {
log('found banned filenames?', foundBannedFilenames);
log('found banned filenames?', foundBannedFilenames)
if (foundBannedFilenames) {
process.exit(-1);
process.exit(-1)
}
})
.catch(function (err) {
console.error(err.message);
process.exit(-1);
console.error(err.message)
process.exit(-1)
})
.done();
.done()
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"ban-sensitive-files": "bin/ban.js",
"sensitive-files": "bin/ban.js",
"ban-sensitive": "bin/ban.js",
"no-sensitive": "bin/ban.js"
"no-sensitive": "bin/ban.js",
"no-way": "bin/ban.js"
},
"version": "0.0.0-semantic-release",
"scripts": {
Expand All @@ -16,7 +17,8 @@
"commit": "git-issues && commit-wizard",
"issues": "git-issues",
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
"size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";"
"size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";",
"lint": "standard bin/*.js src/*.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -45,6 +47,7 @@
"pre-git": {
"commit-msg": "validate-commit-msg",
"pre-commit": [
"npm run lint",
"node bin/ban.js"
],
"pre-push": [
Expand All @@ -61,7 +64,8 @@
"git-issues": "1.2.0",
"mocha": "2.3.4",
"pre-git": "2.0.2",
"semantic-release": "6.0.3"
"semantic-release": "6.0.3",
"standard": "5.4.1"
},
"dependencies": {
"check-more-types": "2.2.0",
Expand Down
96 changes: 50 additions & 46 deletions src/ban-spec.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,77 @@
const la = require('lazy-ass');
const check = require('check-more-types');
const la = require('lazy-ass')
const check = require('check-more-types')

/* global describe, it */
describe('ban', function () {
const isBanned = require('./ban');
const isBanned = require('./ban')

it('is a function', function () {
la(check.fn(isBanned))
})

it('catches a few cases', function () {
const invalid = ['id_rsa', '.bash_history'];
const invalid = ['id_rsa', '.bash_history']
invalid.forEach(function (name) {
la(isBanned(name), name);
});
});
la(isBanned(name), name)
})
})

it('allows other filenames', function () {
const invalid = ['something', 'foo.js'];
const invalid = ['something', 'foo.js']
invalid.forEach(function (name) {
la(!isBanned(name), name);
});
});
la(!isBanned(name), name)
})
})

it('catches long paths', function () {
la(isBanned('foo/bar/id_rsa'));
});
la(isBanned('foo/bar/id_rsa'))
})

it('allows other long paths', function () {
la(!isBanned('foo/bar/Gruntfile.js'));
});
la(!isBanned('foo/bar/Gruntfile.js'))
})

it('catches pem extension', function () {
const name = 'something.pem';
la(isBanned(name), 'pem extension should be banned', name);
});
const name = 'something.pem'
la(isBanned(name), 'pem extension should be banned', name)
})

it('allows pem substring', function () {
const name = 'something-pem.txt';
la(!isBanned(name), 'should be allowed', name);
});
const name = 'something-pem.txt'
la(!isBanned(name), 'should be allowed', name)
})

it('catches tblk extension', function () {
la(isBanned('foo.tblk'));
la(!isBanned('foo.atblk'));
});
la(isBanned('foo.tblk'))
la(!isBanned('foo.atblk'))
})

it('calls provided logger', function () {
var called = 0;
function logger() {
called += 1;
var called = 0
function logger () {
called += 1
}
la(isBanned('foo.tblk', logger));
la(called === 1, 'logger has been called once');
});
la(isBanned('foo.tblk', logger))
la(called === 1, 'logger has been called once')
})

it('supports multiple filename', function () {
var called = 0;
function logger() {
called += 1;
var called = 0
function logger () {
called += 1
}
const filenames = ['foo.tblk', 'id_rsa'];
la(isBanned(filenames, logger));
la(called === 1, 'logger has been called once');
});
const filenames = ['foo.tblk', 'id_rsa']
la(isBanned(filenames, logger))
la(called === 1, 'logger has been called once')
})

it('passes multiple filename', function () {
var called = 0;
function logger() {
called += 1;
var called = 0
function logger () {
called += 1
}
const filenames = ['foo.js', 'bar/baz.js'];
la(!isBanned(filenames, logger));
la(called === 0, 'no problems');
});

});
const filenames = ['foo.js', 'bar/baz.js']
la(!isBanned(filenames, logger))
la(called === 0, 'no problems')
})
})
54 changes: 26 additions & 28 deletions src/ban.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,50 @@
const la = require('lazy-ass');
const check = require('check-more-types');
const rules = require('../git-deny-patterns.json');
la(check.array(rules), 'missing list of rules', rules);
const ruleToTester = require('./rule-to-tester');
la(check.fn(ruleToTester), 'could not get rule to tester');
const la = require('lazy-ass')
const check = require('check-more-types')
const rules = require('../git-deny-patterns.json')
la(check.array(rules), 'missing list of rules', rules)
const ruleToTester = require('./rule-to-tester')
la(check.fn(ruleToTester), 'could not get rule to tester')

const log = require('debug')('ban');
const log = require('debug')('ban')

log('loaded', rules.length, 'rules');
log('loaded', rules.length, 'rules')

// TODO lift?
function formTesters(rules) {
function formTesters (rules) {
return rules.map(function (rule) {
return ruleToTester(rule);
});
return ruleToTester(rule)
})
}

const testers = formTesters(rules);
const testers = formTesters(rules)

const reToRegExp = require('./re-to-regexp');

function singleTester(filenames, logger, tester, k) {
function singleTester (filenames, logger, tester, k) {
return filenames.some(function (filename) {
if (tester(filename)) {
const brokenRule = rules[k];
const brokenRule = rules[k]

var message = 'invalid filename ' + filename;
var message = 'invalid filename ' + filename
if (check.unemptyString(brokenRule.caption)) {
message += '\n - ' + brokenRule.caption;
message += '\n - ' + brokenRule.caption
}
if (brokenRule.description) {
message += '\n - ' + brokenRule.description;
message += '\n - ' + brokenRule.description
}
logger(message);
logger(message)

return true;
return true
}
});
})
}

function isBannedFilename(filename, logger) {
function isBannedFilename (filename, logger) {
la(check.maybe.unemptyString(filename) ||
check.array(filename), 'expected single or list of filenames', filename);
logger = logger || console.error.bind(console);
check.array(filename), 'expected single or list of filenames', filename)
logger = logger || console.error.bind(console)

const filenames = check.array(filename) ? filename : [filename];
const filenames = check.array(filename) ? filename : [filename]

return testers.some(singleTester.bind(null, filenames, logger));
return testers.some(singleTester.bind(null, filenames, logger))
}

module.exports = isBannedFilename;
module.exports = isBannedFilename
Loading

0 comments on commit 635937d

Please sign in to comment.