]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/cmd-shim/index.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / cmd-shim / index.js
1 // On windows, create a .cmd file.
2 // Read the #! in the file to see what it uses.  The vast majority
3 // of the time, this will be either:
4 // "#!/usr/bin/env <prog> <args...>"
5 // or:
6 // "#!<prog> <args...>"
7 //
8 // Write a binroot/pkg.bin + ".cmd" file that has this line in it:
9 // @<prog> <args...> %~dp0<target> %*
10
11 module.exports = cmdShim
12 cmdShim.ifExists = cmdShimIfExists
13
14 var fs = require("graceful-fs")
15
16 var mkdir = require("mkdirp")
17   , path = require("path")
18   , shebangExpr = /^#\!\s*(?:\/usr\/bin\/env)?\s*([^ \t]+)(.*)$/
19
20 function cmdShimIfExists (from, to, cb) {
21   fs.stat(from, function (er) {
22     if (er) return cb()
23     cmdShim(from, to, cb)
24   })
25 }
26
27 // Try to unlink, but ignore errors.
28 // Any problems will surface later.
29 function rm (path, cb) {
30   fs.unlink(path, function(er) {
31     cb()
32   })
33 }
34
35 function cmdShim (from, to, cb) {
36   fs.stat(from, function (er, stat) {
37     if (er)
38       return cb(er)
39
40     cmdShim_(from, to, cb)
41   })
42 }
43
44 function cmdShim_ (from, to, cb) {
45   var then = times(2, next, cb)
46   rm(to, then)
47   rm(to + ".cmd", then)
48
49   function next(er) {
50     writeShim(from, to, cb)
51   }
52 }
53
54 function writeShim (from, to, cb) {
55   // make a cmd file and a sh script
56   // First, check if the bin is a #! of some sort.
57   // If not, then assume it's something that'll be compiled, or some other
58   // sort of script, and just call it directly.
59   mkdir(path.dirname(to), function (er) {
60     if (er)
61       return cb(er)
62     fs.readFile(from, "utf8", function (er, data) {
63       if (er) return writeShim_(from, to, null, null, cb)
64       var firstLine = data.trim().split(/\r*\n/)[0]
65         , shebang = firstLine.match(shebangExpr)
66       if (!shebang) return writeShim_(from, to, null, null, cb)
67       var prog = shebang[1]
68         , args = shebang[2] || ""
69       return writeShim_(from, to, prog, args, cb)
70     })
71   })
72 }
73
74 function writeShim_ (from, to, prog, args, cb) {
75   var shTarget = path.relative(path.dirname(to), from)
76     , target = shTarget.split("/").join("\\")
77     , longProg
78     , shProg = prog && prog.split("\\").join("/")
79     , shLongProg
80   shTarget = shTarget.split("\\").join("/")
81   args = args || ""
82   if (!prog) {
83     prog = "\"%~dp0\\" + target + "\""
84     shProg = "\"$basedir/" + shTarget + "\""
85     args = ""
86     target = ""
87     shTarget = ""
88   } else {
89     longProg = "\"%~dp0\\" + prog + ".exe\""
90     shLongProg = "\"$basedir/" + prog + "\""
91     target = "\"%~dp0\\" + target + "\""
92     shTarget = "\"$basedir/" + shTarget + "\""
93   }
94
95   // @IF EXIST "%~dp0\node.exe" (
96   //   "%~dp0\node.exe" "%~dp0\.\node_modules\npm\bin\npm-cli.js" %*
97   // ) ELSE (
98   //   SETLOCAL
99   //   SET PATHEXT=%PATHEXT:;.JS;=;%
100   //   node "%~dp0\.\node_modules\npm\bin\npm-cli.js" %*
101   // )
102   var cmd
103   if (longProg) {
104     cmd = "@IF EXIST " + longProg + " (\r\n"
105         + "  " + longProg + " " + args + " " + target + " %*\r\n"
106         + ") ELSE (\r\n"
107         + "  @SETLOCAL\r\n"
108         + "  @SET PATHEXT=%PATHEXT:;.JS;=;%\r\n"
109         + "  " + prog + " " + args + " " + target + " %*\r\n"
110         + ")"
111   } else {
112     cmd = "@" + prog + " " + args + " " + target + " %*\r\n"
113   }
114
115   // #!/bin/sh
116   // basedir=`dirname "$0"`
117   //
118   // case `uname` in
119   //     *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
120   // esac
121   //
122   // if [ -x "$basedir/node.exe" ]; then
123   //   "$basedir/node.exe" "$basedir/node_modules/npm/bin/npm-cli.js" "$@"
124   //   ret=$?
125   // else
126   //   node "$basedir/node_modules/npm/bin/npm-cli.js" "$@"
127   //   ret=$?
128   // fi
129   // exit $ret
130
131   var sh = "#!/bin/sh\n"
132
133   if (shLongProg) {
134     sh = sh
135         + "basedir=$(dirname \"$(echo \"$0\" | sed -e 's,\\\\,/,g')\")\n"
136         + "\n"
137         + "case `uname` in\n"
138         + "    *CYGWIN*) basedir=`cygpath -w \"$basedir\"`;;\n"
139         + "esac\n"
140         + "\n"
141
142     sh = sh
143        + "if [ -x "+shLongProg+" ]; then\n"
144        + "  " + shLongProg + " " + args + " " + shTarget + " \"$@\"\n"
145        + "  ret=$?\n"
146        + "else \n"
147        + "  " + shProg + " " + args + " " + shTarget + " \"$@\"\n"
148        + "  ret=$?\n"
149        + "fi\n"
150        + "exit $ret\n"
151   } else {
152     sh = shProg + " " + args + " " + shTarget + " \"$@\"\n"
153        + "exit $?\n"
154   }
155
156   var then = times(2, next, cb)
157   fs.writeFile(to + ".cmd", cmd, "utf8", then)
158   fs.writeFile(to, sh, "utf8", then)
159   function next () {
160     chmodShim(to, cb)
161   }
162 }
163
164 function chmodShim (to, cb) {
165   var then = times(2, cb, cb)
166   fs.chmod(to, 0755, then)
167   fs.chmod(to + ".cmd", 0755, then)
168 }
169
170 function times(n, ok, cb) {
171   var errState = null
172   return function(er) {
173     if (!errState) {
174       if (er)
175         cb(errState = er)
176       else if (--n === 0)
177         ok()
178     }
179   }
180 }