Sha256: e0163471fa7296bab7f97b4633ce9b62d757b0e6742e3ad1e1a036b1a289083c

Contents?: true

Size: 1.42 KB

Versions: 21

Compression:

Stored size: 1.42 KB

Contents

const path = require('path')
const isWindows = require('./is-windows')

module.exports = commandConvert

/**
 * Converts an environment variable usage to be appropriate for the current OS
 * @param {String} command Command to convert
 * @param {Object} env Map of the current environment variable names and their values
 * @param {boolean} normalize If the command should be normalized using `path`
 * after converting
 * @returns {String} Converted command
 */
function commandConvert(command, env, normalize = false) {
  if (!isWindows()) {
    return command
  }
  const envUnixRegex = /\$(\w+)|\${(\w+)}/g // $my_var or ${my_var}
  const convertedCmd = command.replace(envUnixRegex, (match, $1, $2) => {
    const varName = $1 || $2
    // In Windows, non-existent variables are not replaced by the shell,
    // so for example "echo %FOO%" will literally print the string "%FOO%", as
    // opposed to printing an empty string in UNIX. See kentcdodds/cross-env#145
    // If the env variable isn't defined at runtime, just strip it from the command entirely
    return env[varName] ? `%${varName}%` : ''
  })
  // Normalization is required for commands with relative paths
  // For example, `./cmd.bat`. See kentcdodds/cross-env#127
  // However, it should not be done for command arguments.
  // See https://github.com/kentcdodds/cross-env/pull/130#issuecomment-319887970
  return normalize === true ? path.normalize(convertedCmd) : convertedCmd
}

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
appmap-0.72.2 ./node_modules/cross-env/src/command.js
appmap-0.72.1 ./node_modules/cross-env/src/command.js
appmap-0.72.0 ./node_modules/cross-env/src/command.js
appmap-0.71.0 ./node_modules/cross-env/src/command.js
appmap-0.70.2 ./node_modules/cross-env/src/command.js
appmap-0.70.1 ./node_modules/cross-env/src/command.js
appmap-0.70.0 ./node_modules/cross-env/src/command.js
appmap-0.69.0 ./node_modules/cross-env/src/command.js
appmap-0.68.2 ./node_modules/cross-env/src/command.js
appmap-0.68.1 ./node_modules/cross-env/src/command.js
appmap-0.68.0 ./node_modules/cross-env/src/command.js
appmap-0.67.1 ./node_modules/cross-env/src/command.js
appmap-0.67.0 ./node_modules/cross-env/src/command.js
appmap-0.66.2 ./node_modules/cross-env/src/command.js
appmap-0.66.1 ./node_modules/cross-env/src/command.js
appmap-0.66.0 ./node_modules/cross-env/src/command.js
appmap-0.65.1 ./node_modules/cross-env/src/command.js
appmap-0.65.0 ./node_modules/cross-env/src/command.js
appmap-0.64.0 ./node_modules/cross-env/src/command.js
appmap-0.63.0 ./node_modules/cross-env/src/command.js