Blame view

lint.js 1.3 KB
毕凯 authored
1
const shelljs = require('shelljs');
xuqi authored
2
const path = require('path');
毕凯 authored
3 4

const changeFiles = {
毕凯 authored
5
    js: shelljs.exec('git diff --cached --name-only --diff-filter=ACM | grep .js$', {silent: true}).stdout,
xuqi authored
6
    css: shelljs.exec('git diff --cached --name-only --diff-filter=ACM | grep .css$', {silent: true}).stdout
毕凯 authored
7
};
毕凯 authored
8 9 10 11
const lintPath = {
    js: path.resolve('./node_modules/.bin/eslint'),
    css: path.resolve('./node_modules/.bin/stylelint')
};
毕凯 authored
12 13 14 15 16 17 18
const lintResult = {
    js: {},
    css: {}
};

const ext = process.platform === 'win32' ? '.cmd' : ''; // Windows 平台需要加后缀
19 20 21 22 23
// 在执行检查脚本的时候,不显示 NPM 错误日志
if (!shelljs.grep('npm run -s', path.resolve('./.git/hooks/pre-commit')).stdout.trim()) {
    shelljs.sed('-i', 'npm run', 'npm run -s', path.resolve('./.git/hooks/pre-commit'));
}
毕凯 authored
24
if (changeFiles.js) {
xuqi authored
25 26
    changeFiles.js = changeFiles.js.replace(/\n/g, ' ');
    lintResult.js = shelljs.exec(`${lintPath.js}${ext} -c .eslintrc --cache --fix ${changeFiles.js}`);
毕凯 authored
27 28 29
}

if (changeFiles.css) {
xuqi authored
30 31
    changeFiles.css = changeFiles.css.replace(/\n/g, ' ');
    lintResult.css = shelljs.exec(`${lintPath.css}${ext} --config .stylelintrc ${changeFiles.css}`);
毕凯 authored
32 33 34 35 36
}

if (lintResult.js.code || lintResult.css.code) {
    process.exit(lintResult.js.code || lintResult.css.code); // eslint-disable-line
}