@acryps/environment
Advanced tools
Comparing version 2.5.2 to 2.6.0
@@ -9,2 +9,3 @@ "use strict"; | ||
const which_1 = require("which"); | ||
const chain_1 = require("./chain"); | ||
const childProgram = process.argv.slice(2); | ||
@@ -86,2 +87,3 @@ if (!childProgram.length) { | ||
const activeSettings = projectSettings.settings[projectSettings.active]; | ||
const chain = new chain_1.ConfigurationChain(activeSettings); | ||
const packageConfigurationLocation = (0, path_1.join)(projectLocation, 'package.json'); | ||
@@ -104,6 +106,2 @@ if (!(0, fs_1.existsSync)(packageConfigurationLocation)) { | ||
} | ||
function convertToEnvironmentVariableName(...path) { | ||
return path.join('_').replace(/[A-Z]/g, match => `_${match}`).toUpperCase(); | ||
} | ||
const environment = {}; | ||
const inputInterface = (0, promises_1.createInterface)({ | ||
@@ -114,112 +112,107 @@ input: process.stdin, | ||
let saveRequired = false; | ||
async function checkConfiguration(configuration, prefix) { | ||
for (let key in configuration) { | ||
switch (typeof configuration[key]) { | ||
case 'object': { | ||
if (!configuration[key]) { | ||
throw new Error(`${convertToEnvironmentVariableName(...prefix, key)} can't be null`); | ||
// catch cli commands which do not require the environment but not all variables set | ||
switch (childProgram[0]) { | ||
case '--edit': { | ||
chain.walk(environmentConfiguration, async (node) => { | ||
while (true) { | ||
let response = await inputInterface.question(`${node.name} (${node.environmentName})${node.currentValue ? ` [${node.currentValue}]` : (node.defaultValue ? ` [${node.defaultValue}]` : '')}: `); | ||
response = response.trim(); | ||
if (node.currentValue && !response) { | ||
return node.currentValue; | ||
} | ||
await checkConfiguration(configuration[key], [...prefix, key]); | ||
break; | ||
if (node.defaultValue && !response) { | ||
response = node.defaultValue; | ||
} | ||
if (node.isNumber) { | ||
if (isNaN(+response)) { | ||
console.error(`${node.environmentName} must be a number`); | ||
continue; | ||
} | ||
response = `${+response}`; | ||
} | ||
saveRequired = true; | ||
return response; | ||
} | ||
case 'string': { | ||
let savedHead = activeSettings; | ||
for (let part of prefix) { | ||
if (!savedHead[part] || typeof savedHead[part] == 'string') { | ||
savedHead[part] = {}; | ||
}).then(() => { | ||
save(); | ||
process.exit(0); | ||
}); | ||
break; | ||
} | ||
default: { | ||
chain.walk(environmentConfiguration, async (node) => { | ||
while (true) { | ||
let response = await inputInterface.question(`${node.name} (${node.environmentName})${node.defaultValue ? ` [${node.defaultValue}]` : ''}: `); | ||
response = response.trim(); | ||
if (node.defaultValue && !response) { | ||
response = node.defaultValue; | ||
} | ||
if (node.isNumber) { | ||
if (isNaN(+response)) { | ||
console.error(`${node.environmentName} must be a number`); | ||
continue; | ||
} | ||
savedHead = savedHead[part]; | ||
response = `${+response}`; | ||
} | ||
let name = key; | ||
let isNumber = false; | ||
let defaultValue; | ||
if (name.includes('+')) { | ||
isNumber = true; | ||
name = name.replace('+', ''); | ||
saveRequired = true; | ||
return response; | ||
} | ||
}).then(async () => { | ||
if (saveRequired) { | ||
save(); | ||
} | ||
// catch cli commands that require variables set | ||
switch (childProgram[0]) { | ||
case '--export': { | ||
process.stdout.write(`environment --import ${Buffer.from(JSON.stringify(activeSettings), 'utf-8').toString('base64')}\n`); | ||
return process.exit(0); | ||
} | ||
if (name.includes('?')) { | ||
defaultValue = name.split('?')[1]; | ||
name = name.split('?')[0]; | ||
case '--export-json': { | ||
process.stdout.write(`${JSON.stringify(chain.environment)}\n`); | ||
return process.exit(0); | ||
} | ||
while (!(name in savedHead) || typeof savedHead[name] != 'string') { | ||
let response = await inputInterface.question(`${configuration[key]} (${convertToEnvironmentVariableName(...prefix, name)})${defaultValue ? ` [${defaultValue}]` : ''}: `); | ||
response = response.trim(); | ||
if (defaultValue && !response) { | ||
response = defaultValue; | ||
case '--export-cluster': { | ||
const applicationFilter = await inputInterface.question('Cluster Application Name: '); | ||
const environmentFilter = await inputInterface.question('Cluster Environment: '); | ||
for (let name in chain.environment) { | ||
process.stdout.write(`vlc2 var set -a ${applicationFilter} -e ${environmentFilter} -n ${JSON.stringify(name)} -v ${JSON.stringify(chain.environment[name])}\n`); | ||
} | ||
if (isNumber) { | ||
if (isNaN(+response)) { | ||
console.error(`${convertToEnvironmentVariableName(...prefix, name)} must be a number`); | ||
continue; | ||
} | ||
response = `${+response}`; | ||
return process.exit(0); | ||
} | ||
case '--export-kubernetes': { | ||
for (let name in chain.environment) { | ||
process.stdout.write(`- name: ${JSON.stringify(name)}\n value: ${JSON.stringify(chain.environment[name])}\n`); | ||
} | ||
savedHead[name] = response; | ||
return process.exit(0); | ||
} | ||
environment[convertToEnvironmentVariableName(...prefix, name)] = savedHead[name]; | ||
saveRequired = true; | ||
break; | ||
case '--export-shell': { | ||
for (let name in chain.environment) { | ||
process.stdout.write(`${name}=${JSON.stringify(chain.environment[name])}\n`); | ||
} | ||
return process.exit(0); | ||
} | ||
case '--export-dotenv': { | ||
for (let name in chain.environment) { | ||
process.stdout.write(`export ${name}=${JSON.stringify(chain.environment[name])}\n`); | ||
} | ||
return process.exit(0); | ||
} | ||
default: { | ||
const programLocation = (0, which_1.sync)(childProgram[0]); | ||
// do not inject any variables from our state | ||
// passing our own properties like 'active setting' would allow developers to add checks to them instead of using the configured environment variables, which is not intended | ||
const childProcess = (0, child_process_1.spawn)(programLocation, childProgram.slice(1), { | ||
stdio: 'inherit', | ||
env: { | ||
...process.env, | ||
...chain.environment | ||
} | ||
}); | ||
childProcess.on('exit', code => { | ||
process.exit(code ?? 0); | ||
}); | ||
} | ||
} | ||
default: { | ||
throw new Error(`${convertToEnvironmentVariableName(...prefix, key)} must be a string or object`); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
checkConfiguration(environmentConfiguration, []).then(async () => { | ||
if (saveRequired) { | ||
save(); | ||
} | ||
// catch cli commands that require variables set | ||
switch (childProgram[0]) { | ||
case '--export': { | ||
process.stdout.write(`environment --import ${Buffer.from(JSON.stringify(activeSettings), 'utf-8').toString('base64')}\n`); | ||
return process.exit(0); | ||
} | ||
case '--export-json': { | ||
process.stdout.write(`${JSON.stringify(environment)}\n`); | ||
return process.exit(0); | ||
} | ||
case '--export-cluster': { | ||
const applicationFilter = await inputInterface.question('Cluster Application Name: '); | ||
const environmentFilter = await inputInterface.question('Cluster Environment: '); | ||
for (let name in environment) { | ||
process.stdout.write(`vlc2 var set -a ${applicationFilter} -e ${environmentFilter} -n ${JSON.stringify(name)} -v ${JSON.stringify(environment[name])}\n`); | ||
} | ||
return process.exit(0); | ||
} | ||
case '--export-kubernetes': { | ||
for (let name in environment) { | ||
process.stdout.write(`- name: ${JSON.stringify(name)}\n value: ${JSON.stringify(environment[name])}\n`); | ||
} | ||
return process.exit(0); | ||
} | ||
case '--export-shell': { | ||
for (let name in environment) { | ||
process.stdout.write(`${name}=${JSON.stringify(environment[name])}\n`); | ||
} | ||
return process.exit(0); | ||
} | ||
case '--export-dotenv': { | ||
for (let name in environment) { | ||
process.stdout.write(`export ${name}=${JSON.stringify(environment[name])}\n`); | ||
} | ||
return process.exit(0); | ||
} | ||
default: { | ||
const programLocation = (0, which_1.sync)(childProgram[0]); | ||
// do not inject any variables from our state | ||
// passing our own properties like 'active setting' would allow developers to add checks to them instead of using the configured environment variables, which is not intended | ||
const childProcess = (0, child_process_1.spawn)(programLocation, childProgram.slice(1), { | ||
stdio: 'inherit', | ||
env: { | ||
...process.env, | ||
...environment | ||
} | ||
}); | ||
childProcess.on('exit', code => { | ||
process.exit(code ?? 0); | ||
}); | ||
} | ||
} | ||
}); |
{ | ||
"name": "@acryps/environment", | ||
"version": "2.5.2", | ||
"version": "2.6.0", | ||
"license": "GPL-3.0-only", | ||
@@ -5,0 +5,0 @@ "bin": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16471
15
328