docusaurus fixes and correct hero cli implementation

This commit is contained in:
timurgordon
2025-05-13 01:54:47 +03:00
parent cd3c98280e
commit 2d5fbd3337
8 changed files with 347 additions and 12 deletions

77
cfg/config.heroscript Normal file
View File

@@ -0,0 +1,77 @@
!!docusaurus.config
name:"my-documentation"
title:"My Documentation Site"
tagline:"Documentation made simple with V and Docusaurus"
url:"https://docs.example.com"
url_home:"docs/"
base_url:"/"
favicon:"img/favicon.png"
image:"img/hero.png"
copyright:"© 2025 Example Organization"
!!docusaurus.config_meta
description:"Comprehensive documentation for our amazing project"
image:"https://docs.example.com/img/social-card.png"
title:"My Documentation | Official Docs"
!!docusaurus.ssh_connection
name:"production"
host:"example.com"
login:"deploy"
port:22
key_path:"~/.ssh/id_rsa"
!!docusaurus.build_dest
ssh_name:"production"
path:"/var/www/docs"
!!docusaurus.navbar
title:"My Project"
!!docusaurus.navbar_item
label:"Documentation"
href:"/docs"
position:"left"
!!docusaurus.navbar_item
label:"API"
href:"/api"
position:"left"
!!docusaurus.navbar_item
label:"GitHub"
href:"https://github.com/example/repo"
position:"right"
!!docusaurus.footer
style:"dark"
!!docusaurus.footer_item
title:"Documentation"
label:"Introduction"
to:"/docs"
!!docusaurus.footer_item
title:"Documentation"
label:"API Reference"
to:"/api"
!!docusaurus.footer_item
title:"Community"
label:"GitHub"
href:"https://github.com/example/repo"
!!docusaurus.footer_item
title:"Community"
label:"Discord"
href:"https://discord.gg/example"
!!docusaurus.footer_item
title:"More"
label:"Blog"
href:"https://blog.example.com"
!!docusaurus.import_source
url:"https://github.com/example/external-docs"
dest:"external"
replace:"PROJECT_NAME:My Project, VERSION:1.0.0"

1
examples/webtools/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
build

View File

@@ -0,0 +1,77 @@
!!docusaurus.config
name:"my-documentation"
title:"My Documentation Site"
tagline:"Documentation made simple with V and Docusaurus"
url:"https://docs.example.com"
url_home:"docs/"
base_url:"/"
favicon:"img/favicon.png"
image:"img/hero.png"
copyright:"© 2025 Example Organization"
!!docusaurus.config_meta
description:"Comprehensive documentation for our amazing project"
image:"https://docs.example.com/img/social-card.png"
title:"My Documentation | Official Docs"
!!docusaurus.ssh_connection
name:"production"
host:"example.com"
login:"deploy"
port:22
key_path:"~/.ssh/id_rsa"
!!docusaurus.build_dest
ssh_name:"production"
path:"/var/www/docs"
!!docusaurus.navbar
title:"My Project"
!!docusaurus.navbar_item
label:"Documentation"
href:"/docs"
position:"left"
!!docusaurus.navbar_item
label:"API"
href:"/api"
position:"left"
!!docusaurus.navbar_item
label:"GitHub"
href:"https://github.com/example/repo"
position:"right"
!!docusaurus.footer
style:"dark"
!!docusaurus.footer_item
title:"Documentation"
label:"Introduction"
to:"/docs"
!!docusaurus.footer_item
title:"Documentation"
label:"API Reference"
to:"/api"
!!docusaurus.footer_item
title:"Community"
label:"GitHub"
href:"https://github.com/example/repo"
!!docusaurus.footer_item
title:"Community"
label:"Discord"
href:"https://discord.gg/example"
!!docusaurus.footer_item
title:"More"
label:"Blog"
href:"https://blog.example.com"
!!docusaurus.import_source
url:"https://github.com/example/external-docs"
dest:"external"
replace:"PROJECT_NAME:My Project, VERSION:1.0.0"

View File

@@ -0,0 +1,91 @@
#!/bin/bash
# Exit script on any error
set -e
echo "Starting Docusaurus Example with Hero CLI"
# Define the source directory for the Docusaurus site content
# Using a different name (_cli) to avoid conflicts with the previous example
SOURCE_DIR="${HOME}/hero/var/docusaurus_demo_src_cli"
DOCS_SUBDIR="${SOURCE_DIR}/docs"
# Create the site source directory and the docs subdirectory if they don't exist
echo "Creating site source directory: ${SOURCE_DIR}"
mkdir -p "${DOCS_SUBDIR}"
# --- Create Sample Markdown Content ---
# The 'hero docusaurus' command doesn't automatically create content,
# so we do it here like the V example script did.
echo "Creating sample markdown content..."
# Create intro.md
# Using 'EOF' to prevent shell expansion within the heredoc
cat > "${DOCS_SUBDIR}/intro.md" << 'EOF'
---
title: Introduction (CLI Example)
slug: /
sidebar_position: 1
---
# Welcome to My Documentation (CLI Version)
This is a sample documentation site created with Docusaurus and HeroLib V using the `hero docusaurus` command and a HeroScript configuration file.
## Features
- Easy to use
- Markdown support
- Customizable
- Search functionality
## Getting Started
Follow these steps to get started:
1. Installation
2. Configuration
3. Adding content
4. Deployment
EOF
# Create quick-start.md
cat > "${DOCS_SUBDIR}/quick-start.md" << 'EOF'
---
title: Quick Start (CLI Example)
sidebar_position: 2
---
# Quick Start Guide (CLI Version)
This guide will help you get up and running quickly.
## Installation
```bash
$ npm install my-project
```
## Basic Usage
```javascript
import { myFunction } from "my-project";
// Use the function
const result = myFunction();
console.log(result);
```
EOF
echo "Sample markdown content created."
# --- Run Docusaurus Directly via V Script ---
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# -n initializes the site structure if it doesn't exist (--new)
# -d runs the development server (--dev)
hero docusaurus -buildpath "${HOME}/hero/var/docusaurus_demo_src_cli" -path "${SCRIPT_DIR}/cfg/docusaurus_example_config.heroscript" -new -dev
echo "Hero docusaurus command finished. Check for errors or dev server output."

View File

@@ -1,10 +1,11 @@
module herocmds
import freeflowuniverse.herolib.web.docusaurus
import freeflowuniverse.herolib.core.pathlib
import os
import cli { Command, Flag }
pub fn cmd_docusaurus(mut cmdroot Command) {
pub fn cmd_docusaurus(mut cmdroot Command) Command {
mut cmd_run := Command{
name: 'docusaurus'
description: 'Generate, build, run docusaurus sites.'
@@ -35,7 +36,16 @@ pub fn cmd_docusaurus(mut cmdroot Command) {
name: 'path'
abbrev: 'p'
// default: ''
description: 'Path where docusaurus source is.'
description: 'Path where docusaurus configuration is.'
})
cmd_run.add_flag(Flag{
flag: .string
required: false
name: 'buildpath'
abbrev: 'b'
// default: ''
description: 'Path where docusaurus build is.'
})
cmd_run.add_flag(Flag{
@@ -71,6 +81,14 @@ pub fn cmd_docusaurus(mut cmdroot Command) {
description: 'build dev version and publish.'
})
cmd_run.add_flag(Flag{
flag: .bool
required: false
name: 'open'
abbrev: 'o'
description: 'open the site in browser.'
})
cmd_run.add_flag(Flag{
flag: .bool
required: false
@@ -95,36 +113,90 @@ pub fn cmd_docusaurus(mut cmdroot Command) {
})
cmdroot.add_command(cmd_run)
return cmdroot
}
fn cmd_docusaurus_execute(cmd Command) ! {
mut update := cmd.flags.get_bool('update') or { false }
mut init := cmd.flags.get_bool('new') or { false }
mut open := cmd.flags.get_bool('open') or { false }
mut url := cmd.flags.get_string('url') or { '' }
mut publish_path := cmd.flags.get_string('publish') or { '' }
mut deploykey := cmd.flags.get_string('deploykey') or { '' }
mut path := cmd.flags.get_string('path') or { '' }
// --- Build Path Logic ---
mut build_path := cmd.flags.get_string('buildpath') or { '' }
if build_path == '' {
// Default build path if not provided (e.g., use CWD or a specific temp dir)
// Using CWD for now based on previous edits, adjust if needed
build_path = os.getwd()
}
// --- Start: Heroscript Path Logic ---
mut provided_path := cmd.flags.get_string('path') or { '' }
mut heroscript_source_path := ''
build_cfg_dir := os.join_path(build_path, 'cfg')
target_heroscript_path := os.join_path(build_cfg_dir, 'config.heroscript')
if provided_path != '' {
if !os.exists(provided_path) || !os.is_file(provided_path) {
return error('Provided path "${provided_path}" does not exist or is not a file.')
}
heroscript_source_path = provided_path
// --- Copy Heroscript to Build Location ---
os.mkdir_all(build_cfg_dir)!
os.cp(heroscript_source_path, target_heroscript_path)!
} else {
// Path not provided, look in ./cfg/
mut cwd := os.getwd()
cfg_dir := os.join_path(cwd, 'cfg')
if !os.exists(cfg_dir) || !os.is_dir(cfg_dir) {
return error('Flag -path not provided and directory "./cfg" not found in the current working directory.')
}
mut found_files := []string
for file in os.ls(cfg_dir) or { []string{} } {
if file.ends_with('.heroscript') {
found_files << os.join_path(cfg_dir, file)
}
}
if found_files.len == 1 {
heroscript_source_path = found_files[0]
os.mkdir_all(build_cfg_dir)!
os.mv(heroscript_source_path, target_heroscript_path)!
} else if found_files.len == 0 {
return error('Flag -path not provided and no *.heroscript file found in "./cfg".')
} else {
return error('Flag -path not provided and multiple *.heroscript files found in "./cfg". Please specify one using -path.')
}
}
// --- End: Heroscript Path Logic ---
mut buildpublish := cmd.flags.get_bool('buildpublish') or { false }
mut builddevpublish := cmd.flags.get_bool('builddevpublish') or { false }
mut dev := cmd.flags.get_bool('dev') or { false }
// if build== false && build== false && build== false {
// eprintln("specify build, builddev or dev")
// exit(1)
// }
mut docs := docusaurus.new(
update: update
build_path: build_path
heroscript: os.read_file(target_heroscript_path)! // Read the copied heroscript
)!
mut docs := docusaurus.new(update: update)!
mut site := docs.get(
url: url
path: path
path: build_path
update: update
publish_path: publish_path
deploykey: deploykey
init: init
open: open
)!
site.generate()!
if publish_path.len > 0 {
site.build()!
}
@@ -140,4 +212,8 @@ fn cmd_docusaurus_execute(cmd Command) ! {
if dev {
site.dev()!
}
if open {
site.open()!
}
}

View File

@@ -58,6 +58,13 @@ pub fn (mut s DocSite) build_publish() ! {
)!
}
pub fn (mut s DocSite) open() ! {
// Print instructions for user
console.print_item('open browser: ${s.url}')
osal.exec(cmd: 'open https://localhost:3000')!
}
pub fn (mut s DocSite) dev() ! {
s.clean()!
s.generate()!
@@ -76,8 +83,9 @@ pub fn (mut s DocSite) dev() ! {
)!
// Send commands to the screen session
console.print_item('To view the server output:: cd ${s.path_build.path}')
scr.cmd_send('cd ${s.path_build.path}')!
scr.cmd_send('bash develop.sh')!
scr.cmd_send('bun start')!
// Print instructions for user
console.print_header(' Docusaurus Development Server')
@@ -98,6 +106,10 @@ pub fn (mut s DocSite) dev() ! {
// tf.wait()!
println('\n')
if s.args.open {
s.open()!
}
if s.args.watch_changes {
docs_path := '${s.path_src.path}/docs'
watch_docs(docs_path, s.path_src.path, s.path_build.path)!
@@ -208,7 +220,7 @@ fn (mut site DocSite) process_md(mut path pathlib.Path, args MyImport) ! {
fn (mut site DocSite) template_install() ! {
mut gs := gittools.new()!
site.factory.template_install(template_update: false, install: false, delete: false)!
site.factory.template_install(template_update: false, install: true, delete: false)!
cfg := site.config

View File

@@ -18,6 +18,7 @@ pub mut:
production bool
watch_changes bool = true
update bool
open bool
init bool // means create new one if needed
deploykey string
config ?Config

View File

@@ -11,7 +11,7 @@ import time
@[params]
struct TemplateInstallArgs {
template_update bool = true
install bool
install bool = true
delete bool = true
}