Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit 1fed439

Browse files
feat: add support for custom config locations & more cli flags
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent 8e00c4a commit 1fed439

6 files changed

Lines changed: 124 additions & 3 deletions

File tree

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# StaticGen-Node
2+
3+
StaticGen-Node is a simple wrapper around [staticgen](https://github.com/tj/staticgen) which simplifies using this tool in CI-Scenarios.
4+
5+
```bash
6+
# npm
7+
npm install -S staticgen-node
8+
9+
# yarn
10+
yarn add staticgen-node
11+
```
12+
13+
Currently, the latest binaries for linux, osx and windows for amd64 and arm64 are included in the npm package (I might change this in a later version to download the binarys on runtime).
14+
15+
StaticGen-Node also includes a couple of small changes to staticgen to improve the tool for CI, like windows support and all config variables are now also configurable using flags.

build.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ console.log("> removing old files");
1010
shell.rm("-rf", "./staticgen")
1111
console.log("> cloning staticgen");
1212
shell.exec(`git clone --depth 1 --branch v1.1.0 https://github.com/tj/staticgen`, {silent: true})
13-
console.log("> applying windows support patch");
14-
shell.exec("git apply remove-syscalls.patch --directory staticgen")
13+
console.log("> applying patches");
14+
shell.exec("git apply patches/*.patch --directory staticgen")
1515
console.log("> building binaries");
1616
shell.exec("goreleaser --snapshot --skip-publish --rm-dist")

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "staticgen-node",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"license": "MIT",
55
"author": "<mail@henrygressmann.de>",
66
"main": "./lib/index.js",

patches/0001-no-file-config.patch

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
diff --git a/staticgen.go b/staticgen.go
2+
index 63759e8..5a0b2a0 100644
3+
--- a/staticgen.go
4+
+++ b/staticgen.go
5+
@@ -70,15 +70,9 @@ func (g *Generator) Run(ctx context.Context) error {
6+
7+
// Start loads configuration from ./static.json, starts the
8+
// configured server, and begins the crawling process.
9+
-func (g *Generator) Start(ctx context.Context) error {
10+
- // load configuration
11+
- err := g.Config.Load("static.json")
12+
- if err != nil {
13+
- return fmt.Errorf("loading configuration: %w", err)
14+
- }
15+
-
16+
+func (g *Generator) Start(ctx context.Context, config Config) error {
17+
// remove output dir
18+
- err = os.RemoveAll(g.Dir)
19+
+ err := os.RemoveAll(g.Dir)
20+
if err != nil {
21+
return fmt.Errorf("removing output directory: %w", err)
22+
}
23+
@@ -49,8 +49,8 @@ type Generator struct {
24+
25+
// Run starts the configured server command, starts to perform crawling,
26+
// and waits for completion before shutting down the configured server.
27+
-func (g *Generator) Run(ctx context.Context) error {
28+
- err := g.Start(ctx)
29+
+func (g *Generator) Run(ctx context.Context, config Config) error {
30+
+ err := g.Start(ctx, config)
31+
if err != nil {
32+
return fmt.Errorf("starting: %w", err)
33+
}

patches/0002-no-file-config.patch

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
diff --git a/cmd/staticgen/main.go b/cmd/staticgen/main.go
2+
index def98a4..410b86b 100644
3+
--- a/cmd/staticgen/main.go
4+
+++ b/cmd/staticgen/main.go
5+
@@ -62,7 +62,40 @@ func main() {
6+
func generateCmd(app *kingpin.Application) {
7+
cmd := app.Command("generate", "Generate static website").Default()
8+
timeout := cmd.Flag("timeout", "Timeout of website generation").Short('t').Default("15m").String()
9+
+ conf := cmd.Flag("config", "Config path").Default("static.json").String()
10+
+ dir := cmd.Flag("directory", "The static website output directory").Default("build").String()
11+
+ url := cmd.Flag("url", "URL is the target website to crawl").Default("http://127.0.0.1:3000").String()
12+
+ pages := cmd.Flag("pages", "Pages is a list of paths added to crawl").Strings()
13+
+ concurrency := cmd.Flag("concurrency", "Concurrency is the number of concurrent pages to crawl").Default("30").Int()
14+
+ allow404 := cmd.Flag("allow404", "Allow404 can be enabled to opt-in to pages resulting in a 404").Default("false").Bool()
15+
+
16+
cmd.Action(func(_ *kingpin.ParseContext) error {
17+
+ var c staticgen.Config
18+
+
19+
+ if err := c.Load(*conf); err != nil {
20+
+ return fmt.Errorf("loading configuration: %w", err)
21+
+ }
22+
+
23+
+ if c.Dir == "build" {
24+
+ c.Dir = *dir
25+
+ }
26+
+
27+
+ if c.URL == "http://127.0.0.1:3000" {
28+
+ c.URL = *url
29+
+ }
30+
+
31+
+ if len(c.Pages) == 0 {
32+
+ c.Pages = *pages
33+
+ }
34+
+
35+
+ if !c.Allow404 {
36+
+ c.Allow404 = *allow404
37+
+ }
38+
+
39+
+ if c.Concurrency == 30 {
40+
+ c.Concurrency = *concurrency
41+
+ }
42+
+
43+
// generator
44+
g := staticgen.Generator{
45+
HTTPClient: client,
46+
@@ -94,7 +127,7 @@ func generateCmd(app *kingpin.Application) {
47+
done := r.Report(events)
48+
49+
// start
50+
- err = g.Run(ctx)
51+
+ err = g.Run(ctx, c)
52+
if err != nil {
53+
return fmt.Errorf("crawling: %w", err)
54+
}
55+
@@ -109,10 +142,17 @@ func generateCmd(app *kingpin.Application) {
56+
func serveCmd(app *kingpin.Application) {
57+
cmd := app.Command("serve", "Serve the generated website")
58+
addr := cmd.Flag("address", "Bind address").Default("localhost:3000").String()
59+
+ dir := cmd.Flag("directory", "The static website output directory").Default("build").String()
60+
+ conf := cmd.Flag("config", "Config path").Default("static.json").String()
61+
+
62+
cmd.Action(func(_ *kingpin.ParseContext) error {
63+
var c staticgen.Config
64+
+ err := c.Load(*conf)
65+
+
66+
+ if c.Dir == "build" {
67+
+ c.Dir = *dir
68+
+ }
69+
70+
- err := c.Load("static.json")
71+
if err != nil {
72+
return fmt.Errorf("loading configuration: %w", err)
73+
}

0 commit comments

Comments
 (0)