11cb0ef41Sopenharmony_ciconst fs = require('fs')
21cb0ef41Sopenharmony_ciconst { promisify } = require('util')
31cb0ef41Sopenharmony_ciconst { readFileSync } = fs
41cb0ef41Sopenharmony_ciconst readFile = promisify(fs.readFile)
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst extractPath = (path, cmdshimContents) => {
71cb0ef41Sopenharmony_ci  if (/[.]cmd$/.test(path)) {
81cb0ef41Sopenharmony_ci    return extractPathFromCmd(cmdshimContents)
91cb0ef41Sopenharmony_ci  } else if (/[.]ps1$/.test(path)) {
101cb0ef41Sopenharmony_ci    return extractPathFromPowershell(cmdshimContents)
111cb0ef41Sopenharmony_ci  } else {
121cb0ef41Sopenharmony_ci    return extractPathFromCygwin(cmdshimContents)
131cb0ef41Sopenharmony_ci  }
141cb0ef41Sopenharmony_ci}
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst extractPathFromPowershell = cmdshimContents => {
171cb0ef41Sopenharmony_ci  const matches = cmdshimContents.match(/"[$]basedir[/]([^"]+?)"\s+[$]args/)
181cb0ef41Sopenharmony_ci  return matches && matches[1]
191cb0ef41Sopenharmony_ci}
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciconst extractPathFromCmd = cmdshimContents => {
221cb0ef41Sopenharmony_ci  const matches = cmdshimContents.match(/"%(?:~dp0|dp0%)\\([^"]+?)"\s+%[*]/)
231cb0ef41Sopenharmony_ci  return matches && matches[1]
241cb0ef41Sopenharmony_ci}
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciconst extractPathFromCygwin = cmdshimContents => {
271cb0ef41Sopenharmony_ci  const matches = cmdshimContents.match(/"[$]basedir[/]([^"]+?)"\s+"[$]@"/)
281cb0ef41Sopenharmony_ci  return matches && matches[1]
291cb0ef41Sopenharmony_ci}
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciconst wrapError = (thrown, newError) => {
321cb0ef41Sopenharmony_ci  newError.message = thrown.message
331cb0ef41Sopenharmony_ci  newError.code = thrown.code
341cb0ef41Sopenharmony_ci  newError.path = thrown.path
351cb0ef41Sopenharmony_ci  return newError
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciconst notaShim = (path, er) => {
391cb0ef41Sopenharmony_ci  if (!er) {
401cb0ef41Sopenharmony_ci    er = new Error()
411cb0ef41Sopenharmony_ci    Error.captureStackTrace(er, notaShim)
421cb0ef41Sopenharmony_ci  }
431cb0ef41Sopenharmony_ci  er.code = 'ENOTASHIM'
441cb0ef41Sopenharmony_ci  er.message = `Can't read shim path from '${path}', ` +
451cb0ef41Sopenharmony_ci    `it doesn't appear to be a cmd-shim`
461cb0ef41Sopenharmony_ci  return er
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ciconst readCmdShim = path => {
501cb0ef41Sopenharmony_ci  // create a new error to capture the stack trace from this point,
511cb0ef41Sopenharmony_ci  // instead of getting some opaque stack into node's internals
521cb0ef41Sopenharmony_ci  const er = new Error()
531cb0ef41Sopenharmony_ci  Error.captureStackTrace(er, readCmdShim)
541cb0ef41Sopenharmony_ci  return readFile(path).then(contents => {
551cb0ef41Sopenharmony_ci    const destination = extractPath(path, contents.toString())
561cb0ef41Sopenharmony_ci    if (destination) {
571cb0ef41Sopenharmony_ci      return destination
581cb0ef41Sopenharmony_ci    }
591cb0ef41Sopenharmony_ci    throw notaShim(path, er)
601cb0ef41Sopenharmony_ci  }, readFileEr => {
611cb0ef41Sopenharmony_ci    throw wrapError(readFileEr, er)
621cb0ef41Sopenharmony_ci  })
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ciconst readCmdShimSync = path => {
661cb0ef41Sopenharmony_ci  const contents = readFileSync(path)
671cb0ef41Sopenharmony_ci  const destination = extractPath(path, contents.toString())
681cb0ef41Sopenharmony_ci  if (!destination) {
691cb0ef41Sopenharmony_ci    throw notaShim(path)
701cb0ef41Sopenharmony_ci  }
711cb0ef41Sopenharmony_ci  return destination
721cb0ef41Sopenharmony_ci}
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_cireadCmdShim.sync = readCmdShimSync
751cb0ef41Sopenharmony_cimodule.exports = readCmdShim
76