Skip to content

Commit

Permalink
fix option parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
tycobbb committed Aug 23, 2022
1 parent 534e51e commit d42d4bc
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 23 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
# www

a "zero-config" static site builder mostly for me. write html to build sites out of html
with minimal tooling and mostly just html.
with minimal tooling and mostly just html. a companion to [www-os](https://github.com/tycobbb/www-os).

it can build your site. it has a dev server that rebuilds as you make changes (though you still have to reload). it has templating (if you want it). it has build-time data (if you want it). it can change the directory it builds your site to!


## install

install [deno](#install-deno), if you haven't already.

install the tool (this is a long one):

```sh
$ deno install --allow-env --allow-run --allow-read --allow-write --allow-net https://deno.land/x/wvvw@0.0.4/www.ts
$ deno install --allow-env --allow-run --allow-read --allow-write --allow-net https://deno.land/x/wvvw@0.0.5/www.ts
```

### deno
Expand All @@ -32,7 +33,7 @@ care about deno.
to run the dev server:

```sh
$ www . --up
$ www --up .
```

to build your site
Expand All @@ -44,7 +45,13 @@ $ www .
to build your site for production:

```sh
$ www . --prod
$ www --prod .
```

to see help:

```sh
$ www --help
```

## structure
Expand All @@ -61,8 +68,9 @@ maybe! i like it a lot. if you really just want to write html and want to repeat

## todos

- [ ] add `w-frag` (see [notes](./notes.md))
- [ ] add `w-slot` (see [notes](./notes.md))
- [ ] init command
- [ ] cli options should be able to come after positional args
- [ ] package a binary
- [ ] ???
- [ ] you [tell me](https://github.com/tycobbb/www/issues)
Expand Down
8 changes: 4 additions & 4 deletions notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ create a build-time include element w/ something like slots. eta's include synta
down on complex partials imo, pretty cumbersome. preprocess:

```html
<w:include path="window" name="nav" top=5>
<w:include
w:slot="body"
<w:frag path="window" name="nav" top=5>
<w:frag
path="nav"
w:slot="body"
/>

<div w:slot="footer">
footer
</div>
</w:include>
</w:frag>
```

into
Expand Down
8 changes: 1 addition & 7 deletions src/App/Config/Services/DecodeConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@ import { assertEquals } from "../../../Test/mod.ts"
const { test } = Deno

// -- tests --
test("it builds to a custom dir with the long arg", async () => {
test("it builds to a custom dir", async () => {
const decode = new DecodeConfig({ _: ["."], dir: "test" })
const cfg = await decode.call()
assertEquals(cfg.paths.dst.str, "test")
})

test("it builds to a custom dir with the short arg", async () => {
const decode = new DecodeConfig({ _: ["."], d: "test" })
const cfg = await decode.call()
assertEquals(cfg.paths.dst.str, "test")
})
4 changes: 2 additions & 2 deletions src/App/Config/Services/DecodeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class DecodeConfig {
// -- c/helpers
#decodeEnv(): Env {
const m = this
if (m.#args["p"] || m.#args["prod"] || Deno.env.get("PROD") != null) {
if (m.#args.prod || Deno.env.get("PROD") != null) {
return Env.Prod
} else {
return Env.Dev
Expand All @@ -54,7 +54,7 @@ export class DecodeConfig {
}

// parse dist path from cmd line args
const dist = Path.base(m.#args["d"] || m.#args["dir"] || "dist")
const dist = Path.base(m.#args.out || "dist")

// produce paths
return new Paths(
Expand Down
30 changes: 30 additions & 0 deletions src/Cli/Cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { assertEquals } from "../Test/mod.ts"
import { Cli } from "./Cli.ts"

// -- setup --
const { test } = Deno

// -- tests --
test("it parses options", () => {
const strs = ["-d", "output", "--verbose", "input", "-h"]
const args = Cli.parse(strs).args

assertEquals(args._, [
"input"
])

assertEquals(args.help, true)
assertEquals(args.h, args.help)

assertEquals(args.out, "output")
assertEquals(args.o, args.out)

assertEquals(args.prod, false)
assertEquals(args.p, args.prod)

assertEquals(args.up, false)
assertEquals(args.u, args.up)

assertEquals(args.verbose, true)
assertEquals(args.v, args.verbose)
})
24 changes: 19 additions & 5 deletions src/Cli/Cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class Cli {
options:
-u, --up runs as a dev server
-p, --prod runs production mode
-d, --dir <p> sets the output path
-o, --out <p> sets the output path
-v, --verbose prints more logs
-h, --help prints this message
Expand Down Expand Up @@ -137,17 +137,17 @@ export class Cli {

// if the `h/help` flag is set
get isHelp(): boolean {
return this.#args.h || this.#args.help
return this.#args.help
}

// if the `v/verbose` flag is set
get isVerbose(): boolean {
return this.#args.v || this.args.verbose
return this.#args.verbose
}

// if the `u/up` flag is set
get isServerUp(): boolean {
return this.#args.u || this.#args.up
return this.#args.up
}

// draws a heredoc string; trims whitespace and leading padding
Expand Down Expand Up @@ -178,6 +178,20 @@ export class Cli {
// -- factories --
// build the cli from raw cmd line args
static parse(args: string[]) {
return new Cli(parse(args))
return new Cli(parse(args, {
alias: {
"h": "help",
"o": "out",
"p": "prod",
"u": "up",
"v": "verbose",
},
boolean: [
"help",
"prod",
"up",
"verbose",
],
}))
}
}

0 comments on commit d42d4bc

Please sign in to comment.