This commit is contained in:
2025-02-05 10:16:25 +03:00
parent 757358fded
commit be9f37a459
6 changed files with 173 additions and 33 deletions

View File

@@ -1,6 +1,8 @@
module docusaurus
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.osal.screen
import os
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.ui.console
@@ -32,7 +34,7 @@ pub mut:
// publish_path string
build_path string
production bool
watch_changes bool
watch_changes bool = true
}
pub fn (mut f DocusaurusFactory) build_dev(args_ DSiteNewArgs) !&DocSite {
@@ -64,17 +66,42 @@ pub fn (mut f DocusaurusFactory) build(args_ DSiteNewArgs) !&DocSite {
pub fn (mut f DocusaurusFactory) dev(args_ DSiteNewArgs) !&DocSite {
mut s:=f.add(args_)!
s.generate()!
osal.exec(
cmd: '
cd ${s.path_build.path}
bash develop.sh
'
retry: 0
)!
if args.watch_changes{
//TODO: use lib/osal/notifier module to see all changes in docs directory of the source, when changes copy the file to the dest
//TODO: only look at files not starting with # and ending with .md, also for images (.png, .jpeg, .jpg)
}
// Create screen session for docusaurus development server
mut screen_name := 'docusaurus_${s.args.nameshort}'
mut sf := screen.new()!
// Add and start a new screen session
mut scr := sf.add(
name: screen_name
cmd: '/bin/bash'
start: true
attach: false
)!
// Send commands to the screen session
scr.cmd_send('cd ${s.path_build.path}')!
scr.cmd_send('bash develop.sh')!
// Print instructions for user
console.print_header(' Docusaurus Development Server')
console.print_item('Development server is running in a screen session.')
console.print_item('To view the server output:')
console.print_item(' 1. Attach to screen: screen -r ${screen_name}')
console.print_item(' 2. To detach from screen: Press Ctrl+A then D')
console.print_item(' 3. To list all screens: screen -ls')
// Start the watcher in a separate thread
//mut tf:=spawn watch_docs(docs_path, s.path_src.path, s.path_build.path)
//tf.wait()!
println("\n")
if args_.watch_changes {
docs_path := '${s.path_src.path}/docs'
watch_docs(docs_path, s.path_src.path, s.path_build.path)!
}
return s
}
@@ -215,13 +242,15 @@ fn (mut site DocSite) template_install() ! {
}
for item in ['package.json', 'sidebars.ts', 'tsconfig.json','docusaurus.config.ts'] {
mut aa:= template_path.file_get(item)!
aa.copy(dest:"${site.path_build.path}/${item}")! //TODO: use normal os.copy
src_path := os.join_path(template_path.path, item)
dest_path := os.join_path(site.path_build.path, item)
os.cp(src_path, dest_path) or { return error('Failed to copy ${item} to build path: ${err}') }
}
for item in ['.gitignore'] {
mut aa:= template_path.file_get(item)!
aa.copy(dest:"${site.path_src.path}/${item}")! //TODO: use normal os.copy
src_path := os.join_path(template_path.path, item)
dest_path := os.join_path(site.path_src.path, item)
os.cp(src_path, dest_path) or { return error('Failed to copy ${item} to source path: ${err}') }
}
cfg := site.config

View File

@@ -0,0 +1,100 @@
module docusaurus
import freeflowuniverse.herolib.osal.notifier
import os
fn watch_docs(docs_path string, path_src string, path_build string) ! {
mut n := notifier.new('docsite_watcher') or {
eprintln('Failed to create watcher: ${err}')
return
}
n.args["path_src"]=path_src
n.args["path_build"]=path_build
// Add watch with captured args
n.add_watch(docs_path, fn (event notifier.NotifyEvent, path string, args map[string]string) {
handle_file_change(event, path, args) or {
eprintln('Error handling file change: ${err}')
}
})!
n.start()!
}
// handle_file_change processes file system events
fn handle_file_change(event notifier.NotifyEvent, path string, args map[string]string) ! {
file_base := os.base(path)
is_dir := os.is_dir(path)
// Skip files starting with #
if file_base.starts_with('#') {
return
}
// For files (not directories), check extensions
if !is_dir {
ext := os.file_ext(path).to_lower()
if ext !in ['.md', '.png', '.jpeg', '.jpg'] {
return
}
}
// Get relative path from docs directory
rel_path := path.replace('${args["path_src"]}/docs/', '')
dest_path := '${args["path_build"]}/docs/${rel_path}'
match event {
.create, .modify {
if is_dir {
// For directories, just ensure they exist
os.mkdir_all(dest_path) or {
return error('Failed to create directory ${dest_path}: ${err}')
}
println('Created directory: ${rel_path}')
} else {
// For files, ensure parent directory exists and copy
os.mkdir_all(os.dir(dest_path)) or {
return error('Failed to create directory ${os.dir(dest_path)}: ${err}')
}
os.cp(path, dest_path) or {
return error('Failed to copy ${path} to ${dest_path}: ${err}')
}
println('Updated: ${rel_path}')
}
}
.delete {
if os.exists(dest_path) {
if is_dir {
os.rmdir_all(dest_path) or {
return error('Failed to delete directory ${dest_path}: ${err}')
}
println('Deleted directory: ${rel_path}')
} else {
os.rm(dest_path) or {
return error('Failed to delete ${dest_path}: ${err}')
}
println('Deleted: ${rel_path}')
}
}
}
.rename {
// For rename events, fswatch provides the new path in the event
// The old path is already removed, so we just need to handle the new path
if is_dir {
os.mkdir_all(dest_path) or {
return error('Failed to create directory ${dest_path}: ${err}')
}
println('Renamed directory to: ${rel_path}')
} else {
os.mkdir_all(os.dir(dest_path)) or {
return error('Failed to create directory ${os.dir(dest_path)}: ${err}')
}
os.cp(path, dest_path) or {
return error('Failed to copy ${path} to ${dest_path}: ${err}')
}
println('Renamed to: ${rel_path}')
}
}
}
}