|
| 1 | +'use strict' |
| 2 | + |
| 3 | +module.exports.register = function ({ config }) { |
| 4 | + const { rootComponentName, fileName = rootComponentName } = config |
| 5 | + this.on('navigationBuilt', ({ contentCatalog }) => { |
| 6 | + const { versions } = contentCatalog.getComponent(rootComponentName) |
| 7 | + versions.forEach(version => { |
| 8 | + const file = contentCatalog.resolveResource(`${version.version}@${rootComponentName}:ROOT:index.pdf`, {}, 'export', ['export']) |
| 9 | + if (file) { |
| 10 | + if (version.prerelease === '-SNAPSHOT') { |
| 11 | + contentCatalog.removeFile(file) |
| 12 | + } else { |
| 13 | + removeRootComponentNameFromFile(file, fileName) |
| 14 | + } |
| 15 | + } |
| 16 | + }) |
| 17 | + }) |
| 18 | +} |
| 19 | + |
| 20 | +function removeRootComponentNameFromFile(file, fileName) { |
| 21 | + if (file.out) { |
| 22 | + file.out.dirname = removeFirstSegment(file.out.dirname) |
| 23 | + file.out.basename = file.out.basename.replace('index', fileName) |
| 24 | + file.out.path = removeFirstSegment(file.out.path).replace('index', fileName) |
| 25 | + file.out.moduleRootPath = '.' |
| 26 | + file.out.rootPath = '..' |
| 27 | + } |
| 28 | + if (file.pub) { |
| 29 | + if (file.pub.rootPath) { |
| 30 | + file.pub.rootPath = '..' |
| 31 | + } |
| 32 | + if (file.pub.moduleRootPath) { |
| 33 | + file.pub.moduleRootPath = '.' |
| 34 | + } |
| 35 | + file.pub.url = removeFirstSegment(file.pub.url).replace('index', fileName) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +function removeFirstSegment(path) { |
| 40 | + if (path) { |
| 41 | + if (path.startsWith('/')) { |
| 42 | + return ('/' + path.split('/').slice(2).join('/')) || '.' |
| 43 | + } else { |
| 44 | + return (path.split('/').slice(1).join('/')) || '.' |
| 45 | + } |
| 46 | + } |
| 47 | + return path |
| 48 | +} |
0 commit comments