@@ -2,51 +2,52 @@ const { dirname, relative, join, resolve, basename } = require('path')
2
2
const linkGently = require ( './link-gently.js' )
3
3
const manTarget = require ( './man-target.js' )
4
4
5
- const linkMans = ( { path, pkg, top, force } ) => {
5
+ const linkMans = async ( { path, pkg, top, force } ) => {
6
6
const target = manTarget ( { path, top } )
7
- if ( ! target || ! pkg . man || ! Array . isArray ( pkg . man ) || ! pkg . man . length ) {
8
- return Promise . resolve ( [ ] )
7
+ if ( ! target || ! Array . isArray ( pkg ? .man ) || ! pkg . man . length ) {
8
+ return [ ]
9
9
}
10
10
11
- // break any links to c:\\blah or /foo/blah or ../blah
12
- // and filter out duplicates
13
- const set = [ ...new Set ( pkg . man . map ( man =>
14
- man ? join ( '/' , man ) . replace ( / \\ | : / g, '/' ) . slice ( 1 ) : null )
15
- . filter ( man => typeof man === 'string' ) ) ]
16
-
17
- return Promise . all ( set . map ( man => {
18
- const parseMan = man . match ( / ( .* \. ( [ 0 - 9 ] + ) ( \. g z ) ? ) $ / )
11
+ const links = [ ]
12
+ // `new Set` to filter out duplicates
13
+ for ( let man of new Set ( pkg . man ) ) {
14
+ if ( ! man || typeof man !== 'string' ) {
15
+ continue
16
+ }
17
+ // break any links to c:\\blah or /foo/blah or ../blah
18
+ man = join ( '/' , man ) . replace ( / \\ | : / g, '/' ) . slice ( 1 )
19
+ const parseMan = man . match ( / \. ( [ 0 - 9 ] + ) ( \. g z ) ? $ / )
19
20
if ( ! parseMan ) {
20
- return Promise . reject ( Object . assign ( new Error ( 'invalid man entry name\n' +
21
+ throw Object . assign ( new Error ( 'invalid man entry name\n' +
21
22
'Man files must end with a number, ' +
22
23
'and optionally a .gz suffix if they are compressed.'
23
24
) , {
24
25
code : 'EBADMAN' ,
25
26
path,
26
27
pkgid : pkg . _id ,
27
28
man,
28
- } ) )
29
+ } )
29
30
}
30
31
31
- const stem = parseMan [ 1 ]
32
- const sxn = parseMan [ 2 ]
33
- const base = basename ( stem )
32
+ const section = parseMan [ 1 ]
33
+ const base = basename ( man )
34
34
const absFrom = resolve ( path , man )
35
35
/* istanbul ignore if - that unpossible */
36
36
if ( absFrom . indexOf ( path ) !== 0 ) {
37
- return Promise . reject ( Object . assign ( new Error ( 'invalid man entry' ) , {
37
+ throw Object . assign ( new Error ( 'invalid man entry' ) , {
38
38
code : 'EBADMAN' ,
39
39
path,
40
40
pkgid : pkg . _id ,
41
41
man,
42
- } ) )
42
+ } )
43
43
}
44
44
45
- const to = resolve ( target , 'man' + sxn , base )
45
+ const to = resolve ( target , 'man' + section , base )
46
46
const from = relative ( dirname ( to ) , absFrom )
47
47
48
- return linkGently ( { from, to, path, absFrom, force } )
49
- } ) )
48
+ links . push ( linkGently ( { from, to, path, absFrom, force } ) )
49
+ }
50
+ return Promise . all ( links )
50
51
}
51
52
52
53
module . exports = linkMans
0 commit comments