@naturalcycles/nodejs-lib 12.71.0 → 12.72.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,6 +19,11 @@ const script_1 = require("../script");
19
19
  desc: 'Populate $BASH_ENV file if BASH_ENV env variable exists',
20
20
  default: true,
21
21
  },
22
+ githubEnv: {
23
+ type: 'boolean',
24
+ desc: 'Populate $GITHUB_ENV file if GITHUB_ENV env variable exists',
25
+ default: true,
26
+ },
22
27
  fail: {
23
28
  type: 'boolean',
24
29
  desc: 'Fail (exit status 1) on non-existing input file',
@@ -31,7 +36,7 @@ const script_1 = require("../script");
31
36
  type: 'boolean',
32
37
  },
33
38
  });
34
- const { _: args, prefix, saveEnvFile, bashEnv, fail, debug, silent } = argv;
39
+ const { _: args, prefix, saveEnvFile, bashEnv, githubEnv, fail, debug, silent } = argv;
35
40
  if (debug)
36
41
  console.log({ argv });
37
42
  const jsonPath = args[0];
@@ -40,6 +45,7 @@ const script_1 = require("../script");
40
45
  prefix,
41
46
  saveEnvFile,
42
47
  bashEnv,
48
+ githubEnv,
43
49
  fail,
44
50
  debug,
45
51
  silent,
@@ -9,6 +9,10 @@ export interface Json2EnvOptions {
9
9
  * @default true
10
10
  */
11
11
  bashEnv?: boolean;
12
+ /**
13
+ * @default true
14
+ */
15
+ githubEnv?: boolean;
12
16
  /**
13
17
  * @default true
14
18
  */
@@ -29,3 +33,15 @@ export declare function json2env(opt: Json2EnvOptions): void;
29
33
  * export b="c"
30
34
  */
31
35
  export declare function objectToShellExport(o: any, prefix?: string): string;
36
+ /**
37
+ * Turns Object with keys/values into a file of key-value pairs
38
+ *
39
+ * @example
40
+ * { a: 'b', b: 'c'}
41
+ *
42
+ * will turn into:
43
+ *
44
+ * a=b
45
+ * b=c
46
+ */
47
+ export declare function objectToGithubActionsEnv(o: any, prefix?: string): string;
@@ -1,15 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.objectToShellExport = exports.json2env = void 0;
3
+ exports.objectToGithubActionsEnv = exports.objectToShellExport = exports.json2env = void 0;
4
4
  const fs = require("fs-extra");
5
5
  const colors_1 = require("../colors");
6
6
  const JSON2ENV_OPT_DEF = {
7
7
  saveEnvFile: true,
8
8
  bashEnv: true,
9
+ githubEnv: true,
9
10
  fail: true,
10
11
  };
11
12
  function json2env(opt) {
12
- const { jsonPath, prefix, saveEnvFile, bashEnv, fail, debug, silent } = {
13
+ const { jsonPath, prefix, saveEnvFile, bashEnv, githubEnv, fail, debug, silent } = {
13
14
  ...JSON2ENV_OPT_DEF,
14
15
  ...opt,
15
16
  };
@@ -28,8 +29,9 @@ function json2env(opt) {
28
29
  // read file
29
30
  const json = fs.readJsonSync(jsonPath);
30
31
  const exportStr = objectToShellExport(json, prefix);
32
+ const githubStr = objectToGithubActionsEnv(json, prefix);
31
33
  if (debug) {
32
- console.log(json, exportStr);
34
+ console.log(json, exportStr, githubStr);
33
35
  }
34
36
  if (saveEnvFile) {
35
37
  const shPath = `${jsonPath}.sh`;
@@ -42,6 +44,9 @@ function json2env(opt) {
42
44
  if (bashEnv) {
43
45
  appendBashEnv(exportStr);
44
46
  }
47
+ if (githubEnv) {
48
+ appendGithubEnv(githubStr);
49
+ }
45
50
  }
46
51
  exports.json2env = json2env;
47
52
  function appendBashEnv(exportStr) {
@@ -51,6 +56,13 @@ function appendBashEnv(exportStr) {
51
56
  console.log(`BASH_ENV file appended (${(0, colors_1.dimGrey)(BASH_ENV)})`);
52
57
  }
53
58
  }
59
+ function appendGithubEnv(exportStr) {
60
+ const { GITHUB_ENV } = process.env;
61
+ if (GITHUB_ENV) {
62
+ fs.appendFileSync(GITHUB_ENV, exportStr + '\n');
63
+ console.log(`GITHUB_ENV file appended (${(0, colors_1.dimGrey)(GITHUB_ENV)})`);
64
+ }
65
+ }
54
66
  /**
55
67
  * Turns Object with keys/values into a *.sh script that exports all keys as values.
56
68
  *
@@ -74,3 +86,26 @@ function objectToShellExport(o, prefix = '') {
74
86
  .join('\n');
75
87
  }
76
88
  exports.objectToShellExport = objectToShellExport;
89
+ /**
90
+ * Turns Object with keys/values into a file of key-value pairs
91
+ *
92
+ * @example
93
+ * { a: 'b', b: 'c'}
94
+ *
95
+ * will turn into:
96
+ *
97
+ * a=b
98
+ * b=c
99
+ */
100
+ function objectToGithubActionsEnv(o, prefix = '') {
101
+ return Object.keys(o)
102
+ .map(k => {
103
+ const v = o[k];
104
+ if (v) {
105
+ return `${prefix}${k}=${v}`;
106
+ }
107
+ })
108
+ .filter(Boolean)
109
+ .join('\n');
110
+ }
111
+ exports.objectToGithubActionsEnv = objectToGithubActionsEnv;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "12.71.0",
3
+ "version": "12.72.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
@@ -19,6 +19,11 @@ runScript(() => {
19
19
  desc: 'Populate $BASH_ENV file if BASH_ENV env variable exists',
20
20
  default: true,
21
21
  },
22
+ githubEnv: {
23
+ type: 'boolean',
24
+ desc: 'Populate $GITHUB_ENV file if GITHUB_ENV env variable exists',
25
+ default: true,
26
+ },
22
27
  fail: {
23
28
  type: 'boolean',
24
29
  desc: 'Fail (exit status 1) on non-existing input file',
@@ -32,7 +37,7 @@ runScript(() => {
32
37
  },
33
38
  })
34
39
 
35
- const { _: args, prefix, saveEnvFile, bashEnv, fail, debug, silent } = argv
40
+ const { _: args, prefix, saveEnvFile, bashEnv, githubEnv, fail, debug, silent } = argv
36
41
  if (debug) console.log({ argv })
37
42
 
38
43
  const jsonPath = args[0] as string
@@ -42,6 +47,7 @@ runScript(() => {
42
47
  prefix,
43
48
  saveEnvFile,
44
49
  bashEnv,
50
+ githubEnv,
45
51
  fail,
46
52
  debug,
47
53
  silent,
@@ -15,6 +15,11 @@ export interface Json2EnvOptions {
15
15
  */
16
16
  bashEnv?: boolean
17
17
 
18
+ /**
19
+ * @default true
20
+ */
21
+ githubEnv?: boolean
22
+
18
23
  /**
19
24
  * @default true
20
25
  */
@@ -27,11 +32,12 @@ export interface Json2EnvOptions {
27
32
  const JSON2ENV_OPT_DEF: Partial<Json2EnvOptions> = {
28
33
  saveEnvFile: true,
29
34
  bashEnv: true,
35
+ githubEnv: true,
30
36
  fail: true,
31
37
  }
32
38
 
33
39
  export function json2env(opt: Json2EnvOptions): void {
34
- const { jsonPath, prefix, saveEnvFile, bashEnv, fail, debug, silent } = {
40
+ const { jsonPath, prefix, saveEnvFile, bashEnv, githubEnv, fail, debug, silent } = {
35
41
  ...JSON2ENV_OPT_DEF,
36
42
  ...opt,
37
43
  }
@@ -56,8 +62,10 @@ export function json2env(opt: Json2EnvOptions): void {
56
62
  const json = fs.readJsonSync(jsonPath)
57
63
 
58
64
  const exportStr = objectToShellExport(json, prefix)
65
+ const githubStr = objectToGithubActionsEnv(json, prefix)
66
+
59
67
  if (debug) {
60
- console.log(json, exportStr)
68
+ console.log(json, exportStr, githubStr)
61
69
  }
62
70
 
63
71
  if (saveEnvFile) {
@@ -73,6 +81,10 @@ export function json2env(opt: Json2EnvOptions): void {
73
81
  if (bashEnv) {
74
82
  appendBashEnv(exportStr)
75
83
  }
84
+
85
+ if (githubEnv) {
86
+ appendGithubEnv(githubStr)
87
+ }
76
88
  }
77
89
 
78
90
  function appendBashEnv(exportStr: string): void {
@@ -84,6 +96,15 @@ function appendBashEnv(exportStr: string): void {
84
96
  }
85
97
  }
86
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
+
87
108
  /**
88
109
  * Turns Object with keys/values into a *.sh script that exports all keys as values.
89
110
  *
@@ -106,3 +127,26 @@ export function objectToShellExport(o: any, prefix = ''): string {
106
127
  .filter(Boolean)
107
128
  .join('\n')
108
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
+ */
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
+ }