"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/read-cmd-shim/index.js" (7 Feb 2017, 1505 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 'use strict'
    2 var fs = require('graceful-fs')
    3 
    4 function extractPath (path, cmdshimContents) {
    5   if (/[.]cmd$/.test(path)) {
    6     return extractPathFromCmd(cmdshimContents)
    7   } else {
    8     return extractPathFromCygwin(cmdshimContents)
    9   }
   10 }
   11 
   12 function extractPathFromCmd (cmdshimContents) {
   13   var matches = cmdshimContents.match(/"%~dp0\\([^"]+?)"\s+%[*]/)
   14   return matches && matches[1]
   15 }
   16 
   17 function extractPathFromCygwin (cmdshimContents) {
   18   var matches = cmdshimContents.match(/"[$]basedir[/]([^"]+?)"\s+"[$]@"/)
   19   return matches && matches[1]
   20 }
   21 
   22 function wrapError (thrown, newError) {
   23   newError.message = thrown.message
   24   newError.code = thrown.code
   25   return newError
   26 }
   27 
   28 function notaShim (path, er) {
   29   if (!er) {
   30     er = new Error()
   31     Error.captureStackTrace(er, notaShim)
   32   }
   33   er.code = 'ENOTASHIM'
   34   er.message = "Can't read shim path from '" + path + "', it doesn't appear to be a cmd-shim"
   35   return er
   36 }
   37 
   38 var readCmdShim = module.exports = function (path, cb) {
   39   var er = new Error()
   40   Error.captureStackTrace(er, readCmdShim)
   41   fs.readFile(path, function (readFileEr, contents) {
   42     if (readFileEr) return cb(wrapError(readFileEr, er))
   43     var destination = extractPath(path, contents.toString())
   44     if (destination) return cb(null, destination)
   45     return cb(notaShim(path, er))
   46   })
   47 }
   48 
   49 module.exports.sync = function (path) {
   50   var contents = fs.readFileSync(path)
   51   var destination = extractPath(path, contents.toString())
   52   if (!destination) throw notaShim(path)
   53   return destination
   54 }