-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·92 lines (78 loc) · 3.05 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
#!/usr/bin/env node
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const program = require('commander');
const DEFAULT_TS_CONFIG_FILE_PATH = './tsconfig.json';
const DEFAULT_JSON_INDENTATION = 4;
const environmentVariableRegex = new RegExp(/\$\{([\w]+)\}/, 'g');
program
.version('0.0.5')
.option('-i, --input [inputFile]', 'Specifies the path of the input JSON file')
.option('-o, --output [outputFile]', 'Specifies the path of the output JSON file')
.parse(process.argv);
generateTSConfig(program.input, program.output);
function generateTSConfig(inputFilePath, outputFilePath = DEFAULT_TS_CONFIG_FILE_PATH)
{
let config = JSON.parse(fs.readFileSync(inputFilePath, 'utf8'));
replaceConfigKeyIfExists(config, 'compilerOptions.baseUrl', path.dirname(outputFilePath));
replaceConfigKeyIfExists(config, 'extends', path.dirname(outputFilePath));
const absoluteBaseUrlPath = replaceAllMatches(environmentVariableRegex, config.compilerOptions.baseUrl);
walkTheObjectAndReplaceEnvironmentVariables(config, absoluteBaseUrlPath);
fs.writeFileSync(outputFilePath ? outputFilePath : DEFAULT_TS_CONFIG_FILE_PATH, JSON.stringify(config, null,
DEFAULT_JSON_INDENTATION));
console.log(`Successfully created the file '${outputFilePath}'.`);
}
function walkTheObjectAndReplaceEnvironmentVariables(config, absoluteBaseUrlPath)
{
for (let key in config)
{
if (typeof config[key] === 'object')
{
walkTheObjectAndReplaceEnvironmentVariables(config[key], absoluteBaseUrlPath);
}
else
{
if (typeof config[key] === 'string')
{
config[key] = replaceAllMatches(environmentVariableRegex, config[key]);
if (path.isAbsolute(path.dirname(config[key])))
{
config[key] = getRelativePathTo(absoluteBaseUrlPath, config[key]);
}
}
}
}
}
function getRelativePathTo(baseDir, targetPath)
{
const relativePath = path.relative(baseDir, path.isAbsolute(path.dirname(targetPath)) ? targetPath :
path.dirname(targetPath));
if (path.dirname(relativePath) === '.')
{
return './' + relativePath;
}
return relativePath;
}
function replaceAllMatches(regex, text)
{
return text.replace(regex, function (substring, variableName)
{
const environmentVariableValue = process.env[variableName];
if (!environmentVariableValue)
{
throw new Error(`environment variable '${variableName}' has no value.`);
}
return process.env[variableName];
});
}
function replaceConfigKeyIfExists(config, keyPathToReplace, targetPath)
{
if(!_.has(config, keyPathToReplace))
{
return;
}
const valueToReplace = _.get(config, keyPathToReplace);
const pathFromEnvVar = replaceAllMatches(environmentVariableRegex, valueToReplace);
_.set(config, keyPathToReplace, path.isAbsolute(pathFromEnvVar) ? getRelativePathTo(targetPath, pathFromEnvVar) : pathFromEnvVar);
}