|
2 | 2 | // either rejects or resolves to nothing. return value not relevant. |
3 | 3 | const isWindows = require('./is-windows.js') |
4 | 4 | const binTarget = require('./bin-target.js') |
5 | | -const {resolve, dirname} = require('path') |
| 5 | +const { resolve, dirname } = require('path') |
6 | 6 | const readCmdShim = require('read-cmd-shim') |
7 | 7 | const fs = require('fs') |
8 | | -const {promisify} = require('util') |
| 8 | +const { promisify } = require('util') |
9 | 9 | const readlink = promisify(fs.readlink) |
10 | 10 |
|
11 | | -const checkBin = async ({bin, path, top, global, force}) => { |
| 11 | +const checkBin = async ({ bin, path, top, global, force }) => { |
12 | 12 | // always ok to clobber when forced |
13 | 13 | // always ok to clobber local bins, or when forced |
14 | | - if (force || !global || !top) |
| 14 | + if (force || !global || !top) { |
15 | 15 | return |
| 16 | + } |
16 | 17 |
|
17 | 18 | // ok, need to make sure, then |
18 | | - const target = resolve(binTarget({path, top}), bin) |
| 19 | + const target = resolve(binTarget({ path, top }), bin) |
19 | 20 | path = resolve(path) |
20 | | - return isWindows ? checkShim({target, path}) : checkLink({target, path}) |
| 21 | + return isWindows ? checkShim({ target, path }) : checkLink({ target, path }) |
21 | 22 | } |
22 | 23 |
|
23 | 24 | // only enoent is allowed. anything else is a problem. |
24 | | -const handleReadLinkError = async ({er, target}) => |
| 25 | +const handleReadLinkError = async ({ er, target }) => |
25 | 26 | er.code === 'ENOENT' ? null |
26 | | - : failEEXIST({target}) |
| 27 | + : failEEXIST({ target }) |
27 | 28 |
|
28 | | -const checkLink = async ({target, path}) => { |
| 29 | +const checkLink = async ({ target, path }) => { |
29 | 30 | const current = await readlink(target) |
30 | | - .catch(er => handleReadLinkError({er, target})) |
| 31 | + .catch(er => handleReadLinkError({ er, target })) |
31 | 32 |
|
32 | | - if (!current) |
| 33 | + if (!current) { |
33 | 34 | return |
| 35 | + } |
34 | 36 |
|
35 | 37 | const resolved = resolve(dirname(target), current) |
36 | 38 |
|
37 | | - if (resolved.toLowerCase().indexOf(path.toLowerCase()) !== 0) |
38 | | - return failEEXIST({target}) |
| 39 | + if (resolved.toLowerCase().indexOf(path.toLowerCase()) !== 0) { |
| 40 | + return failEEXIST({ target }) |
| 41 | + } |
39 | 42 | } |
40 | 43 |
|
41 | | -const handleReadCmdShimError = ({er, target}) => |
| 44 | +const handleReadCmdShimError = ({ er, target }) => |
42 | 45 | er.code === 'ENOENT' ? null |
43 | | - : failEEXIST({target}) |
| 46 | + : failEEXIST({ target }) |
44 | 47 |
|
45 | | -const failEEXIST = ({target}) => |
| 48 | +const failEEXIST = ({ target }) => |
46 | 49 | Promise.reject(Object.assign(new Error('EEXIST: file already exists'), { |
47 | 50 | path: target, |
48 | 51 | code: 'EEXIST', |
49 | 52 | })) |
50 | 53 |
|
51 | | -const checkShim = async ({target, path}) => { |
| 54 | +const checkShim = async ({ target, path }) => { |
52 | 55 | const shims = [ |
53 | 56 | target, |
54 | 57 | target + '.cmd', |
55 | 58 | target + '.ps1', |
56 | 59 | ] |
57 | 60 | await Promise.all(shims.map(async target => { |
58 | 61 | const current = await readCmdShim(target) |
59 | | - .catch(er => handleReadCmdShimError({er, target})) |
| 62 | + .catch(er => handleReadCmdShimError({ er, target })) |
60 | 63 |
|
61 | | - if (!current) |
| 64 | + if (!current) { |
62 | 65 | return |
| 66 | + } |
63 | 67 |
|
64 | 68 | const resolved = resolve(dirname(target), current.replace(/\\/g, '/')) |
65 | 69 |
|
66 | | - if (resolved.toLowerCase().indexOf(path.toLowerCase()) !== 0) |
67 | | - return failEEXIST({target}) |
| 70 | + if (resolved.toLowerCase().indexOf(path.toLowerCase()) !== 0) { |
| 71 | + return failEEXIST({ target }) |
| 72 | + } |
68 | 73 | })) |
69 | 74 | } |
70 | 75 |
|
|
0 commit comments