| 171 | } |
| 172 | |
| 173 | const dumpScript = async (p) => { |
| 174 | const d = (await fs.readFile(p, 'utf8')).replace(/^#!.*?\n/, '') |
| 175 | await new Promise((res, rej) => { |
| 176 | let done = false |
| 177 | process.stdout.on('error', er => { |
| 178 | if (done) { |
| 179 | return |
| 180 | } |
| 181 | |
| 182 | done = true |
| 183 | |
| 184 | // Darwin is a pain sometimes. |
| 185 | // |
| 186 | // This is necessary because the "source" or "." program in bash on OS X closes its file argument before reading from it, meaning that you get exactly 1 write, which will work most of the time, and will always raise an EPIPE. |
| 187 | // |
| 188 | // Really, one should not be tossing away EPIPE errors, or any errors, so casually. |
| 189 | // But, without this, `. <(npm completion)` can never ever work on OS X. |
| 190 | // TODO Ignoring coverage, see 'non EPIPE errors cause failures' test. |
| 191 | /* istanbul ignore next */ |
| 192 | if (er.errno === 'EPIPE') { |
| 193 | res() |
| 194 | } else { |
| 195 | rej(er) |
| 196 | } |
| 197 | }) |
| 198 | |
| 199 | process.stdout.write(d, () => { |
| 200 | if (done) { |
| 201 | return |
| 202 | } |
| 203 | |
| 204 | done = true |
| 205 | res() |
| 206 | }) |
| 207 | }) |
| 208 | } |
| 209 | |
| 210 | const unescape = w => w.charAt(0) === '\'' ? w.replace(/^'|'$/g, '') |
| 211 | : w.replace(/\\ /g, ' ') |