@cloud-cli/env 1.0.0 → 1.1.1
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/README.md +17 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +18 -9
- package/package.json +5 -13
- package/src/index.spec.js +10 -3
- package/src/index.ts +22 -9
- package/tsconfig.json +1 -8
package/README.md
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
1
|
# env
|
|
2
2
|
|
|
3
3
|
Storage for app environment variables
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
`cloudy.conf.mjs`:
|
|
8
|
+
```ts
|
|
9
|
+
import env from '@cloud-cli/env'
|
|
10
|
+
export default { env };
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
cy env.reload
|
|
15
|
+
cy env.set --app test --key key --value 123
|
|
16
|
+
cy env.apps
|
|
17
|
+
cy env.show --app test
|
|
18
|
+
cy env.get --app test --key key
|
|
19
|
+
cy env.delete --app test --key key
|
|
20
|
+
```
|
package/dist/index.d.ts
CHANGED
|
@@ -9,14 +9,16 @@ export interface AppKeyValue extends App, KeyValue {
|
|
|
9
9
|
}
|
|
10
10
|
declare function reload(): void;
|
|
11
11
|
declare function show(options: App): Promise<unknown>;
|
|
12
|
+
declare function apps(): Promise<unknown>;
|
|
12
13
|
declare function set(options: AppKeyValue): Promise<unknown>;
|
|
13
14
|
declare function remove(options: AppKeyValue): Promise<unknown>;
|
|
14
15
|
declare function get(options: Omit<AppKeyValue, 'value'>): Promise<unknown>;
|
|
15
16
|
declare const _default: {
|
|
16
|
-
reload: typeof reload;
|
|
17
|
-
show: typeof show;
|
|
18
17
|
get: typeof get;
|
|
19
18
|
set: typeof set;
|
|
20
|
-
|
|
19
|
+
show: typeof show;
|
|
20
|
+
remove: typeof remove;
|
|
21
|
+
apps: typeof apps;
|
|
22
|
+
reload: typeof reload;
|
|
21
23
|
};
|
|
22
24
|
export default _default;
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,8 @@ import sqlite from 'sqlite3';
|
|
|
2
2
|
import { join } from 'path';
|
|
3
3
|
const appNotSpecifiedError = new Error('App not specified');
|
|
4
4
|
const keyNotSpecifiedError = new Error('Key not specified');
|
|
5
|
+
/* istanbul ignore next */
|
|
6
|
+
const valueOrError = (resolve, reject) => (error, value = null) => error ? reject(error) : resolve(value);
|
|
5
7
|
let db;
|
|
6
8
|
const columns = {
|
|
7
9
|
App: 'app',
|
|
@@ -10,9 +12,7 @@ const columns = {
|
|
|
10
12
|
};
|
|
11
13
|
function close() {
|
|
12
14
|
try {
|
|
13
|
-
|
|
14
|
-
db.close();
|
|
15
|
-
}
|
|
15
|
+
db?.close();
|
|
16
16
|
}
|
|
17
17
|
catch { }
|
|
18
18
|
;
|
|
@@ -23,7 +23,10 @@ function reload() {
|
|
|
23
23
|
if (inMemory) {
|
|
24
24
|
sqlite.verbose();
|
|
25
25
|
}
|
|
26
|
-
const databasePath =
|
|
26
|
+
const databasePath = {
|
|
27
|
+
true: ':memory:',
|
|
28
|
+
false: join(process.cwd(), 'env.db')
|
|
29
|
+
}[String(inMemory)];
|
|
27
30
|
db = new sqlite.Database(databasePath);
|
|
28
31
|
db.run(`CREATE TABLE IF NOT EXISTS env (
|
|
29
32
|
${columns.App} TEXT,
|
|
@@ -46,7 +49,14 @@ async function show(options) {
|
|
|
46
49
|
${columns.Value}
|
|
47
50
|
FROM env WHERE
|
|
48
51
|
${columns.App} = ?`)
|
|
49
|
-
.all([app], (
|
|
52
|
+
.all([app], valueOrError(resolve, reject));
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
async function apps() {
|
|
56
|
+
return new Promise((resolve, reject) => {
|
|
57
|
+
db
|
|
58
|
+
.prepare(`SELECT ${columns.App} FROM env`)
|
|
59
|
+
.all([], valueOrError(resolve, reject));
|
|
50
60
|
});
|
|
51
61
|
}
|
|
52
62
|
async function set(options) {
|
|
@@ -87,7 +97,7 @@ async function get(options) {
|
|
|
87
97
|
${columns.Value}
|
|
88
98
|
FROM env WHERE
|
|
89
99
|
${columns.App} = ? AND ${columns.Key} = ?`)
|
|
90
|
-
.all([app, key], (
|
|
100
|
+
.all([app, key], valueOrError(resolve, reject));
|
|
91
101
|
});
|
|
92
102
|
});
|
|
93
103
|
}
|
|
@@ -96,9 +106,8 @@ async function query(statement, inputs) {
|
|
|
96
106
|
db.serialize(() => {
|
|
97
107
|
db
|
|
98
108
|
.prepare(statement)
|
|
99
|
-
.run(inputs)
|
|
100
|
-
.finalize((error) => (error ? reject(error) : resolve(null)));
|
|
109
|
+
.run(inputs, valueOrError(resolve, reject));
|
|
101
110
|
});
|
|
102
111
|
});
|
|
103
112
|
}
|
|
104
|
-
export default {
|
|
113
|
+
export default { get, set, show, remove, apps, reload };
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloud-cli/env",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Storage for app environment variables",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
|
+
"prettier": "@cloud-cli/prettier-config",
|
|
8
9
|
"scripts": {
|
|
9
10
|
"test": "jest",
|
|
10
11
|
"tdd": "jest --watchAll",
|
|
@@ -15,18 +16,9 @@
|
|
|
15
16
|
],
|
|
16
17
|
"license": "MIT",
|
|
17
18
|
"devDependencies": {
|
|
18
|
-
"@
|
|
19
|
-
"@
|
|
20
|
-
"@
|
|
21
|
-
"@types/jest": "^27.0.1",
|
|
22
|
-
"@types/node": "^14.18.21",
|
|
23
|
-
"babel-jest": "^29.4.3",
|
|
24
|
-
"jest": "^29.4.3",
|
|
25
|
-
"nodemon": "^2.0.16",
|
|
26
|
-
"ts-jest": "^29.0.5",
|
|
27
|
-
"ts-jest-resolver": "^2.0.0",
|
|
28
|
-
"ts-node": "^9.1.1",
|
|
29
|
-
"typescript": "^4.8.2"
|
|
19
|
+
"@cloud-cli/jest-config": "^1.0.0",
|
|
20
|
+
"@cloud-cli/prettier-config": "^1.0.0",
|
|
21
|
+
"@cloud-cli/typescript-config": "^1.0.1"
|
|
30
22
|
},
|
|
31
23
|
"repository": {
|
|
32
24
|
"type": "git",
|
package/src/index.spec.js
CHANGED
|
@@ -12,17 +12,24 @@ describe('env', () => {
|
|
|
12
12
|
env.reload();
|
|
13
13
|
await expect(env.set({ app: '', key: '' })).rejects.toEqual(new Error('App not specified'));
|
|
14
14
|
await expect(env.set({ app: 'test', key: '' })).rejects.toEqual(new Error('Key not specified'));
|
|
15
|
-
|
|
15
|
+
|
|
16
|
+
await expect(env.get({ app: '', key: '' })).rejects.toEqual(new Error('App not specified'));
|
|
17
|
+
await expect(env.get({ app: 'test', key: '' })).rejects.toEqual(new Error('Key not specified'));
|
|
18
|
+
await expect(env.get({ app: 'test', key: 'key' })).resolves.toEqual([]);
|
|
16
19
|
|
|
17
20
|
// upsert
|
|
18
21
|
await expect(env.set(envVariable)).resolves.toBeNull();
|
|
19
22
|
await expect(env.set(envVariable)).resolves.toBeNull();
|
|
20
23
|
|
|
21
|
-
await expect(env.get({ app: 'test', key: 'key' })).resolves.toEqual(envVariable);
|
|
24
|
+
await expect(env.get({ app: 'test', key: 'key' })).resolves.toEqual([envVariable]);
|
|
22
25
|
|
|
26
|
+
await expect(env.show({ app: '' })).rejects.toEqual(new Error('App not specified'));
|
|
23
27
|
await expect(env.show(app)).resolves.toEqual([envVariable]);
|
|
28
|
+
await expect(env.apps()).resolves.toEqual([{ app: 'test' }]);
|
|
24
29
|
|
|
25
|
-
await expect(env.
|
|
30
|
+
await expect(env.remove({ app: '', key: '' })).rejects.toEqual(new Error('App not specified'));
|
|
31
|
+
await expect(env.remove({ app: 'test', key: '' })).rejects.toEqual(new Error('Key not specified'));
|
|
32
|
+
await expect(env.remove(envVariable)).resolves.toBeNull();
|
|
26
33
|
await expect(env.show(app)).resolves.toEqual([]);
|
|
27
34
|
});
|
|
28
35
|
});
|
package/src/index.ts
CHANGED
|
@@ -3,6 +3,10 @@ import { join } from 'path';
|
|
|
3
3
|
|
|
4
4
|
const appNotSpecifiedError = new Error('App not specified');
|
|
5
5
|
const keyNotSpecifiedError = new Error('Key not specified');
|
|
6
|
+
|
|
7
|
+
/* istanbul ignore next */
|
|
8
|
+
const valueOrError = (resolve, reject) => (error, value = null) => error ? reject(error) : resolve(value)
|
|
9
|
+
|
|
6
10
|
let db: sqlite.Database;
|
|
7
11
|
|
|
8
12
|
const columns = {
|
|
@@ -24,9 +28,7 @@ export interface AppKeyValue extends App, KeyValue { }
|
|
|
24
28
|
|
|
25
29
|
function close() {
|
|
26
30
|
try {
|
|
27
|
-
|
|
28
|
-
db.close();
|
|
29
|
-
}
|
|
31
|
+
db?.close();
|
|
30
32
|
} catch { };
|
|
31
33
|
}
|
|
32
34
|
|
|
@@ -39,7 +41,10 @@ function reload() {
|
|
|
39
41
|
sqlite.verbose();
|
|
40
42
|
}
|
|
41
43
|
|
|
42
|
-
const databasePath =
|
|
44
|
+
const databasePath = {
|
|
45
|
+
true: ':memory:',
|
|
46
|
+
false: join(process.cwd(), 'env.db')
|
|
47
|
+
}[String(inMemory)];
|
|
43
48
|
|
|
44
49
|
db = new sqlite.Database(databasePath);
|
|
45
50
|
db.run(`CREATE TABLE IF NOT EXISTS env (
|
|
@@ -67,7 +72,15 @@ async function show(options: App) {
|
|
|
67
72
|
FROM env WHERE
|
|
68
73
|
${columns.App} = ?`
|
|
69
74
|
)
|
|
70
|
-
.all([app], (
|
|
75
|
+
.all([app], valueOrError(resolve, reject));
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function apps() {
|
|
80
|
+
return new Promise((resolve, reject) => {
|
|
81
|
+
db
|
|
82
|
+
.prepare(`SELECT ${columns.App} FROM env`)
|
|
83
|
+
.all([], valueOrError(resolve, reject));
|
|
71
84
|
});
|
|
72
85
|
}
|
|
73
86
|
|
|
@@ -121,7 +134,7 @@ async function get(options: Omit<AppKeyValue, 'value'>) {
|
|
|
121
134
|
FROM env WHERE
|
|
122
135
|
${columns.App} = ? AND ${columns.Key} = ?`
|
|
123
136
|
)
|
|
124
|
-
.all([app, key], (
|
|
137
|
+
.all([app, key], valueOrError(resolve, reject));
|
|
125
138
|
});
|
|
126
139
|
});
|
|
127
140
|
}
|
|
@@ -131,10 +144,10 @@ async function query(statement, inputs) {
|
|
|
131
144
|
db.serialize(() => {
|
|
132
145
|
db
|
|
133
146
|
.prepare(statement)
|
|
134
|
-
.run(inputs)
|
|
135
|
-
.finalize((error) => (error ? reject(error) : resolve(null)));
|
|
147
|
+
.run(inputs, valueOrError(resolve, reject));
|
|
136
148
|
});
|
|
137
149
|
})
|
|
138
150
|
}
|
|
139
151
|
|
|
140
|
-
|
|
152
|
+
|
|
153
|
+
export default { get, set, show, remove, apps, reload };
|
package/tsconfig.json
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
1
|
{
|
|
2
|
+
"extends": "@cloud-cli/typescript-config",
|
|
2
3
|
"compilerOptions": {
|
|
3
|
-
"moduleResolution": "node",
|
|
4
|
-
"module": "esnext",
|
|
5
|
-
"target": "es2020",
|
|
6
|
-
"lib": ["ESNext"],
|
|
7
|
-
"esModuleInterop": true,
|
|
8
|
-
"noUnusedLocals": true,
|
|
9
|
-
"noUnusedParameters": true,
|
|
10
4
|
"outDir": "./dist",
|
|
11
|
-
"declaration": true
|
|
12
5
|
},
|
|
13
6
|
"include": ["./src/**/*.ts"]
|
|
14
7
|
}
|