@naturalcycles/nodejs-lib 12.71.0 → 12.74.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;
@@ -38,6 +38,8 @@ function getAjv(opt) {
38
38
  return ajv;
39
39
  }
40
40
  exports.getAjv = getAjv;
41
+ const TS_2500 = 16725225600; // 2500-01-01
42
+ const TS_2000 = 946684800; // 2000-01-01
41
43
  function addCustomAjvFormats(ajv) {
42
44
  return (ajv
43
45
  .addFormat('id', /^[a-z0-9_]{6,64}$/)
@@ -50,15 +52,25 @@ function addCustomAjvFormats(ajv) {
50
52
  .addFormat('unixTimestamp', {
51
53
  type: 'number',
52
54
  validate: (n) => {
53
- // 16725225600 is 2500-01-01 in seconds
54
- return n >= 0 && n < 16725225600;
55
+ return n >= 0 && n < TS_2500;
56
+ },
57
+ })
58
+ .addFormat('unixTimestamp2000', {
59
+ type: 'number',
60
+ validate: (n) => {
61
+ return n >= TS_2000 && n < TS_2500;
55
62
  },
56
63
  })
57
64
  .addFormat('unixTimestampMillis', {
58
65
  type: 'number',
59
66
  validate: (n) => {
60
- // 16725225600000 is 2500-01-01 in milliseconds
61
- return n >= 0 && n < 16725225600000;
67
+ return n >= 0 && n < TS_2500 * 1000;
68
+ },
69
+ })
70
+ .addFormat('unixTimestampMillis2000', {
71
+ type: 'number',
72
+ validate: (n) => {
73
+ return n >= TS_2000 * 1000 && n < TS_2500 * 1000;
62
74
  },
63
75
  })
64
76
  .addFormat('utcOffset', {
@@ -29,7 +29,22 @@ export declare const SLUG_PATTERN: RegExp;
29
29
  * "Slug" - a valid URL, filename, etc.
30
30
  */
31
31
  export declare const slugSchema: import("./string.extensions").ExtendedStringSchema;
32
+ /**
33
+ * Between years 1970 and 2050
34
+ */
32
35
  export declare const unixTimestampSchema: import("./number.extensions").ExtendedNumberSchema;
36
+ /**
37
+ * Between years 2000 and 2050
38
+ */
39
+ export declare const unixTimestamp2000Schema: import("./number.extensions").ExtendedNumberSchema;
40
+ /**
41
+ * Between years 1970 and 2050
42
+ */
43
+ export declare const unixTimestampMillisSchema: import("./number.extensions").ExtendedNumberSchema;
44
+ /**
45
+ * Between years 2000 and 2050
46
+ */
47
+ export declare const unixTimestampMillis2000Schema: import("./number.extensions").ExtendedNumberSchema;
33
48
  export declare const verSchema: import("./number.extensions").ExtendedNumberSchema;
34
49
  /**
35
50
  * Be careful, by default emailSchema does TLD validation. To disable it - use `stringSchema.email({tld: false}).lowercase()`
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.savedDBEntitySchema = exports.baseDBEntitySchema = exports.ipAddressSchema = exports.utcOffsetSchema = exports.userAgentSchema = exports.semVerSchema = exports.SEM_VER_PATTERN = exports.emailSchema = exports.verSchema = exports.unixTimestampSchema = exports.slugSchema = exports.SLUG_PATTERN = exports.idSchema = exports.anyObjectSchema = exports.anySchema = exports.oneOfSchema = exports.objectSchema = exports.arraySchema = exports.urlSchema = exports.binarySchema = exports.dateStringSchema = exports.percentageSchema = exports.integerSchema = exports.numberSchema = exports.stringSchema = exports.booleanDefaultToFalseSchema = exports.booleanSchema = void 0;
3
+ exports.savedDBEntitySchema = exports.baseDBEntitySchema = exports.ipAddressSchema = exports.utcOffsetSchema = exports.userAgentSchema = exports.semVerSchema = exports.SEM_VER_PATTERN = exports.emailSchema = exports.verSchema = exports.unixTimestampMillis2000Schema = exports.unixTimestampMillisSchema = exports.unixTimestamp2000Schema = exports.unixTimestampSchema = exports.slugSchema = exports.SLUG_PATTERN = exports.idSchema = exports.anyObjectSchema = exports.anySchema = exports.oneOfSchema = exports.objectSchema = exports.arraySchema = exports.urlSchema = exports.binarySchema = exports.dateStringSchema = exports.percentageSchema = exports.integerSchema = exports.numberSchema = exports.stringSchema = exports.booleanDefaultToFalseSchema = exports.booleanSchema = void 0;
4
4
  const joi_extensions_1 = require("./joi.extensions");
5
5
  exports.booleanSchema = joi_extensions_1.Joi.boolean();
6
6
  exports.booleanDefaultToFalseSchema = joi_extensions_1.Joi.boolean().default(false);
@@ -40,8 +40,30 @@ exports.SLUG_PATTERN = /^[a-z0-9-]*$/;
40
40
  * "Slug" - a valid URL, filename, etc.
41
41
  */
42
42
  exports.slugSchema = exports.stringSchema.regex(/^[a-z0-9-]{1,255}$/);
43
- // 16725225600 is 2500-01-01
44
- exports.unixTimestampSchema = exports.numberSchema.integer().min(0).max(16725225600);
43
+ const TS_2500 = 16725225600; // 2500-01-01
44
+ const TS_2000 = 946684800; // 2000-01-01
45
+ /**
46
+ * Between years 1970 and 2050
47
+ */
48
+ exports.unixTimestampSchema = exports.numberSchema.integer().min(0).max(TS_2500);
49
+ /**
50
+ * Between years 2000 and 2050
51
+ */
52
+ exports.unixTimestamp2000Schema = exports.numberSchema.integer().min(0).min(TS_2000).max(TS_2500);
53
+ /**
54
+ * Between years 1970 and 2050
55
+ */
56
+ exports.unixTimestampMillisSchema = exports.numberSchema
57
+ .integer()
58
+ .min(0)
59
+ .max(TS_2500 * 1000);
60
+ /**
61
+ * Between years 2000 and 2050
62
+ */
63
+ exports.unixTimestampMillis2000Schema = exports.numberSchema
64
+ .integer()
65
+ .min(TS_2000 * 1000)
66
+ .max(TS_2500 * 1000);
45
67
  // 2
46
68
  exports.verSchema = exports.numberSchema.optional().integer().min(1).max(100);
47
69
  /**
@@ -64,11 +86,11 @@ exports.utcOffsetSchema = exports.numberSchema
64
86
  exports.ipAddressSchema = exports.stringSchema.ip();
65
87
  exports.baseDBEntitySchema = objectSchema({
66
88
  id: exports.stringSchema.optional(),
67
- created: exports.unixTimestampSchema.optional(),
68
- updated: exports.unixTimestampSchema.optional(),
89
+ created: exports.unixTimestamp2000Schema.optional(),
90
+ updated: exports.unixTimestamp2000Schema.optional(),
69
91
  });
70
92
  exports.savedDBEntitySchema = objectSchema({
71
93
  id: exports.stringSchema,
72
- created: exports.unixTimestampSchema,
73
- updated: exports.unixTimestampSchema,
94
+ created: exports.unixTimestamp2000Schema,
95
+ updated: exports.unixTimestamp2000Schema,
74
96
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "12.71.0",
3
+ "version": "12.74.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
+ }
@@ -43,6 +43,9 @@ export function getAjv(opt?: Options): Ajv {
43
43
  return ajv
44
44
  }
45
45
 
46
+ const TS_2500 = 16725225600 // 2500-01-01
47
+ const TS_2000 = 946684800 // 2000-01-01
48
+
46
49
  function addCustomAjvFormats(ajv: Ajv): Ajv {
47
50
  return (
48
51
  ajv
@@ -56,15 +59,25 @@ function addCustomAjvFormats(ajv: Ajv): Ajv {
56
59
  .addFormat('unixTimestamp', {
57
60
  type: 'number',
58
61
  validate: (n: number) => {
59
- // 16725225600 is 2500-01-01 in seconds
60
- return n >= 0 && n < 16725225600
62
+ return n >= 0 && n < TS_2500
63
+ },
64
+ })
65
+ .addFormat('unixTimestamp2000', {
66
+ type: 'number',
67
+ validate: (n: number) => {
68
+ return n >= TS_2000 && n < TS_2500
61
69
  },
62
70
  })
63
71
  .addFormat('unixTimestampMillis', {
64
72
  type: 'number',
65
73
  validate: (n: number) => {
66
- // 16725225600000 is 2500-01-01 in milliseconds
67
- return n >= 0 && n < 16725225600000
74
+ return n >= 0 && n < TS_2500 * 1000
75
+ },
76
+ })
77
+ .addFormat('unixTimestampMillis2000', {
78
+ type: 'number',
79
+ validate: (n: number) => {
80
+ return n >= TS_2000 * 1000 && n < TS_2500 * 1000
68
81
  },
69
82
  })
70
83
  .addFormat('utcOffset', {
@@ -57,8 +57,31 @@ export const SLUG_PATTERN = /^[a-z0-9-]*$/
57
57
  */
58
58
  export const slugSchema = stringSchema.regex(/^[a-z0-9-]{1,255}$/)
59
59
 
60
- // 16725225600 is 2500-01-01
61
- export const unixTimestampSchema = numberSchema.integer().min(0).max(16725225600)
60
+ const TS_2500 = 16725225600 // 2500-01-01
61
+ const TS_2000 = 946684800 // 2000-01-01
62
+
63
+ /**
64
+ * Between years 1970 and 2050
65
+ */
66
+ export const unixTimestampSchema = numberSchema.integer().min(0).max(TS_2500)
67
+ /**
68
+ * Between years 2000 and 2050
69
+ */
70
+ export const unixTimestamp2000Schema = numberSchema.integer().min(0).min(TS_2000).max(TS_2500)
71
+ /**
72
+ * Between years 1970 and 2050
73
+ */
74
+ export const unixTimestampMillisSchema = numberSchema
75
+ .integer()
76
+ .min(0)
77
+ .max(TS_2500 * 1000)
78
+ /**
79
+ * Between years 2000 and 2050
80
+ */
81
+ export const unixTimestampMillis2000Schema = numberSchema
82
+ .integer()
83
+ .min(TS_2000 * 1000)
84
+ .max(TS_2500 * 1000)
62
85
 
63
86
  // 2
64
87
  export const verSchema = numberSchema.optional().integer().min(1).max(100)
@@ -88,12 +111,12 @@ export const ipAddressSchema = stringSchema.ip()
88
111
 
89
112
  export const baseDBEntitySchema = objectSchema<BaseDBEntity>({
90
113
  id: stringSchema.optional(),
91
- created: unixTimestampSchema.optional(),
92
- updated: unixTimestampSchema.optional(),
114
+ created: unixTimestamp2000Schema.optional(),
115
+ updated: unixTimestamp2000Schema.optional(),
93
116
  })
94
117
 
95
118
  export const savedDBEntitySchema = objectSchema<SavedDBEntity>({
96
119
  id: stringSchema,
97
- created: unixTimestampSchema,
98
- updated: unixTimestampSchema,
120
+ created: unixTimestamp2000Schema,
121
+ updated: unixTimestamp2000Schema,
99
122
  })