/
link-bins.js
216 lines (198 loc) · 6.09 KB
/
link-bins.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const t = require('tap')
const BB = require('bluebird')
const binLinks = BB.promisify(require('../'))
// forking between cmd-shims and symlinks is already handled by
// the gentle-fs.binLink module. just test the unix handling here.
const _FAKE_PLATFORM_ = process.platform === 'win32' ? 'unix' : null
const fs = require('fs')
const mkdirp = require('mkdirp').sync
const rimraf = require('rimraf').sync
const {basename, resolve} = require('path')
const me = resolve(__dirname, basename(__filename, '.js'))
rimraf(me)
mkdirp(me)
const globalDir = resolve(me, 'node_modules')
t.teardown(() => rimraf(me))
const log = {
clearProgress () {},
info () {},
showProgress () {},
silly () {},
verbose () {}
}
// create some stuff that's already in the bin/man folders
const globalBin = resolve(me, 'bin')
mkdirp(globalBin)
fs.writeFileSync(globalBin + '/foo', 'pre-existing foo bin')
mkdirp(me + '/node_modules/bar/bin')
fs.writeFileSync(me + '/node_modules/bar/bin/bar.js', 'original bar')
fs.symlinkSync(me + '/node_modules/bar/bin/bar.js', me + '/bin/bar')
const prefixes = [
me,
globalBin,
globalDir,
resolve(me, 'share/man/man1')
]
mkdirp(me + '/share/man/man1')
fs.writeFileSync(me + '/share/man/man1/foo.1', 'pre-existing foo man')
mkdirp(me + '/node_modules/bar/man')
fs.writeFileSync(me + '/node_modules/bar/man/bar.1', 'original bar man')
fs.symlinkSync(me + '/node_modules/bar/man/bar.1', me + '/share/man/man1/bar.1')
t.test('foo package cannot link, pre-existing stuff there', t => {
const foo = resolve(me, 'node_modules/foo')
mkdirp(foo)
const pkg = {
name: 'foo',
version: '1.2.3',
bin: 'foo.js',
man: ['foo.1', { not: 'a manpage string' }]
}
fs.writeFileSync(foo + '/package.json', JSON.stringify(pkg))
fs.writeFileSync(foo + '/foo.1', 'how to foo\n')
fs.writeFileSync(foo + '/foo.js', '#!/usr/bin/env node\nconsole.log("foo")')
// might get it on the bin or the man, but one of these will EEXIST
return t.rejects(binLinks(pkg, foo, true, {
prefix: me,
global: true,
globalBin,
globalDir,
log,
pkgId: `${pkg.name}@${pkg.version}`,
name: pkg.name,
_FAKE_PLATFORM_,
prefixes: prefixes.concat(foo)
}), { code: 'EEXIST' }).then(() => {
// ensure we still have our preexisting bits
t.equal(fs.readFileSync(me + '/bin/foo', 'utf8'), 'pre-existing foo bin')
t.equal(fs.readFileSync(me + '/share/man/man1/foo.1', 'utf8'), 'pre-existing foo man')
})
})
t.test('foo package can link with --force link', t => {
const foo = resolve(me, 'node_modules/foo')
mkdirp(foo)
const pkg = {
name: 'foo',
version: '1.2.3',
bin: 'foo.js',
man: ['foo.1']
}
fs.writeFileSync(foo + '/package.json', JSON.stringify(pkg))
fs.writeFileSync(foo + '/foo.1', 'how to foo\n')
fs.writeFileSync(foo + '/foo.js', '#!/usr/bin/env node\nconsole.log("foo")')
return binLinks(pkg, foo, true, {
prefix: me,
global: true,
globalBin,
globalDir,
log,
pkgId: `${pkg.name}@${pkg.version}`,
name: pkg.name,
_FAKE_PLATFORM_,
force: true,
prefixes: prefixes.concat(foo)
}).then(() => {
// ensure we got our links made
t.equal(fs.readFileSync(me + '/bin/foo', 'utf8'), '#!/usr/bin/env node\nconsole.log("foo")')
if (process.platform !== 'win32') {
t.equal(fs.readFileSync(me + '/share/man/man1/foo.1', 'utf8'), 'how to foo\n')
}
})
})
t.test('bar package can update, links are ours', t => {
const bar = resolve(me, 'node_modules/bar')
mkdirp(bar)
const pkg = {
name: 'bar',
version: '1.2.3',
bin: 'bar.js',
man: ['bar.1']
}
fs.writeFileSync(bar + '/package.json', JSON.stringify(pkg))
fs.writeFileSync(bar + '/bar.1', 'how to bar\n')
fs.writeFileSync(bar + '/bar.js', '#!/usr/bin/env node\nconsole.log("bar")')
return binLinks(pkg, bar, true, {
prefix: me,
parseable: true,
global: true,
globalBin,
globalDir,
log,
pkgId: `${pkg.name}@${pkg.version}`,
name: pkg.name,
_FAKE_PLATFORM_,
prefixes: prefixes.concat(bar, bar + '/man')
}).then(() => {
// ensure we got our links made
t.equal(fs.readFileSync(me + '/bin/bar', 'utf8'), '#!/usr/bin/env node\nconsole.log("bar")')
if (process.platform !== 'win32') {
t.equal(fs.readFileSync(me + '/share/man/man1/bar.1', 'utf8'), 'how to bar\n')
}
})
})
t.test('cannot overwrite with another package with the same bin', t => {
const baz = resolve(me, 'node_modules/@scope/baz')
mkdirp(baz)
const pkg = {
name: '@scope/baz',
version: '1.2.3',
bin: { bar: 'baz.js' }
}
fs.writeFileSync(baz + '/package.json', JSON.stringify(pkg))
fs.writeFileSync(baz + '/baz.js', '#!/usr/bin/env node\nconsole.log("baz")')
return t.rejects(binLinks(pkg, baz, true, {
global: true,
prefix: me,
globalBin,
globalDir,
log,
pkgId: `${pkg.name}@${pkg.version}`,
_FAKE_PLATFORM_,
name: pkg.name,
prefixes: prefixes.concat(baz)
}), { code: 'EEXIST' }).then(() => {
// ensure bar is still intact
t.equal(fs.readFileSync(me + '/bin/bar', 'utf8'), '#!/usr/bin/env node\nconsole.log("bar")')
})
})
t.test('nothing to link', t => {
const qux = resolve(me, 'node_modules/qux')
mkdirp(qux)
const pkg = {
name: 'qux',
version: '1.2.3'
}
fs.writeFileSync(qux + '/package.json', JSON.stringify(pkg))
return binLinks(pkg, qux, true, {
global: true,
prefix: me,
globalBin,
globalDir,
log,
pkgId: `${pkg.name}@${pkg.version}`,
_FAKE_PLATFORM_,
name: pkg.name,
prefixes: prefixes.concat(qux)
})
})
t.test('invalid man page name', t => {
const badman = resolve(me, 'node_modules/badman')
mkdirp(badman)
const pkg = {
name: 'badman',
version: '1.2.3',
man: ['manpage']
}
fs.writeFileSync(badman + '/package.json', JSON.stringify(pkg))
fs.writeFileSync(badman + '/manpage', JSON.stringify(pkg))
return t.rejects(binLinks(pkg, badman, true, {
global: true,
prefix: me,
globalBin,
globalDir,
log,
pkgId: `${pkg.name}@${pkg.version}`,
_FAKE_PLATFORM_,
name: pkg.name,
prefixes: prefixes.concat(badman)
}), { message: 'manpage is not a valid name for a man file.' })
})