UNPKG

2.84 kBPlain TextView Raw
1import * as fs from 'fs-extra'
2import { dimGrey } from '../colors'
3
4export interface Json2EnvOptions {
5 jsonPath: string
6 prefix?: string
7
8 /**
9 * @default true
10 */
11 saveEnvFile?: boolean
12
13 /**
14 * @default true
15 */
16 bashEnv?: boolean
17
18 /**
19 * @default true
20 */
21 githubEnv?: boolean
22
23 /**
24 * @default true
25 */
26 fail?: boolean
27
28 debug?: boolean
29 silent?: boolean
30}
31
32const JSON2ENV_OPT_DEF: Partial<Json2EnvOptions> = {
33 saveEnvFile: true,
34 bashEnv: true,
35 githubEnv: true,
36 fail: true,
37}
38
39export 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 // read file
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
90function 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
99function 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 * Turns Object with keys/values into a *.sh script that exports all keys as values.
110 *
111 * @example
112 * { a: 'b', b: 'c'}
113 *
114 * will turn into:
115 *
116 * export a="b"
117 * export b="c"
118 */
119export 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 * Turns Object with keys/values into a file of key-value pairs
133 *
134 * @example
135 * { a: 'b', b: 'c'}
136 *
137 * will turn into:
138 *
139 * a=b
140 * b=c
141 */
142export 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}