From 9e4c80f25a5823c3375ea9396e684860fab19267 Mon Sep 17 00:00:00 2001 From: Hexagon Date: Sun, 14 Apr 2024 20:35:37 +0200 Subject: [PATCH] Make environment variable code cross-runtime --- deno.json | 1 + docs/src/changelog.md | 4 ++++ lib/core/runner.ts | 20 +++++++++++++++----- plugins/splunk-hec/mod.ts | 5 +++-- telemetry.ts | 11 ++++++----- 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/deno.json b/deno.json index 6b9b213..b83c4a5 100644 --- a/deno.json +++ b/deno.json @@ -27,6 +27,7 @@ }, "imports": { + "@cross/env": "jsr:@cross/env@^1.0.0", "@cross/fs": "jsr:@cross/fs@^0.0.9", "@cross/service": "jsr:@cross/service@^1.0.0", "@cross/test": "jsr:@cross/test@^0.0.9", diff --git a/docs/src/changelog.md b/docs/src/changelog.md index ba95322..24c80b6 100644 --- a/docs/src/changelog.md +++ b/docs/src/changelog.md @@ -9,6 +9,10 @@ nav_order: 13 All notable changes to this project will be documented in this section. +## [1.0.0-rc.19] - 2024-04-14 + +- chore(core): Make environment variable code cross-runtime. + ## [1.0.0-rc.18] - 2024-04-14 - fix(packaging): Fix regression bug in upgrader after moving to jsr.io diff --git a/lib/core/runner.ts b/lib/core/runner.ts index b3ddebd..2a4a39a 100644 --- a/lib/core/runner.ts +++ b/lib/core/runner.ts @@ -9,6 +9,8 @@ import type { ProcessConfiguration, Pup } from "./pup.ts" import { readLines, StringReader } from "@std/io" import { BaseRunner, type RunnerCallback, type RunnerResult } from "../types/runner.ts" import { $, type CommandChild } from "dax-sh" +import { getEnv, setEnv } from "@cross/env" + /** * Represents a task runner that executes tasks as regular processes. * Extends the BaseRunner class. @@ -32,6 +34,7 @@ class Runner extends BaseRunner { } const env = this.createEnvironmentConfig() + console.log(env) const child = this.prepareCommand(env) this.process = child.spawn() @@ -114,12 +117,19 @@ class Runner extends BaseRunner { * Extends the PATH environment variable with the path specified in the process configuration. */ private extendPath() { + const targetPaths: string[] = [] + + const pathEnv = getEnv("PATH") + if (pathEnv !== undefined) { + targetPaths.push(pathEnv) + } + if (this.processConfig.path) { - if (Deno.env.has("PATH")) { - Deno.env.set("PATH", `${Deno.env.get("PATH")}:${this.processConfig.path}`) - } else { - Deno.env.set("PATH", `${this.processConfig.path}`) - } + targetPaths.push(this.processConfig.path) + } + + if (targetPaths.length > 0) { + setEnv("PATH", targetPaths.join(":")) } } diff --git a/plugins/splunk-hec/mod.ts b/plugins/splunk-hec/mod.ts index 8c5e1c6..c22cac1 100644 --- a/plugins/splunk-hec/mod.ts +++ b/plugins/splunk-hec/mod.ts @@ -6,6 +6,7 @@ import { type PluginApi, type PluginConfiguration, PluginImplementation } from "../../mod.ts" import { HECClient } from "./hec.ts" +import { getEnv } from "@cross/env" export class PupPlugin extends PluginImplementation { private hecClient: HECClient @@ -24,8 +25,8 @@ export class PupPlugin extends PluginImplementation { hecToken: string } - const url = hecUrl || Deno.env.get("PUP_SPLUNK_HEC_URL") || "" - const token = hecToken || Deno.env.get("PUP_SPLUNK_HEC_TOKEN") || "" + const url = hecUrl || getEnv("PUP_SPLUNK_HEC_URL") || "" + const token = hecToken || getEnv("PUP_SPLUNK_HEC_TOKEN") || "" this.hecClient = new HECClient(url, token) diff --git a/telemetry.ts b/telemetry.ts index 08f98fc..6bb6b57 100644 --- a/telemetry.ts +++ b/telemetry.ts @@ -32,6 +32,7 @@ import { EventEmitter, type EventHandler } from "./lib/common/eventemitter.ts" import { FileIPC } from "./lib/common/ipc.ts" import { exists, isDir } from "@cross/fs" +import { getEnv } from "@cross/env" export interface TelemetryData { sender: string @@ -82,8 +83,8 @@ export class PupTelemetry { } private async sendMainTelemetry() { - const pupTempPath = Deno.env.get("PUP_TEMP_STORAGE") - const pupProcessId = Deno.env.get("PUP_PROCESS_ID") + const pupTempPath = getEnv("PUP_TEMP_STORAGE") + const pupProcessId = getEnv("PUP_PROCESS_ID") if (pupTempPath && (await exists(pupTempPath)) && pupProcessId) { const data: TelemetryData = { @@ -99,8 +100,8 @@ export class PupTelemetry { } private async checkIpc() { - const pupTempPath = Deno.env.get("PUP_TEMP_STORAGE") - const pupProcessId = Deno.env.get("PUP_PROCESS_ID") + const pupTempPath = getEnv("PUP_TEMP_STORAGE") + const pupProcessId = getEnv("PUP_PROCESS_ID") if (pupTempPath && (await isDir(pupTempPath)) && pupProcessId) { const ipcPath = `${pupTempPath}/.${pupProcessId}.ipc` // Process-specific IPC path @@ -155,7 +156,7 @@ export class PupTelemetry { } async emit(targetProcessId: string, event: string, eventData?: T) { - const pupTempPath = Deno.env.get("PUP_TEMP_STORAGE") + const pupTempPath = getEnv("PUP_TEMP_STORAGE") if (pupTempPath && (await isDir(pupTempPath)) && targetProcessId) { const ipcPath = `${pupTempPath}/.${targetProcessId}.ipc` // Target process IPC path