You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -48,16 +46,12 @@ export function generate(init: Context) {
48
46
}
49
47
```
50
48
51
-
:::
52
-
53
49
```sh
54
50
npx pinion generators/app.tpl.ts
55
51
```
56
52
57
53
The [runGenerators](./api.md#rungenerators) tasks also allows to run all `.tpl.ts` generators in a folder in alphabetical order using the current context.
58
54
59
-
::: code-group
60
-
61
55
```ts [generators/app.tpl.ts]
62
56
// Get current file and directory in an ES module
63
57
import { fileURLToPath } from'node:url'
@@ -108,8 +102,6 @@ export function generate(init: Context) {
108
102
}
109
103
```
110
104
111
-
:::
112
-
113
105
## Embedability
114
106
115
107
Since a generator is just a function that can be called anywhere, Pinion generators can be composed, tested or used programatically e.g. in your own CLI tools just by importing the file, initialising a context and calling the generator:
Similar to an embedded generator, automated tests can be written by initializing the context - usually with a temporary path - and passing all context variables necessary to [skip user prompts](./user-input.md#prompting):
139
131
140
-
::: code-group
141
-
142
132
```ts [tests/readme.tpl.test.ts]
143
133
import { describe, it } from'node:test'
144
134
importassertfrom'node:assert'
@@ -221,8 +211,6 @@ export function generate(init: Context) {
Copy file name to clipboardExpand all lines: content/pinion/docs/generators.md
+2-8Lines changed: 2 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,8 +6,6 @@ outline: deep
6
6
7
7
A Pinion generator is any file that has a `generate` export which takes a context and returns a Promise with that context. In its most basic form a Pinion generator looks like this:
The above example is a bit contrived because we can use TypeScript functions directly in templates. It's not necessary to define variables outside of templates like it is with other generator tools.
96
94
97
95
The concept to be learned is that you can add properties to `context` in the promise and they will be available in the template. This would be more useful if we extracted the function into its own module and reused it in multiple generators.
@@ -102,8 +100,6 @@ What's really cool is that there's no special magic to learn. To make a reusable
102
100
103
101
A task is any step within the `generate` function. Pinion tasks rely on functional programming through `Promise.then` chains. You can follow each step in the generator and they are still bundled as a testable and embedable plain function.
Pinion uses [Inquirer](https://www.npmjs.com/package/inquirer) under the hood to ask questions. The `prompt` task takes an array or object of [Inquirer questions](https://www.npmjs.com/package/inquirer#question) and returns a function that takes a context and returns a promise with the updated context. We can then use this context in the next step to render our template with the answers.
Copy file name to clipboardExpand all lines: content/pinion/docs/templates.md
-12Lines changed: 0 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,8 +16,6 @@ There are three main helpers for working with file pointers:
16
16
17
17
The following example asks for the path the `readme.md` file should be generated in and then puts the path together dynamically based on the context:
18
18
19
-
::: code-group
20
-
21
19
```ts [generators/readme.tpl.ts]
22
20
import {
23
21
PinionContext,
@@ -74,8 +72,6 @@ export function generate(init: Context) {
74
72
}
75
73
```
76
74
77
-
:::
78
-
79
75
```sh
80
76
npx pinion generators/readme.tpl.ts
81
77
```
@@ -91,8 +87,6 @@ While you could use them in your own tasks, Pinion tries to avoid difficult-to-d
91
87
92
88
It also allows composing templates by splitting them up into separate functions, and you have the entire JavaScript ecosystem (like [Lodash](https://lodash.com/)) available as helpers:
Copy file name to clipboardExpand all lines: content/pinion/docs/user-input.md
-8Lines changed: 0 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,8 +10,6 @@ This section shows how to handle user input through prompts and CLI arguments.
10
10
11
11
As [we have seen before](./index.md#asking-questions), the [prompt](./api.md#prompt) task allows to ask questions from the command line using the [Inquirer](https://www.npmjs.com/package/inquirer) library. It gets passed a [list of Inquirer questions](https://www.npmjs.com/package/inquirer#question) that can also be put together based on the current context. This can be used to e.g. ask questions conditionally or skip them if the value is already passed (e.g. in an automated test):
12
12
13
-
::: code-group
14
-
15
13
```ts [generators/readme.tpl.ts]
16
14
import {
17
15
PinionContext,
@@ -63,8 +61,6 @@ export function generate(init: Context) {
`context.argv` would be `['--description', 'hello', 'something']`. You can parse the list yourself and Pinion also ships with a [commander task](./api.md#commander) that uses the `commander` module to create command-line interfaces. The following example adds `--name` and `--description` command line arguments and skips prompting the user if it is passed.
81
77
82
-
::: code-group
83
-
84
78
```ts [generators/readme.tpl.ts]
85
79
import {
86
80
Command,
@@ -141,8 +135,6 @@ export function generate(init: Context) {
141
135
}
142
136
```
143
137
144
-
:::
145
-
146
138
```sh
147
139
npx pinion generators/readme.tpl.ts --name Test --description "The description from the command line"
0 commit comments