1const { dirname, relative, join, resolve, basename } = require('path') 2const linkGently = require('./link-gently.js') 3const manTarget = require('./man-target.js') 4 5const linkMans = async ({ path, pkg, top, force }) => { 6 const target = manTarget({ path, top }) 7 if (!target || !Array.isArray(pkg?.man) || !pkg.man.length) { 8 return [] 9 } 10 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]+)(\.gz)?$/) 20 if (!parseMan) { 21 throw Object.assign(new Error('invalid man entry name\n' + 22 'Man files must end with a number, ' + 23 'and optionally a .gz suffix if they are compressed.' 24 ), { 25 code: 'EBADMAN', 26 path, 27 pkgid: pkg._id, 28 man, 29 }) 30 } 31 32 const section = parseMan[1] 33 const base = basename(man) 34 const absFrom = resolve(path, man) 35 /* istanbul ignore if - that unpossible */ 36 if (absFrom.indexOf(path) !== 0) { 37 throw Object.assign(new Error('invalid man entry'), { 38 code: 'EBADMAN', 39 path, 40 pkgid: pkg._id, 41 man, 42 }) 43 } 44 45 const to = resolve(target, 'man' + section, base) 46 const from = relative(dirname(to), absFrom) 47 48 links.push(linkGently({ from, to, path, absFrom, force })) 49 } 50 return Promise.all(links) 51} 52 53module.exports = linkMans 54