Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preview build POC (step 1: local) #794

Open
wants to merge 48 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
a49a53b
Preview build POC
osfameron Oct 3, 2024
0b451b5
tidy
osfameron Oct 3, 2024
8acaac7
Fix bug - allow empty configuration
osfameron Oct 3, 2024
0894745
add deepmerge package
osfameron Oct 4, 2024
29812cd
Add `preview --init`
osfameron Oct 4, 2024
464adc8
WIP remote build
osfameron Oct 4, 2024
9269dce
Moved preview.js into Antora extension
osfameron Dec 20, 2024
8c724ce
add lib/preview.js
osfameron Dec 20, 2024
95d854f
actually run `gh` action
osfameron Dec 20, 2024
3767547
fixes
osfameron Dec 20, 2024
a2ff887
fix url mapping
osfameron Dec 20, 2024
48be83d
Fix invocation of `preview`
osfameron Dec 20, 2024
03430b2
gh repo clone, minor fixes
osfameron Dec 23, 2024
09d06e5
config via `HEAD`
osfameron Dec 23, 2024
8a91d27
documentation
osfameron Dec 23, 2024
35d3977
report-tree
osfameron Dec 23, 2024
4ce13e4
move tree.html to subdirectory
osfameron Dec 23, 2024
24325f3
update build for preview-toc
osfameron Jan 6, 2025
1def5f8
encode emoji
osfameron Jan 6, 2025
2503881
encode document emoji
osfameron Jan 6, 2025
eaa9b93
add report-tree to staging build
osfameron Jan 7, 2025
c79008c
update staging playbook
osfameron Jan 7, 2025
a75813f
add report-tree to staging build
osfameron Jan 7, 2025
9ece686
update staging playbook
osfameron Jan 7, 2025
046e1b3
upgrade antora
osfameron Jan 7, 2025
7516c04
add github URL to tree
osfameron Jan 7, 2025
b7cbfe9
add playbook JSON, display tweaks
osfameron Jan 7, 2025
dd85e47
improve output
osfameron Jan 7, 2025
e5c2424
default start_page
osfameron Jan 7, 2025
54d9ba5
remove TODO
osfameron Jan 7, 2025
ef465e0
Add link to start page
osfameron Jan 7, 2025
8e7de50
add docs-ai to preview sources
osfameron Jan 7, 2025
c71ec3f
Superset Release 1.0.5
zeba-shireen Jan 8, 2025
a2cfe92
purge-previews sketch
osfameron Jan 8, 2025
94a52f2
date fiddling for cross-platform
osfameron Jan 8, 2025
42e7519
fix
osfameron Jan 8, 2025
8a1fb09
updating report-tree test
osfameron Jan 9, 2025
df40e7e
update builds template to include PR/featured
osfameron Jan 9, 2025
c993dea
support for preview-watermark
osfameron Jan 10, 2025
f8cc5f3
bump preview ui
osfameron Jan 10, 2025
8fd818e
Add edge server component. Remove local UI bundle
ElliotFrancisHunter Jan 10, 2025
f1644d2
fix ln of preview script
osfameron Jan 10, 2025
14763d1
ignore /preview/
osfameron Jan 10, 2025
6f560e8
ui 206
osfameron Jan 10, 2025
465c03e
ui 207
osfameron Jan 10, 2025
e8bfc4c
better diagnostics for remote build with unpushed changes
osfameron Jan 14, 2025
ac1c4bc
Better output for build details
osfameron Jan 15, 2025
2056dab
Helpful error if preview.yml doesn't contain the repo
osfameron Jan 17, 2025
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
9 changes: 9 additions & 0 deletions antora-playbook.preview.local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
site:
title: Couchbase Docs Local Preview
output:
dir: ./preview
clean: true
asciidoc:
attributes:
kroki-server-url: null
RayOffiah marked this conversation as resolved.
Show resolved Hide resolved

120 changes: 120 additions & 0 deletions scripts/preview/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions scripts/preview/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "module",
"dependencies": {
"find-up": "^7.0.0",
"yaml": "^2.5.1"
}
}
198 changes: 198 additions & 0 deletions scripts/preview/preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import path from 'node:path'
import fs from 'node:fs'
import yaml from 'yaml'
import {findUp, pathExists} from 'find-up'
import { promisify } from 'node:util'
import child_process from 'node:child_process'
import deepmerge from '@fastify/deepmerge'
RayOffiah marked this conversation as resolved.
Show resolved Hide resolved
const doExec = promisify(child_process.exec);

// MAIN block
{
let antora = await getLocalAntora()
await buildAntora(antora)
}

// MAIN FUNCTIONS
async function getLocalAntora() {
const antoraPath = await findUp('antora.yml')
let antora = yaml.parse(
fs.readFileSync(antoraPath).toString())

const dotGit = path.dirname(
await findUp('.git', {type: "directory"}))

const branch = await exec('git branch --show-current')

const docsSite = getLocalDocsSite(dotGit)

const previewConfig = antora.ext?.preview?.[branch] || antora.ext?.preview?.DEFAULT || {}

antora = {
...antora,
repo: dotGit,
repoShort: path.basename(dotGit),
docsSite,
branches: branch,
previewConfig,
...startPath(dotGit, path.dirname(antoraPath)),
}

const sources = mapSources_local(antora)
return {
...antora,
sources
}
}

function writePlaybook({docsSite, sources, previewConfig}) {
// copy the master playbook
let masterPlaybook = path.resolve(docsSite, 'antora-playbook.yml')
let playbook = yaml.parse(
fs.readFileSync(masterPlaybook).toString())

// add the new sources we've customized
playbook.content.sources = sources

// do overrides
let overridePlaybook = path.resolve(docsSite, 'antora-playbook.preview.local.yml')
const override = yaml.parse(
fs.readFileSync(overridePlaybook).toString())

playbook = deepmerge({all: true})(
playbook,
override,
previewConfig.override || {})
console.dir(playbook, {depth: 4})

// write out the preview playbook
let previewPlaybook = path.resolve(docsSite, 'antora-playbook.preview.generated.yml')
fs.writeFileSync(previewPlaybook, yaml.stringify(playbook))

return playbook
}

async function buildAntora(antora) {
const {docsSite} = antora
const playbook = writePlaybook(antora)
const output = playbook.output?.dir || './public'
process.chdir(docsSite)
await spawn('npx', 'antora', 'antora-playbook.preview.generated.yml')
await spawn('open', `${output}/index.html`)
}

//////////
// HELPER FUNCTIONS

function startPath(from, to) {
const rel = path.relative(from, to)
if (rel == '') {
return {}
} else {
return { start_path: rel }
}
}

function make_resolveLocal(baseRepo) {
const repoPath = ((process.env.REPO_PATH || '..')
.split(':')
.map((p) => path.resolve(baseRepo, p)))

return (repo) =>
repoPath
.map((p) => path.resolve(p, repo))
.find((p) => fs.existsSync(p))
}

function mapSources_local({ext, repo, repoShort, docsSite, start_path, branch, previewConfig}) {
const defaultSources = {
'docs-site': {
url: '.',
branches: 'HEAD',
start_path: 'home/'
},
[repoShort]: {
url: repo,
branches: 'HEAD',
start_path,
}
}

const resolveLocal = make_resolveLocal(docsSite)

let sources = []
if (sources = previewConfig?.sources) {
sources = flatmapObj(sources,
(k, _v) => {
let url
if (url = resolveLocal(k)) {
return {url}
}
else {
console.error(`Didn't find ${k} in ${repoPath}`)
return null
}
}
)
}
sources = {
...defaultSources,
...sources
}
return Object.values(sources)
}

function getLocalDocsSite(repo) {
let docsSite
if (docsSite = process.env.DOCS_SITE_PATH) {
return docsSite
}
const resolveLocal = make_resolveLocal(repo)
return resolveLocal('docs-site')
}

///////////
// GENERIC HELPER functions

function flatmapObj(obj, fn) {
return Object.fromEntries(
Object.entries(obj)
.flatMap(([k,v]) => {
let ret
if (ret = fn(k,v)) {
return [[k, {...v, ...ret}]]
}
else {
return []
}
}))
}

async function exec(...args) {
const { stdout, stderr } = await doExec.apply(null, args)

if (stderr.length) {
console.error(stderr)
}
if (stdout.length) {
return stdout.trimRight()
}
}

function spawn(...command) {
let p = child_process.spawn(command[0], command.slice(1))
return new Promise((resolve) => {
p.stdout.on("data", (x) => {
process.stdout.write(x.toString())
})
p.stderr.on("data", (x) => {
process.stderr.write(x.toString())
})
p.on('error', (x) => {
throw new Error(x.toString())
})
p.on("exit", (code) => {
resolve(code)
})
})
}