eslint-plugin-intl-mobile
Version:
Intl Mobile ESLint Rules.
209 lines (178 loc) • 4.95 kB
JavaScript
function createEslintrc(parser, parserOptions) {
return `
{
"extends": [
"plugin:eslint-plugin-intl-mobile/all"
],
"plugins": ["eslint-plugin-intl-mobile"],
"parser": ${parser},
"parserOptions": ${parserOptions},
"rules": {}
}
`;
}
function computedScore(ruleMap) {
var score = 100;
score -= ruleMap['yen-full-width-detect'] * 50;
score -= ruleMap['json-parse-try-catch'] * 10;
score -= ruleMap['rpc-no-number-parameter'] * 5;
score -= ruleMap['typeof-warning'] * 5;
score -= ruleMap['while-warning'] * 5;
if (score < 0) score = 0;
return score;
}
var program = require('commander').program;
var fs = require('fs');
var path = require('path');
var spawn = require('child_process').spawn;
var colors = require('colors');
var ora = require('ora');
var cwd = process.cwd();
program
.option('-p, --parser <parser>', '指定 eslint parser')
.option('-r, --rate', '打分模式')
.option('-f, --filter', '仅显示 intl-mobile 验证结果')
.option('-e, --es6', '采用 es6 parserOptions')
.option('-d, --dirs <dirs>', '指定检测目录');
program.parse(process.argv);
// 处理参数
var parser = program.parser;
if (!parser) {
parser = '""';
} else {
parser = `"${parser}"`;
}
var dirs = program.dirs;
if (!dirs) {
dirs = './';
}
var rate = program.rate;
var filter = program.filter;
var parserOptions = {
ecmaFeatures: {
jsx: true
}
};
var es6 = program.es6;
if (es6) {
parserOptions = {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {
jsx: true
}
};
}
// 主逻辑
var selfPath = require.resolve('./index');
var lintCliPath = path.join(selfPath, '../../node_modules/.bin/eslint');
if (!fs.existsSync(lintCliPath)) {
lintCliPath = path.join(cwd, './node_modules/.bin/eslint');
}
var eslintrc = createEslintrc(parser, JSON.stringify(parserOptions));
console.log(colors.green('执行的规则...'));
console.log('.eslintrc-intl-mobile.json');
console.log(eslintrc);
var spinner = ora({
text: colors.green('执行扫描中...'),
}).start();
var filePath = path.join(cwd, './.eslintrc-intl-mobile.json');
fs.writeFileSync(filePath, eslintrc, 'utf8');
var lint;
if (filter) {
var grep = spawn('grep', ['-E', '\\.ts|\\.js|\\.jsx|\\.tsx|intl-mobile']);
lint = spawn(`${lintCliPath}`, [
'--ext',
'.jsx,.js,.ts,.tsx',
'-c',
`${filePath}`,
'--ignore-path',
'.gitignore',
'--no-error-on-unmatched-pattern',
`${path.join(cwd, dirs)}`
]);
lint.stdout.pipe(grep.stdin);
grep.stdout.on('data', (data) => {
const dataStr = `${data}`;
console.log(dataStr);
});
grep.stderr.on('data', (data) => {
console.error(`Error: ${data}`);
});
lint.on('close', () => {
fs.unlink(filePath, function(err) {
if (err) throw err;
});
spinner.stop();
});
} else if (rate) {
var grep = spawn('grep', ['intl-mobile']);
var rateMap = {
'chain-warning': 0,
'json-parse-try-catch': 0,
'rpc-no-number-parameter': 0,
'typeof-warning': 0,
'while-warning': 0,
'yen-full-width-detect': 0,
};
lint = spawn(`${lintCliPath}`, [
'--ext',
'.jsx,.js,.ts,.tsx',
'-c',
`${filePath}`,
'--ignore-path',
'.gitignore',
'--no-error-on-unmatched-pattern',
`${path.join(cwd, dirs)}`
]);
lint.stdout.pipe(grep.stdin);
grep.stdout.on('data', (data) => {
const dataStr = `${data}`;
// console.log(dataStr);
Object.keys(rateMap).forEach(function(rule) {
if (!rateMap[rule]) rateMap[rule] = 0;
rateMap[rule] += (dataStr.match(new RegExp(`intl-mobile\/${rule}`, 'g')) || []).length || 0;
});
});
grep.stderr.on('data', (data) => {
console.error(`Error: ${data}`);
});
grep.on('close', () => {
fs.unlink(filePath, function(err) {
if (err) throw err;
});
spinner.stop();
console.log(colors.green('对应规则出现的次数'));
console.log(JSON.stringify(rateMap, null, 2));
var score = computedScore(rateMap);
if (isNaN(score)) {
console.log(colors.red('扫描貌似挂了,请确定你的依赖正常...'));
} else if(score === 100) {
console.log(colors.green(`未扫描出结果,你的系统得分为:${score}`));
console.log(`你可以通过运行 'npx eslint-intl-mobile' 来查看 eslint 检测输出`);
} else {
console.log(colors.red(`你的系统得分为:${score}`));
}
console.log(`你可以通过运行 'npx eslint-intl-mobile -f' 来查看仅输出 intl-mobile 筛选的结果`);
});
} else {
lint = spawn(`${lintCliPath}`, [
'--ext',
'.jsx,.js,.ts,.tsx',
'-c',
`${filePath}`,
'--ignore-path',
'.gitignore',
'--no-error-on-unmatched-pattern',
`${path.join(cwd, dirs)}`
], {
stdio: 'inherit',
});
lint.on('close', () => {
spinner.stop();
fs.unlink(filePath, function(err) {
if (err) throw err;
});
});
}