@beseif-solutions/prow-core 0.0.22 → 0.0.24
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.
- package/dist/core.d.ts +2 -2
- package/dist/core.js +2 -2
- package/dist/entities/category.js +1 -1
- package/dist/entities/credentials.d.ts +11 -0
- package/dist/entities/credentials.js +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -1
- package/dist/minified-utils/properties.d.ts +8 -0
- package/dist/minified-utils/{env.js → properties.js} +16 -40
- package/package.json +1 -1
- package/src/core.ts +3 -3
- package/src/entities/category.ts +1 -1
- package/src/entities/credentials.ts +13 -9
- package/src/index.ts +2 -1
- package/src/minified-utils/properties.ts +61 -0
- package/dist/minified-utils/env.d.ts +0 -13
- package/src/minified-utils/env.ts +0 -90
package/dist/core.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import axios from "axios";
|
|
2
2
|
import _ from "lodash";
|
|
3
3
|
import moment from "moment-timezone";
|
|
4
|
-
import {
|
|
4
|
+
import { Properties } from "./minified-utils/properties";
|
|
5
5
|
import { i18n } from "./minified-utils/locales";
|
|
6
6
|
import Joi from "joi";
|
|
7
7
|
export type Core = {
|
|
8
8
|
axios: typeof axios;
|
|
9
9
|
moment: typeof moment;
|
|
10
10
|
lodash: typeof _;
|
|
11
|
-
env:
|
|
11
|
+
env: Properties;
|
|
12
12
|
t: ReturnType<typeof i18n>[`t`];
|
|
13
13
|
joi: typeof Joi;
|
|
14
14
|
};
|
package/dist/core.js
CHANGED
|
@@ -8,7 +8,7 @@ const axios_1 = __importDefault(require("axios"));
|
|
|
8
8
|
const lodash_1 = __importDefault(require("lodash"));
|
|
9
9
|
const moment_timezone_1 = __importDefault(require("moment-timezone"));
|
|
10
10
|
const proxy_1 = require("./minified-utils/proxy");
|
|
11
|
-
const
|
|
11
|
+
const properties_1 = require("./minified-utils/properties");
|
|
12
12
|
const locales_1 = require("./minified-utils/locales");
|
|
13
13
|
const path_1 = __importDefault(require("path"));
|
|
14
14
|
const joi_1 = __importDefault(require("joi"));
|
|
@@ -39,7 +39,7 @@ const core = (options) => ({
|
|
|
39
39
|
}),
|
|
40
40
|
moment: moment_timezone_1.default,
|
|
41
41
|
lodash: lodash_1.default,
|
|
42
|
-
env: new
|
|
42
|
+
env: new properties_1.Properties(options.path, `.env`),
|
|
43
43
|
t: (0, locales_1.i18n)(path_1.default.join(options.path, `/locales`), { locale: options.locale }).t,
|
|
44
44
|
joi: joi_1.default,
|
|
45
45
|
});
|
|
@@ -11,7 +11,7 @@ const getCategories = async () => {
|
|
|
11
11
|
if (!data || typeof data !== `string`) {
|
|
12
12
|
throw new Error(`Could not obtain category list`);
|
|
13
13
|
}
|
|
14
|
-
return data.split(`\n`).map((d) => d.trim());
|
|
14
|
+
return data.split(`\n`).map((d) => d.trim()).filter((d) => d);
|
|
15
15
|
}
|
|
16
16
|
catch (e) {
|
|
17
17
|
throw e;
|
|
@@ -10,6 +10,7 @@ type CommonCredentials<Config extends CredentialsConfig = {}> = Common<Config> &
|
|
|
10
10
|
credentials: Config[`credentials`];
|
|
11
11
|
inputs: undefined;
|
|
12
12
|
outputs: {
|
|
13
|
+
username?: string;
|
|
13
14
|
valid: boolean;
|
|
14
15
|
};
|
|
15
16
|
}>;
|
|
@@ -71,6 +72,16 @@ export type SessionCredentials<Config extends CredentialsConfig = {}> = CommonCr
|
|
|
71
72
|
credentials: Config[`credentials`];
|
|
72
73
|
};
|
|
73
74
|
}>;
|
|
75
|
+
refresh?: CoreFunction<{
|
|
76
|
+
credentials: Config[`credentials`];
|
|
77
|
+
inputs: {
|
|
78
|
+
fields: Record<string, any>;
|
|
79
|
+
};
|
|
80
|
+
outputs: {
|
|
81
|
+
username?: string;
|
|
82
|
+
credentials: Config[`credentials`];
|
|
83
|
+
};
|
|
84
|
+
}>;
|
|
74
85
|
};
|
|
75
86
|
};
|
|
76
87
|
export type StaticCredentials<Config extends CredentialsConfig = {}> = CommonCredentials<Config> & {
|
|
@@ -30,6 +30,7 @@ const sessionCredentialsSchema = () => joi_1.default.object({
|
|
|
30
30
|
type: joi_1.default.valid(`session`).required(),
|
|
31
31
|
functions: joi_1.default.object({
|
|
32
32
|
connect: joi_1.default.function().required(),
|
|
33
|
+
refresh: joi_1.default.function(),
|
|
33
34
|
}).required(),
|
|
34
35
|
})
|
|
35
36
|
.concat(commonCredentialsSchema())
|
package/dist/index.d.ts
CHANGED
|
@@ -6,3 +6,4 @@ export { Action, Event, ImmediateTrigger, ScheduledTrigger, Trigger, actionSchem
|
|
|
6
6
|
export { Provider, providerSchema } from './entities/provider';
|
|
7
7
|
export { Field, SelectFieldChoice, ValidationResult, extract, fieldSchema, fieldsSchema, validate } from './minified-utils/fields';
|
|
8
8
|
export { i18n, localizeFields, localizeInputs, localizeOutputs, localizeProperties } from './minified-utils/locales';
|
|
9
|
+
export { Properties } from './minified-utils/properties';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.localizeProperties = exports.localizeOutputs = exports.localizeInputs = exports.localizeFields = exports.i18n = exports.validate = exports.fieldsSchema = exports.fieldSchema = exports.extract = exports.providerSchema = exports.triggerSchema = exports.scheduledTriggerSchema = exports.immediateTriggerSchema = exports.eventSchema = exports.actionSchema = exports.sessionCredentialsSchema = exports.staticCredentialsSchema = exports.oauthCredentialsSchema = exports.credentialsSchema = exports.idSchema = exports.getCategories = exports.core = void 0;
|
|
3
|
+
exports.Properties = exports.localizeProperties = exports.localizeOutputs = exports.localizeInputs = exports.localizeFields = exports.i18n = exports.validate = exports.fieldsSchema = exports.fieldSchema = exports.extract = exports.providerSchema = exports.triggerSchema = exports.scheduledTriggerSchema = exports.immediateTriggerSchema = exports.eventSchema = exports.actionSchema = exports.sessionCredentialsSchema = exports.staticCredentialsSchema = exports.oauthCredentialsSchema = exports.credentialsSchema = exports.idSchema = exports.getCategories = exports.core = void 0;
|
|
4
4
|
var core_1 = require("./core");
|
|
5
5
|
Object.defineProperty(exports, "core", { enumerable: true, get: function () { return core_1.core; } });
|
|
6
6
|
var category_1 = require("./entities/category");
|
|
@@ -31,3 +31,5 @@ Object.defineProperty(exports, "localizeFields", { enumerable: true, get: functi
|
|
|
31
31
|
Object.defineProperty(exports, "localizeInputs", { enumerable: true, get: function () { return locales_1.localizeInputs; } });
|
|
32
32
|
Object.defineProperty(exports, "localizeOutputs", { enumerable: true, get: function () { return locales_1.localizeOutputs; } });
|
|
33
33
|
Object.defineProperty(exports, "localizeProperties", { enumerable: true, get: function () { return locales_1.localizeProperties; } });
|
|
34
|
+
var properties_1 = require("./minified-utils/properties");
|
|
35
|
+
Object.defineProperty(exports, "Properties", { enumerable: true, get: function () { return properties_1.Properties; } });
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare class Properties {
|
|
2
|
+
private readonly filepath;
|
|
3
|
+
private readonly properties;
|
|
4
|
+
private readonly getFile;
|
|
5
|
+
constructor(root: string, filename: any);
|
|
6
|
+
readonly read: (name: string, unknown?: boolean) => Promise<string>;
|
|
7
|
+
readonly write: (name: string, value: string) => Promise<void>;
|
|
8
|
+
}
|
|
@@ -3,15 +3,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.Properties = void 0;
|
|
7
7
|
const dotenv_1 = require("dotenv");
|
|
8
8
|
const fs_1 = require("fs");
|
|
9
9
|
const lodash_1 = __importDefault(require("lodash"));
|
|
10
10
|
const path_1 = __importDefault(require("path"));
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
this.getEnv = (flag) => {
|
|
11
|
+
class Properties {
|
|
12
|
+
constructor(root, filename) {
|
|
13
|
+
this.getFile = (flag) => {
|
|
15
14
|
try {
|
|
16
15
|
const fd = (0, fs_1.openSync)(this.filepath, flag);
|
|
17
16
|
const buffer = (0, fs_1.readFileSync)(fd);
|
|
@@ -22,19 +21,12 @@ class Env {
|
|
|
22
21
|
throw e;
|
|
23
22
|
}
|
|
24
23
|
};
|
|
25
|
-
this.read = async (name,
|
|
24
|
+
this.read = async (name, unknown = false) => {
|
|
26
25
|
try {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if (!options.create) {
|
|
30
|
-
throw new Error(`Environment variable not found: ${name}`);
|
|
31
|
-
}
|
|
32
|
-
value = await this.prompt(`${name}: `);
|
|
33
|
-
await this.write(name, value);
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
value = lodash_1.default.get(this.env, name);
|
|
26
|
+
if (!lodash_1.default.has(this.properties, name) && !unknown) {
|
|
27
|
+
throw new Error(`Environment variable not found: ${name}`);
|
|
37
28
|
}
|
|
29
|
+
const value = lodash_1.default.get(this.properties, name, undefined);
|
|
38
30
|
return value;
|
|
39
31
|
}
|
|
40
32
|
catch (e) {
|
|
@@ -44,10 +36,10 @@ class Env {
|
|
|
44
36
|
this.write = async (name, value) => {
|
|
45
37
|
try {
|
|
46
38
|
const fd = (0, fs_1.openSync)(this.filepath, `w`);
|
|
47
|
-
this.
|
|
39
|
+
this.properties[name] = value;
|
|
48
40
|
const arr = [];
|
|
49
|
-
for (const key of Object.keys(this.
|
|
50
|
-
arr.push(`${key}=${this.
|
|
41
|
+
for (const key of Object.keys(this.properties)) {
|
|
42
|
+
arr.push(`${key}=${this.properties[key]}`);
|
|
51
43
|
}
|
|
52
44
|
const buffer = Buffer.from(arr.join(`\n`));
|
|
53
45
|
(0, fs_1.writeSync)(fd, buffer, 0, buffer.length);
|
|
@@ -57,35 +49,19 @@ class Env {
|
|
|
57
49
|
throw e;
|
|
58
50
|
}
|
|
59
51
|
};
|
|
60
|
-
this.prompt = async (text) => new Promise(async (resolve, reject) => {
|
|
61
|
-
try {
|
|
62
|
-
const stdin = (0, readline_1.createInterface)({
|
|
63
|
-
input: process.stdin,
|
|
64
|
-
output: process.stdout,
|
|
65
|
-
});
|
|
66
|
-
stdin.on(`close`, () => { reject(`Aborted`); });
|
|
67
|
-
stdin.question(text, (answer) => {
|
|
68
|
-
resolve(answer);
|
|
69
|
-
stdin.close();
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
catch (e) {
|
|
73
|
-
reject(e.message || e);
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
52
|
if (path_1.default.isAbsolute(root)) {
|
|
77
|
-
this.filepath = path_1.default.resolve(root,
|
|
53
|
+
this.filepath = path_1.default.resolve(root, filename);
|
|
78
54
|
}
|
|
79
55
|
else {
|
|
80
56
|
const absolute = process.cwd();
|
|
81
|
-
this.filepath = path_1.default.resolve(absolute, root,
|
|
57
|
+
this.filepath = path_1.default.resolve(absolute, root, filename);
|
|
82
58
|
}
|
|
83
59
|
try {
|
|
84
|
-
this.
|
|
60
|
+
this.properties = this.getFile(`r`);
|
|
85
61
|
}
|
|
86
62
|
catch (e) {
|
|
87
63
|
if (e?.code === `ENOENT`) {
|
|
88
|
-
this.
|
|
64
|
+
this.properties = this.getFile(`w`);
|
|
89
65
|
}
|
|
90
66
|
else {
|
|
91
67
|
throw e;
|
|
@@ -93,4 +69,4 @@ class Env {
|
|
|
93
69
|
}
|
|
94
70
|
}
|
|
95
71
|
}
|
|
96
|
-
exports.
|
|
72
|
+
exports.Properties = Properties;
|
package/package.json
CHANGED
package/src/core.ts
CHANGED
|
@@ -2,7 +2,7 @@ import axios, { AxiosError, AxiosResponse } from "axios";
|
|
|
2
2
|
import _ from "lodash";
|
|
3
3
|
import moment from "moment-timezone";
|
|
4
4
|
import { accessProxy, logProxy, ProxyLogs } from "./minified-utils/proxy";
|
|
5
|
-
import {
|
|
5
|
+
import { Properties } from "./minified-utils/properties";
|
|
6
6
|
import { i18n } from "./minified-utils/locales";
|
|
7
7
|
import path from "path";
|
|
8
8
|
import Joi from "joi";
|
|
@@ -11,7 +11,7 @@ export type Core = {
|
|
|
11
11
|
axios: typeof axios,
|
|
12
12
|
moment: typeof moment,
|
|
13
13
|
lodash: typeof _,
|
|
14
|
-
env:
|
|
14
|
+
env: Properties,
|
|
15
15
|
t: ReturnType<typeof i18n>[`t`],
|
|
16
16
|
joi: typeof Joi,
|
|
17
17
|
};
|
|
@@ -50,7 +50,7 @@ export const core = (options: CoreOptions): Core => ({
|
|
|
50
50
|
}),
|
|
51
51
|
moment: moment,
|
|
52
52
|
lodash: _,
|
|
53
|
-
env: new
|
|
53
|
+
env: new Properties(options.path, `.env`),
|
|
54
54
|
t: i18n(path.join(options.path, `/locales`), { locale: options.locale }).t,
|
|
55
55
|
joi: Joi,
|
|
56
56
|
});
|
package/src/entities/category.ts
CHANGED
|
@@ -4,6 +4,6 @@ export const getCategories = async (): Promise<string[]> => {
|
|
|
4
4
|
try {
|
|
5
5
|
const { data } = await axios.get(`https://dev-beseif.s3.eu-west-1.amazonaws.com/prow/categories.txt`);
|
|
6
6
|
if (!data || typeof data !== `string`) { throw new Error(`Could not obtain category list`); }
|
|
7
|
-
return data.split(`\n`).map((d) => d.trim());
|
|
7
|
+
return data.split(`\n`).map((d) => d.trim()).filter((d) => d);
|
|
8
8
|
} catch (e) { throw e; }
|
|
9
9
|
};
|
|
@@ -11,7 +11,10 @@ type CommonCredentials<Config extends CredentialsConfig = {}> = Common<Config> &
|
|
|
11
11
|
check: CoreFunction<{
|
|
12
12
|
credentials: Config[`credentials`],
|
|
13
13
|
inputs: undefined,
|
|
14
|
-
outputs: {
|
|
14
|
+
outputs: {
|
|
15
|
+
username?: string,
|
|
16
|
+
valid: boolean,
|
|
17
|
+
},
|
|
15
18
|
}>,
|
|
16
19
|
},
|
|
17
20
|
};
|
|
@@ -66,14 +69,14 @@ export type SessionCredentials<Config extends CredentialsConfig = {}> = CommonCr
|
|
|
66
69
|
credentials: Config[`credentials`],
|
|
67
70
|
},
|
|
68
71
|
}>,
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
72
|
+
refresh?: CoreFunction<{
|
|
73
|
+
credentials: Config[`credentials`],
|
|
74
|
+
inputs: { fields: Record<string, any> },
|
|
75
|
+
outputs: {
|
|
76
|
+
username?: string,
|
|
77
|
+
credentials: Config[`credentials`],
|
|
78
|
+
},
|
|
79
|
+
}>,
|
|
77
80
|
},
|
|
78
81
|
};
|
|
79
82
|
|
|
@@ -112,6 +115,7 @@ export const sessionCredentialsSchema = () =>
|
|
|
112
115
|
type: Joi.valid(`session`).required(),
|
|
113
116
|
functions: Joi.object({
|
|
114
117
|
connect: Joi.function().required(),
|
|
118
|
+
refresh: Joi.function(),
|
|
115
119
|
}).required(),
|
|
116
120
|
})
|
|
117
121
|
.concat(commonCredentialsSchema())
|
package/src/index.ts
CHANGED
|
@@ -5,4 +5,5 @@ export { Credentials, OAuthCredentials, StaticCredentials, SessionCredentials, c
|
|
|
5
5
|
export { Action, Event, ImmediateTrigger, ScheduledTrigger, Trigger, actionSchema, eventSchema, immediateTriggerSchema, scheduledTriggerSchema, triggerSchema } from './entities/events';
|
|
6
6
|
export { Provider, providerSchema } from './entities/provider';
|
|
7
7
|
export { Field, SelectFieldChoice, ValidationResult, extract, fieldSchema, fieldsSchema, validate } from './minified-utils/fields';
|
|
8
|
-
export { i18n, localizeFields, localizeInputs, localizeOutputs, localizeProperties } from './minified-utils/locales';
|
|
8
|
+
export { i18n, localizeFields, localizeInputs, localizeOutputs, localizeProperties } from './minified-utils/locales';
|
|
9
|
+
export { Properties } from './minified-utils/properties';
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { DotenvParseOutput, parse } from "dotenv";
|
|
2
|
+
import { closeSync, openSync, readFileSync, writeSync } from "fs";
|
|
3
|
+
import _ from "lodash";
|
|
4
|
+
import path from "path";
|
|
5
|
+
|
|
6
|
+
export class Properties {
|
|
7
|
+
|
|
8
|
+
private readonly filepath: string;
|
|
9
|
+
private readonly properties: DotenvParseOutput;
|
|
10
|
+
|
|
11
|
+
private readonly getFile = (flag: `w` | `r`): DotenvParseOutput => {
|
|
12
|
+
try {
|
|
13
|
+
const fd = openSync(this.filepath, flag);
|
|
14
|
+
const buffer = readFileSync(fd);
|
|
15
|
+
const parsed = parse(buffer);
|
|
16
|
+
return parsed;
|
|
17
|
+
} catch (e) { throw e; }
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
constructor(root: string, filename) {
|
|
21
|
+
if (path.isAbsolute(root)) {
|
|
22
|
+
this.filepath = path.resolve(root, filename);
|
|
23
|
+
} else {
|
|
24
|
+
const absolute = process.cwd();
|
|
25
|
+
this.filepath = path.resolve(absolute, root, filename);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
this.properties = this.getFile(`r`);
|
|
30
|
+
} catch (e) {
|
|
31
|
+
if (e?.code === `ENOENT`) {
|
|
32
|
+
this.properties = this.getFile(`w`);
|
|
33
|
+
} else { throw e; }
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public readonly read = async (name: string, unknown = false): Promise<string> => {
|
|
38
|
+
try {
|
|
39
|
+
if (!_.has(this.properties, name) && !unknown) { throw new Error(`Environment variable not found: ${name}`); }
|
|
40
|
+
const value = _.get(this.properties, name, undefined);
|
|
41
|
+
return value;
|
|
42
|
+
} catch (e) { throw e; }
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
public readonly write = async (name: string, value: string): Promise<void> => {
|
|
46
|
+
try {
|
|
47
|
+
const fd = openSync(this.filepath, `w`);
|
|
48
|
+
|
|
49
|
+
this.properties[name] = value;
|
|
50
|
+
|
|
51
|
+
const arr: string[] = [];
|
|
52
|
+
for (const key of Object.keys(this.properties)) {
|
|
53
|
+
arr.push(`${key}=${this.properties[key]}`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const buffer = Buffer.from(arr.join(`\n`));
|
|
57
|
+
writeSync(fd, buffer, 0, buffer.length);
|
|
58
|
+
closeSync(fd);
|
|
59
|
+
} catch (e) { throw e; }
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
type ReadOptions = {
|
|
2
|
-
create?: boolean;
|
|
3
|
-
};
|
|
4
|
-
export declare class Env {
|
|
5
|
-
private readonly filepath;
|
|
6
|
-
private readonly env;
|
|
7
|
-
private readonly getEnv;
|
|
8
|
-
constructor(root: string);
|
|
9
|
-
readonly read: (name: string, options?: ReadOptions) => Promise<string>;
|
|
10
|
-
readonly write: (name: string, value: string) => Promise<void>;
|
|
11
|
-
private readonly prompt;
|
|
12
|
-
}
|
|
13
|
-
export {};
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { DotenvParseOutput, parse } from "dotenv";
|
|
2
|
-
import { closeSync, openSync, readFileSync, writeSync } from "fs";
|
|
3
|
-
import _ from "lodash";
|
|
4
|
-
import path from "path";
|
|
5
|
-
import { createInterface } from "readline";
|
|
6
|
-
|
|
7
|
-
type ReadOptions = {
|
|
8
|
-
create?: boolean,
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export class Env {
|
|
12
|
-
|
|
13
|
-
private readonly filepath: string;
|
|
14
|
-
private readonly env: DotenvParseOutput;
|
|
15
|
-
|
|
16
|
-
private readonly getEnv = (flag: `w` | `r`): DotenvParseOutput => {
|
|
17
|
-
try {
|
|
18
|
-
const fd = openSync(this.filepath, flag);
|
|
19
|
-
const buffer = readFileSync(fd);
|
|
20
|
-
const parsed = parse(buffer);
|
|
21
|
-
return parsed;
|
|
22
|
-
} catch (e) { throw e; }
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
constructor(root: string) {
|
|
26
|
-
if (path.isAbsolute(root)) {
|
|
27
|
-
this.filepath = path.resolve(root, `.env`);
|
|
28
|
-
} else {
|
|
29
|
-
const absolute = process.cwd();
|
|
30
|
-
this.filepath = path.resolve(absolute, root, `.env`);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
try {
|
|
34
|
-
this.env = this.getEnv(`r`);
|
|
35
|
-
} catch (e) {
|
|
36
|
-
if (e?.code === `ENOENT`) {
|
|
37
|
-
this.env = this.getEnv(`w`);
|
|
38
|
-
} else { throw e; }
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
public readonly read = async (name: string, options: ReadOptions = {}): Promise<string> => {
|
|
43
|
-
try {
|
|
44
|
-
let value: string;
|
|
45
|
-
if (!_.has(this.env, name)) {
|
|
46
|
-
if (!options.create) { throw new Error(`Environment variable not found: ${name}`); }
|
|
47
|
-
value = await this.prompt(`${name}: `);
|
|
48
|
-
await this.write(name, value);
|
|
49
|
-
} else {
|
|
50
|
-
value = _.get(this.env, name);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return value;
|
|
54
|
-
} catch (e) { throw e; }
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
public readonly write = async (name: string, value: string): Promise<void> => {
|
|
58
|
-
try {
|
|
59
|
-
const fd = openSync(this.filepath, `w`);
|
|
60
|
-
|
|
61
|
-
this.env[name] = value;
|
|
62
|
-
|
|
63
|
-
const arr: string[] = [];
|
|
64
|
-
for (const key of Object.keys(this.env)) {
|
|
65
|
-
arr.push(`${key}=${this.env[key]}`);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const buffer = Buffer.from(arr.join(`\n`));
|
|
69
|
-
writeSync(fd, buffer, 0, buffer.length);
|
|
70
|
-
closeSync(fd);
|
|
71
|
-
} catch (e) { throw e; }
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
private readonly prompt = async (text: string): Promise<string> =>
|
|
75
|
-
new Promise(async (resolve, reject) => {
|
|
76
|
-
try {
|
|
77
|
-
const stdin = createInterface({
|
|
78
|
-
input: process.stdin,
|
|
79
|
-
output: process.stdout,
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
stdin.on(`close`, () => { reject(`Aborted`); });
|
|
83
|
-
stdin.question(text, (answer) => {
|
|
84
|
-
resolve(answer);
|
|
85
|
-
stdin.close();
|
|
86
|
-
});
|
|
87
|
-
} catch (e) { reject(e.message || e); }
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
}
|