Blame view

lint-commit.js 1.84 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,
ccbikai(👎🏻🍜) authored
6 7
    css: shelljs.exec('git diff --cached --name-only --diff-filter=ACM | grep .css$', {silent: true}).stdout,
    vue: shelljs.exec('git diff --cached --name-only --diff-filter=ACM | grep .vue$', {silent: true}).stdout,
毕凯 authored
8
};
ccbikai(👎🏻🍜) authored
9
毕凯 authored
10 11 12 13
const lintPath = {
    js: path.resolve('./node_modules/.bin/eslint'),
    css: path.resolve('./node_modules/.bin/stylelint')
};
毕凯 authored
14 15
const lintResult = {
    js: {},
ccbikai(👎🏻🍜) authored
16 17 18
    css: {},
    vueScript: {},
    vueStyle: {}
毕凯 authored
19 20 21 22
};

const ext = process.platform === 'win32' ? '.cmd' : ''; // Windows 平台需要加后缀
23 24 25 26 27
// 在执行检查脚本的时候,不显示 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
28
if (changeFiles.js) {
xuqi authored
29
    changeFiles.js = changeFiles.js.replace(/\n/g, ' ');
30
    lintResult.js = shelljs.exec(`${lintPath.js}${ext} -c .eslintrc --cache ${changeFiles.js}`);
毕凯 authored
31 32 33
}

if (changeFiles.css) {
xuqi authored
34
    changeFiles.css = changeFiles.css.replace(/\n/g, ' ');
ccbikai(👎🏻🍜) authored
35 36 37 38 39
    lintResult.css = shelljs.exec(`${lintPath.css}${ext} --syntax scss --config .stylelintrc ${changeFiles.css}`);
}

if (changeFiles.vue) {
    changeFiles.vue = changeFiles.vue.replace(/\n/g, ' ');
40
    lintResult.vueScript = shelljs.exec(`${lintPath.js}${ext} -c .eslintrc --cache ${changeFiles.vue}`);
ccbikai(👎🏻🍜) authored
41
    lintResult.vueStyle = shelljs.exec(`${lintPath.css}${ext} --syntax scss --extract --config .stylelintrc ${changeFiles.vue}`); // eslint-disable-line
毕凯 authored
42 43
}
ccbikai(👎🏻🍜) authored
44 45 46 47
const errorCode = lintResult.js.code || lintResult.css.code || lintResult.vueScript.code || lintResult.vueStyle.code;

if (errorCode) {
    process.exit(errorCode); // eslint-disable-line
毕凯 authored
48
}