Skip to content

Commit

Permalink
chore: repo overall update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtao25 committed Nov 25, 2024
1 parent 85ecf3c commit 1234426
Show file tree
Hide file tree
Showing 16 changed files with 670 additions and 74 deletions.
4 changes: 4 additions & 0 deletions plugins/vite-plugin-canyon/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,7 @@ demo/
.idea/

pnpm-lock.yaml

dist-test

.canyon_output
5 changes: 5 additions & 0 deletions plugins/vite-plugin-canyon/feature/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function add(a,b) {
return a + b
}

console.log(add(1,2))
15 changes: 11 additions & 4 deletions plugins/vite-plugin-canyon/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-canyon",
"version": "0.0.1-beta.1",
"version": "0.0.1-beta.2",
"license": "MIT",
"files": [
"dist/*"
Expand All @@ -18,7 +18,8 @@
"scripts": {
"dev": "unbuild --stub",
"build": "unbuild",
"prepublishOnly": "npm run build"
"prepublishOnly": "npm run build",
"test": "vite build"
},
"keywords": [
"vite",
Expand All @@ -29,14 +30,20 @@
"canyon"
],
"dependencies": {
"picocolors": "^1.0.0"
"picocolors": "^1.0.0",
"@babel/generator": "^7.26.2",
"@babel/parser": "^7.26.2",
"@babel/traverse": "^7.25.9",
"@babel/types": "^7.26.0"
},
"peerDependencies": {
"vite": ">=2.9.1"
},
"devDependencies": {
"@types/node": "^20.10.6",
"typescript": "^5.3.3",
"unbuild": "^2.0.0"
"unbuild": "^2.0.0",
"vite": "latest",
"vite-plugin-istanbul": "latest"
}
}
13 changes: 13 additions & 0 deletions plugins/vite-plugin-canyon/src/ci_providers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IProvider } from '../types'

import * as providerGitHubactions from './provider_githubactions'
import * as providerGitLabci from './provider_gitlabci'
import * as providerVercel from './provider_vercel'

const providerList: IProvider[] = [
providerGitHubactions,
providerGitLabci,
providerVercel
]

export default providerList
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { IServiceParams, UploaderEnvs, UploaderInputs } from '../types'


export function detect(envs: UploaderEnvs): boolean {
return Boolean(envs.GITHUB_ACTIONS)
}

function _getBranch(inputs: UploaderInputs): string {
const { args, envs } = inputs
const branchRegex = /refs\/heads\/(.*)/
const branchMatches = branchRegex.exec(envs.GITHUB_REF || '')
let branch
if (branchMatches) {
branch = branchMatches[1]
}

if (envs.GITHUB_HEAD_REF && envs.GITHUB_HEAD_REF !== '') {
branch = envs.GITHUB_HEAD_REF
}
return args.branch || branch || ''
}

function _getJob(envs: UploaderEnvs): string {
return (envs.GITHUB_WORKFLOW || '')
}

function _getService(): string {
return 'github-actions'
}

export function getServiceName(): string {
return 'GitHub Actions'
}

function _getSHA(inputs: UploaderInputs): string {
const { args, envs } = inputs
return args.sha || envs.GITHUB_SHA
}

function _getSlug(inputs: UploaderInputs): string {
const { args, envs } = inputs
// if (args.slug !== '') return args.slug
return envs.GITHUB_REPOSITORY_ID || ''
}

export function getServiceParams(inputs: UploaderInputs): IServiceParams {
return {
branch: _getBranch(inputs),
// build: _getBuild(inputs),
// buildURL: await _getBuildURL(inputs),
commit: _getSHA(inputs),
// job: _getJob(inputs.envs),
// pr: _getPR(inputs),
service: _getService(),
slug: _getSlug(inputs),
}
}

export function getEnvVarNames(): string[] {
return [
'GITHUB_ACTION',
'GITHUB_HEAD_REF',
'GITHUB_REF',
'GITHUB_REPOSITORY_ID',
'GITHUB_RUN_ID',
'GITHUB_SERVER_URL',
'GITHUB_SHA',
'GITHUB_WORKFLOW',
]
}
86 changes: 86 additions & 0 deletions plugins/vite-plugin-canyon/src/ci_providers/provider_gitlabci.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { IServiceParams, UploaderEnvs, UploaderInputs } from '../types'

// import { parseSlugFromRemoteAddr } from '../helpers/git'

export function detect(envs: UploaderEnvs): boolean {
return Boolean(envs.GITLAB_CI)
}

function _getBuild(inputs: UploaderInputs): string {
const { args, envs } = inputs
return args.build || envs.CI_BUILD_ID || envs.CI_JOB_ID || ''
}

function _getBuildURL(): string {
return ''
}

function _getBranch(inputs: UploaderInputs): string {
const { args, envs } = inputs
return args.branch || envs.CI_BUILD_REF_NAME || envs.CI_COMMIT_REF_NAME || ''
}

function _getJob(): string {
return ''
}

function _getPR(inputs: UploaderInputs): string {
const { args } = inputs
return args.pr || ''
}

function _getService(): string {
return 'gitlab'
}

export function getServiceName(): string {
return 'GitLab CI'
}

function _getSHA(inputs: UploaderInputs): string {
const { args, envs } = inputs
return args.sha || envs.CI_MERGE_REQUEST_SOURCE_BRANCH_SHA || envs.CI_BUILD_REF || envs.CI_COMMIT_SHA || ''
}

function _getSlug(inputs: UploaderInputs): string {
const { args, envs } = inputs
// if (args.slug !== '') return args.slug
// const remoteAddr = envs.CI_BUILD_REPO || envs.CI_REPOSITORY_URL || ''
// return (
// envs.CI_PROJECT_ID
// // remoteAddr||
// // envs.CI_PROJECT_ID ||
// // parseSlugFromRemoteAddr(remoteAddr) ||
// // ''
// )
return envs.CI_PROJECT_ID
}

export function getServiceParams(inputs: UploaderInputs): IServiceParams {
return {
branch: _getBranch(inputs),
// build: _getBuild(inputs),
// buildURL: _getBuildURL(),
commit: _getSHA(inputs),
// job: _getJob(),
// pr: _getPR(inputs),
service: _getService(),
slug: _getSlug(inputs),
}
}

export function getEnvVarNames(): string[] {
return [
'CI_BUILD_ID',
'CI_BUILD_REF',
'CI_BUILD_REF_NAME',
'CI_BUILD_REPO',
'CI_COMMIT_REF_NAME',
'CI_COMMIT_SHA',
'CI_JOB_ID',
'CI_MERGE_REQUEST_SOURCE_BRANCH_SHA',
'CI_PROJECT_PATH',
'CI_REPOSITORY_URL',
'GITLAB_CI',
]
}
67 changes: 67 additions & 0 deletions plugins/vite-plugin-canyon/src/ci_providers/provider_vercel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { IServiceParams, UploaderEnvs, UploaderInputs } from '../types'


export function detect(envs: UploaderEnvs): boolean {
return Boolean(envs.VERCEL)
}

function _getBranch(inputs: UploaderInputs): string {
const { args, envs } = inputs
const branchRegex = /refs\/heads\/(.*)/
const branchMatches = branchRegex.exec(envs.VERCEL_GIT_COMMIT_REF || '')
let branch
if (branchMatches) {
branch = branchMatches[1]
}

if (envs.VERCEL_GIT_COMMIT_REF && envs.VERCEL_GIT_COMMIT_REF !== '') {
branch = envs.VERCEL_GIT_COMMIT_REF
}
return args.branch || branch || ''
}


function _getService(): string {
return 'vercel'
}

export function getServiceName(): string {
return 'Vercel'
}

function _getSHA(inputs: UploaderInputs): string {
const { args, envs } = inputs
return args.sha || envs.VERCEL_GIT_COMMIT_SHA
}

function _getSlug(inputs: UploaderInputs): string {
const { args, envs } = inputs
// if (args.slug !== '') return args.slug
return envs.VERCEL_GIT_REPO_ID || ''
}

export function getServiceParams(inputs: UploaderInputs): IServiceParams {
return {
branch: _getBranch(inputs),
// build: _getBuild(inputs),
// buildURL: await _getBuildURL(inputs),
commit: _getSHA(inputs),
// job: _getJob(inputs.envs),
// pr: _getPR(inputs),
service: _getService(),
slug: _getSlug(inputs),
}
}

export function getEnvVarNames(): string[] {
return [
'GITHUB_ACTION',
'GITHUB_HEAD_REF',
'GITHUB_REF',
'GITHUB_REPOSITORY_ID',
'GITHUB_RUN_ID',
'GITHUB_SERVER_URL',
'GITHUB_SHA',
'GITHUB_WORKFLOW',
]
}
23 changes: 23 additions & 0 deletions plugins/vite-plugin-canyon/src/helpers/extract-coverage-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export function extractCoverageData(scriptContent) {
const reg0 = /var\s+coverageData\s*=\s*({[\s\S]*?});/;
const reg1 = /var\s+(\w+)\s*=\s*function\s*\(\)\s*\{([\s\S]*?)\}\(\);/
try {
// 可能性一
const match0 = reg0.exec(scriptContent);
if (match0) {
const objectString = match0[1];
return new Function('return ' + objectString)();
}
// 可能性二
const match1 = reg1.exec(scriptContent);
if (match1) {
const functionBody = match1[2];
const func = new Function(functionBody + 'return coverageData;');
const result = func();
return result;
}
} catch (e) {
return null;
}
return null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import fs from 'fs';
import path from 'path'
import {extractCoverageData} from "./extract-coverage-data";

export const generateInitialCoverage = (paramsPath) => {
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-map-${Math.random()}.json`, JSON.stringify({
[initialCoverageDataForTheCurrentFile.path]: initialCoverageDataForTheCurrentFile
}, null, 2), 'utf-8');
}
return initialCoverageDataForTheCurrentFile;
}
Loading

0 comments on commit 1234426

Please sign in to comment.