1- module . exports . register = function ( { config, playbook } ) {
2- playbook . content . sources . push ( {
3- url :"../preview/" + process . env . PR_START_PATH ,
4- branches : "HEAD"
1+ const path = require ( 'node:path' )
2+ const fs = require ( 'node:fs' )
3+ const os = require ( 'node:os' )
4+ const yaml = require ( 'yaml' )
5+ const { promisify} = require ( 'node:util' )
6+ const child_process = require ( 'node:child_process' )
7+ const deepmerge = require ( '@fastify/deepmerge' )
8+ const doExec = promisify ( child_process . exec )
9+
10+ module . exports . register = async function ( { config, playbook } ) {
11+
12+ const url = process . env . PR_URL
13+ const branch = process . env . PR_BRANCH
14+ let fromDir = process . env . PREVIEW_FROMDIR
15+
16+ const repo = path . basename ( url )
17+
18+ let is_remote ;
19+ if ( fromDir ) {
20+ is_remote = false
21+ fromDir = [ fromDir ]
22+ }
23+ else {
24+ is_remote = true
25+ // fromDir = ... TODO
26+ }
27+
28+ const antoraPath = await findAntora ( fromDir )
29+ console . log ( { antoraPath} )
30+
31+ let antora = {
32+ url,
33+ repo,
34+ branch,
35+ playbook,
36+ ...startPath ( url , path . dirname ( antoraPath ) ) ,
37+ ...readAntora ( antoraPath , branch )
38+ }
39+
40+ const sources = mapSources_local ( antora )
41+ console . dir ( sources )
42+ playbook . content . sources = sources
43+ this . updateVariables ( { playbook } )
44+
45+ this . on ( 'contextStarted' , async ( { config, playbook} ) => {
46+ throw new Error ( "RARR" )
47+ playbook . content . sources = sources
48+
49+ playbook = deepmerge ( { all : true } ) (
50+ playbook ,
51+ antora . previewConfig . override || { } )
52+
53+ console . dir ( playbook . content )
54+
55+ this . updateVariables ( { playbook } )
556 } )
657}
58+
59+ function make_resolveLocal ( baseRepo ) {
60+ const repoPath = ( ( process . env . REPO_PATH || '..' )
61+ . split ( ':' )
62+ . map ( ( p ) => path . resolve ( baseRepo , p ) ) )
63+
64+ return ( repo ) =>
65+ repoPath
66+ . map ( ( p ) => path . resolve ( p , repo ) )
67+ . find ( ( p ) => fs . existsSync ( p ) )
68+ }
69+
70+ function mapSources_local ( antora ) {
71+ const resolver = make_resolveLocal ( '.' )
72+ return mapSources ( { ...antora , resolver} )
73+ }
74+
75+ function readAntora ( antoraPath , branch ) {
76+ let antora = yaml . parse (
77+ fs . readFileSync ( antoraPath ) . toString ( ) )
78+
79+ antora . previewConfig =
80+ antora . ext ?. preview ?. [ branch ] ||
81+ antora . ext ?. preview ?. DEFAULT || { }
82+
83+ return antora
84+ }
85+
86+ function mapSources ( { repo, url, start_path, previewConfig, resolver} ) {
87+ const defaultSources = {
88+ // 'docs-site': {
89+ // url: '.',
90+ // branches: 'HEAD',
91+ // start_path: 'home/'
92+ // },
93+ [ repo ] : {
94+ url,
95+ branches : 'HEAD' ,
96+ start_path,
97+ }
98+ }
99+
100+ let sources = [ ]
101+ if ( sources = previewConfig ?. sources ) {
102+ sources = flatmapObj ( sources ,
103+ ( k , v ) => {
104+ let url
105+ if ( url = resolver ( k ) ) {
106+ return { url, ...v }
107+ }
108+ else {
109+ console . error ( `Didn't find ${ k } in ${ repoPath } ` )
110+ return null
111+ }
112+ }
113+ )
114+ }
115+ sources = {
116+ ...defaultSources ,
117+ ...sources
118+ }
119+
120+ return Object . values ( sources )
121+ }
122+
123+ async function findAntora ( paths ) {
124+ const { findUp} = await import ( 'find-up' )
125+
126+ for ( let p of paths ) {
127+ let a
128+ if ( a = await findUp ( 'antora.yml' ,
129+ { cwd : path . resolve ( p ) } ) )
130+ { return a }
131+ else { throw new Error ( "Couldn't find antora.yml" ) }
132+ }
133+ }
134+
135+ function startPath ( from , to ) {
136+ const rel = path . relative ( from , to )
137+ if ( rel == '' ) {
138+ return { }
139+ } else {
140+ return { start_path : rel }
141+ }
142+ }
143+
144+ ///////////
145+ // GENERIC HELPER functions
146+
147+ function flatmapObj ( obj , fn ) {
148+ return Object . fromEntries (
149+ Object . entries ( obj )
150+ . flatMap ( ( [ k , v ] ) => {
151+ let ret
152+ if ( ret = fn ( k , v ) ) {
153+ return [ [ k , { ...v , ...ret } ] ]
154+ }
155+ else {
156+ return [ ]
157+ }
158+ } ) )
159+ }
0 commit comments