-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
127 lines (106 loc) · 4.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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);