Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 52 additions & 10 deletions lib/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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 }
}

Comment thread
osfameron marked this conversation as resolved.
// 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())
Expand Down Expand Up @@ -133,8 +177,6 @@ module.exports.register = function () {
Object.entries(updatedSources)
.map(urlMapper)))

console.dir(mappedSources, {depth: 5})

const startPage =
{ site:
{ startPage:
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions scripts/preview
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
osfameron marked this conversation as resolved.
export PREVIEW_BRANCH=$(git branch --show-current)
export PREVIEW_START_PATH=.${ANTORA#$TOPLEVEL}
export PREVIEW_CONFIG=${PREVIEW_CONFIG:-$PREVIEW_BRANCH}
Expand Down Expand Up @@ -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"
Expand Down
Loading