@benshawmean/envedit 1.1.1 → 1.1.2

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/index.ts CHANGED
@@ -1,566 +1,25 @@
1
1
  #!/usr/bin/env bun
2
- import fs from "node:fs";
3
-
4
- import { input, password, select, checkbox, confirm } from "@inquirer/prompts";
5
- import { parse } from "dotenv";
6
2
  import yargs from "yargs";
7
3
  import { hideBin } from "yargs/helpers";
8
4
 
9
- import { NotFoundError } from "./lib/error";
10
- import { Configuration, EnvironmentFile, VariableType } from "./lib/config";
5
+ import { logger } from "#lib/logger";
11
6
 
12
7
  await yargs(hideBin(process.argv))
13
8
  .scriptName("envedit")
14
- .command(
15
- "environment",
16
- "Manage environments",
17
- (yargs) => yargs
18
- .command(
19
- "list",
20
- "List all environments",
21
- () => { },
22
- async () => {
23
- const config = new Configuration().loadConfigurationFile();
24
-
25
- for (const env of config.configuration.environments) {
26
- console.dir(env);
27
- }
28
- }
29
- )
30
- .command(
31
- "add",
32
- "Add a new environment",
33
- () => { },
34
- async () => {
35
- const config = new Configuration().loadConfigurationFile();
36
-
37
- const name = await input({
38
- message: "Enter the environment name:",
39
- validate: (input) => {
40
- if (!input) {
41
- return "Environment name cannot be empty";
42
- }
43
- if (config.configuration.environments.find(env => env.name === input)) {
44
- return "Environment name already exists";
45
- }
46
- return true;
47
- },
48
- });
49
-
50
- const prefix = await input({
51
- message: "Enter the environment prefix:",
52
- });
53
-
54
- const file = await input({
55
- message: "Enter the environment file path:",
56
- default: `.env.${prefix}`,
57
- });
58
-
59
- const isActive = await confirm({
60
- message: "Is this environment active?",
61
- });
62
-
63
- config
64
- .addEnvironment({ name, prefix, file, isActive })
65
- .saveConfigurationFile();
66
-
67
- const envFile = new EnvironmentFile(file).load();
68
-
69
- if (!fs.existsSync(file)) {
70
- const shouldClone = await confirm({
71
- message: "Do you want to clone a pre-existing environment into the new file?",
72
- });
73
-
74
- if (shouldClone) {
75
- const clonedEnv = await select({
76
- message: "Select the environment to clone from:",
77
- choices: config.configuration.environments.map(env => ({
78
- name: env.name,
79
- value: env.name,
80
- })),
81
- });
82
-
83
- const selectedEnv = config.getEnvironment(clonedEnv);
84
-
85
- if (!selectedEnv) {
86
- throw new NotFoundError(`Environment not found: ${clonedEnv}`);
87
- }
88
-
89
- const selectedEnvFile = new EnvironmentFile(selectedEnv.file);
90
-
91
- selectedEnvFile.load();
92
-
93
- for (const [key, value] of Object.entries(selectedEnvFile.content)) {
94
- envFile.setVariable(key, value);
95
- }
96
- } else {
97
- for (const variable of config.configuration.variables) {
98
- const request = {
99
- message: `Enter the value for ${variable.name} in environment ${name}:`,
100
- mask: variable.type === VariableType.Private ? "*" : undefined,
101
- };
102
-
103
- const value = variable.type === VariableType.Private
104
- ? await password(request)
105
- : await input(request);
106
-
107
- envFile.setVariable(variable.name, value);
108
- }
109
- }
110
-
111
- envFile.save();
112
- }
113
- }
114
- )
115
- .command(
116
- "remove",
117
- "Remove an environment",
118
- () => { },
119
- async () => {
120
- const config = new Configuration().loadConfigurationFile();
121
-
122
- const name = await select({
123
- message: "Select the environment to remove:",
124
- choices: config.configuration.environments.map(env => ({
125
- name: env.name,
126
- value: env.name,
127
- })),
128
- });
129
-
130
- const selectedEnv = config.getEnvironment(name);
131
-
132
- if (!selectedEnv) {
133
- throw new NotFoundError(`Environment not found: ${name}`);
134
- }
135
-
136
- config
137
- .removeEnvironment(name)
138
- .saveConfigurationFile();
139
-
140
- if (fs.existsSync(selectedEnv.file)) {
141
- const toDelete = await confirm({
142
- message: `Environment file found: ${selectedEnv.file}. Do you want to delete it?`
143
- });
144
-
145
- if (toDelete) {
146
- fs.unlinkSync(selectedEnv.file);
147
- }
148
- }
149
- }
150
- )
151
- .command(
152
- "edit",
153
- "Edit an environment",
154
- () => { },
155
- async () => {
156
- const config = new Configuration().loadConfigurationFile();
157
-
158
- const name = await select({
159
- message: "Select the environment to edit:",
160
- choices: config.configuration.environments.map(env => ({
161
- name: env.name,
162
- value: env.name,
163
- })),
164
- });
165
-
166
- const selectedEnv = config.getEnvironment(name);
167
-
168
- if (!selectedEnv) {
169
- throw new NotFoundError(`Environment not found: ${name}`);
170
- }
171
-
172
- const itemsToEdit = await checkbox({
173
- message: "Select the items to edit:",
174
- choices: [
175
- { name: "Name", value: "name" },
176
- { name: "Prefix", value: "prefix" },
177
- { name: "File", value: "file" },
178
- ],
179
- });
180
-
181
- const updatedEnv = { ...selectedEnv };
182
-
183
- if (itemsToEdit.includes("name")) {
184
- updatedEnv.name = await input({
185
- message: "Enter the new environment name:",
186
- default: selectedEnv.name,
187
- });
188
- }
189
-
190
- if (itemsToEdit.includes("prefix")) {
191
- updatedEnv.prefix = await input({
192
- message: "Enter the new environment prefix:",
193
- default: selectedEnv.prefix,
194
- });
195
- }
196
-
197
- if (itemsToEdit.includes("file")) {
198
- updatedEnv.file = await input({
199
- message: "Enter the new environment file path:",
200
- default: selectedEnv.file,
201
- });
202
- }
203
-
204
- config
205
- .editEnvironment(name, updatedEnv)
206
- .saveConfigurationFile();
207
-
208
- if (itemsToEdit.includes("file") && fs.existsSync(selectedEnv.file)) {
209
- const toRename = await confirm({
210
- message: `Old environment file found: ${selectedEnv.file}. Do you want to rename it to the new file path: ${updatedEnv.file}?`
211
- });
212
-
213
- if (toRename) {
214
- fs.renameSync(selectedEnv.file, updatedEnv.file);
215
- }
216
- }
217
- }
218
- )
219
- .demandCommand(),
220
- )
221
- .command(
222
- "variable",
223
- "Manage environment variables",
224
- (yargs) => yargs
225
- .command(
226
- "list",
227
- "List all environment variables",
228
- () => { },
229
- async () => {
230
- const config = new Configuration().loadConfigurationFile();
231
-
232
- const activeEnv = config.getActiveEnvironment();
233
-
234
- if (!activeEnv) {
235
- throw new NotFoundError(`No active environment found. Please set an active environment using the "use" command.`);
236
- }
237
-
238
- const envContent = fs.readFileSync(activeEnv.file, "utf-8");
239
-
240
- const parsedEnv = parse(envContent);
241
-
242
- for (const [key, value] of Object.entries(parsedEnv)) {
243
- const variableConfiguration = config.getVariable(key);
244
- if (variableConfiguration) {
245
- const data = {
246
- name: key,
247
- value: variableConfiguration.type === VariableType.Private ? value.replaceAll(/./gm, '*') : value,
248
- type: variableConfiguration.type,
249
- };
250
-
251
- console.dir(data);
252
- }
253
- }
254
- }
255
- )
256
- .command(
257
- "add",
258
- "Add a new environment variable",
259
- () => { },
260
- async () => {
261
- const config = new Configuration().loadConfigurationFile();
262
-
263
- const name = await input({
264
- message: "Enter the variable name:",
265
- validate: (input) => {
266
- if (!input) {
267
- return "Variable name cannot be empty";
268
- }
269
- if (config.getVariable(input)) {
270
- return "Variable name already exists";
271
- }
272
- return true;
273
- },
274
- });
275
-
276
- const type = await select({
277
- message: "Select the variable type:",
278
- choices: [
279
- { name: "Public", value: VariableType.Public },
280
- { name: "Private", value: VariableType.Private },
281
- ],
282
- });
283
-
284
- config
285
- .addVariable({ name, type })
286
- .saveConfigurationFile();
287
-
288
- const shouldGloballySet = await confirm({
289
- message: "Do you want to set this variable with a global value for all environments?",
290
- });
291
-
292
- const valuesToSet = config.configuration.environments.map(env => ({
293
- envName: env.name,
294
- value: '',
295
- }));
296
-
297
- if (shouldGloballySet) {
298
- const request = {
299
- message: `Enter the global value for ${name}:`,
300
- mask: type === VariableType.Private ? "*" : undefined,
301
- };
302
-
303
- const globalValue = type === VariableType.Private
304
- ? await password(request)
305
- : await input(request);
306
-
307
- valuesToSet.forEach((envValue) => { envValue.value = globalValue });
308
- } else {
309
- for (const envValue of valuesToSet) {
310
- const request = {
311
- message: `Enter the value for ${name} in environment ${envValue.envName}:`,
312
- mask: type === VariableType.Private ? "*" : undefined,
313
- };
314
-
315
- envValue.value = type === VariableType.Private
316
- ? await password(request)
317
- : await input(request);
318
- }
319
- }
320
-
321
- for (const envValue of valuesToSet) {
322
- const env = config.getEnvironment(envValue.envName);
323
-
324
- if (!env) {
325
- throw new NotFoundError(`Environment not found: ${envValue.envName}`);
326
- }
327
-
328
- EnvironmentFile.quickUpdate(env.file, name, envValue.value);
329
- }
330
- }
331
- )
332
- .command(
333
- "remove",
334
- "Remove an environment variable",
335
- () => { },
336
- async () => {
337
- const config = new Configuration().loadConfigurationFile();
338
-
339
- const name = await select({
340
- message: "Select the variable to remove:",
341
- choices: config.configuration.variables.map(variable => ({
342
- name: variable.name,
343
- value: variable.name,
344
- })),
345
- });
346
-
347
- if (!config.getVariable(name)) {
348
- throw new NotFoundError(`Variable not found: ${name}`);
349
- }
350
-
351
- config
352
- .removeVariable(name)
353
- .saveConfigurationFile();
354
-
355
- for (const env of config.configuration.environments) {
356
- const envFile = new EnvironmentFile(env.file).load();
357
-
358
- if (envFile.content[name] !== undefined) {
359
- envFile.removeVariable(name).save();
360
- }
361
- }
362
- }
363
- )
364
- .command(
365
- "edit",
366
- "Edit an environment variable",
367
- () => { },
368
- async () => {
369
- const config = new Configuration().loadConfigurationFile();
370
-
371
- const name = await select({
372
- message: "Select the variable to edit:",
373
- choices: config.configuration.variables.map(variable => ({
374
- name: variable.name,
375
- value: variable.name,
376
- })),
377
- });
378
-
379
- const variableConfiguration = config.getVariable(name);
380
-
381
- if (!variableConfiguration) {
382
- throw new NotFoundError(`Variable not found: ${name}`);
383
- }
384
-
385
- const isEditingConfig = await select({
386
- message: "Are you editing the variable configuration (name/type) or the variable value?",
387
- choices: [
388
- { name: "Configuration", value: true },
389
- { name: "Value", value: false },
390
- ],
391
- });
392
-
393
- if (isEditingConfig) {
394
- const itemsToEdit = await checkbox({
395
- message: "Select the items to edit:",
396
- choices: [
397
- { name: "Name", value: "name" },
398
- { name: "Type", value: "type" },
399
- ],
400
- });
401
-
402
- const updatedVariable = { ...variableConfiguration };
403
-
404
- if (itemsToEdit.includes("name")) {
405
- updatedVariable.name = await input({
406
- message: "Enter the new variable name:",
407
- default: variableConfiguration.name,
408
- });
409
-
410
- for (const env of config.configuration.environments) {
411
- const envFile = new EnvironmentFile(env.file).load();
412
-
413
- if (envFile.content[name] !== undefined) {
414
- const value = envFile.content[name];
415
- envFile.removeVariable(name);
416
- envFile.setVariable(updatedVariable.name, value);
417
- envFile.save();
418
- }
419
- }
420
- }
421
-
422
- if (itemsToEdit.includes("type")) {
423
- updatedVariable.type = await select({
424
- message: "Select the new variable type:",
425
- choices: [
426
- { name: "Public", value: VariableType.Public },
427
- { name: "Private", value: VariableType.Private },
428
- ],
429
- default: variableConfiguration.type,
430
- });
431
- }
432
-
433
- config
434
- .editVariable(name, updatedVariable)
435
- .saveConfigurationFile();
436
- } else {
437
- let valuesToSet = config.configuration.environments.map(env => ({
438
- envName: env.name,
439
- value: '',
440
- }));
441
-
442
- const shouldGloballySet = await confirm({
443
- message: "Do you want to set this variable for all environments?",
444
- });
445
-
446
- if (shouldGloballySet) {
447
- const request = {
448
- message: `Enter the global value for ${variableConfiguration.name}:`,
449
- mask: variableConfiguration.type === VariableType.Private ? "*" : undefined,
450
- };
451
-
452
- const globalValue = variableConfiguration.type === VariableType.Private
453
- ? await password(request)
454
- : await input(request);
455
-
456
- valuesToSet.forEach((envValue) => { envValue.value = globalValue });
457
- } else {
458
- for (const envValue of valuesToSet) {
459
- const request = {
460
- message: `Enter the value for ${variableConfiguration.name} in environment ${envValue.envName}:`,
461
- mask: variableConfiguration.type === VariableType.Private ? "*" : undefined,
462
- };
463
-
464
- envValue.value = variableConfiguration.type === VariableType.Private
465
- ? await password(request)
466
- : await input(request);
467
- }
468
- }
469
-
470
- for (const envValue of valuesToSet) {
471
- const env = config.getEnvironment(envValue.envName);
472
-
473
- if (!env) {
474
- throw new NotFoundError(`Environment not found: ${envValue.envName}`);
475
- }
476
-
477
- EnvironmentFile.quickUpdate(env.file, variableConfiguration.name, envValue.value);
478
- }
479
- }
480
- }
481
- )
482
- .demandCommand(),
483
- )
484
- .command(
485
- "setup",
486
- "Setup the environment",
487
- () => { },
488
- async () => {
489
- new Configuration().loadConfigurationFile();
490
-
491
- if (fs.existsSync('.git')) {
492
- const gitignorePath = '.gitignore';
493
-
494
- if (!fs.existsSync(gitignorePath)) {
495
- const toCreate = await confirm({
496
- message: `Git Repository without a git ignore file not found: ${gitignorePath}. Do you want to create it?`
497
- });
498
-
499
- if (!toCreate) {
500
- console.error(`Git Repository without a git ignore file not found: ${gitignorePath}`);
501
- process.exit(1);
502
- }
503
-
504
- fs.writeFileSync(gitignorePath, "");
505
- }
506
-
507
- const expectedItems = ['.env', '.env.*', 'env.config.yaml'];
508
-
509
- for (const item of expectedItems) {
510
- if (!fs.readFileSync(gitignorePath, "utf-8").includes(item)) {
511
- const toAdd = await confirm({
512
- message: `The item ${item} is not in the git ignore file. Do you want to add it?`
513
- });
514
-
515
- if (!toAdd) {
516
- console.error(`The item ${item} is not in the git ignore file.`);
517
- process.exit(1);
518
- }
519
-
520
- fs.appendFileSync(gitignorePath, `\n${item}`);
521
- }
522
- }
9
+ .usage("Usage: $0 <command>")
10
+ .commandDir(
11
+ `${import.meta.dir}/cmd`,
12
+ {
13
+ extensions: ['ts'],
14
+ visit: (commandObject, commandPath, filename) => {
15
+ logger.debug(`Loading command: ${commandPath}`);
16
+
17
+ logger.debug(`Command Name: ${commandObject.command}`);
18
+ logger.debug(`Command Description: ${commandObject.describe}`);
19
+
20
+ return commandObject;
523
21
  }
524
- }
525
- )
526
- .command(
527
- "use",
528
- "Set the current environment",
529
- () => { },
530
- async () => {
531
- const config = new Configuration().loadConfigurationFile();
532
-
533
- const name = await select({
534
- message: "Select the environment to use",
535
- choices: config.configuration.environments.map(env => ({
536
- name: env.name + (env.isActive ? " (active)" : ""),
537
- value: env.name,
538
- })),
539
- default: config.getActiveEnvironment()?.name,
540
- });
541
-
542
- const selectedEnv = config.getEnvironment(name);
543
-
544
- if (!selectedEnv) {
545
- throw new NotFoundError(`Environment not found: ${name}`);
546
- }
547
-
548
- if (!selectedEnv.isActive) {
549
- for (const env of config.configuration.environments) {
550
- env.isActive = env.name === name;
551
- }
552
-
553
- config.saveConfigurationFile();
554
- }
555
-
556
- const envFilePath = selectedEnv.file;
557
-
558
- if (fs.existsSync('.env')) {
559
- fs.unlinkSync('.env');
560
- }
561
-
562
- fs.symlinkSync(envFilePath, '.env', 'file');
563
- }
22
+ },
564
23
  )
565
24
  .demandCommand()
566
25
  .parseAsync();
package/lib/config.ts CHANGED
@@ -2,11 +2,11 @@ import fs from 'fs';
2
2
 
3
3
  import { parse } from 'dotenv';
4
4
 
5
- import { NotFoundError } from './error';
5
+ import { NotFoundError } from '#lib/error';
6
6
 
7
7
  export enum VariableType {
8
- Public = 'public',
9
- Private = 'private',
8
+ Public,
9
+ Private,
10
10
  }
11
11
 
12
12
  export interface VariableConfiguration {
@@ -22,12 +22,13 @@ export interface EnviornmentConfiguration {
22
22
  }
23
23
 
24
24
  export interface GeneralConfiguration {
25
+ version: number;
25
26
  environments: EnviornmentConfiguration[];
26
27
  variables: VariableConfiguration[];
27
28
  }
28
29
 
29
30
  export class Configuration {
30
- private config: GeneralConfiguration = { environments: [], variables: [] };
31
+ private config: GeneralConfiguration = { version: 1, environments: [], variables: [] };
31
32
  private readonly configFilePath: string = 'env.config.yaml';
32
33
  constructor(config: Partial<GeneralConfiguration> = {}) {
33
34
  if (config.environments) {
@@ -37,9 +38,13 @@ export class Configuration {
37
38
  if (config.variables) {
38
39
  this.config.variables = config.variables;
39
40
  }
41
+ if (config.version) {
42
+ this.config.version = config.version;
43
+ }
40
44
  }
41
45
  public get configuration(): GeneralConfiguration {
42
46
  return {
47
+ version: this.config.version,
43
48
  environments: this.config.environments || [],
44
49
  variables: this.config.variables || [],
45
50
  };
@@ -68,7 +73,11 @@ export class Configuration {
68
73
  }
69
74
 
70
75
  public saveConfigurationFile() {
71
- fs.writeFileSync(this.configFilePath, Bun.YAML.stringify(this.configuration, null, 2));
76
+ fs.writeFileSync(
77
+ this.configFilePath,
78
+ Bun.YAML.stringify(this.configuration, null, 2),
79
+ { mode: 0o600 },
80
+ );
72
81
 
73
82
  return this;
74
83
  }
@@ -181,7 +190,7 @@ export class EnvironmentFile {
181
190
 
182
191
  public load() {
183
192
  if (!fs.existsSync(this.path)) {
184
- fs.writeFileSync(this.path, '');
193
+ fs.writeFileSync(this.path, '', { mode: 0o600 });
185
194
  }
186
195
 
187
196
  this.rawContent = fs.readFileSync(this.path, "utf-8");
package/lib/logger.ts ADDED
@@ -0,0 +1,15 @@
1
+ import pino from 'pino';
2
+
3
+ const level = process.env.LOG_LEVEL || 'info';
4
+
5
+ export const logger = pino({
6
+ level,
7
+ transport: {
8
+ target: 'pino-pretty',
9
+ options: {
10
+ colorize: true,
11
+ translateTime: 'SYS:standard',
12
+ ignore: 'pid,hostname',
13
+ },
14
+ },
15
+ });
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@benshawmean/envedit",
3
3
  "module": "index.ts",
4
4
  "type": "module",
5
- "version": "1.1.1",
5
+ "version": "1.1.2",
6
6
  "license": "GPL-3.0-only",
7
7
  "bin": {
8
8
  "envedit": "index.ts"
@@ -25,6 +25,8 @@
25
25
  "dependencies": {
26
26
  "@inquirer/prompts": "^8.5.2",
27
27
  "dotenv": "^17.4.2",
28
+ "pino": "^10.3.1",
29
+ "pino-pretty": "^13.1.3",
28
30
  "yargs": "^18.1.0"
29
31
  }
30
32
  }
package/tsconfig.json CHANGED
@@ -29,6 +29,7 @@
29
29
 
30
30
  "paths": {
31
31
  "@/*": ["./*"],
32
+ "#lib/*": ["./lib/*"]
32
33
  }
33
34
  }
34
35
  }