-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.js
executable file
·63 lines (47 loc) · 1.91 KB
/
env.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env -S deno run --allow-read --allow-write --allow-env --allow-net --allow-run --no-check
import { API as DenoAPI } from "./api_deno.js";
import { load, parse, stringify } from "https://deno.land/[email protected]/dotenv/mod.ts";
export default async function _env(args) {
const functionName = args._.shift()
const _env_str = args._.join("\n")
const _env_file = args.reset ? '' : await Deno.readTextFile(`functions/${functionName}/.env`).catch(e => '')
const _env_ = `${_env_file}\n${_env_str}`
const _env_parsed = parse(_env_)
const _env_str_final = stringify(_env_parsed)
//console.log(_env_parsed, _env_str_final)
await Deno.writeTextFile(`functions/${functionName}/.env`, _env_str_final);
const PROJECT_REF = Deno.env.get('PROJECT_REF')
const DENO_DEPLOY_TOKEN = Deno.env.get("DENO_DEPLOY_TOKEN")
const DENO_DEPLOY_PROJECT = `${PROJECT_REF}-${functionName}`
const FUNCTIONS_DOMAIN = Deno.env.get("FUNCTIONS_DOMAIN")
const envVars = await load({
envPath: `functions/${functionName}/.env`,
defaultsPath: `.env.defaults`,
export: false
});
const denoAPI = DenoAPI.fromToken(DENO_DEPLOY_TOKEN)
const denoProject = await denoAPI.getProject(DENO_DEPLOY_PROJECT);
//let cenvs = {}
denoProject.envVars.forEach(e => {
if (!envVars[e]) {
envVars[e] = null
}
//cenvs[e] = null
})
if (denoProject === null) {
console.log('denoProject doesnt exists')
} else {
const res = await denoAPI.requestJson(`/projects/${denoProject.id}/env`, {
method: 'PATCH',
body: envVars
})
console.log(`
[Done]
%c
https://${PROJECT_REF}.${FUNCTIONS_DOMAIN}/${functionName}
https://${PROJECT_REF}-${functionName}.${FUNCTIONS_DOMAIN}
%c
${_env_str_final}`, 'color: lime', 'background-color: #222;color:#999')
// console.log(envVars)
}
}