diff --git a/lib/preview.js b/lib/preview.js index 3dab1edac1..a071d6dbb8 100644 --- a/lib/preview.js +++ b/lib/preview.js @@ -10,7 +10,11 @@ const util = require('util') const exec = util.promisify(require('child_process').exec) module.exports.register = function () { - this.once('playbookBuilt', async function ({ playbook }) { + + this.once('playbookBuilt', rebuildPlaybook) + + async function rebuildPlaybook ({ playbook }) { + // deep-copy playbook to allow it to be updated const env = playbook.env playbook = JSON.parse(JSON.stringify(playbook)) @@ -76,16 +80,56 @@ module.exports.register = function () { } } - const mapLocalUrl = (repo, url) => (args.repoPath - .map(p => path.resolve(p, repo)) - .find(fs.existsSync) - || url) + function subdirs(p) { + return ( + fs.readdirSync(p) + .map(d => path.resolve(p, d)) + .filter(i => fs.statSync(i).isDirectory()) + ) + } + + function* iterate(p, level = 0) { + + if (! fs.existsSync(p)) return + + const stats = fs.statSync(p) + if (stats.isDirectory()) { + yield p + const s1 = subdirs(p) + for (const d of s1) { + yield d + } + for (const d of s1) { + const s2 = subdirs(d) + for (const d of s2) { + yield d + } + } + } + } + + const isMainGitDir = p => { + const pwg = `${p}/.git` + if (! fs.existsSync(pwg)) { return false } + return fs.statSync(pwg).isDirectory() + } + + function mapLocalUrl (repo, url) { + for (const repoPath of args.repoPath) { + const p = path.resolve(repoPath, repo) + + const mainGitDir = iterate(p).find(isMainGitDir) + if (mainGitDir) { return mainGitDir } + } + + // fallback to the original (remote) url + return url + } const basePath = path.resolve( mapLocalUrl(args.repo, 'UNEXPECTED'), args.startPath || source.start_path || '') const antoraYmlPath = `${basePath}/antora.yml` - console.log(antoraYmlPath) const readYaml = (path) => fs.existsSync(path) && yaml.parse(fs.readFileSync(path).toString()) @@ -133,8 +177,6 @@ module.exports.register = function () { Object.entries(updatedSources) .map(urlMapper))) - console.dir(mappedSources, {depth: 5}) - const startPage = { site: { startPage: @@ -152,11 +194,11 @@ module.exports.register = function () { playbook.asciidoc.attributes['page-watermark'] = `${date} ${args.repo} ${watermark_branch}` - // console.dir(playbook, {depth: 5}); process.exit(1) // reinflate .env before updating playbook.env = env + this.updateVariables({ playbook }) - }) + } } function notfound(args) { diff --git a/scripts/preview b/scripts/preview index 8d664ceaef..52b208a496 100755 --- a/scripts/preview +++ b/scripts/preview @@ -193,7 +193,7 @@ fi ANTORA=$(realpath $(dirname $ANTORA)) TOPLEVEL=$(git rev-parse --show-toplevel) -export PREVIEW_REPO=$(basename $TOPLEVEL) +export PREVIEW_REPO=$(gh repo view --json name --jq .name) export PREVIEW_BRANCH=$(git branch --show-current) export PREVIEW_START_PATH=.${ANTORA#$TOPLEVEL} export PREVIEW_CONFIG=${PREVIEW_CONFIG:-$PREVIEW_BRANCH} @@ -283,7 +283,7 @@ else cd $DOCS_SITE echo - ANTORA="npx antora --extension lib/preview.js antora-playbook.preview.yml --stacktrace --url '$(pwd)/preview'" + ANTORA="npx antora --extension ./lib/preview.js antora-playbook.preview.yml --stacktrace --url '$(pwd)/preview'" if [ -n "$DEBUG" ]; then echo "Running in debug mode"