Skip to content

Instantly share code, notes, and snippets.

@szkrd
Created March 29, 2018 08:55
Show Gist options
  • Save szkrd/4ad2ef68df2b854ccebe934644d1b3be to your computer and use it in GitHub Desktop.
Save szkrd/4ad2ef68df2b854ccebe934644d1b3be to your computer and use it in GitHub Desktop.
Lint modified files (but not yet committed) files.
require('colors');
const shell = require('shelljs');
const MAX_FILES = 30;
let lintCmd = 'node ./node_modules/eslint/bin/eslint';
let scssCmd = 'node ./node_modules/stylelint/bin/stylelint';
let status = shell.exec('git status --porcelain', { silent: true }).stdout;
let log = s => console.log(`• ${s}`.cyan);
status = status
.trim()
.split('\n')
.reduce((acc, line) => {
let flags = line
.substr(0, 2)
.split('')
.filter(c => c.trim());
let names = line.substr(2).trim();
let name = names.replace(/.*?->\s+/, '');
acc.push({ flags, name });
return acc;
}, []);
let files = status
.filter(item => !item.flags.includes('D'))
.map(item => item.name);
let scssFiles = files.filter(name => name.endsWith('scss'));
let jsFiles = files.filter(name => name.endsWith('js'));
let scssCode = 0;
let eslintCode = 0;
// js
if (jsFiles.length > 0) {
if (jsFiles.length > MAX_FILES) {
console.error('you have too many js files, sry');
process.exit(1);
}
log(`eslint needed: checking ${jsFiles.length} js files...`);
eslintCode = shell.exec(lintCmd + ' ' + jsFiles.join(' ')).code;
} else {
log('no eslint lint needed');
}
// scss
if (scssFiles.length > 0) {
if (scssFiles.length > MAX_FILES) {
console.error('you have too many scss files, sry');
process.exit(2);
}
log(`styellint needed: checking ${scssFiles.length} scss files...`);
scssCode = shell.exec(scssCmd + ' ' + scssFiles.join(' ')).code;
} else {
log('no stylelint needed');
}
let finalCode = eslintCode || scssCode;
console.log(finalCode ? ':('.red : ':)'.green);
process.exit(finalCode);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment