a code dependency analysis tool for ts
npm install code-analysis-ts

code-analysis-ts是一款TS代码依赖扫描分析工具,可用于生成代码依赖分析报告,实现代码评分,代码告警,“脏”代码拦截等功能。支持CLI/API两种使用模式,通过npm script可以快速集成到业务CI自动化流程中。
``javascript`
npm install code-analysis-ts --save-dev
// or
yarn add code-analysis-ts --dev Config
新建 analysis.config.js 配置文件:
`javascript
const { execSync } = require('child_process'); // 子进程操作
const DefaultBranch = 'master'; // 默认分支常量
function getGitBranch() { // 获取当然分支
try{
const branchName = execSync('git symbolic-ref --short -q HEAD', {
encoding: 'utf8'
}).trim();
// console.log(branchName);
return branchName;
}catch(e){
return DefaultBranch;
}
}
module.exports = {
scanSource: [{ // 必须,待扫描源码的配置信息
name: 'Market', // 必填,项目名称
path: ['src'], // 必填,需要扫描的文件路径(基准路径为配置文件所在路径)
packageFile: 'package.json', // 可选,package.json 文件路径配置,用于收集依赖的版本信息
format: null, // 可选, 文件路径格式化函数,默认为null,一般不需要配置
httpRepo: https://gitlab.xxx.com/xxx/-/blob/${getGitBranch()}/ // 可选,项目gitlab/github url的访问前缀,用于点击行信息跳转,不填则不跳转`
}],
analysisTarget: 'framework', // 必须,要分析的目标依赖名
analysisPlugins: [], // 可选,自定义分析插件,默认为空数组,一般不需要配置
blackList: ['app.localStorage.set'], // 可选,需要标记的黑名单api,默认为空数组
browserApis: ['window','document','history','location'], // 可选,要分析的BrowserApi,默认为空数组
reportDir: 'report', // 可选,生成代码分析报告的目录,默认为'report',不支持多级目录配置
reportTitle: 'Market依赖调用分析报告', // 可选,分析报告标题,默认为'依赖调用分析报告'
isScanVue: true, // 可选,是否要扫描分析vue中的ts代码,默认为false
scorePlugin: 'default', // 可选,评分插件: Function|'default'|null, default表示运行默认插件,默认为null表示不评分
alarmThreshold: 90 // 可选,开启代码告警的阈值分数(0-100),默认为null表示关闭告警逻辑 (CLI模式生效)
}Mode
$3
`javascript
// package.json 片段,添加bin command到npm script
...
"scripts": {
"analysis": "ca analysis"
}
...
$ npm run analysis
// or
$ yarn analysis
`$3
`javascript
const analysis = require('code-analysis-ts'); // 代码分析包
const { execSync } = require('child_process'); // 子进程操作
const DefaultBranch = 'master'; // 默认分支常量
function getGitBranch() { // 获取当然分支
try{
const branchName = execSync('git symbolic-ref --short -q HEAD', {
encoding: 'utf8'
}).trim();
// console.log(branchName);
return branchName;
}catch(e){
return DefaultBranch;
}
}
async function scan() {
try{
const { report, diagnosisInfos } = await analysis({
scanSource: [{ // 必须,待扫描源码的配置信息
name: 'Market', // 必填,项目名称
path: ['src'], // 必填,需要扫描的文件路径(基准路径为配置文件所在路径)
packageFile: 'package.json', // 可选,package.json 文件路径配置,用于收集依赖的版本信息
format: null, // 可选, 文件路径格式化函数,默认为null,一般不需要配置
httpRepo: https://gitlab.xxx.com/xxx/-/blob/${getGitBranch()}/ // 可选,项目gitlab/github url的访问前缀,用于点击行信息跳转,不填则不跳转
}],
analysisTarget: 'framework', // 必须,要分析的目标依赖名
analysisPlugins: [], // 可选,自定义分析插件,默认为空数组,一般不需要配置
blackList: ['app.localStorage.set'], // 可选,需要标记的黑名单api,默认为空数组
browserApis: ['window','document','history','location'], // 可选,要分析的BrowserApi,默认为空数组
reportDir: 'report', // 可选,生成代码分析报告的目录,默认为'report',不支持多级目录配置
reportTitle: 'Market依赖调用分析报告', // 可选,分析报告标题,默认为'依赖调用分析报告'
isScanVue: true, // 可选,是否要扫描分析vue中的ts代码,默认为false
scorePlugin: 'default', // 可选,评分插件: Function|'default'|null, default表示运行默认插件,默认为null表示不评分
});
// console.log(report);
// console.log(diagnosisInfos);
}catch(e){
console.log(e);
}
};
scan();
`Demo
code-demo演示如何使用code-analysis-ts的demo项目,使用github pages部署代码分析报告
javascript
// scorePlugin.js
// 评分插件
exports.myScoreDeal = function (analysisContext){
// console.log(analysisContext);
const { pluginsQueue, browserQueue, parseErrorInfos } = analysisContext;
const mapNames = pluginsQueue.map(item=>item.mapName).concat(browserQueue.map(item=>item.mapName));
let score = 100; // 初始分数
let message =[]; // 代码建议 // 黑名单API扣分处理
if(mapNames.length>0){
mapNames.forEach((item)=>{
Object.keys(analysisContext[item]).forEach((sitem)=>{
if(analysisContext[item][sitem].isBlack){
score = score - 5;
message.push(sitem + ' 属于黑名单api,请勿使用');
}
})
})
}
// 解析AST异常的扣分处理
if(parseErrorInfos.length >0){
score = score - 3*parseErrorInfos.length;
let tempMessage ='';
tempMessage = parseErrorInfos.length + ' 个文件解析&分析AST时发生错误,请修复';
message.push(tempMessage);
}
// 最低0分
if(score <0)score =0;
return {
score: score,
message: message
}
}
//analysis.config.js
const { myScoreDeal } = require('./scorePlugin.js'); // 自定义评分插件
module.exports = {
...
scorePlugin: myScoreDeal,
...
}
``