e2sm 0.1.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 +3 -0
- package/package.json +40 -0
- package/src/index.ts +169 -0
- package/src/lib.ts +90 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 mfyuu
|
|
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
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "e2sm",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"bin": {
|
|
5
|
+
"e2sm": "src/index.ts"
|
|
6
|
+
},
|
|
7
|
+
"files": [
|
|
8
|
+
"src/index.ts",
|
|
9
|
+
"src/lib.ts"
|
|
10
|
+
],
|
|
11
|
+
"type": "module",
|
|
12
|
+
"module": "src/index.ts",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"prepare": "lefthook install",
|
|
15
|
+
"preinstall": "npx only-allow bun",
|
|
16
|
+
"fmt": "oxfmt",
|
|
17
|
+
"fmt:check": "oxfmt --check",
|
|
18
|
+
"lint": "oxlint",
|
|
19
|
+
"lint-types": "oxlint --type-aware",
|
|
20
|
+
"check": "oxfmt && oxlint --type-aware",
|
|
21
|
+
"typecheck": "tsc --noEmit"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@clack/prompts": "^0.11.0",
|
|
25
|
+
"gunshi": "0.27.5"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@changesets/changelog-github": "^0.5.2",
|
|
29
|
+
"@changesets/cli": "^2.29.8",
|
|
30
|
+
"@gunshi/docs": "0.27.5",
|
|
31
|
+
"@types/bun": "latest",
|
|
32
|
+
"lefthook": "^2.0.15",
|
|
33
|
+
"oxfmt": "^0.26.0",
|
|
34
|
+
"oxlint": "^1.41.0",
|
|
35
|
+
"oxlint-tsgolint": "^0.11.1"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"typescript": "^5"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
import * as p from "@clack/prompts";
|
|
3
|
+
import { cli, define } from "gunshi";
|
|
4
|
+
import pkg from "../package.json";
|
|
5
|
+
import { parseEnvContent, validateUnknownFlags } from "./lib";
|
|
6
|
+
|
|
7
|
+
function isCancel(value: unknown): value is symbol {
|
|
8
|
+
return p.isCancel(value);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const command = define({
|
|
12
|
+
name: "e2sm",
|
|
13
|
+
description: "Upload .env file to AWS Secrets Manager",
|
|
14
|
+
args: {
|
|
15
|
+
dryRun: {
|
|
16
|
+
type: "boolean",
|
|
17
|
+
short: "d",
|
|
18
|
+
toKebab: true,
|
|
19
|
+
description: "Preview JSON output without uploading",
|
|
20
|
+
},
|
|
21
|
+
profile: {
|
|
22
|
+
type: "string",
|
|
23
|
+
short: "p",
|
|
24
|
+
description: "AWS profile to use",
|
|
25
|
+
},
|
|
26
|
+
input: {
|
|
27
|
+
type: "string",
|
|
28
|
+
short: "i",
|
|
29
|
+
description: "Path to the .env file (skip interactive prompt)",
|
|
30
|
+
},
|
|
31
|
+
name: {
|
|
32
|
+
type: "string",
|
|
33
|
+
short: "n",
|
|
34
|
+
description: "Secret name for AWS Secrets Manager (skip interactive prompt)",
|
|
35
|
+
},
|
|
36
|
+
region: {
|
|
37
|
+
type: "string",
|
|
38
|
+
short: "r",
|
|
39
|
+
description: "AWS region to use (e.g., ap-northeast-1)",
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
run: async (ctx) => {
|
|
43
|
+
// Validate unknown flags first
|
|
44
|
+
const unknownFlagError = validateUnknownFlags(ctx.tokens, ctx.args);
|
|
45
|
+
if (unknownFlagError) {
|
|
46
|
+
console.error(unknownFlagError);
|
|
47
|
+
process.exit(1);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const isDryRun = ctx.values.dryRun;
|
|
51
|
+
const profile = ctx.values.profile;
|
|
52
|
+
const inputFlag = ctx.values.input;
|
|
53
|
+
const nameFlag = ctx.values.name;
|
|
54
|
+
const region = ctx.values.region;
|
|
55
|
+
|
|
56
|
+
p.intro(`e2sm v${pkg.version} - env to AWS Secrets Manager`);
|
|
57
|
+
|
|
58
|
+
// 1. Get env file path (from flag or interactively)
|
|
59
|
+
let envFilePath: string;
|
|
60
|
+
|
|
61
|
+
if (inputFlag) {
|
|
62
|
+
envFilePath = inputFlag;
|
|
63
|
+
} else {
|
|
64
|
+
const result = await p.text({
|
|
65
|
+
message: "Enter the path to your .env file:",
|
|
66
|
+
placeholder: ".env.local",
|
|
67
|
+
defaultValue: ".env.local",
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
if (isCancel(result)) {
|
|
71
|
+
p.cancel("Operation cancelled");
|
|
72
|
+
process.exit(0);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
envFilePath = result;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// 2. Read and parse env file
|
|
79
|
+
const file = Bun.file(envFilePath);
|
|
80
|
+
const exists = await file.exists();
|
|
81
|
+
|
|
82
|
+
if (!exists) {
|
|
83
|
+
p.cancel(`File not found: ${envFilePath}`);
|
|
84
|
+
process.exit(1);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const content = await file.text();
|
|
88
|
+
const envData = parseEnvContent(content);
|
|
89
|
+
|
|
90
|
+
if (Object.keys(envData).length === 0) {
|
|
91
|
+
p.cancel("No valid environment variables found in the file");
|
|
92
|
+
process.exit(1);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const jsonString = JSON.stringify(envData);
|
|
96
|
+
|
|
97
|
+
// 3. Dry-run mode: preview with jq
|
|
98
|
+
if (isDryRun) {
|
|
99
|
+
p.log.info("Dry-run mode: Previewing JSON output");
|
|
100
|
+
const jqResult = await Bun.$`echo ${jsonString} | jq -C .`.text();
|
|
101
|
+
console.log(jqResult);
|
|
102
|
+
p.outro("Dry-run complete");
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// 4. Get secret name (from flag or interactively)
|
|
107
|
+
let secretName: string;
|
|
108
|
+
|
|
109
|
+
if (nameFlag) {
|
|
110
|
+
secretName = nameFlag;
|
|
111
|
+
} else {
|
|
112
|
+
const result = await p.text({
|
|
113
|
+
message: "Enter the secret name for AWS Secrets Manager:",
|
|
114
|
+
placeholder: "my-app/default",
|
|
115
|
+
defaultValue: "my-app/default",
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
if (isCancel(result)) {
|
|
119
|
+
p.cancel("Operation cancelled");
|
|
120
|
+
process.exit(0);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
secretName = result;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// 5. Upload to AWS Secrets Manager
|
|
127
|
+
const spinner = p.spinner();
|
|
128
|
+
spinner.start("Uploading to AWS Secrets Manager...");
|
|
129
|
+
|
|
130
|
+
const profileArgs = profile ? ["--profile", profile] : [];
|
|
131
|
+
const regionArgs = region ? ["--region", region] : [];
|
|
132
|
+
|
|
133
|
+
// First, try to check if the secret already exists
|
|
134
|
+
const describeResult =
|
|
135
|
+
await Bun.$`aws secretsmanager describe-secret --secret-id ${secretName} ${profileArgs} ${regionArgs} 2>/dev/null`.nothrow();
|
|
136
|
+
|
|
137
|
+
if (describeResult.exitCode === 0) {
|
|
138
|
+
// Secret exists, update it
|
|
139
|
+
const updateResult =
|
|
140
|
+
await Bun.$`aws secretsmanager put-secret-value --secret-id ${secretName} --secret-string ${jsonString} ${profileArgs} ${regionArgs}`.nothrow();
|
|
141
|
+
|
|
142
|
+
if (updateResult.exitCode !== 0) {
|
|
143
|
+
spinner.stop("Failed to update secret");
|
|
144
|
+
p.cancel(`Error: ${updateResult.stderr.toString()}`);
|
|
145
|
+
process.exit(1);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
spinner.stop("Secret updated successfully");
|
|
149
|
+
} else {
|
|
150
|
+
// Secret doesn't exist, create it
|
|
151
|
+
const createResult =
|
|
152
|
+
await Bun.$`aws secretsmanager create-secret --name ${secretName} --secret-string ${jsonString} ${profileArgs} ${regionArgs}`.nothrow();
|
|
153
|
+
|
|
154
|
+
if (createResult.exitCode !== 0) {
|
|
155
|
+
spinner.stop("Failed to create secret");
|
|
156
|
+
p.cancel(`Error: ${createResult.stderr.toString()}`);
|
|
157
|
+
process.exit(1);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
spinner.stop("Secret created successfully");
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
p.outro(`Secret '${secretName}' has been saved to AWS Secrets Manager`);
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
await cli(process.argv.slice(2), command, {
|
|
168
|
+
version: pkg.version,
|
|
169
|
+
});
|
package/src/lib.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { ArgSchema, ArgToken } from "gunshi";
|
|
2
|
+
|
|
3
|
+
export function toKebabCase(str: string): string {
|
|
4
|
+
return str.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Validates that all provided flags are known options.
|
|
9
|
+
* @returns Error message if an unknown flag is found, null otherwise
|
|
10
|
+
*/
|
|
11
|
+
export function validateUnknownFlags(
|
|
12
|
+
tokens: ArgToken[],
|
|
13
|
+
args: Record<string, ArgSchema>,
|
|
14
|
+
): string | null {
|
|
15
|
+
// Build set of known option names
|
|
16
|
+
const knownOptions = new Set<string>();
|
|
17
|
+
|
|
18
|
+
// Built-in options
|
|
19
|
+
knownOptions.add("help");
|
|
20
|
+
knownOptions.add("h");
|
|
21
|
+
knownOptions.add("version");
|
|
22
|
+
knownOptions.add("v");
|
|
23
|
+
|
|
24
|
+
// User-defined options
|
|
25
|
+
for (const [key, schema] of Object.entries(args)) {
|
|
26
|
+
knownOptions.add(key);
|
|
27
|
+
knownOptions.add(toKebabCase(key));
|
|
28
|
+
|
|
29
|
+
if (schema.short) {
|
|
30
|
+
knownOptions.add(schema.short);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Check for unknown options
|
|
35
|
+
for (const token of tokens) {
|
|
36
|
+
if (token.kind === "option" && token.name) {
|
|
37
|
+
// Handle --no- prefix for negatable options
|
|
38
|
+
const name = token.name.startsWith("no-") ? token.name.slice(3) : token.name;
|
|
39
|
+
|
|
40
|
+
if (!knownOptions.has(name)) {
|
|
41
|
+
return `Unknown option: --${token.name}`;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function parseEnvContent(content: string): Record<string, string> {
|
|
50
|
+
const result: Record<string, string> = {};
|
|
51
|
+
|
|
52
|
+
for (const line of content.split("\n")) {
|
|
53
|
+
const trimmed = line.trim();
|
|
54
|
+
|
|
55
|
+
// Skip empty lines and comments
|
|
56
|
+
if (trimmed === "" || trimmed.startsWith("#")) {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const equalIndex = trimmed.indexOf("=");
|
|
61
|
+
if (equalIndex === -1) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const key = trimmed.slice(0, equalIndex).trim();
|
|
66
|
+
let value = trimmed.slice(equalIndex + 1).trim();
|
|
67
|
+
|
|
68
|
+
// Check if value is quoted
|
|
69
|
+
const isQuoted =
|
|
70
|
+
(value.startsWith('"') && value.endsWith('"')) ||
|
|
71
|
+
(value.startsWith("'") && value.endsWith("'"));
|
|
72
|
+
|
|
73
|
+
// Remove surrounding quotes if present
|
|
74
|
+
if (isQuoted) {
|
|
75
|
+
value = value.slice(1, -1);
|
|
76
|
+
} else {
|
|
77
|
+
// Remove inline comment for unquoted values
|
|
78
|
+
const commentIndex = value.indexOf("#");
|
|
79
|
+
if (commentIndex !== -1) {
|
|
80
|
+
value = value.slice(0, commentIndex).trim();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (key) {
|
|
85
|
+
result[key] = value;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return result;
|
|
90
|
+
}
|