Skip to content

Commit

Permalink
Make environment variable code cross-runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Apr 14, 2024
1 parent 6993b76 commit 9e4c80f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
1 change: 1 addition & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions docs/src/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 15 additions & 5 deletions lib/core/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -32,6 +34,7 @@ class Runner extends BaseRunner {
}

const env = this.createEnvironmentConfig()
console.log(env)
const child = this.prepareCommand(env)

this.process = child.spawn()
Expand Down Expand Up @@ -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(":"))
}
}

Expand Down
5 changes: 3 additions & 2 deletions plugins/splunk-hec/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down
11 changes: 6 additions & 5 deletions telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand All @@ -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
Expand Down Expand Up @@ -155,7 +156,7 @@ export class PupTelemetry {
}

async emit<T>(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
Expand Down

0 comments on commit 9e4c80f

Please sign in to comment.