@ben-studios/envedit 0.0.0-stage → 1.0.1

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/README.md CHANGED
@@ -1,3 +1,98 @@
1
- # Temporary Holding Version
1
+ # ![EnvEdit Icon](https://cdn.benshawmean.com/icons/envedit-icon.png){width=32} envedit
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ `envedit` is a simple CLI application used to manage environments.
4
+ It allows you to create, edit, and delete custom environments, as well as
5
+ control the variables within them. It is designed to be a simple and easy-to-use tool for managing environments.
6
+
7
+ ## Installation
8
+
9
+ To install `envedit`, you must have [Bun](https://bun.sh/) installed.
10
+ Once you have Bun installed, you can install `envedit` using the following command:
11
+
12
+ ```bash
13
+ bun add @benshawmean/envedit
14
+ ```
15
+
16
+ or you can install it globally using the following command:
17
+
18
+ ```bash
19
+ bun add -g @benshawmean/envedit
20
+ ```
21
+
22
+ After installation, you can run `envedit` from the command line.
23
+
24
+ ## Setup
25
+
26
+ Setting up the application is simple. You first run the setup command,
27
+ which will create a configuration file in your current working directory.
28
+ All you need to do is run the following command:
29
+
30
+ ```bash
31
+ envedit setup
32
+ ```
33
+
34
+ After that, you should see a new file called `env.config.yaml` in your current working directory.
35
+ You can then add a new environment by running the following command and following the prompts:
36
+
37
+ ```bash
38
+ envedit environment add
39
+ ```
40
+
41
+ To add a new variable to an environment, you can run the following command and follow the prompts:
42
+
43
+ ```bash
44
+ envedit variable add
45
+ ```
46
+
47
+ After you have completed that, you can use the environment by running the following command
48
+ and selecting the environment you want to use from the list of available environments:
49
+
50
+ ```bash
51
+ envedit use
52
+ ```
53
+
54
+ ## Schema
55
+
56
+ I have released a JSON Schema spec. file for the outputted file. It can be found at <https://benshawmean.com/api/schema/envedit>.
57
+
58
+
59
+ ### IDE Support for Schema
60
+
61
+ To add validation and description support for the schema, please try the following things:
62
+
63
+ #### VSCode
64
+
65
+ For VSCode (and/or VSCode Insiders), add the following to your `settings.json` file:
66
+
67
+ ```json
68
+ "json.schemaDownload.trustedDomains": {
69
+ "https://benshawmean.com/": true,
70
+ },
71
+ "json.schemas": [
72
+ {
73
+ "fileMatch": ["**/env.config.yaml"],
74
+ "url": "https://benshawmean.com/api/schema/envedit",
75
+ }
76
+ ],
77
+ "yaml.schemas": {
78
+ "https://benshawmean.com/api/schema/envedit": "**/env.config.yaml"
79
+ }
80
+ ```
81
+
82
+ #### JetBrains IDEs
83
+
84
+ For JetBrains IDEs, you can do this by doing these steps:
85
+
86
+ 1. In Settings, head to Languages & Frameworks > Schemas and DTDs > JSON Schema Mappings
87
+ 2. Click the + (Add) icon
88
+ 3. Name your mapping "EnvEdit"
89
+ 4. Provide the URL `https://benshawmean.com/api/schema/envedit`
90
+ 5. Define the file path pattern `**/env.config.yaml`
91
+ 6. Click OK
92
+
93
+ Do note that this is on a project level, so you will need to do this for each project you want to use the schema in.
94
+
95
+ ## License
96
+
97
+ This project is licensed under the GNU General Public License version 3.
98
+ See the [LICENSE](https://benshawmean.com/legal/global/license/gpl3) file for more details.
package/ci/npm.sh ADDED
@@ -0,0 +1,4 @@
1
+ #! /usr/bin/env bash
2
+
3
+ echo "//${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}" > .npmrc
4
+ echo "@${CI_PROJECT_ROOT_NAMESPACE}:registry=https://${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/" >> .npmrc
@@ -0,0 +1,10 @@
1
+ import Commands from "./environmentCmd";
2
+
3
+ export default {
4
+ command: 'environment',
5
+ describe: 'Manage environments',
6
+ builder: (yargs) => yargs
7
+ .usage('Usage: envedit environment <command> [options]')
8
+ .command(Commands)
9
+ .demandCommand(),
10
+ } as CommandModule;
@@ -0,0 +1,115 @@
1
+ import {
2
+ existsSync as exists,
3
+ unlinkSync as unlink,
4
+ symlinkSync as symlink,
5
+ } from "node:fs";
6
+
7
+ import { confirm, input, password, select } from "@inquirer/prompts";
8
+
9
+ import { Configuration, EnvironmentFile, VariableType } from "#lib/config";
10
+ import { NotFoundError } from "#lib/error";
11
+ import { Logger } from "#lib/logger";
12
+
13
+ export default {
14
+ command: 'add',
15
+ describe: 'Add a new environment',
16
+ builder: (yargs) => yargs,
17
+ handler: async (argv) => {
18
+ const config = new Configuration();
19
+ const logger = new Logger(argv.level);
20
+
21
+ const name = await input({
22
+ message: "Enter the environment name:",
23
+ validate: (input) => {
24
+ if (!input) {
25
+ return "Environment name cannot be empty";
26
+ }
27
+ if (config.envs.has(input)) {
28
+ return "Environment name already exists";
29
+ }
30
+ return true;
31
+ },
32
+ });
33
+
34
+ const prefix = await input({
35
+ message: "Enter the environment prefix:",
36
+ });
37
+
38
+ const file = await input({
39
+ message: "Enter the environment file path:",
40
+ default: `.env.${prefix}`,
41
+ });
42
+
43
+ const isActive = await confirm({
44
+ message: "Is this environment active?",
45
+ default: !config.active,
46
+ });
47
+
48
+ logger.debug(`Adding new environment "${name}" with prefix "${prefix}" and file: "${file}"`);
49
+
50
+ config.envs.set(name, { name, prefix, file, isActive });
51
+ config.save();
52
+
53
+ const envFile = new EnvironmentFile(file);
54
+
55
+ if (!exists(file)) {
56
+ logger.debug(`Environment file not found: ${file}`);
57
+
58
+ const shouldClone = await confirm({
59
+ message: "Do you want to clone a pre-existing environment into the new file?",
60
+ });
61
+
62
+ if (shouldClone) {
63
+ logger.debug(`Cloning a pre-existing environment into the new file: ${file}`);
64
+
65
+ const clonedEnv = await select({
66
+ message: "Select the environment to clone from:",
67
+ choices: Array.from(config.envs.keys()),
68
+ });
69
+
70
+ const selectedEnv = config.envs.get(clonedEnv);
71
+
72
+ if (!selectedEnv) {
73
+ throw new NotFoundError(`Environment not found: ${clonedEnv}`);
74
+ }
75
+
76
+ const envSpecificFile = new EnvironmentFile(selectedEnv.file);
77
+
78
+ for (const [key, value] of Object.entries(envSpecificFile.vars)) {
79
+ envFile.vars.set(key, value);
80
+ }
81
+
82
+ envFile.save();
83
+ } else {
84
+ for (const [_, variable] of config.vars) {
85
+ const request = {
86
+ message: `Enter the value for ${variable.name} in environment ${name}:`,
87
+ mask: variable.type === VariableType.Private ? "*" : undefined,
88
+ };
89
+
90
+ const value = variable.type === VariableType.Private
91
+ ? await password(request)
92
+ : await input(request);
93
+
94
+ logger
95
+ .debug(`Setting variable "${variable.name}" with value "${value}" in environment "${name}"`);
96
+
97
+ envFile.vars.set(variable.name, value);
98
+ }
99
+ }
100
+
101
+ envFile.save();
102
+ }
103
+
104
+ if (isActive) {
105
+ config.active = name;
106
+
107
+ if (exists('.env')) {
108
+ unlink('.env');
109
+ }
110
+
111
+ const properEnv = config.envs.get(name)!;
112
+ symlink(properEnv.file, '.env', 'file');
113
+ }
114
+ }
115
+ } satisfies CommandModule;
@@ -0,0 +1,74 @@
1
+ import {
2
+ renameSync as rename,
3
+ existsSync as exists,
4
+ } from "node:fs";
5
+
6
+ import { input, select, checkbox } from "@inquirer/prompts";
7
+
8
+ import { Configuration } from "#lib/config";
9
+ import { NotFoundError } from "#lib/error";
10
+ import { Logger } from "#lib/logger";
11
+
12
+ export default {
13
+ command: 'edit',
14
+ describe: 'Edit an environment',
15
+ builder: (yargs) => yargs,
16
+ handler: async (argv) => {
17
+ const config = new Configuration();
18
+ const logger = new Logger(argv.level);
19
+
20
+ const name = await select({
21
+ message: "Select the environment to edit:",
22
+ choices: Array.from(config.envs.keys()),
23
+ });
24
+
25
+ const selectedEnv = config.envs.get(name);
26
+
27
+ if (!selectedEnv) {
28
+ throw new NotFoundError(`Environment not found: ${name}`);
29
+ }
30
+
31
+ const itemsToEdit = await checkbox({
32
+ message: "Select the items to edit:",
33
+ choices: [
34
+ { name: "Name", value: "name" },
35
+ { name: "Prefix", value: "prefix" },
36
+ { name: "File", value: "file" },
37
+ ],
38
+ });
39
+
40
+ const updatedEnv = { ...selectedEnv };
41
+
42
+ if (itemsToEdit.includes("name")) {
43
+ logger.debug(`Editing environment name from "${selectedEnv.name}" to "${updatedEnv.name}"`);
44
+ updatedEnv.name = await input({
45
+ message: "Enter the new environment name:",
46
+ default: selectedEnv.name,
47
+ });
48
+ }
49
+
50
+ if (itemsToEdit.includes("prefix")) {
51
+ logger.debug(`Editing environment prefix from "${selectedEnv.prefix}" to "${updatedEnv.prefix}"`);
52
+ updatedEnv.prefix = await input({
53
+ message: "Enter the new environment prefix:",
54
+ default: selectedEnv.prefix,
55
+ });
56
+ }
57
+
58
+ if (itemsToEdit.includes("file")) {
59
+ logger.debug(`Editing environment file from "${selectedEnv.file}" to "${updatedEnv.file}"`);
60
+ updatedEnv.file = await input({
61
+ message: "Enter the new environment file path:",
62
+ default: selectedEnv.file,
63
+ });
64
+ }
65
+
66
+ config.envs.set(name, updatedEnv);
67
+ config.save();
68
+
69
+ if (itemsToEdit.includes("file") && exists(selectedEnv.file)) {
70
+ logger.debug(`Renaming environment file from "${selectedEnv.file}" to "${updatedEnv.file}"`);
71
+ rename(selectedEnv.file, updatedEnv.file);
72
+ }
73
+ }
74
+ } satisfies CommandModule;
@@ -0,0 +1,6 @@
1
+ import Add from "./add";
2
+ import Edit from "./edit";
3
+ import List from "./list";
4
+ import Remove from "./remove";
5
+
6
+ export default [Add, Edit, List, Remove]
@@ -0,0 +1,16 @@
1
+ import { Configuration } from "#lib/config";
2
+ import { Logger } from "#lib/logger";
3
+
4
+ export default {
5
+ command: 'list',
6
+ describe: 'List all environments',
7
+ builder: (yargs) => yargs,
8
+ handler: async (argv) => {
9
+ const config = new Configuration();
10
+ const logger = new Logger(argv.level);
11
+
12
+ logger.debug(`Listing all environments`);
13
+
14
+ console.log(JSON.stringify(Array.from(config.envs.values()), null, 2));
15
+ }
16
+ } satisfies CommandModule;
@@ -0,0 +1,47 @@
1
+ import {
2
+ existsSync as exists,
3
+ unlinkSync as unlink,
4
+ } from "node:fs";
5
+
6
+ import { confirm, select } from "@inquirer/prompts";
7
+
8
+ import { Configuration } from "#lib/config";
9
+ import { NotFoundError } from "#lib/error";
10
+ import { Logger } from "#lib/logger";
11
+
12
+ export default {
13
+ command: 'remove',
14
+ describe: 'Remove an environment',
15
+ builder: (yargs) => yargs,
16
+ handler: async (argv) => {
17
+ const config = new Configuration();
18
+ const logger = new Logger(argv.level);
19
+
20
+ const name = await select({
21
+ message: "Select the environment to remove:",
22
+ choices: Array.from(config.envs.keys()),
23
+ });
24
+
25
+ const selectedEnv = config.envs.get(name);
26
+
27
+ if (!selectedEnv) {
28
+ throw new NotFoundError(`Environment not found: ${name}`);
29
+ }
30
+
31
+ logger.debug(`Removing environment "${name}" with file: "${selectedEnv.file}"`);
32
+
33
+ config.envs.delete(name);
34
+ config.save();
35
+
36
+ if (exists(selectedEnv.file)) {
37
+ const toDelete = await confirm({
38
+ message: `Environment file found: ${selectedEnv.file}. Do you want to delete it?`
39
+ });
40
+
41
+ if (toDelete) {
42
+ logger.debug(`Deleting environment file: ${selectedEnv.file}`);
43
+ unlink(selectedEnv.file);
44
+ }
45
+ }
46
+ }
47
+ } satisfies CommandModule;
@@ -0,0 +1,7 @@
1
+ import Environment from './environment';
2
+ import Variable from './variable';
3
+ import Use from './use';
4
+ import Init from './init';
5
+ import Setup from './setup';
6
+
7
+ export default [Environment, Variable, Use, Init, Setup];
@@ -0,0 +1,60 @@
1
+ import {
2
+ existsSync as exists,
3
+ readFileSync as read,
4
+ writeFileSync as write,
5
+ appendFileSync as append,
6
+ } from "node:fs";
7
+
8
+ import { confirm } from "@inquirer/prompts";
9
+
10
+ import { Configuration } from "#lib/config";
11
+ import { Logger } from "#lib/logger";
12
+
13
+ export default {
14
+ command: 'init',
15
+ describe: 'Initialize the project for use with envedit',
16
+ builder: (yargs) => yargs
17
+ .usage('Usage: envedit init')
18
+ .epilogue("Note that this command is meant to be used when the project is FIRST setup, not to setup based on a pre-existing configuration. for that, please run the 'setup' command"),
19
+ handler: async (argv) => {
20
+ const logger = new Logger(argv.level);
21
+ new Configuration();
22
+
23
+ if (exists('.git')) {
24
+ logger.debug(`Git repository found. Checking for git ignore file.`);
25
+
26
+ const gitignorePath = '.gitignore';
27
+
28
+ if (!exists('.gitignore')) {
29
+ const toCreate = await confirm({
30
+ message: `Git Repository without a git ignore file not found. Do you want to create it?`
31
+ });
32
+
33
+ if (toCreate) {
34
+ write('.gitignore', "");
35
+ }
36
+ }
37
+
38
+ const expectedItems = ['.env', '.env.*'];
39
+
40
+ logger.debug(`Checking for expected items in git ignore file: ${expectedItems.join(", ")}`);
41
+
42
+ for (const item of expectedItems) {
43
+ if (read(gitignorePath, "utf-8").includes(item)) {
44
+ logger.debug(`Item not found in git ignore file: ${item}`);
45
+
46
+ const toAdd = await confirm({
47
+ message: `The item ${item} is not in the git ignore file. Do you want to add it?`
48
+ });
49
+
50
+ if (!toAdd) {
51
+ console.error(`The item ${item} is not in the git ignore file.`);
52
+ process.exit(1);
53
+ }
54
+
55
+ append(gitignorePath, `\n${item}`);
56
+ }
57
+ }
58
+ }
59
+ }
60
+ } as CommandModule;
@@ -0,0 +1,46 @@
1
+ import {
2
+ lstatSync as lstat,
3
+ existsSync as exists,
4
+ copyFileSync as copy,
5
+ unlinkSync as unlink,
6
+ symlinkSync as symlink,
7
+ } from 'node:fs';
8
+
9
+ import { Configuration, EnvironmentFile } from "#lib/config";
10
+ import { NotFoundError } from "#lib/error";
11
+
12
+ export default {
13
+ command: 'setup',
14
+ describe: 'Setup the project for use with envedit',
15
+ builder: (yargs) => yargs
16
+ .usage('Usage: envedit setup')
17
+ .epilogue("Note that this command is meant to be used after the project has been setup. To make a clean start, please run the 'init' command instead."),
18
+ handler: async (_argv) => {
19
+ if (!exists('env.config.yaml')) {
20
+ throw new NotFoundError("Could not find configuration file. Did you run the init command?")
21
+ }
22
+
23
+ const config = new Configuration();
24
+
25
+ for (const environment of config.envs.values()) {
26
+ const envFile = new EnvironmentFile(environment.file);
27
+
28
+ if (environment.isActive) {
29
+ if (exists('.env') && !lstat('.env').isSymbolicLink()) {
30
+ copy('.env', environment.file);
31
+ unlink('.env');
32
+ }
33
+
34
+ symlink(environment.file, '.env', 'file');
35
+ }
36
+
37
+ for (const variable of config.vars.values()) {
38
+ if (!envFile.vars.has(variable.name)) {
39
+ envFile.vars.set(variable.name, '');
40
+ }
41
+ }
42
+
43
+ envFile.save();
44
+ }
45
+ }
46
+ } as CommandModule;
package/cli/cmd/use.ts ADDED
@@ -0,0 +1,60 @@
1
+ import {
2
+ lstatSync as lstat,
3
+ existsSync as exists,
4
+ unlinkSync as unlink,
5
+ symlinkSync as symlink,
6
+ } from 'node:fs';
7
+
8
+ import { select } from "@inquirer/prompts";
9
+
10
+ import { Configuration } from "#lib/config";
11
+ import { NotFoundError } from "#lib/error";
12
+ import { Logger } from "#lib/logger";
13
+
14
+ export default {
15
+ command: 'use',
16
+ describe: 'Set the current environment to use',
17
+ builder: (yargs) => yargs
18
+ .usage('Usage: envedit use'),
19
+ handler: async (argv) => {
20
+ const logger = new Logger(argv.level);
21
+ const config = new Configuration();
22
+
23
+ const name = await select({
24
+ message: "Select the environment to use",
25
+ choices: Array.from(config.envs.keys()).map(env => ({
26
+ name: env + (config.envs.get(env)?.isActive ? " (active)" : ""),
27
+ value: env,
28
+ })),
29
+ default: config.active,
30
+ });
31
+
32
+ const selectedEnv = config.envs.get(name);
33
+
34
+ if (!selectedEnv) {
35
+ throw new NotFoundError(`Environment not found: ${name}`);
36
+ }
37
+
38
+ logger.debug(`Setting current environment to "${name}" with file: "${selectedEnv.file}"`);
39
+
40
+ if (!selectedEnv.isActive) {
41
+ logger.debug(`Updating active environment in configuration file to "${name}"`);
42
+
43
+ config.active = selectedEnv.name;
44
+
45
+ config.save();
46
+ }
47
+
48
+ logger.debug(`Creating symlink to environment file: ${selectedEnv.file}`);
49
+
50
+ const envFilePath = selectedEnv.file;
51
+
52
+ if (exists('.env')) {
53
+ logger.debug(`Removing existing symlink or file: .env`);
54
+
55
+ unlink('.env');
56
+ }
57
+
58
+ symlink(envFilePath, '.env', 'file');
59
+ }
60
+ } as CommandModule;
@@ -0,0 +1,10 @@
1
+ import Commands from "./variableCmd";
2
+
3
+ export default {
4
+ command: 'variable',
5
+ describe: 'Manage environment variables',
6
+ builder: (yargs) => yargs
7
+ .usage('Usage: envedit variable <command> [options]')
8
+ .command(Commands)
9
+ .demandCommand(),
10
+ } as CommandModule;
@@ -0,0 +1,92 @@
1
+ import { confirm, input, password, select } from "@inquirer/prompts";
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: 'add',
9
+ describe: 'Add a new environment variable',
10
+ builder: (yargs) => yargs,
11
+ handler: async (argv) => {
12
+ const config = new Configuration();
13
+ const logger = new Logger(argv.level);
14
+
15
+ const name = await input({
16
+ message: "Enter the variable name:",
17
+ validate: (input) => {
18
+ if (!input) {
19
+ return "Variable name cannot be empty";
20
+ }
21
+ if (config.vars.has(input)) {
22
+ return "Variable name already exists";
23
+ }
24
+ return true;
25
+ },
26
+ });
27
+
28
+ const type = await select({
29
+ message: "Select the variable type:",
30
+ choices: [
31
+ { name: "Public", value: VariableType.Public },
32
+ { name: "Private", value: VariableType.Private },
33
+ ],
34
+ });
35
+
36
+ logger.debug(`Adding new variable "${name}" with type "${type}"`);
37
+
38
+ config.vars.set(name, { name, type });
39
+ config.save();
40
+
41
+ const shouldGloballySet = await confirm({
42
+ message: "Do you want to set this variable with a global value for all environments?",
43
+ });
44
+
45
+ const valuesToSet = Array.from(config.envs.values()).map(env => ({
46
+ envName: env.name,
47
+ value: '',
48
+ }));
49
+
50
+ if (shouldGloballySet) {
51
+ const request = {
52
+ message: `Enter the global value for ${name}:`,
53
+ mask: type === VariableType.Private ? "*" : undefined,
54
+ };
55
+
56
+ const globalValue = type === VariableType.Private
57
+ ? await password(request)
58
+ : await input(request);
59
+
60
+ logger.debug(`Setting variable "${name}" with global value "${globalValue}" for all environments`);
61
+
62
+ valuesToSet.forEach((envValue) => { envValue.value = globalValue });
63
+ } else {
64
+ for (const envValue of valuesToSet) {
65
+ const request = {
66
+ message: `Enter the value for ${name} in environment ${envValue.envName}:`,
67
+ mask: type === VariableType.Private ? "*" : undefined,
68
+ };
69
+
70
+ envValue.value = type === VariableType.Private
71
+ ? await password(request)
72
+ : await input(request);
73
+
74
+ logger.debug(`Setting variable "${name}" with value "${envValue.value}" for environment "${envValue.envName}"`);
75
+ }
76
+ }
77
+
78
+ for (const envValue of valuesToSet) {
79
+ const env = config.envs.get(envValue.envName);
80
+ logger.debug(`Retrieving environment: ${envValue.envName}`);
81
+
82
+ if (!env) {
83
+ throw new NotFoundError(`Environment not found: ${envValue.envName}`);
84
+ }
85
+
86
+ const envFile = new EnvironmentFile(env.file);
87
+
88
+ envFile.vars.set(name, envValue.value);
89
+ envFile.save();
90
+ }
91
+ }
92
+ } satisfies CommandModule;