@benshawmean/envedit 1.1.0 → 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.
@@ -0,0 +1,161 @@
1
+ import { confirm, input, password, select, checkbox } from "@inquirer/prompts";
2
+ import type { CommandModule } from "yargs";
3
+
4
+ import { Configuration, EnvironmentFile, VariableType } from "#lib/config";
5
+ import { NotFoundError } from "#lib/error";
6
+ import { logger } from "#lib/logger";
7
+
8
+ export default {
9
+ command: 'edit',
10
+ describe: 'Edit an environment variable',
11
+ builder: (yargs) => yargs,
12
+ handler: async (_args) => {
13
+ const config = new Configuration().loadConfigurationFile();
14
+
15
+ const name = await select({
16
+ message: "Select the variable to edit:",
17
+ choices: config.configuration.variables.map(variable => ({
18
+ name: variable.name,
19
+ value: variable.name,
20
+ })),
21
+ });
22
+
23
+ const variableConfiguration = config.getVariable(name);
24
+
25
+ if (!variableConfiguration) {
26
+ throw new NotFoundError(`Variable not found: ${name}`);
27
+ }
28
+
29
+ const isEditingConfig = await select({
30
+ message: "Are you editing the variable configuration (name/type) or the variable value?",
31
+ choices: [
32
+ { name: "Configuration", value: true },
33
+ { name: "Value", value: false },
34
+ ],
35
+ });
36
+
37
+ if (isEditingConfig) {
38
+ logger
39
+ .debug(`Editing variable configuration for: ${name}`);
40
+
41
+ const itemsToEdit = await checkbox({
42
+ message: "Select the items to edit:",
43
+ choices: [
44
+ { name: "Name", value: "name" },
45
+ { name: "Type", value: "type" },
46
+ ],
47
+ });
48
+
49
+ const updatedVariable = { ...variableConfiguration };
50
+
51
+ if (itemsToEdit.includes("name")) {
52
+ updatedVariable.name = await input({
53
+ message: "Enter the new variable name:",
54
+ default: variableConfiguration.name,
55
+ });
56
+
57
+ for (const env of config.configuration.environments) {
58
+ const envFile = new EnvironmentFile(env.file).load();
59
+
60
+ logger
61
+ .debug(`Editing variable "${name}" in environment file: ${env.file}`);
62
+
63
+ if (envFile.content[name] !== undefined) {
64
+ const value = envFile.content[name];
65
+ envFile.removeVariable(name);
66
+ envFile.setVariable(updatedVariable.name, value);
67
+ envFile.save();
68
+ }
69
+ }
70
+ }
71
+
72
+ if (itemsToEdit.includes("type")) {
73
+ updatedVariable.type = await select({
74
+ message: "Select the new variable type:",
75
+ choices: [
76
+ { name: "Public", value: VariableType.Public },
77
+ { name: "Private", value: VariableType.Private },
78
+ ],
79
+ default: variableConfiguration.type,
80
+ });
81
+
82
+ logger
83
+ .debug(`Editing variable type for "${name}" to "${updatedVariable.type}"`);
84
+ }
85
+
86
+ config
87
+ .editVariable(name, updatedVariable)
88
+ .saveConfigurationFile();
89
+ } else {
90
+ logger
91
+ .debug(`Adding editing variable value for: ${name}`);
92
+
93
+ let valuesToSet = config.configuration.environments.map(env => ({
94
+ envName: env.name,
95
+ value: '',
96
+ }));
97
+
98
+ const shouldGloballySet = await confirm({
99
+ message: "Do you want to set this variable for all environments?",
100
+ });
101
+
102
+ if (shouldGloballySet) {
103
+ const request = {
104
+ message: `Enter the global value for ${variableConfiguration.name}:`,
105
+ mask: variableConfiguration.type === VariableType.Private ? "*" : undefined,
106
+ };
107
+
108
+ const globalValue = variableConfiguration.type === VariableType.Private
109
+ ? await password(request)
110
+ : await input(request);
111
+
112
+ logger
113
+ .debug(`Setting variable "${variableConfiguration.name}" with global value "${globalValue}" for all environments`);
114
+
115
+ valuesToSet.forEach((envValue) => { envValue.value = globalValue });
116
+ } else {
117
+ const envsToEdit = await checkbox({
118
+ message: "Select the environments to edit the variable value for:",
119
+ choices: config.configuration.environments.map(env => ({
120
+ name: env.name,
121
+ value: env.name,
122
+ })),
123
+ });
124
+
125
+ logger
126
+ .debug(`Selected environments for editing: ${envsToEdit.join(", ")}`);
127
+
128
+ valuesToSet = valuesToSet.filter(envValue => envsToEdit.includes(envValue.envName));
129
+
130
+ if (valuesToSet.length === 0) {
131
+ console.log("No environments selected. Exiting.");
132
+ return;
133
+ }
134
+
135
+ for (const envValue of valuesToSet) {
136
+ const request = {
137
+ message: `Enter the value for ${variableConfiguration.name} in environment ${envValue.envName}:`,
138
+ mask: variableConfiguration.type === VariableType.Private ? "*" : undefined,
139
+ };
140
+
141
+ envValue.value = variableConfiguration.type === VariableType.Private
142
+ ? await password(request)
143
+ : await input(request);
144
+ }
145
+ }
146
+
147
+ for (const envValue of valuesToSet) {
148
+ const env = config.getEnvironment(envValue.envName);
149
+
150
+ if (!env) {
151
+ throw new NotFoundError(`Environment not found: ${envValue.envName}`);
152
+ }
153
+
154
+ logger
155
+ .debug(`Setting variable "${variableConfiguration.name}" with value "${envValue.value}" in environment file: ${env.file}`);
156
+
157
+ EnvironmentFile.quickUpdate(env.file, variableConfiguration.name, envValue.value);
158
+ }
159
+ }
160
+ }
161
+ } satisfies CommandModule;
@@ -0,0 +1,43 @@
1
+ import type { CommandModule } from "yargs";
2
+
3
+ import { Configuration, EnvironmentFile, VariableType } from "#lib/config";
4
+ import { NotFoundError } from "#lib/error";
5
+ import { logger } from "#lib/logger";
6
+
7
+ export default {
8
+ command: 'list',
9
+ describe: 'List all environment variables',
10
+ builder: (yargs) => yargs,
11
+ handler: async (_args) => {
12
+ const config = new Configuration().loadConfigurationFile();
13
+
14
+ const activeEnv = config.getActiveEnvironment();
15
+
16
+ if (!activeEnv) {
17
+ throw new NotFoundError(`No active environment found. Please set an active environment using the "use" command.`);
18
+ }
19
+
20
+ const envFile = new EnvironmentFile(activeEnv.file).load();
21
+
22
+ const mappedVariables: Array<{ name: string; value: string; type: VariableType }> = [];
23
+
24
+ for (const [key, value] of Object.entries(envFile.content)) {
25
+ const variableConfiguration = config.getVariable(key);
26
+ if (variableConfiguration) {
27
+ logger.debug(`Mapping variable "${key}" with value "${value}" and type "${variableConfiguration.type}"`);
28
+
29
+ const data = {
30
+ name: key,
31
+ value: variableConfiguration.type === VariableType.Private ? value.replaceAll(/./gm, '*') : value,
32
+ type: variableConfiguration.type,
33
+ };
34
+
35
+ mappedVariables.push(data);
36
+ }
37
+ }
38
+
39
+ console.log(
40
+ JSON.stringify(mappedVariables, null, 2)
41
+ )
42
+ }
43
+ } satisfies CommandModule;
@@ -0,0 +1,44 @@
1
+ import { select } from "@inquirer/prompts";
2
+ import type { CommandModule } from "yargs";
3
+
4
+ import { Configuration, EnvironmentFile } from "#lib/config";
5
+ import { NotFoundError } from "#lib/error";
6
+ import { logger } from "#lib/logger";
7
+
8
+ export default {
9
+ command: 'remove',
10
+ describe: 'Remove an environment variable',
11
+ builder: (yargs) => yargs,
12
+ handler: async (_args) => {
13
+ const config = new Configuration().loadConfigurationFile();
14
+
15
+ const name = await select({
16
+ message: "Select the variable to remove:",
17
+ choices: config.configuration.variables.map(variable => ({
18
+ name: variable.name,
19
+ value: variable.name,
20
+ })),
21
+ });
22
+
23
+ if (!config.getVariable(name)) {
24
+ throw new NotFoundError(`Variable not found: ${name}`);
25
+ }
26
+
27
+ logger
28
+ .debug(`Removing variable "${name}" from configuration and all environment files`);
29
+
30
+ config
31
+ .removeVariable(name)
32
+ .saveConfigurationFile();
33
+
34
+ for (const env of config.configuration.environments) {
35
+ const envFile = new EnvironmentFile(env.file).load();
36
+
37
+ if (envFile.content[name] !== undefined) {
38
+ logger
39
+ .debug(`Removing variable "${name}" from environment file: ${env.file}`);
40
+ envFile.removeVariable(name).save();
41
+ }
42
+ }
43
+ }
44
+ } satisfies CommandModule;
@@ -0,0 +1,21 @@
1
+ import type { CommandModule } from "yargs";
2
+
3
+ import Add from "./variable/add";
4
+ import Edit from "./variable/edit";
5
+ import List from "./variable/list";
6
+ import Remove from "./variable/remove";
7
+
8
+ const data = {
9
+ command: 'variable',
10
+ describe: 'Manage environment variables',
11
+ builder: (yargs) => yargs
12
+ .usage('Usage: envedit variable <command> [options]')
13
+ .command([Add, Edit, List, Remove])
14
+ .demandCommand(),
15
+ handler: async (_args) => {}
16
+ } satisfies CommandModule;
17
+
18
+ export const command = data.command;
19
+ export const describe = data.describe;
20
+ export const builder = data.builder;
21
+ export const handler = data.handler;