Skip to content

Commit

Permalink
fix: Allow Whitespace in CLI Variable Values (#105)
Browse files Browse the repository at this point in the history
* chore: lint fixes

* fix: allow whitespace in cli variable values
  • Loading branch information
Swivelgames authored Mar 7, 2024
1 parent 052cb84 commit b4314ea
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env node

var spawn = require('cross-spawn')
var path = require('path')
const spawn = require('cross-spawn')
const path = require('path')

var argv = require('minimist')(process.argv.slice(2))
var dotenv = require('dotenv')
var dotenvExpand = require('dotenv-expand').expand
const argv = require('minimist')(process.argv.slice(2))
const dotenv = require('dotenv')
const dotenvExpand = require('dotenv-expand').expand

function printHelp () {
console.log([
Expand All @@ -29,14 +29,14 @@ if (argv.help) {
process.exit()
}

const override = argv.o || argv.override;
const override = argv.o || argv.override

if (argv.c && override) {
console.error('Invalid arguments. Cascading env variables conflicts with overrides.')
process.exit(1)
}

var paths = []
let paths = []
if (argv.e) {
if (typeof argv.e === 'string') {
paths.push(argv.e)
Expand All @@ -56,22 +56,23 @@ if (argv.c) {
}

function validateCmdVariable (param) {
if (!param.match(/^\w+=[a-zA-Z0-9"=^!?%@_&\-/:;.]+$/)) {
console.error('Unexpected argument ' + param + '. Expected variable in format variable=value')
const [, key, val] = param.match(/^(\w+)=([\s\S]+)$/m) || []
if (!key || !val) {
console.error(`Invalid variable name. Expected variable in format '-v variable=value', but got: \`-v ${param}\`.`)
process.exit(1)
}

return param
return [key, val]
}
var variables = []
const variables = []
if (argv.v) {
if (typeof argv.v === 'string') {
variables.push(validateCmdVariable(argv.v))
} else {
variables.push(...argv.v.map(validateCmdVariable))
}
}
var parsedVariables = dotenv.parse(Buffer.from(variables.join('\n')))
const parsedVariables = Object.fromEntries(variables)

if (argv.debug) {
console.log(paths)
Expand All @@ -80,20 +81,23 @@ if (argv.debug) {
}

paths.forEach(function (env) {
var parsedFile = dotenv.config({ path: path.resolve(env), override })
const parsedFile = dotenv.config({ path: path.resolve(env), override })
if (argv.expand !== false) {
dotenvExpand(parsedFile)
}
})
Object.assign(process.env, parsedVariables)

if (argv.p) {
var value = process.env[argv.p]
const value = process.env[argv.p]
if (typeof value === 'string') {
value = `\`${value}\``
}
console.log(value != null ? value : '')
process.exit()
}

var command = argv._[0]
const command = argv._[0]
if (!command) {
printHelp()
process.exit(1)
Expand Down

0 comments on commit b4314ea

Please sign in to comment.