Skip to content

chore: add getEnv helper #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/gptscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import http from "http"
import path from "path"
import child_process from "child_process"
import {fileURLToPath} from "url"
import {gunzipSync} from "zlib";

export interface GlobalOpts {
APIKey?: string
Expand Down Expand Up @@ -809,6 +810,22 @@ export interface PromptResponse {
responses: Record<string, string>
}

export function getEnv(key: string, def: string = ''): string {
let v = process.env[key] || ''
if (v == '') {
return def
}

if (v.startsWith('{"_gz":"') && v.endsWith('"}')) {
try {
return gunzipSync(Buffer.from(v.slice(8, -2), 'base64')).toString('utf8')
} catch (e) {
}
}

return v
}

function getCmdPath(): string {
if (process.env.GPTSCRIPT_BIN) {
return process.env.GPTSCRIPT_BIN
Expand Down
13 changes: 12 additions & 1 deletion tests/gptscript.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as gptscript from "../src/gptscript"
import {ArgumentSchemaType, PropertyType, RunEventType, ToolType} from "../src/gptscript"
import {ArgumentSchemaType, getEnv, PropertyType, RunEventType, ToolType} from "../src/gptscript"
import path from "path"
import {fileURLToPath} from "url"

Expand Down Expand Up @@ -535,4 +535,15 @@ describe("gptscript module", () => {

expect(run.err).toEqual("")
})

test("test get_env default", async () => {
const env = getEnv('TEST_ENV_MISSING', 'foo')
expect(env).toEqual('foo')
})

test("test get_env", async () => {
process.env.TEST_ENV = '{"_gz":"H4sIAEosrGYC/ytJLS5RKEvMKU0FACtB3ewKAAAA"}'
const env = getEnv('TEST_ENV', 'missing')
expect(env).toEqual('test value')
})
})