-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85ecf3c
commit 1234426
Showing
16 changed files
with
670 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,3 +129,7 @@ demo/ | |
.idea/ | ||
|
||
pnpm-lock.yaml | ||
|
||
dist-test | ||
|
||
.canyon_output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
70 changes: 70 additions & 0 deletions
70
plugins/vite-plugin-canyon/src/ci_providers/provider_githubactions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
86
plugins/vite-plugin-canyon/src/ci_providers/provider_gitlabci.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
67
plugins/vite-plugin-canyon/src/ci_providers/provider_vercel.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
plugins/vite-plugin-canyon/src/helpers/extract-coverage-data.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
19 changes: 19 additions & 0 deletions
19
plugins/vite-plugin-canyon/src/helpers/generate-initial-coverage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.