envcraft-cli 0.0.0-stage → 1.0.0
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 +350 -2
- package/package.json +38 -4
- package/src/cli-store.js +233 -0
- package/src/cli.js +236 -0
- package/src/config.js +90 -0
- package/src/manager.js +261 -0
- package/src/storage/db.js +309 -0
- package/src/storage/encryption.js +99 -0
- package/src/storage/firebase-db.js +241 -0
- package/src/storage/ssh-file-db.js +360 -0
- package/src/tui.js +130 -0
- package/src/utils/logger.js +78 -0
- package/src/utils/parser.js +45 -0
- package/src/vm.js +49 -0
package/src/cli.js
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { parseArgs } from './utils/parser.js';
|
|
4
|
+
import { EnvManager } from './manager.js';
|
|
5
|
+
import { logError, logSuccess, table, sectionTable } from './utils/logger.js';
|
|
6
|
+
import { CONFIG_PATH, getSetting, setSetting, unsetSetting, listSettings } from './config.js';
|
|
7
|
+
import { fetchVmEnv } from './vm.js';
|
|
8
|
+
|
|
9
|
+
async function handleConfig(params) {
|
|
10
|
+
const [action, name, value] = params;
|
|
11
|
+
switch (action) {
|
|
12
|
+
case 'set':
|
|
13
|
+
if (!name || value === undefined) throw new Error('Usage: envman config set <setting> <value>');
|
|
14
|
+
setSetting(name, value);
|
|
15
|
+
logSuccess(`Set ${name} = ${value} (${CONFIG_PATH})`);
|
|
16
|
+
break;
|
|
17
|
+
case 'get': {
|
|
18
|
+
if (!name) throw new Error('Usage: envman config get <setting>');
|
|
19
|
+
const current = getSetting(name);
|
|
20
|
+
console.log(current ?? '');
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
case 'unset':
|
|
24
|
+
if (!name) throw new Error('Usage: envman config unset <setting>');
|
|
25
|
+
unsetSetting(name);
|
|
26
|
+
logSuccess(`Removed ${name}`);
|
|
27
|
+
break;
|
|
28
|
+
case 'list':
|
|
29
|
+
case undefined:
|
|
30
|
+
console.log(`Config file: ${CONFIG_PATH}`);
|
|
31
|
+
listSettings().forEach(({ name: n, value: v }) => console.log(` ${n.padEnd(14)} ${v ?? '(not set)'}`));
|
|
32
|
+
break;
|
|
33
|
+
default:
|
|
34
|
+
throw new Error('Usage: envman config <set|get|unset|list> [setting] [value]');
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function handleVm(params) {
|
|
39
|
+
const [action, outFile] = params;
|
|
40
|
+
if (action !== 'fetch') throw new Error('Usage: envman vm fetch [outputPath]');
|
|
41
|
+
const { settings, file } = await fetchVmEnv(outFile);
|
|
42
|
+
logSuccess(`Saved ${settings.user}@${settings.host}:${settings.envPath} to ${file}`);
|
|
43
|
+
const sections = await new EnvManager(file).listSections();
|
|
44
|
+
sectionTable(sections);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function main() {
|
|
48
|
+
try {
|
|
49
|
+
const args = parseArgs(process.argv.slice(2));
|
|
50
|
+
const file = args.options.file || args.options.f || '.env';
|
|
51
|
+
const format = args.options.format || 'table';
|
|
52
|
+
const globalMode = args.options.global || args.options.g;
|
|
53
|
+
|
|
54
|
+
if (!args.command || args.options.help || args.options.h) {
|
|
55
|
+
showHelp();
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (args.options.version || args.options.v) {
|
|
60
|
+
console.log('envman 1.0.0');
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (args.command === 'config') {
|
|
65
|
+
await handleConfig(args.params);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (args.command === 'vm') {
|
|
70
|
+
await handleVm(args.params);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const manager = new EnvManager(file, globalMode);
|
|
75
|
+
|
|
76
|
+
switch (args.command) {
|
|
77
|
+
case 'set':
|
|
78
|
+
if (args.params.length < 2) throw new Error('Usage: envman set <key> <value>');
|
|
79
|
+
await manager.set(args.params[0], args.params[1]);
|
|
80
|
+
logSuccess(`Set ${args.params[0]} = ${args.params[1]}`);
|
|
81
|
+
break;
|
|
82
|
+
|
|
83
|
+
case 'get':
|
|
84
|
+
if (args.params.length < 1) throw new Error('Usage: envman get <key>');
|
|
85
|
+
const value = await manager.get(args.params[0]);
|
|
86
|
+
console.log(value || '');
|
|
87
|
+
break;
|
|
88
|
+
|
|
89
|
+
case 'list':
|
|
90
|
+
case 'ls':
|
|
91
|
+
const vars = await manager.list();
|
|
92
|
+
const prefix = args.options.prefix;
|
|
93
|
+
const filtered = prefix ? Object.fromEntries(
|
|
94
|
+
Object.entries(vars).filter(([k]) => k.startsWith(prefix))
|
|
95
|
+
) : vars;
|
|
96
|
+
|
|
97
|
+
if (format === 'json') {
|
|
98
|
+
console.log(JSON.stringify(filtered, null, 2));
|
|
99
|
+
} else if (format === 'shell') {
|
|
100
|
+
Object.entries(filtered).forEach(([k, v]) => {
|
|
101
|
+
console.log(`export ${k}="${v}"`);
|
|
102
|
+
});
|
|
103
|
+
} else if (globalMode) {
|
|
104
|
+
table(filtered);
|
|
105
|
+
} else {
|
|
106
|
+
const sections = (await manager.listSections()).map((section) => ({
|
|
107
|
+
...section,
|
|
108
|
+
vars: section.vars.filter(({ key }) => !prefix || key.startsWith(prefix)),
|
|
109
|
+
}));
|
|
110
|
+
sectionTable(sections);
|
|
111
|
+
}
|
|
112
|
+
break;
|
|
113
|
+
|
|
114
|
+
case 'delete':
|
|
115
|
+
case 'del':
|
|
116
|
+
case 'rm':
|
|
117
|
+
if (args.params.length < 1) throw new Error('Usage: envman delete <key>');
|
|
118
|
+
await manager.delete(args.params[0]);
|
|
119
|
+
logSuccess(`Deleted ${args.params[0]}`);
|
|
120
|
+
break;
|
|
121
|
+
|
|
122
|
+
case 'load':
|
|
123
|
+
if (args.params.length < 1) throw new Error('Usage: envman load <file>');
|
|
124
|
+
const loaded = await manager.loadFromFile(args.params[0]);
|
|
125
|
+
logSuccess(`Loaded ${loaded} variables from ${args.params[0]}`);
|
|
126
|
+
break;
|
|
127
|
+
|
|
128
|
+
case 'save':
|
|
129
|
+
if (args.params.length < 1) throw new Error('Usage: envman save <file>');
|
|
130
|
+
await manager.saveToFile(args.params[0]);
|
|
131
|
+
logSuccess(`Saved variables to ${args.params[0]}`);
|
|
132
|
+
break;
|
|
133
|
+
|
|
134
|
+
case 'copy':
|
|
135
|
+
if (args.params.length < 2) throw new Error('Usage: envman copy <source> <dest>');
|
|
136
|
+
await manager.copyFile(args.params[0], args.params[1]);
|
|
137
|
+
logSuccess(`Copied ${args.params[0]} to ${args.params[1]}`);
|
|
138
|
+
break;
|
|
139
|
+
|
|
140
|
+
case 'validate':
|
|
141
|
+
if (args.params.length < 1) throw new Error('Usage: envman validate <file>');
|
|
142
|
+
const result = await manager.validate(args.params[0]);
|
|
143
|
+
if (result.valid) {
|
|
144
|
+
logSuccess('File is valid');
|
|
145
|
+
} else {
|
|
146
|
+
console.log('Validation errors:');
|
|
147
|
+
result.errors.forEach(e => logError(e));
|
|
148
|
+
}
|
|
149
|
+
break;
|
|
150
|
+
|
|
151
|
+
case 'merge':
|
|
152
|
+
if (args.params.length < 2) throw new Error('Usage: envman merge <file1> <file2>');
|
|
153
|
+
const merged = await manager.merge(args.params[0], args.params[1]);
|
|
154
|
+
if (format === 'json') {
|
|
155
|
+
console.log(JSON.stringify(merged, null, 2));
|
|
156
|
+
} else {
|
|
157
|
+
table(merged);
|
|
158
|
+
}
|
|
159
|
+
break;
|
|
160
|
+
|
|
161
|
+
case 'diff':
|
|
162
|
+
if (args.params.length < 2) throw new Error('Usage: envman diff <file1> <file2>');
|
|
163
|
+
const diff = await manager.diff(args.params[0], args.params[1]);
|
|
164
|
+
console.log(diff);
|
|
165
|
+
break;
|
|
166
|
+
|
|
167
|
+
case 'export':
|
|
168
|
+
const exported = await manager.list();
|
|
169
|
+
Object.entries(exported).forEach(([k, v]) => {
|
|
170
|
+
console.log(`export ${k}="${v}"`);
|
|
171
|
+
});
|
|
172
|
+
break;
|
|
173
|
+
|
|
174
|
+
case 'help':
|
|
175
|
+
showHelp();
|
|
176
|
+
break;
|
|
177
|
+
|
|
178
|
+
default:
|
|
179
|
+
throw new Error(`Unknown command: ${args.command}`);
|
|
180
|
+
}
|
|
181
|
+
} catch (error) {
|
|
182
|
+
logError(error.message);
|
|
183
|
+
process.exit(1);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function showHelp() {
|
|
188
|
+
console.log(`
|
|
189
|
+
envman - Environment Variables Management CLI
|
|
190
|
+
|
|
191
|
+
Usage:
|
|
192
|
+
envman <command> [options]
|
|
193
|
+
|
|
194
|
+
Commands:
|
|
195
|
+
set <key> <value> Set an environment variable
|
|
196
|
+
get <key> Get an environment variable
|
|
197
|
+
list (ls) List all environment variables
|
|
198
|
+
delete <key> (del, rm) Delete an environment variable
|
|
199
|
+
load <file> Load variables from a file
|
|
200
|
+
save <file> Save variables to a file
|
|
201
|
+
copy <source> <dest> Copy variables from one file to another
|
|
202
|
+
validate <file> Validate a file
|
|
203
|
+
merge <file1> <file2> Merge two files
|
|
204
|
+
diff <file1> <file2> Compare two files
|
|
205
|
+
export Export variables in shell format
|
|
206
|
+
config set|get|unset|list Manage VM/SSH settings (vm.host, vm.user, vm.key, vm.env-path)
|
|
207
|
+
vm fetch [outputPath] Download the remote env file over SSH
|
|
208
|
+
help Show this help message
|
|
209
|
+
|
|
210
|
+
Options:
|
|
211
|
+
--file, -f <path> Target file (default: .env)
|
|
212
|
+
--format <format> Output format: json, table, shell (default: table)
|
|
213
|
+
--prefix <prefix> Filter by prefix
|
|
214
|
+
--global, -g Use environment variables instead of file
|
|
215
|
+
--help, -h Show help
|
|
216
|
+
--version, -v Show version
|
|
217
|
+
|
|
218
|
+
Examples:
|
|
219
|
+
envman set API_KEY "secret123" --file .env.production
|
|
220
|
+
envman get API_KEY
|
|
221
|
+
envman list --format json
|
|
222
|
+
envman load .env.example
|
|
223
|
+
envman validate .env
|
|
224
|
+
envman diff .env .env.example
|
|
225
|
+
envman export | bash
|
|
226
|
+
envman config set vm.host 203.0.113.10
|
|
227
|
+
envman config set vm.user ubuntu
|
|
228
|
+
envman config set vm.key ~/.ssh/my_server_key
|
|
229
|
+
envman vm fetch
|
|
230
|
+
`);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
main().catch(err => {
|
|
234
|
+
logError(err.message);
|
|
235
|
+
process.exit(1);
|
|
236
|
+
});
|
package/src/config.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync, mkdirSync, chmodSync } from 'node:fs';
|
|
2
|
+
import { homedir, userInfo } from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* User settings for the VM / SSH features, stored outside the repo.
|
|
7
|
+
* Location: $ENVMAN_CONFIG, or ~/.config/envman/config.json.
|
|
8
|
+
*/
|
|
9
|
+
export const CONFIG_PATH = process.env.ENVMAN_CONFIG
|
|
10
|
+
|| path.join(homedir(), '.config', 'envman', 'config.json');
|
|
11
|
+
|
|
12
|
+
// Setting name shown to the user -> field stored under `vm` in the config file
|
|
13
|
+
const VM_KEYS = {
|
|
14
|
+
'vm.host': 'host',
|
|
15
|
+
'vm.user': 'user',
|
|
16
|
+
'vm.key': 'keyPath',
|
|
17
|
+
'vm.env-path': 'envPath',
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export function loadConfig() {
|
|
21
|
+
if (!existsSync(CONFIG_PATH)) return {};
|
|
22
|
+
try {
|
|
23
|
+
return JSON.parse(readFileSync(CONFIG_PATH, 'utf-8'));
|
|
24
|
+
} catch (err) {
|
|
25
|
+
throw new Error(`Cannot read config file ${CONFIG_PATH}: ${err.message}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function saveConfig(config) {
|
|
30
|
+
mkdirSync(path.dirname(CONFIG_PATH), { recursive: true, mode: 0o700 });
|
|
31
|
+
writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2) + '\n', { mode: 0o600 });
|
|
32
|
+
chmodSync(CONFIG_PATH, 0o600);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function fieldFor(name) {
|
|
36
|
+
const field = VM_KEYS[name];
|
|
37
|
+
if (!field) {
|
|
38
|
+
throw new Error(`Unknown setting "${name}". Valid settings: ${Object.keys(VM_KEYS).join(', ')}`);
|
|
39
|
+
}
|
|
40
|
+
return field;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function getSetting(name) {
|
|
44
|
+
return loadConfig().vm?.[fieldFor(name)];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function setSetting(name, value) {
|
|
48
|
+
const field = fieldFor(name);
|
|
49
|
+
const config = loadConfig();
|
|
50
|
+
config.vm = { ...config.vm, [field]: value };
|
|
51
|
+
saveConfig(config);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function unsetSetting(name) {
|
|
55
|
+
const field = fieldFor(name);
|
|
56
|
+
const config = loadConfig();
|
|
57
|
+
if (config.vm) delete config.vm[field];
|
|
58
|
+
saveConfig(config);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function listSettings() {
|
|
62
|
+
const vm = loadConfig().vm || {};
|
|
63
|
+
return Object.entries(VM_KEYS).map(([name, field]) => ({ name, value: vm[field] }));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function expandHome(p) {
|
|
67
|
+
if (p === '~') return homedir();
|
|
68
|
+
if (p.startsWith('~/')) return path.join(homedir(), p.slice(2));
|
|
69
|
+
return p;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Resolve VM/SSH settings. Priority: explicit overrides > environment variables > config file > defaults.
|
|
74
|
+
*/
|
|
75
|
+
export function resolveVmSettings(overrides = {}) {
|
|
76
|
+
const saved = loadConfig().vm || {};
|
|
77
|
+
const pick = (override, envName, field) => override || process.env[envName] || saved[field];
|
|
78
|
+
|
|
79
|
+
const host = pick(overrides.host, 'VM_HOST', 'host');
|
|
80
|
+
if (!host) {
|
|
81
|
+
throw new Error('No server configured. Set one with: envman config set vm.host <host>');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
host,
|
|
86
|
+
user: pick(overrides.user, 'VM_USER', 'user') || userInfo().username,
|
|
87
|
+
keyPath: expandHome(pick(overrides.keyPath, 'VM_KEY', 'keyPath') || '~/.ssh/id_rsa'),
|
|
88
|
+
envPath: pick(overrides.envPath, 'VM_ENV_PATH', 'envPath') || '~/.env',
|
|
89
|
+
};
|
|
90
|
+
}
|
package/src/manager.js
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync, existsSync } from 'fs';
|
|
2
|
+
import { dirname } from 'path';
|
|
3
|
+
import { mkdirSync } from 'fs';
|
|
4
|
+
|
|
5
|
+
export class EnvManager {
|
|
6
|
+
constructor(file = '.env', globalMode = false) {
|
|
7
|
+
this.file = file;
|
|
8
|
+
this.globalMode = globalMode;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// Parse .env file format
|
|
12
|
+
static parse(content) {
|
|
13
|
+
const vars = {};
|
|
14
|
+
const lines = content.split('\n');
|
|
15
|
+
|
|
16
|
+
for (const line of lines) {
|
|
17
|
+
const trimmed = line.trim();
|
|
18
|
+
// Skip empty lines and comments
|
|
19
|
+
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
20
|
+
|
|
21
|
+
const eqIndex = trimmed.indexOf('=');
|
|
22
|
+
if (eqIndex === -1) continue;
|
|
23
|
+
|
|
24
|
+
const key = trimmed.slice(0, eqIndex).trim();
|
|
25
|
+
let value = trimmed.slice(eqIndex + 1).trim();
|
|
26
|
+
|
|
27
|
+
// Remove quotes if present
|
|
28
|
+
if ((value.startsWith('"') && value.endsWith('"')) ||
|
|
29
|
+
(value.startsWith("'") && value.endsWith("'"))) {
|
|
30
|
+
value = value.slice(1, -1);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
vars[key] = value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return vars;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Parse .env file format, grouping variables under the comment that precedes them
|
|
40
|
+
static parseSections(content) {
|
|
41
|
+
const sections = [];
|
|
42
|
+
let current = { title: null, vars: [] };
|
|
43
|
+
|
|
44
|
+
for (const line of content.split(/\r?\n/)) {
|
|
45
|
+
const trimmed = line.trim();
|
|
46
|
+
if (!trimmed) continue;
|
|
47
|
+
|
|
48
|
+
if (trimmed.startsWith('#')) {
|
|
49
|
+
// A comment starts a new section, unless the current one has no variables yet
|
|
50
|
+
if (current.vars.length > 0 || current.title !== null) sections.push(current);
|
|
51
|
+
current = { title: trimmed.replace(/^#+\s*/, ''), vars: [] };
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const eqIndex = trimmed.indexOf('=');
|
|
56
|
+
if (eqIndex === -1) continue;
|
|
57
|
+
|
|
58
|
+
const key = trimmed.slice(0, eqIndex).trim().replace(/^export\s+/, '');
|
|
59
|
+
let value = trimmed.slice(eqIndex + 1).trim();
|
|
60
|
+
|
|
61
|
+
// Remove quotes if present
|
|
62
|
+
if ((value.startsWith('"') && value.endsWith('"')) ||
|
|
63
|
+
(value.startsWith("'") && value.endsWith("'"))) {
|
|
64
|
+
value = value.slice(1, -1);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
current.vars.push({ key, value });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (current.vars.length > 0) sections.push(current);
|
|
71
|
+
return sections;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Format variables to .env file format
|
|
75
|
+
static format(vars) {
|
|
76
|
+
return Object.entries(vars)
|
|
77
|
+
.map(([k, v]) => {
|
|
78
|
+
// Quote values with spaces or special characters
|
|
79
|
+
const needsQuotes = v.includes(' ') || v.includes('\n') || v.includes('=');
|
|
80
|
+
const quotedValue = needsQuotes ? `"${v.replace(/"/g, '\\"')}"` : v;
|
|
81
|
+
return `${k}=${quotedValue}`;
|
|
82
|
+
})
|
|
83
|
+
.join('\n') + '\n';
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async set(key, value) {
|
|
87
|
+
if (this.globalMode) {
|
|
88
|
+
process.env[key] = value;
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const vars = await this.list();
|
|
93
|
+
vars[key] = value;
|
|
94
|
+
this._write(vars);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async get(key) {
|
|
98
|
+
if (this.globalMode) {
|
|
99
|
+
return process.env[key] || null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const vars = await this.list();
|
|
103
|
+
return vars[key] || null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async list() {
|
|
107
|
+
if (this.globalMode) {
|
|
108
|
+
return { ...process.env };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (!existsSync(this.file)) {
|
|
112
|
+
return {};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const content = readFileSync(this.file, 'utf-8');
|
|
116
|
+
return EnvManager.parse(content);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Same as list(), but keeps each variable's comment section
|
|
120
|
+
async listSections() {
|
|
121
|
+
if (!existsSync(this.file)) {
|
|
122
|
+
return [];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const content = readFileSync(this.file, 'utf-8');
|
|
126
|
+
return EnvManager.parseSections(content);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async delete(key) {
|
|
130
|
+
if (this.globalMode) {
|
|
131
|
+
delete process.env[key];
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const vars = await this.list();
|
|
136
|
+
delete vars[key];
|
|
137
|
+
this._write(vars);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async loadFromFile(sourceFile) {
|
|
141
|
+
if (!existsSync(sourceFile)) {
|
|
142
|
+
throw new Error(`File not found: ${sourceFile}`);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const content = readFileSync(sourceFile, 'utf-8');
|
|
146
|
+
const newVars = EnvManager.parse(content);
|
|
147
|
+
const currentVars = await this.list();
|
|
148
|
+
const merged = { ...currentVars, ...newVars };
|
|
149
|
+
this._write(merged);
|
|
150
|
+
|
|
151
|
+
return Object.keys(newVars).length;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async saveToFile(destFile) {
|
|
155
|
+
const vars = await this.list();
|
|
156
|
+
const dir = dirname(destFile);
|
|
157
|
+
|
|
158
|
+
if (dir && !existsSync(dir)) {
|
|
159
|
+
mkdirSync(dir, { recursive: true });
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
writeFileSync(destFile, EnvManager.format(vars), 'utf-8');
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async copyFile(sourceFile, destFile) {
|
|
166
|
+
if (!existsSync(sourceFile)) {
|
|
167
|
+
throw new Error(`File not found: ${sourceFile}`);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const content = readFileSync(sourceFile, 'utf-8');
|
|
171
|
+
const dir = dirname(destFile);
|
|
172
|
+
|
|
173
|
+
if (dir && !existsSync(dir)) {
|
|
174
|
+
mkdirSync(dir, { recursive: true });
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
writeFileSync(destFile, content, 'utf-8');
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async validate(file) {
|
|
181
|
+
if (!existsSync(file)) {
|
|
182
|
+
return { valid: false, errors: [`File not found: ${file}`] };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const errors = [];
|
|
186
|
+
try {
|
|
187
|
+
const content = readFileSync(file, 'utf-8');
|
|
188
|
+
const lines = content.split('\n');
|
|
189
|
+
|
|
190
|
+
for (let i = 0; i < lines.length; i++) {
|
|
191
|
+
const line = lines[i].trim();
|
|
192
|
+
if (!line || line.startsWith('#')) continue;
|
|
193
|
+
|
|
194
|
+
const eqIndex = line.indexOf('=');
|
|
195
|
+
if (eqIndex === -1) {
|
|
196
|
+
errors.push(`Line ${i + 1}: No '=' found in: ${line}`);
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const key = line.slice(0, eqIndex).trim();
|
|
201
|
+
if (!key.match(/^[A-Z_][A-Z0-9_]*$/i)) {
|
|
202
|
+
errors.push(`Line ${i + 1}: Invalid key name: ${key}`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return { valid: errors.length === 0, errors };
|
|
207
|
+
} catch (err) {
|
|
208
|
+
return { valid: false, errors: [err.message] };
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async merge(file1, file2) {
|
|
213
|
+
if (!existsSync(file1)) throw new Error(`File not found: ${file1}`);
|
|
214
|
+
if (!existsSync(file2)) throw new Error(`File not found: ${file2}`);
|
|
215
|
+
|
|
216
|
+
const content1 = readFileSync(file1, 'utf-8');
|
|
217
|
+
const content2 = readFileSync(file2, 'utf-8');
|
|
218
|
+
|
|
219
|
+
const vars1 = EnvManager.parse(content1);
|
|
220
|
+
const vars2 = EnvManager.parse(content2);
|
|
221
|
+
|
|
222
|
+
return { ...vars1, ...vars2 };
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async diff(file1, file2) {
|
|
226
|
+
if (!existsSync(file1)) throw new Error(`File not found: ${file1}`);
|
|
227
|
+
if (!existsSync(file2)) throw new Error(`File not found: ${file2}`);
|
|
228
|
+
|
|
229
|
+
const content1 = readFileSync(file1, 'utf-8');
|
|
230
|
+
const content2 = readFileSync(file2, 'utf-8');
|
|
231
|
+
|
|
232
|
+
const vars1 = EnvManager.parse(content1);
|
|
233
|
+
const vars2 = EnvManager.parse(content2);
|
|
234
|
+
|
|
235
|
+
const allKeys = new Set([...Object.keys(vars1), ...Object.keys(vars2)]);
|
|
236
|
+
const diffs = [];
|
|
237
|
+
|
|
238
|
+
for (const key of allKeys) {
|
|
239
|
+
const val1 = vars1[key];
|
|
240
|
+
const val2 = vars2[key];
|
|
241
|
+
|
|
242
|
+
if (val1 === undefined) {
|
|
243
|
+
diffs.push(`+ ${key}="${val2}" (only in ${file2})`);
|
|
244
|
+
} else if (val2 === undefined) {
|
|
245
|
+
diffs.push(`- ${key}="${val1}" (only in ${file1})`);
|
|
246
|
+
} else if (val1 !== val2) {
|
|
247
|
+
diffs.push(`~ ${key}: "${val1}" → "${val2}"`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return diffs.length > 0 ? diffs.join('\n') : 'Files are identical';
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
_write(vars) {
|
|
255
|
+
const dir = dirname(this.file);
|
|
256
|
+
if (dir && !existsSync(dir)) {
|
|
257
|
+
mkdirSync(dir, { recursive: true });
|
|
258
|
+
}
|
|
259
|
+
writeFileSync(this.file, EnvManager.format(vars), 'utf-8');
|
|
260
|
+
}
|
|
261
|
+
}
|