1 | import * as fs from 'fs-extra'
|
2 | import { dimGrey } from '../colors'
|
3 |
|
4 | export interface Json2EnvOptions {
|
5 | jsonPath: string
|
6 | prefix?: string
|
7 |
|
8 | |
9 |
|
10 |
|
11 | saveEnvFile?: boolean
|
12 |
|
13 | |
14 |
|
15 |
|
16 | bashEnv?: boolean
|
17 |
|
18 | |
19 |
|
20 |
|
21 | githubEnv?: boolean
|
22 |
|
23 | |
24 |
|
25 |
|
26 | fail?: boolean
|
27 |
|
28 | debug?: boolean
|
29 | silent?: boolean
|
30 | }
|
31 |
|
32 | const JSON2ENV_OPT_DEF: Partial<Json2EnvOptions> = {
|
33 | saveEnvFile: true,
|
34 | bashEnv: true,
|
35 | githubEnv: true,
|
36 | fail: true,
|
37 | }
|
38 |
|
39 | export function json2env(opt: Json2EnvOptions): void {
|
40 | const { jsonPath, prefix, saveEnvFile, bashEnv, githubEnv, fail, debug, silent } = {
|
41 | ...JSON2ENV_OPT_DEF,
|
42 | ...opt,
|
43 | }
|
44 |
|
45 | if (!fs.pathExistsSync(jsonPath)) {
|
46 | if (fail) {
|
47 | throw new Error(`Path doesn't exist: ${jsonPath}`)
|
48 | }
|
49 |
|
50 | if (!silent) {
|
51 | console.log(`json2env input file doesn't exist, skipping without error (${jsonPath})`)
|
52 | }
|
53 |
|
54 | if (bashEnv) {
|
55 | appendBashEnv('')
|
56 | }
|
57 |
|
58 | return
|
59 | }
|
60 |
|
61 |
|
62 | const json = fs.readJsonSync(jsonPath)
|
63 |
|
64 | const exportStr = objectToShellExport(json, prefix)
|
65 | const githubStr = objectToGithubActionsEnv(json, prefix)
|
66 |
|
67 | if (debug) {
|
68 | console.log(json, exportStr, githubStr)
|
69 | }
|
70 |
|
71 | if (saveEnvFile) {
|
72 | const shPath = `${jsonPath}.sh`
|
73 | fs.writeFileSync(shPath, exportStr)
|
74 |
|
75 | if (!silent) {
|
76 | console.log(`json2env created ${dimGrey(shPath)}:`)
|
77 | console.log(exportStr)
|
78 | }
|
79 | }
|
80 |
|
81 | if (bashEnv) {
|
82 | appendBashEnv(exportStr)
|
83 | }
|
84 |
|
85 | if (githubEnv) {
|
86 | appendGithubEnv(githubStr)
|
87 | }
|
88 | }
|
89 |
|
90 | function appendBashEnv(exportStr: string): void {
|
91 | const { BASH_ENV } = process.env
|
92 | if (BASH_ENV) {
|
93 | fs.appendFileSync(BASH_ENV, exportStr + '\n')
|
94 |
|
95 | console.log(`BASH_ENV file appended (${dimGrey(BASH_ENV)})`)
|
96 | }
|
97 | }
|
98 |
|
99 | function appendGithubEnv(exportStr: string): void {
|
100 | const { GITHUB_ENV } = process.env
|
101 | if (GITHUB_ENV) {
|
102 | fs.appendFileSync(GITHUB_ENV, exportStr + '\n')
|
103 |
|
104 | console.log(`GITHUB_ENV file appended (${dimGrey(GITHUB_ENV)})`)
|
105 | }
|
106 | }
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | export function objectToShellExport(o: any, prefix = ''): string {
|
120 | return Object.keys(o)
|
121 | .map(k => {
|
122 | const v = o[k]
|
123 | if (v) {
|
124 | return `export ${prefix}${k}="${v}"`
|
125 | }
|
126 | })
|
127 | .filter(Boolean)
|
128 | .join('\n')
|
129 | }
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | export function objectToGithubActionsEnv(o: any, prefix = ''): string {
|
143 | return Object.keys(o)
|
144 | .map(k => {
|
145 | const v = o[k]
|
146 | if (v) {
|
147 | return `${prefix}${k}=${v}`
|
148 | }
|
149 | })
|
150 | .filter(Boolean)
|
151 | .join('\n')
|
152 | }
|