@h3y6e/gunshi-plugin-env 0.0.0-stage → 0.0.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.
- package/LICENSE +21 -0
- package/README.md +71 -2
- package/lib/index.d.ts +25 -0
- package/lib/index.js +113 -0
- package/package.json +55 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 heyhoe
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,72 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @h3y6e/gunshi-plugin-env
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> Fill [gunshi](https://gunshi.dev) flags from environment variables.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
# npm
|
|
9
|
+
npm install @h3y6e/gunshi-plugin-env
|
|
10
|
+
|
|
11
|
+
# deno
|
|
12
|
+
deno add jsr:@h3y6e/gunshi-plugin-env
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Requires gunshi 1.x.
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import env, { pluginId } from "@h3y6e/gunshi-plugin-env";
|
|
21
|
+
import type { EnvExtension, PluginId } from "@h3y6e/gunshi-plugin-env";
|
|
22
|
+
import { cli, defineWithTypes } from "gunshi";
|
|
23
|
+
|
|
24
|
+
const command = defineWithTypes<{ extensions: Record<PluginId, EnvExtension> }>()({
|
|
25
|
+
name: "greet",
|
|
26
|
+
args: {
|
|
27
|
+
loud: { type: "boolean", description: "Shout" },
|
|
28
|
+
},
|
|
29
|
+
run: (ctx) => {
|
|
30
|
+
if (ctx.values.loud) return console.log("HELLO!");
|
|
31
|
+
console.log(`hello (${ctx.extensions[pluginId].name("loud")}=1 to shout)`);
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
await cli(process.argv.slice(2), command, { name: "greet", plugins: [env()] });
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
greet # hello (GREET_LOUD=1 to shout)
|
|
40
|
+
greet --loud # HELLO!
|
|
41
|
+
GREET_LOUD=1 greet # HELLO!
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Features
|
|
45
|
+
|
|
46
|
+
### Variable names
|
|
47
|
+
|
|
48
|
+
A flag not given on the command line takes `<CLI>_<FLAG>` in upper snake case: `--dry-run` of `greet` reads `GREET_DRY_RUN`. Positionals, `--help`, and `--version` are not filled.
|
|
49
|
+
|
|
50
|
+
### Values
|
|
51
|
+
|
|
52
|
+
As in [usage](https://usage.jdx.dev), a boolean is true only for `1`, `true`, `True`, or `TRUE`. Other types are parsed and validated by gunshi as on the command line, `conflicts` included.
|
|
53
|
+
|
|
54
|
+
### Context extension
|
|
55
|
+
|
|
56
|
+
`ctx.extensions[pluginId].name(key)` returns the variable of a flag.
|
|
57
|
+
|
|
58
|
+
## Limitations
|
|
59
|
+
|
|
60
|
+
This plugin fills the values just before the command's `run` and puts them only in the context passed to `run`. Before that, gunshi validates arguments with the values from the command line alone, and it passes its own context to the hooks. This leads to the following:
|
|
61
|
+
|
|
62
|
+
- A `required` flag has to be given on the command line; a variable does not satisfy it. Leave `required` off flags meant to come from variables.
|
|
63
|
+
- `onBeforeCommand` and `onAfterCommand` see the values before the variables fill them. Read the filled values in the command's `run`.
|
|
64
|
+
- Of other plugins, only the decorators of those listed after env in `plugins` see the filled values. Extensions see only the values before the variables fill them.
|
|
65
|
+
|
|
66
|
+
## API References
|
|
67
|
+
|
|
68
|
+
[JSR](https://jsr.io/@h3y6e/gunshi-plugin-env/doc)
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
[MIT](LICENSE)
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { PluginWithExtension } from "gunshi/plugin";
|
|
2
|
+
//#region src/plugin.d.ts
|
|
3
|
+
/** The unique identifier of the env plugin */
|
|
4
|
+
export declare const pluginId: "h3y6e:env";
|
|
5
|
+
type PluginId = typeof pluginId;
|
|
6
|
+
/** The env plugin's extension on the command context */
|
|
7
|
+
interface EnvExtension {
|
|
8
|
+
/** The environment variable that fills the flag with this key */
|
|
9
|
+
name: (key: string) => string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Env plugin
|
|
13
|
+
*
|
|
14
|
+
* A flag that is not given on the command line takes the value of `<CLI>_<FLAG>`, both in upper
|
|
15
|
+
* snake case. As in usage, a boolean is true for `1`, `true`, `True`, or `TRUE` and false
|
|
16
|
+
* otherwise; any other type is parsed and validated by gunshi as on the command line, and the value
|
|
17
|
+
* is checked against `conflicts`. A required flag still has to be given on the command line,
|
|
18
|
+
* because gunshi checks it before plugins run, and the command hooks see the values before the
|
|
19
|
+
* variables fill them.
|
|
20
|
+
*
|
|
21
|
+
* @returns A defined plugin that fills flags from environment variables
|
|
22
|
+
*/
|
|
23
|
+
export default function env(): PluginWithExtension<EnvExtension>;
|
|
24
|
+
//#endregion
|
|
25
|
+
export type { EnvExtension, PluginId };
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { parseArgs, resolveArgs } from "gunshi";
|
|
2
|
+
import { plugin } from "gunshi/plugin";
|
|
3
|
+
import { kebabnize } from "gunshi/utils";
|
|
4
|
+
//#region src/plugin.ts
|
|
5
|
+
/**
|
|
6
|
+
* @license MIT
|
|
7
|
+
* @author h3y6e
|
|
8
|
+
*/
|
|
9
|
+
/** The unique identifier of the env plugin */
|
|
10
|
+
const pluginId = "h3y6e:env";
|
|
11
|
+
const BUILTINS = /* @__PURE__ */ new Set(["help", "version"]);
|
|
12
|
+
function snake(text) {
|
|
13
|
+
return text.replaceAll(/([a-z0-9])([A-Z])|([A-Z])([A-Z][a-z])/g, "$1$3_$2$4").replaceAll(/[^A-Za-z0-9]+/g, "_").replaceAll(/^_|_$/g, "").toUpperCase();
|
|
14
|
+
}
|
|
15
|
+
function flagName(key, arg, toKebab) {
|
|
16
|
+
return toKebab || arg.toKebab ? kebabnize(key) : key;
|
|
17
|
+
}
|
|
18
|
+
function fromEnv(key, arg, raw, toKebab) {
|
|
19
|
+
const value = arg.type === "boolean" ? String([
|
|
20
|
+
"1",
|
|
21
|
+
"true",
|
|
22
|
+
"True",
|
|
23
|
+
"TRUE"
|
|
24
|
+
].includes(raw)) : raw;
|
|
25
|
+
const { type, choices, parse, multiple } = arg;
|
|
26
|
+
const schema = {
|
|
27
|
+
type,
|
|
28
|
+
choices,
|
|
29
|
+
parse,
|
|
30
|
+
multiple,
|
|
31
|
+
toKebab: arg.toKebab
|
|
32
|
+
};
|
|
33
|
+
const token = `--${flagName(key, arg, toKebab)}=${value}`;
|
|
34
|
+
return resolveArgs({ [key]: schema }, parseArgs([token]), { toKebab });
|
|
35
|
+
}
|
|
36
|
+
function conflictError(args, given, toKebab) {
|
|
37
|
+
const schemas = {};
|
|
38
|
+
const argv = [];
|
|
39
|
+
for (const [key, arg] of Object.entries(args)) {
|
|
40
|
+
if (!given(key)) continue;
|
|
41
|
+
schemas[key] = {
|
|
42
|
+
type: "string",
|
|
43
|
+
conflicts: arg.conflicts,
|
|
44
|
+
toKebab: arg.toKebab
|
|
45
|
+
};
|
|
46
|
+
argv.push(`--${flagName(key, arg, toKebab)}=_`);
|
|
47
|
+
}
|
|
48
|
+
return resolveArgs(schemas, parseArgs(argv), { toKebab }).error;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Env plugin
|
|
52
|
+
*
|
|
53
|
+
* A flag that is not given on the command line takes the value of `<CLI>_<FLAG>`, both in upper
|
|
54
|
+
* snake case. As in usage, a boolean is true for `1`, `true`, `True`, or `TRUE` and false
|
|
55
|
+
* otherwise; any other type is parsed and validated by gunshi as on the command line, and the value
|
|
56
|
+
* is checked against `conflicts`. A required flag still has to be given on the command line,
|
|
57
|
+
* because gunshi checks it before plugins run, and the command hooks see the values before the
|
|
58
|
+
* variables fill them.
|
|
59
|
+
*
|
|
60
|
+
* @returns A defined plugin that fills flags from environment variables
|
|
61
|
+
*/
|
|
62
|
+
function env() {
|
|
63
|
+
return plugin({
|
|
64
|
+
id: pluginId,
|
|
65
|
+
name: "env",
|
|
66
|
+
extension: (ctx) => {
|
|
67
|
+
const cli = ctx.env.name;
|
|
68
|
+
if (!cli) throw new Error("The env plugin names variables after the CLI; give cli() a name");
|
|
69
|
+
return { name: (key) => `${snake(cli)}_${snake(key)}` };
|
|
70
|
+
},
|
|
71
|
+
setup(ctx) {
|
|
72
|
+
ctx.decorateCommand((run) => async (c) => {
|
|
73
|
+
const { name } = c.extensions[pluginId];
|
|
74
|
+
const values = { ...c.values };
|
|
75
|
+
const args = c.args;
|
|
76
|
+
const filled = /* @__PURE__ */ new Set();
|
|
77
|
+
const errors = [];
|
|
78
|
+
for (const [key, arg] of Object.entries(args)) {
|
|
79
|
+
const builtin = BUILTINS.has(key) && c.env.globalOptions?.has(key);
|
|
80
|
+
if (arg.type === "positional" || builtin || c.explicit[key]) continue;
|
|
81
|
+
const raw = process.env[name(key)];
|
|
82
|
+
if (raw === void 0) continue;
|
|
83
|
+
const parsed = fromEnv(key, arg, raw, c.toKebab);
|
|
84
|
+
if (parsed.error) errors.push(...parsed.error.errors);
|
|
85
|
+
else {
|
|
86
|
+
values[key] = parsed.values[key];
|
|
87
|
+
filled.add(key);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const given = (k) => c.explicit[k] || filled.has(k);
|
|
91
|
+
errors.push(...conflictError(args, given, c.toKebab)?.errors ?? []);
|
|
92
|
+
if (errors.length > 0) {
|
|
93
|
+
const error = new AggregateError(errors);
|
|
94
|
+
const message = await c.env.renderValidationErrors?.(c, error);
|
|
95
|
+
if (message) c.log(message);
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
98
|
+
return run({
|
|
99
|
+
...c,
|
|
100
|
+
values
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region src/index.ts
|
|
108
|
+
/**
|
|
109
|
+
* @license MIT
|
|
110
|
+
* @author h3y6e
|
|
111
|
+
*/
|
|
112
|
+
//#endregion
|
|
113
|
+
export { env as default, pluginId };
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@h3y6e/gunshi-plugin-env",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "gunshi plugin that fills flags from environment variables",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cli",
|
|
7
|
+
"env",
|
|
8
|
+
"gunshi",
|
|
9
|
+
"plugin"
|
|
10
|
+
],
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/h3y6e/gunshi-plugins/issues"
|
|
13
|
+
},
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": "h3y6e",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/h3y6e/gunshi-plugins.git",
|
|
19
|
+
"directory": "packages/env"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"lib"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"module": "lib/index.js",
|
|
27
|
+
"types": "lib/index.d.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./lib/index.d.ts",
|
|
31
|
+
"import": "./lib/index.js",
|
|
32
|
+
"require": "./lib/index.js",
|
|
33
|
+
"default": "./lib/index.js"
|
|
34
|
+
},
|
|
35
|
+
"./package.json": "./package.json"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsdown",
|
|
42
|
+
"lint:jsr": "deno publish --dry-run --allow-dirty",
|
|
43
|
+
"prepack": "aubr build",
|
|
44
|
+
"typecheck:deno": "deno check src/index.ts"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"gunshi": "^1.0.0-rc.1",
|
|
48
|
+
"publint": "^0.3.25",
|
|
49
|
+
"tsdown": "^0.23.0"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"gunshi": "^1.0.0-rc.1"
|
|
53
|
+
},
|
|
54
|
+
"engines": {
|
|
55
|
+
"node": ">= 22.12"
|
|
56
|
+
}
|
|
57
|
+
}
|