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 19, 2024
1 parent 500dce0 commit bf963eb
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 14 deletions.
2 changes: 2 additions & 0 deletions plugins/babel-plugin-canyon/src/ci_providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ 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
67 changes: 67 additions & 0 deletions plugins/babel-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',
'GITHUB_RUN_ID',
'GITHUB_SERVER_URL',
'GITHUB_SHA',
'GITHUB_WORKFLOW',
]
}
64 changes: 64 additions & 0 deletions plugins/babel-plugin-canyon/src/helpers/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* We really only need three log levels
* * Error
* * Info
* * Verbose
*/

function _getTimestamp() {
return new Date().toISOString()
}

/**
*
* @param {string} message - message to log
* @param {boolean} shouldVerbose - value of the verbose flag
* @return void
*/
export function verbose(message: string, shouldVerbose: boolean): void {
if (shouldVerbose === true) {
console.debug(`[${_getTimestamp()}] ['verbose'] ${message}`)
}
}

/**
*
* @param {string} message - message to log
* @return void
*/
export function logError(message: string): void {
console.error(`[${_getTimestamp()}] ['error'] ${message}`)
}

/**
*
* @param {string} message - message to log
* @return void
*/
export function info(message: string): void {
console.log(`[${_getTimestamp()}] ['info'] ${message}`)
}

export class UploadLogger {
private static _instance: UploadLogger
logLevel = 'info'

private constructor() {
// Intentionally empty
}

static getInstance(): UploadLogger {
if (!UploadLogger._instance) {
UploadLogger._instance = new UploadLogger()
}
return UploadLogger._instance;
}

static setLogLevel(level: string) {
UploadLogger.getInstance().logLevel = level
}

static verbose(message: string) {
verbose(message, UploadLogger.getInstance().logLevel === 'verbose')
}
}
53 changes: 53 additions & 0 deletions plugins/babel-plugin-canyon/src/helpers/provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import providers from '../ci_providers'
import { info, logError, UploadLogger } from '../helpers/logger'
import { IServiceParams, UploaderInputs } from '../types'

export function detectProvider(
inputs: UploaderInputs,
hasToken = false,
): Partial<IServiceParams> {
const { args } = inputs
let serviceParams: Partial<IServiceParams> | undefined

// check if we have a complete set of manual overrides (slug, SHA)
if (args.sha && (args.slug || hasToken)) {
// We have the needed args for a manual override
info(`Using manual override from args.`)
serviceParams = {
commit: args.sha,
...(hasToken ? {} : { slug: args.slug }),
}
} else {
serviceParams = undefined
}

// loop though all providers
try {
const serviceParams = walkProviders(inputs)
return { ...serviceParams, ...serviceParams }
} catch (error) {
// if fails, display message explaining failure, and explaining that SHA and slug need to be set as args
if (typeof serviceParams !== 'undefined') {
logError(`Error detecting repos setting using git: ${error}`)
} else {
// throw new Error(
// '\nUnable to detect SHA and slug, please specify them manually.\nSee the help for more details.',
// )
}
}
return serviceParams
}

export function walkProviders(inputs: UploaderInputs): IServiceParams {
for (const provider of providers) {
if (provider.detect(inputs.envs)) {
info(`Detected ${provider.getServiceName()} as the CI provider.`)
UploadLogger.verbose('-> Using the following env variables:')
for (const envVarName of provider.getEnvVarNames()) {
UploadLogger.verbose(` ${envVarName}: ${inputs.envs[envVarName]}`)
}
return provider.getServiceParams(inputs)
}
}
throw new Error(`Unable to detect provider.`)
}
29 changes: 15 additions & 14 deletions plugins/babel-plugin-canyon/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import generate from "@babel/generator";
// import generate from "@babel/generator";
import { declare } from "@babel/helper-plugin-utils";
import packageJson from "../package.json";
// import packageJson from "../package.json";
import {visitorProgramExit} from "./visitor-program-exit";
import providers from './ci_providers'
// import providers from './ci_providers'
import {detectProvider} from "./helpers/provider";

export default declare((api, config) => {
api.assertVersion(7);
return {
visitor: {
Program: {
exit: (path) => {
for (const provider of providers) {
if (provider.detect(process.env)) {
const con = provider.getServiceParams({
envs: process.env,
// @ts-ignore
args: {}
})
console.log(con)
// 侦测流水线
// 优先级:手动设置 > CI/CD提供商
const serviceParams = detectProvider({
envs: process.env,
args: {
projectID: config.projectID,
sha: config.sha,
instrumentCwd: config.instrumentCwd,
branch: config.branch,
}
}
// throw new Error(`Unable to detect provider.`)
// console.log("Babel Plugin Canyon")
})
console.log(serviceParams,'serviceParams')
visitorProgramExit(api,path)
}
},
Expand Down

0 comments on commit bf963eb

Please sign in to comment.