-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex
executable file
·56 lines (45 loc) · 1.45 KB
/
index
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
#!/usr/bin/env node
'use strict'
const fs = require('fs');
const yaml = require('js-yaml');
const program = require('commander');
program
.version(JSON.parse(require('fs').readFileSync(
require.main.filename.match(/^(.+)\/.+$/)[1] + '/package.json')).version)
.option('-i --input-file <fileName>',
'input file .env.yaml',
'.env.yaml')
.option('-o --output-file <fileName>',
'output file .env',
'.env')
.option('-e --environ <environ>',
'select env default local',
'local')
.usage('{options}')
.parse();
const options = program.opts();
const yamlFilePath = options.inputFile;
const envFilePath = options.outputFile;
const environ = options.environ;
// 既存の .env ファイルを削除
if (fs.existsSync(envFilePath)) {
fs.unlinkSync(envFilePath);
}
const yamlContent = fs.readFileSync(yamlFilePath, 'utf8');
const data = yaml.load(yamlContent);
const envData = [];
for (const key in data) {
if (typeof data[key] === 'object') {
const keys = Object.keys(data[key]);
if (keys.includes(environ)) {
envData.push(`${key}="${data[key][environ]}"`);
} else if (keys.includes("default")) {
envData.push(`${key}="${data[key]["default"]}"`);
}
} else {
envData.push(`${key}="${data[key]}"`);
}
}
fs.writeFileSync(envFilePath, envData.join('\n'), 'utf8');
// console.log('.env ファイルが生成されました:');
// console.log(envData.join('\n'));