dotscribe 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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +15 -0
  3. package/index.ts +339 -0
  4. package/package.json +39 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 dotscribe
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
@@ -0,0 +1,15 @@
1
+ # cli
2
+
3
+ To install dependencies:
4
+
5
+ ```bash
6
+ bun install
7
+ ```
8
+
9
+ To run:
10
+
11
+ ```bash
12
+ bun run index.ts
13
+ ```
14
+
15
+ This project was created using `bun init` in bun v1.3.13. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.
package/index.ts ADDED
@@ -0,0 +1,339 @@
1
+ #!/usr/bin/env bun
2
+ import { readFileSync, existsSync } from "fs";
3
+ import { resolve } from "path";
4
+ import { createInterface } from "readline";
5
+
6
+ const MISTRAL_API_KEY = process.env.MISTRAL_API_KEY;
7
+ const API_BASE = process.env.DOTSCRIBE_API_URL || "http://localhost:3000";
8
+
9
+ interface AnalyzedVariable {
10
+ key: string;
11
+ description: string;
12
+ type: string;
13
+ required: boolean;
14
+ source?: string;
15
+ example?: string;
16
+ }
17
+
18
+ interface CliOptions {
19
+ output?: string;
20
+ markdown: boolean;
21
+ strict: boolean;
22
+ envPath?: string;
23
+ stdin: boolean;
24
+ help: boolean;
25
+ }
26
+
27
+ const COLORS = {
28
+ reset: "\x1b[0m",
29
+ bright: "\x1b[1m",
30
+ dim: "\x1b[2m",
31
+ green: "\x1b[32m",
32
+ yellow: "\x1b[33m",
33
+ red: "\x1b[31m",
34
+ cyan: "\x1b[36m",
35
+ gray: "\x1b[90m",
36
+ };
37
+
38
+ function log(msg: string, color = "") {
39
+ console.log(color + msg + COLORS.reset);
40
+ }
41
+
42
+ function error(msg: string) {
43
+ log(`Error: ${msg}`, COLORS.red);
44
+ }
45
+
46
+ function warn(msg: string) {
47
+ log(`Warning: ${msg}`, COLORS.yellow);
48
+ }
49
+
50
+ function info(msg: string) {
51
+ log(msg, COLORS.cyan);
52
+ }
53
+
54
+ function success(msg: string) {
55
+ log(msg, COLORS.green);
56
+ }
57
+
58
+ function dim(msg: string) {
59
+ log(msg, COLORS.gray);
60
+ }
61
+
62
+ function parseArgs(): CliOptions {
63
+ const args = process.argv.slice(2);
64
+ const options: CliOptions = {
65
+ markdown: false,
66
+ strict: false,
67
+ stdin: false,
68
+ help: false,
69
+ };
70
+
71
+ for (let i = 0; i < args.length; i++) {
72
+ const arg = args[i];
73
+ if (arg === undefined) continue;
74
+ if (arg === "-o" || arg === "--output") {
75
+ options.output = args[++i];
76
+ } else if (arg === "--markdown" || arg === "-m") {
77
+ options.markdown = true;
78
+ } else if (arg === "--strict" || arg === "-s") {
79
+ options.strict = true;
80
+ } else if (arg === "--stdin") {
81
+ options.stdin = true;
82
+ } else if (arg === "--help" || arg === "-h") {
83
+ options.help = true;
84
+ } else if (!arg.startsWith("-")) {
85
+ options.envPath = arg;
86
+ }
87
+ }
88
+
89
+ return options;
90
+ }
91
+
92
+ function findEnvFile(path?: string): string | null {
93
+ const searchPaths = path
94
+ ? [resolve(path)]
95
+ : [
96
+ resolve(".env"),
97
+ resolve(process.cwd(), ".env"),
98
+ resolve(process.cwd(), ".env.local"),
99
+ ];
100
+
101
+ for (const p of searchPaths) {
102
+ if (existsSync(p)) {
103
+ return p;
104
+ }
105
+ }
106
+ return null;
107
+ }
108
+
109
+ function parseEnvContent(content: string): string[] {
110
+ const lines = content.split(/\r?\n/);
111
+ const keys: string[] = [];
112
+
113
+ for (const line of lines) {
114
+ let trimmed = line.trim();
115
+ if (!trimmed || trimmed.startsWith("#")) continue;
116
+
117
+ const commentIndex = trimmed.indexOf("#");
118
+ if (commentIndex > 0) {
119
+ trimmed = trimmed.slice(0, commentIndex).trim();
120
+ }
121
+ if (!trimmed) continue;
122
+
123
+ const equalsIndex = trimmed.indexOf("=");
124
+ if (equalsIndex === -1) continue;
125
+
126
+ const key = trimmed.slice(0, equalsIndex).trim();
127
+ if (key) keys.push(key);
128
+ }
129
+
130
+ return keys;
131
+ }
132
+
133
+ function detectPotentialSecrets(keys: string[]): string[] {
134
+ const secretPatterns = [
135
+ /secret/i,
136
+ /password/i,
137
+ /key/i,
138
+ /token/i,
139
+ /api_key/i,
140
+ /apikey/i,
141
+ /private/i,
142
+ /credential/i,
143
+ ];
144
+
145
+ return keys.filter((key) => secretPatterns.some((p) => p.test(key)));
146
+ }
147
+
148
+ async function callApi(endpoint: string, body: unknown): Promise<unknown> {
149
+ const response = await fetch(`${API_BASE}${endpoint}`, {
150
+ method: "POST",
151
+ headers: { "Content-Type": "application/json" },
152
+ body: JSON.stringify(body),
153
+ });
154
+
155
+ if (!response.ok) {
156
+ const err = await response.json().catch(() => ({ error: "Unknown error" }));
157
+ throw new Error((err as { error: string }).error || `API error: ${response.status}`);
158
+ }
159
+
160
+ return response.json();
161
+ }
162
+
163
+ async function analyzeEnv(content: string): Promise<AnalyzedVariable[]> {
164
+ const result = (await callApi("/api/analyze", { content })) as { variables: AnalyzedVariable[] };
165
+ return result.variables;
166
+ }
167
+
168
+ async function formatEnv(variables: AnalyzedVariable[], asMarkdown: boolean) {
169
+ const result = (await callApi("/api/format", {
170
+ variables,
171
+ options: { includeDescriptions: true },
172
+ })) as { envExample: string; markdownTable: string };
173
+
174
+ return asMarkdown ? result.markdownTable : result.envExample;
175
+ }
176
+
177
+ async function checkHealth(): Promise<boolean> {
178
+ try {
179
+ const response = await fetch(`${API_BASE}/api/health`);
180
+ return response.ok;
181
+ } catch {
182
+ return false;
183
+ }
184
+ }
185
+
186
+ async function readStdin(): Promise<string> {
187
+ return new Promise((resolve) => {
188
+ let content = "";
189
+ process.stdin.on("data", (chunk) => (content += chunk.toString()));
190
+ process.stdin.on("end", () => resolve(content));
191
+ });
192
+ }
193
+
194
+ function printHelp() {
195
+ log(`
196
+ ${COLORS.bright}dotscribe${COLORS.reset} - Document your .env files with AI
197
+
198
+ ${COLORS.bright}USAGE:${COLORS.reset}
199
+ dotscribe [OPTIONS] [ENV_FILE]
200
+
201
+ ${COLORS.bright}OPTIONS:${COLORS.reset}
202
+ -o, --output <file> Save output to file
203
+ -m, --markdown Output as markdown table
204
+ -s, --strict Fail if secrets detected without .gitignore
205
+ --stdin Read .env from stdin
206
+ -h, --help Show this help message
207
+
208
+ ${COLORS.bright}EXAMPLES:${COLORS.reset}
209
+ dotscribe Analyze .env in current directory
210
+ dotscribe .env.production Analyze specific file
211
+ dotscribe -o .env.example Save to file
212
+ dotscribe --markdown Output as markdown table
213
+ dotscribe --strict Fail on secrets without protection
214
+ cat .env | dotscribe --stdin Read from pipe
215
+
216
+ ${COLORS.bright}ENVIRONMENT:${COLORS.reset}
217
+ MISTRAL_API_KEY Your Mistral API key (for local development)
218
+ DOTSCRIBE_API_URL API endpoint (default: http://localhost:3000)
219
+ `);
220
+ }
221
+
222
+ async function main() {
223
+ const options = parseArgs();
224
+
225
+ if (options.help) {
226
+ printHelp();
227
+ process.exit(0);
228
+ }
229
+
230
+ dim("dotscribe v0.1.0");
231
+ console.log();
232
+
233
+ info("Checking API health...");
234
+ const healthy = await checkHealth();
235
+
236
+ if (!healthy) {
237
+ warn("API not reachable at " + API_BASE);
238
+ warn("Make sure the API server is running: bun run dev --filter=web");
239
+ console.log();
240
+
241
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
242
+ const answer = await new Promise<string>((resolve) => {
243
+ rl.question("Continue anyway? (y/N): ", resolve);
244
+ });
245
+ rl.close();
246
+
247
+ if (answer.toLowerCase() !== "y") {
248
+ process.exit(1);
249
+ }
250
+ } else {
251
+ success("API is healthy");
252
+ }
253
+ console.log();
254
+
255
+ let content: string;
256
+
257
+ if (options.stdin) {
258
+ info("Reading from stdin...");
259
+ content = await readStdin();
260
+ } else {
261
+ const envPath = findEnvFile(options.envPath);
262
+ if (!envPath) {
263
+ error("No .env file found. Specify a path or run in a directory with .env");
264
+ process.exit(1);
265
+ }
266
+ info(`Reading from ${envPath}...`);
267
+ content = readFileSync(envPath, "utf-8");
268
+ }
269
+
270
+ const keys = parseEnvContent(content);
271
+ if (keys.length === 0) {
272
+ error("No valid environment variables found");
273
+ process.exit(1);
274
+ }
275
+
276
+ success(`Found ${keys.length} environment variable(s)`);
277
+ console.log();
278
+
279
+ if (options.strict) {
280
+ const secrets = detectPotentialSecrets(keys);
281
+ if (secrets.length > 0) {
282
+ const hasGitignore = existsSync(resolve(".gitignore"));
283
+ if (!hasGitignore) {
284
+ warn(`Potential secrets detected: ${secrets.join(", ")}`);
285
+ warn("No .gitignore found - these values will be committed!");
286
+
287
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
288
+ const answer = await new Promise<string>((resolve) => {
289
+ rl.question("Continue anyway? (y/N): ", resolve);
290
+ });
291
+ rl.close();
292
+
293
+ if (answer.toLowerCase() !== "y") {
294
+ process.exit(1);
295
+ }
296
+ }
297
+ }
298
+ }
299
+
300
+ info("Analyzing environment variables...");
301
+
302
+ let variables: AnalyzedVariable[];
303
+ try {
304
+ variables = await analyzeEnv(content);
305
+ } catch (e) {
306
+ error(`Analysis failed: ${(e as Error).message}`);
307
+ process.exit(1);
308
+ }
309
+
310
+ success(`Analyzed ${variables.length} variable(s)`);
311
+ console.log();
312
+
313
+ info("Generating documentation...");
314
+ let output: string;
315
+ try {
316
+ output = await formatEnv(variables, options.markdown);
317
+ } catch (e) {
318
+ error(`Formatting failed: ${(e as Error).message}`);
319
+ process.exit(1);
320
+ }
321
+
322
+ if (options.output) {
323
+ const outputPath = resolve(options.output);
324
+ import("fs").then(({ writeFileSync }) => {
325
+ writeFileSync(outputPath, output, "utf-8");
326
+ success(`Saved to ${outputPath}`);
327
+ });
328
+ } else {
329
+ console.log(output);
330
+ }
331
+
332
+ console.log();
333
+ success("Done!");
334
+ }
335
+
336
+ main().catch((e) => {
337
+ error((e as Error).message);
338
+ process.exit(1);
339
+ });
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "dotscribe",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "Document your .env files with AI — interactive CLI that analyzes env vars and generates docs",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/your-org/dotscribe.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/your-org/dotscribe/issues"
13
+ },
14
+ "homepage": "https://github.com/your-org/dotscribe#readme",
15
+ "keywords": [
16
+ "dotenv",
17
+ "env",
18
+ "environment-variables",
19
+ "documentation",
20
+ "cli",
21
+ "ai"
22
+ ],
23
+ "bin": {
24
+ "dotscribe": "./index.ts"
25
+ },
26
+ "scripts": {
27
+ "dev": "bun run index.ts",
28
+ "start": "bun run index.ts",
29
+ "build": "echo \"CLI has no build output\"",
30
+ "lint": "echo \"CLI has no lint\"",
31
+ "check-types": "tsc --noEmit",
32
+ "prepublishOnly": "bunx tsc --noEmit"
33
+ },
34
+ "files": ["index.ts", "README.md", "LICENSE"],
35
+ "devDependencies": {
36
+ "@types/bun": "latest",
37
+ "typescript": "^5"
38
+ }
39
+ }