refactor: Improve docusaurus import and site handling

- Simplify command logic to use a single defined site
- Enhance git import to resolve paths relative to project root
- Add `docusaurus.export` action to trigger `build_publish`
- Change asset import destination from `docs` to `static`
- Add `dsite_get_only` helper for simplified site access
This commit is contained in:
Mahmoud-Emad
2025-08-13 04:19:32 +03:00
parent f885563982
commit e9bcf6ef69
6 changed files with 91 additions and 71 deletions

View File

@@ -137,12 +137,6 @@ fn cmd_docusaurus_execute(cmd Command) ! {
git_pull: update
)!
// `docusaurus_path` is a pathlib.Path we need its string representation
if os.exists(os.join_path(docusaurus_path.path, 'cfg'))==false {
return error('Docusaurus configuration directory not found at: ${os.join_path(docusaurus_path.path,
'cfg')}')
}
console.print_header('Running Docusaurus for: ${docusaurus_path.path}')
// The `playcmds.run` helper expects a string path. Use the underlying
@@ -152,21 +146,22 @@ fn cmd_docusaurus_execute(cmd Command) ! {
reset: false
)!
// // ---------- ACTIONS ----------
// mut dsite_opt := docusaurus.dsite_add(
// sitename: 'default'
// path: docusaurus_path.path
// )!
// TODO: We need to load the sitename instead, or maybe remove it
mut dsite := docusaurus.dsite_get_only() or {
return error('No docusaurus site named "default" and not exactly one site defined. Please set site.config name:"..." or add a --name flag support.')
}
// if buildpublish {
// dsite_opt.build()!
// } else if dev {
// dsite_opt.dev(
// open: open_
// watch_changes: false
// )!
// } else {
// // default: just build the static site
// dsite_opt.build()!
// }
if buildpublish {
// Build and publish production-ready artifacts
dsite.build_publish()!
} else if dev {
// Run local development server
dsite.dev(
open: open_
watch_changes: false
)!
} else {
// Default: just build the static site
dsite.build()!
}
}

View File

@@ -45,14 +45,6 @@ pub fn run(args_ PlayArgs) ! {
docusaurus.play(mut plbook)!
println("=========____________===========")
println(plbook)
println("=========____________===========")
if true{panic("sdsds")}
// Ensure we did not leave any actions unprocessed
plbook.empty_check()!
}

View File

@@ -1,43 +1,50 @@
module docusaurus
import freeflowuniverse.herolib.osal.screen
import freeflowuniverse.herolib.develop.gittools
import os
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.web.site as sitemodule
import freeflowuniverse.herolib.osal.core as osal
import freeflowuniverse.herolib.ui.console
import time
@[params]
pub struct ImportParams {
path string
git_url string
path string
git_url string
git_reset bool
git_root string
git_pull bool
dest string
git_root string
git_pull bool
dest string
}
pub fn (mut site DocSite) import() ! {
pub fn (mut site DocSite) import() ! {
for importparams in site.importparams {
console.print_header('Importing: ${importparams.path} from ${importparams.git_url}')
mut f := factory_get()!
mut mypath := ''
mut target_path := if os.is_abs_path(importparams.path) {
importparams.path
} else {
os.abs_path(os.join_path(importparams.git_root, importparams.path))
}
mut mypath := gittools.get_repo_path(
// Use gittools to get/update the repo, then navigate to the specific path
repo_path := gittools.get_repo_path(
git_pull: importparams.git_pull
git_reset: importparams.git_reset
git_url: importparams.git_url
path: importparams.path
path: importparams.git_root
)!
println(site)
if true{panic("3456789")}
mut mypatho := pathlib.get(repo_path)
// TODO: We need to think about a better way to do it
mypatho.path = repo_path + '/' + importparams.path.all_after('/')
mut mypatho := pathlib.get(mypath)
mut static_dest := '${f.path_build.path}/static'
println('static_dest: ${static_dest}')
mypatho.copy(dest: '${f.path_build.path}/docs/${importparams.dest}', delete: false)!
if importparams.dest.len > 0 {
static_dest = '${static_dest}/${importparams.dest}'
}
mypatho.copy(dest: static_dest, delete: false)!
// println(item)
// // replace: {'NAME': 'MyName', 'URGENCY': 'red'}
@@ -52,5 +59,4 @@ pub fn (mut site DocSite) import() ! {
// ]
// )!
}
}

View File

@@ -1,21 +1,17 @@
module docusaurus
import os
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.develop.gittools
import freeflowuniverse.herolib.web.site
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.osal.core as osal
@[params]
pub struct AddArgs {
pub mut:
sitename string // needs to exist in web.site module
sitename string // needs to exist in web.site module
}
pub fn dsite_define(sitename string) ! {
console.print_header('Add Docusaurus Site: ${sitename}')
mut f := factory_get()!
@@ -47,6 +43,26 @@ pub fn dsite_get(name_ string) !&DocSite {
pub fn dsite_exists(name_ string) !bool {
name := texttools.name_fix(name_)
d := docusaurus_sites[name] or { return false }
_ := docusaurus_sites[name] or { return false }
return true
}
// dsite_names returns the list of defined docusaurus site names.
pub fn dsite_names() []string {
mut names := []string{}
for k, _ in docusaurus_sites {
names << k
}
return names
}
// dsite_get_only returns the only defined site, or an error if there are none or more than one.
pub fn dsite_get_only() !&DocSite {
if docusaurus_sites.len != 1 {
return error('expected exactly one docusaurus site to be defined, found ${docusaurus_sites.len}')
}
for _, v in docusaurus_sites {
return v
}
return error('no docusaurus site found')
}

View File

@@ -1,7 +1,8 @@
module docusaurus
import freeflowuniverse.herolib.core.playbook { PlayBook, Action }
import freeflowuniverse.herolib.core.playbook { PlayBook }
import freeflowuniverse.herolib.web.site
import os
pub fn play(mut plbook PlayBook) ! {
if !plbook.exists(filter: 'docusaurus.') {
@@ -17,12 +18,12 @@ pub fn play(mut plbook PlayBook) ! {
// 3. Process `docusaurus.add` actions to create sites.
mut param_define := action_define.params
mut f := factory_set(
_ := factory_set(
path_build: param_define.get_default('path_build', '')!
path_publish: param_define.get_default('path_publish', '')!
reset: param_define.get_default_false('reset')
template_update: param_define.get_default_false('template_update')
install: param_define.get_default_false('install')
install: param_define.get_default_false('install')
)!
site_name := param_define.get('name') or {
@@ -33,19 +34,24 @@ pub fn play(mut plbook PlayBook) ! {
action_define.done = true
mut dsite := dsite_get(site_name)!
//imports
// imports
mut actions_import := plbook.find(filter: 'docusaurus.import')!
for mut action in actions_import {
mut p := action.params
// TODO: We need to get the repo path from the path
// Import paths like ../docusaurus are authored relative to the project root (docs_owh)
// project_root = dirname(dirname(plbook.path)) since plbook.path = ebooks/owh_investment_memo
mut project_root := os.abs_path(os.join_path(plbook.path, '..', '..'))
dsite.importparams << ImportParams{
path: p.get_default('path', '')!
git_url: p.get_default('git_url', '')!
git_reset: p.get_default_false('git_reset')
git_pull: p.get_default_false('git_pull')
dest: p.get_default('dest', '')!
path: p.get_default('path', '')!
git_url: p.get_default('git_url', '')!
git_reset: p.get_default_false('git_reset')
git_pull: p.get_default_false('git_pull')
git_root: project_root
dest: p.get_default('dest', '')!
}
action.done = true
}
}
mut actions_dev := plbook.find(filter: 'docusaurus.dev')!
if actions_dev.len > 1 {
@@ -54,9 +60,9 @@ pub fn play(mut plbook PlayBook) ! {
for mut action in actions_dev {
mut p := action.params
dsite.dev(
host: p.get_default('host', 'localhost')!
port: p.get_int_default('port', 3000)!
open: p.get_default_false('open')
host: p.get_default('host', 'localhost')!
port: p.get_int_default('port', 3000)!
open: p.get_default_false('open')
)!
action.done = true
}
@@ -66,10 +72,18 @@ pub fn play(mut plbook PlayBook) ! {
return error('Multiple "docusaurus.build" actions found. Only one is allowed.')
}
for mut action in actions_build {
mut p := action.params
dsite.build()!
action.done = true
}
mut actions_export := plbook.find(filter: 'docusaurus.export')!
if actions_export.len > 1 {
return error('Multiple "docusaurus.export" actions found. Only one is allowed.')
}
for mut action in actions_export {
dsite.build_publish()!
action.done = true
}
plbook.ensure_processed(filter: 'docusaurus.')!
}

View File

@@ -9,8 +9,6 @@ pub fn play(mut plbook PlayBook) ! {
return
}
println('DEBUG: Before the error')
mut config_action := plbook.ensure_once(filter: 'site.config')!
mut p := config_action.params
@@ -55,7 +53,6 @@ pub fn play(mut plbook PlayBook) ! {
play_build_dest_dev(mut plbook, mut config)!
play_pages(mut plbook, mut website)!
}
fn play_import(mut plbook PlayBook, mut config SiteConfig) ! {