-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 48c7f60
Showing
6 changed files
with
4,331 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# envsnap | ||
.envsnap | ||
# logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
# dependencies | ||
node_modules/ | ||
jspm_packages/ | ||
# npm | ||
.npm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Secton | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<p align="center"> | ||
<img src="https://cdn.secton.org/envsnap/logo.svg" height="60" alt="envsnap Logo" /> | ||
</p> | ||
|
||
<p align="center"> | ||
<a href="https://github.com/WeAreSecton/envsnap"> | ||
<img src="https://img.shields.io/github/repo-size/WeAreSecton/envsnap" alt="Repo Size" /> | ||
</a> | ||
<a href="https://github.com/WeAreSecton/envsnap/issues"> | ||
<img src="https://img.shields.io/github/issues/WeAreSecton/envsnap" alt="Issues" /> | ||
</a> | ||
<a href="https://github.com/WeAreSecton/envsnap/blob/main/LICENSE"> | ||
<img src="https://img.shields.io/github/license/WeAreSecton/envsnap" alt="License" /> | ||
</a> | ||
</p> | ||
|
||
## Features | ||
|
||
- **Create** environments with a single command | ||
- **Set** and **Use** environments seamlessly | ||
- **List** all available environments | ||
- **Delete** environments when they're no longer needed | ||
- **Automatic** validation of `.env` file formats *(coming soon)* | ||
|
||
## Installation | ||
|
||
To install `envsnap`, you need **Node.js** and **npm** installed on your system. Once you have those, you can install `envsnap` globally using *npm*: | ||
|
||
```sh | ||
npm install -g envsnap | ||
``` | ||
|
||
Or use *npx* to run it without global installation: | ||
|
||
```sh | ||
npx envsnap <command> | ||
``` | ||
|
||
## Usage | ||
|
||
### Create an environment | ||
```sh | ||
npx envsnap create staging | ||
``` | ||
|
||
### Set the active environment | ||
```sh | ||
npx envsnap set staging | ||
``` | ||
|
||
### Use a specific environment | ||
```sh | ||
npx envsnap use staging | ||
``` | ||
|
||
### Delete an environment | ||
```sh | ||
npx envsnap delete staging | ||
``` | ||
|
||
### List all environemnts | ||
```sh | ||
npx envsnap list | ||
``` | ||
|
||
## License | ||
**envsnap** is licensed under the MIT License. See the <a href="https://github.com/WeAreSecton/envsnap/blob/main/LICENSE">LICENSE</a> file for more information. | ||
|
||
###### Thank you for using envsnap! We hope it makes managing your environment configurations a breeze. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#!/usr/bin/env node | ||
|
||
const { Command } = require('commander'); | ||
const chalk = require('chalk'); | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
const dotenv = require('dotenv'); | ||
const ora = require('ora'); | ||
|
||
const program = new Command(); | ||
program.name('envsnap'); | ||
const CONFIG_DIR = path.join(process.cwd(), '.envsnap'); | ||
|
||
const ensureConfigDir = () => { | ||
fs.ensureDirSync(CONFIG_DIR); | ||
}; | ||
|
||
const fileExists = (filePath) => fs.pathExistsSync(filePath); | ||
|
||
// !!! EXPERIMENTAL | ||
const gitWarning = () => { | ||
/*if (fs.pathExistsSync(path.join(process.cwd(), '.git'))) { | ||
if (fs.pathExistsSync(GITIGNORE_PATH) && fs.readFileSync(GITIGNORE_PATH, 'utf8').split('\n').some(line => line.trim() !== '.envsnap')) { | ||
console.warn(chalk.yellow('Warning: .envsnap is not listed in .gitignore. Your environment files might be committed to the repository.')); | ||
} | ||
}*/ | ||
}; | ||
|
||
program | ||
.command('create <environment>') | ||
.alias('init') | ||
.description('Create a new environment configuration') | ||
.action((env) => { | ||
ensureConfigDir(); | ||
gitWarning(); | ||
const envFilePath = path.join(CONFIG_DIR, `.env.${env}`); | ||
const spinner = ora(`Creating and initializing environment ${env}...`).start(); | ||
|
||
if (fileExists(envFilePath)) { | ||
spinner.fail(chalk.red(`Environment ${env} already exists.`)); | ||
process.exit(1); | ||
} | ||
|
||
fs.writeFileSync(envFilePath, '', 'utf8'); | ||
spinner.succeed(chalk.green(`Environment ${env} created.`)); | ||
}); | ||
|
||
program | ||
.command('set <environment>') | ||
.description('Set the active environment') | ||
.action((env) => { | ||
ensureConfigDir(); | ||
gitWarning(); | ||
const envFilePath = path.join(CONFIG_DIR, `.env.${env}`); | ||
const targetEnvFile = path.join(process.cwd(), '.env'); | ||
const spinner = ora(`Setting environment to ${env}...`).start(); | ||
|
||
if (!fileExists(envFilePath)) { | ||
spinner.fail(chalk.red(`Environment ${env} does not exist.`)); | ||
process.exit(1); | ||
} | ||
|
||
fs.copyFileSync(envFilePath, targetEnvFile); | ||
spinner.succeed(chalk.green(`Environment set to ${env}.`)); | ||
}); | ||
|
||
program | ||
.command('use <environment>') | ||
.description('Use the specified environment variables in the current process') | ||
.action((env) => { | ||
ensureConfigDir(); | ||
gitWarning(); | ||
const envFilePath = path.join(CONFIG_DIR, `.env.${env}`); | ||
const spinner = ora(`Loading environment ${env}...`).start(); | ||
|
||
if (!fileExists(envFilePath)) { | ||
spinner.fail(chalk.red(`Environment ${env} does not exist.`)); | ||
process.exit(1); | ||
} | ||
|
||
dotenv.config({ path: envFilePath }); | ||
spinner.succeed(chalk.green(`Environment ${env} loaded.`)); | ||
}); | ||
|
||
program | ||
.command('list') | ||
.description('List all available environments') | ||
.action(() => { | ||
ensureConfigDir(); | ||
gitWarning(); | ||
const spinner = ora('Listing available environments...').start(); | ||
const files = fs.readdirSync(CONFIG_DIR) | ||
.filter(file => file.startsWith('.env.')) | ||
.map(file => file.replace('.env.', '')); | ||
|
||
if (files.length === 0) { | ||
spinner.warn(chalk.yellow('No environments found.')); | ||
} else { | ||
spinner.info(chalk.blue('Available environments:')); | ||
files.forEach(env => console.log(`- ${env}`)); | ||
} | ||
}); | ||
|
||
program | ||
.command('delete <environment>') | ||
.description('Delete a specific environment configuration') | ||
.action((env) => { | ||
ensureConfigDir(); | ||
gitWarning(); | ||
const envFilePath = path.join(CONFIG_DIR, `.env.${env}`); | ||
const spinner = ora(`Deleting environment ${env}...`).start(); | ||
|
||
if (!fileExists(envFilePath)) { | ||
spinner.fail(chalk.red(`Environment ${env} does not exist.`)); | ||
process.exit(1); | ||
} | ||
|
||
fs.removeSync(envFilePath); | ||
spinner.succeed(chalk.green(`Environment ${env} deleted.`)); | ||
}); | ||
|
||
program | ||
.action((command) => { | ||
program.help(); | ||
}); | ||
|
||
program.parse(process.argv); |
Oops, something went wrong.