From 1dc73c4226e11efbfe8a225e489470ef249d16fa Mon Sep 17 00:00:00 2001 From: Tsiry Sandratraina Date: Sun, 3 Mar 2024 18:44:09 +0000 Subject: [PATCH] make it work with Node.js and Bun --- .github/workflows/ci.yml | 10 ++++++++++ README.md | 2 +- deno.json | 2 +- deps.ts | 6 +++++- src/dagger/jobs.ts | 16 ++++++---------- src/dagger/lib.ts | 14 ++++++++++---- src/dagger/pipeline.ts | 5 +++-- 7 files changed, 36 insertions(+), 19 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3fba3ad..02e6f47 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,3 +20,13 @@ jobs: run: dagger run deno run -A src/dagger/runner.ts env: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} + publish: + needs: tests + runs-on: ubuntu-latest + permissions: + contents: read + id-token: write + steps: + - uses: actions/checkout@v4 + - name: Publish package + run: npx jsr publish diff --git a/README.md b/README.md index ae88128..ae57c75 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ dagger call upload --src . --token CODECOV_TOKEN | CODECOV_URL | Your Codecov URL. | Optional | | COVERAGE_FILE | Your coverage file. | Optional | -## 📝 Jobs +## ✨ Jobs | Job | Description | |---------|----------------------------------| diff --git a/deno.json b/deno.json index 0cbffd0..c83e65d 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@fluentci/codecov", - "version": "0.9.0", + "version": "0.9.1", "exports": "./mod.ts", "importMap": "import_map.json", "tasks": { diff --git a/deps.ts b/deps.ts index 1e5c0ba..8880d84 100644 --- a/deps.ts +++ b/deps.ts @@ -1,7 +1,7 @@ export { assertEquals } from "jsr:@std/testing@0.218.2/asserts"; export type { DirectoryID, SecretID } from "./sdk/client.gen.ts"; -export { Directory, Secret } from "./sdk/client.gen.ts"; +export { dag, Directory, Secret, Container } from "./sdk/client.gen.ts"; export { brightGreen } from "jsr:@std/fmt@0.218.2/colors"; export { stringifyTree } from "npm:stringify-tree@1.1.1"; import { gql } from "npm:graphql-request@6.1.0"; @@ -14,6 +14,10 @@ const snakeCase = _.default.snakeCase; const camelCase = _.default.camelCase; export { snakeCase, camelCase }; +import * as env from "jsr:@tsirysndr/env-js"; +export { env }; +export { exit } from "jsr:@tsirysndr/exit-js"; + export { ClientError, GraphQLClient } from "npm:graphql-request@6.1.0"; export { DaggerSDKError, diff --git a/src/dagger/jobs.ts b/src/dagger/jobs.ts index a8f3a0c..4b8bda3 100644 --- a/src/dagger/jobs.ts +++ b/src/dagger/jobs.ts @@ -2,8 +2,7 @@ * @module codecov * @description Uploads code coverage to Codecov ☂️ */ -import { Directory, Secret } from "../../deps.ts"; -import { dag } from "../../sdk/client.gen.ts"; +import { dag, Directory, Secret, env, exit } from "../../deps.ts"; import { getDirectory, getCodecovToken } from "./lib.ts"; export enum Job { @@ -27,7 +26,8 @@ export async function upload( const secret = await getCodecovToken(token); if (!secret) { console.error("CODECOV_TOKEN is not set. Skipping code coverage upload."); - Deno.exit(1); + exit(1); + return ""; } const ctr = dag @@ -46,18 +46,14 @@ export async function upload( .withDirectory("/app", context, { exclude }) .withWorkdir("/app") .withSecretVariable("CODECOV_TOKEN", secret) - .withEnvVariable("CODECOV_URL", Deno.env.get("CODECOV_URL") || "") + .withEnvVariable("CODECOV_URL", env.get("CODECOV_URL") || "") .withExec(["ls", "-la"]) .withExec([ "sh", "-c", `codecov -t $CODECOV_TOKEN ${ - Deno.env.get("CODECOV_URL") ? `--url $CODECOV_URL` : "" - } ${ - Deno.env.get("COVERAGE_FILE") - ? `-f ${Deno.env.get("COVERAGE_FILE")}` - : "" - }`, + env.get("CODECOV_URL") ? `--url $CODECOV_URL` : "" + } ${env.get("COVERAGE_FILE") ? `-f ${env.get("COVERAGE_FILE")}` : ""}`, ]); const result = await ctr.stdout(); diff --git a/src/dagger/lib.ts b/src/dagger/lib.ts index 05f7c19..90e4f26 100644 --- a/src/dagger/lib.ts +++ b/src/dagger/lib.ts @@ -1,5 +1,11 @@ -import { Directory, DirectoryID, Secret, SecretID } from "../../deps.ts"; -import { dag } from "../../sdk/client.gen.ts"; +import { + dag, + env, + Directory, + DirectoryID, + Secret, + SecretID, +} from "../../deps.ts"; export const getDirectory = async ( src: string | Directory | undefined = "." @@ -24,8 +30,8 @@ export const getDirectory = async ( }; export const getCodecovToken = async (token?: string | Secret) => { - if (Deno.env.get("CODECOV_TOKEN")) { - return dag.setSecret("CODECOV_TOKEN", Deno.env.get("CODECOV_TOKEN")!); + if (env.get("CODECOV_TOKEN")) { + return dag.setSecret("CODECOV_TOKEN", env.get("CODECOV_TOKEN")!); } if (token && typeof token === "string") { try { diff --git a/src/dagger/pipeline.ts b/src/dagger/pipeline.ts index 5b90849..eb05a80 100644 --- a/src/dagger/pipeline.ts +++ b/src/dagger/pipeline.ts @@ -1,4 +1,5 @@ import * as jobs from "./jobs.ts"; +import { env } from "../../deps.ts"; const { upload, runnableJobs } = jobs; @@ -8,7 +9,7 @@ export default async function pipeline(src = ".", args: string[] = []) { return; } - await upload(src, Deno.env.get("CODECOV_TOKEN") || ""); + await upload(src, env.get("CODECOV_TOKEN") || ""); } async function runSpecificJobs(args: jobs.Job[]) { @@ -17,6 +18,6 @@ async function runSpecificJobs(args: jobs.Job[]) { if (!job) { throw new Error(`Job ${name} not found`); } - await job(".", Deno.env.get("CODECOV_TOKEN") || ""); + await job(".", env.get("CODECOV_TOKEN") || ""); } }