Skip to content

Commit

Permalink
feat(babel-plugin-canyon): add ci
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtao25 committed Feb 14, 2025
1 parent 4c86b67 commit aa7c136
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 13 deletions.
8 changes: 8 additions & 0 deletions plugins/babel-plugin-canyon/features/jian.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function add (a,b) {
if (a>b){
return a-b
}
return a+b
}

add(1, 2);
2 changes: 1 addition & 1 deletion plugins/babel-plugin-canyon/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-canyon",
"version": "2.0.10",
"version": "2.0.11",
"description": "",
"scripts": {
"release": "babel src --extensions \".ts\" --out-dir lib",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ import fs from 'fs';
import path from 'path'
import {extractCoverageData} from "./extract-coverage-data";

export const generateInitialCoverage = (paramsPath) => {
export const generateInitialCoverage = (paramsPath,serviceParams) => {
const initialCoverageDataForTheCurrentFile = extractCoverageData(paramsPath)
const filePath = './.canyon_output/coverage-final.json';
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, {recursive: true});
}
// 防止返回的数据为空
if (initialCoverageDataForTheCurrentFile && initialCoverageDataForTheCurrentFile.path) {
fs.writeFileSync(`./.canyon_output/coverage-final-${Math.random()}.json`, JSON.stringify({
[initialCoverageDataForTheCurrentFile.path]: initialCoverageDataForTheCurrentFile
}, null, 2), 'utf-8');
// 判断是否是CI环境
// CI环境才生成.canyon_output/coverage-final.json文件
if (serviceParams.ci) {
const filePath = './.canyon_output/coverage-final.json';
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, {recursive: true});
}
// 防止返回的数据为空
if (initialCoverageDataForTheCurrentFile && initialCoverageDataForTheCurrentFile.path) {
fs.writeFileSync(`./.canyon_output/coverage-final-${String(Math.random()).replace('0.','')}.json`, JSON.stringify({
[initialCoverageDataForTheCurrentFile.path]: initialCoverageDataForTheCurrentFile
}, null, 2), 'utf-8');
}
}
return initialCoverageDataForTheCurrentFile;
}
10 changes: 10 additions & 0 deletions plugins/babel-plugin-canyon/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ function trim(obj) {
}, {});
}

let onlyOne = true

export default declare((api, config, dirname) => {
api.assertVersion(7);
return {
Expand Down Expand Up @@ -71,8 +73,16 @@ export default declare((api, config, dirname) => {
// 自配置
dsn: config.dsn || process.env['DSN'] || 'http://localhost:3000',
reporter: config.reporter || process.env['REPORTER'] || '-',
ci: config.ci || process.env['CI'] || false,
})

if (onlyOne){
console.log(`canyon args: ----------------`)
console.log(servePa)
console.log(`canyon args: ----------------`)
}
onlyOne = false

const {initialCoverageDataForTheCurrentFile} = visitorProgramExit(api, path, servePa)


Expand Down
2 changes: 1 addition & 1 deletion plugins/babel-plugin-canyon/src/visitor-program-exit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import generate from "@babel/generator";

export const visitorProgramExit = (api,path,serviceParams) => {

const initialCoverageDataForTheCurrentFile = generateInitialCoverage(generate(path.node).code)
const initialCoverageDataForTheCurrentFile = generateInitialCoverage(generate(path.node).code,serviceParams)

if (generate(path.node).code.includes('coverageData')) {
const t = api.types;
Expand Down
1 change: 1 addition & 0 deletions plugins/babel-plugin-canyon/test-babel-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = {
// special: true, //默认false
keepMap: true, // 默认false
// addAttributes:['branch'], //默认全部,有的话用有的,适用于生产
ci:true, // 默认false
}]
]
}

0 comments on commit aa7c136

Please sign in to comment.