Skip to content

Commit

Permalink
Merge pull request #76 from ibuildthecloud/main
Browse files Browse the repository at this point in the history
chore: add getEnv helper
  • Loading branch information
ibuildthecloud authored Aug 2, 2024
2 parents 04d0a85 + 8c663fd commit 3db5f9f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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')
})
})

0 comments on commit 3db5f9f

Please sign in to comment.