Skip to content

Commit

Permalink
update docs to show how to type ctx options
Browse files Browse the repository at this point in the history
  • Loading branch information
arimgibson committed May 13, 2023
1 parent fc2930a commit e9417b1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ task('build', async ctx => {
})
```

And, if using TypeScript, add types to your options through the `task` generic:

```ts
import { task, desc, option, strict } from 'foy'

type BuildOptions = {
watch: boolean
}

desc('Build ts files with tsc')
option('-w, --watch', 'watch file changes')
strict() // This will throw an error if you passed some options that doesn't defined via `option()`
task<BuildOptions>('build', async ctx => { // ctx.options now has type BuildOptions instead of unknown
await ctx.exec(`tsc ${ctx.options.watch ? '-w' : ''}`)
})
```

```sh
foy build -w
```
Expand Down
13 changes: 13 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ <h2 id="write-a-foyfile">Write a Foyfile</h2>
task(&#39;build&#39;, async ctx =&gt; {
await ctx.exec(`tsc ${ctx.options.watch ? &#39;-w&#39; : &#39;&#39;}`)
})</code></pre>
<p>And, if using TypeScript, add types to your options through the <code>task</code> generic:</p>
<pre><code class="language-ts">import { task, desc, option, strict } from &#39;foy&#39;

type BuildOptions = {
watch: boolean
}

desc(&#39;Build ts files with tsc&#39;)
option(&#39;-w, --watch&#39;, &#39;watch file changes&#39;)
strict() // This will throw an error if you passed some options that doesn&#39;t defined via `option()`
task&lt;BuildOptions&gt;(&#39;build&#39;, async ctx =&gt; { // ctx.options now has type BuildOptions instead of unknown
await ctx.exec(`tsc ${ctx.options.watch ? &#39;-w&#39; : &#39;&#39;}`)
})</code></pre>
<pre><code class="language-sh">foy build -w</code></pre>
<p>Warning! If you want to set flags like strict for all tasks, please use <code>setGlobalOptions</code>:</p>
<pre><code class="language-ts">import { setGlobalOptions } from &#39;foy&#39;
Expand Down

0 comments on commit e9417b1

Please sign in to comment.