n8n-docgen 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +142 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +190 -0
- package/dist/describe.d.ts +37 -0
- package/dist/describe.js +223 -0
- package/dist/generate.d.ts +35 -0
- package/dist/generate.js +120 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +6 -0
- package/dist/markers.d.ts +8 -0
- package/dist/markers.js +24 -0
- package/dist/mermaid.d.ts +4 -0
- package/dist/mermaid.js +69 -0
- package/dist/render.d.ts +27 -0
- package/dist/render.js +109 -0
- package/dist/workflow.d.ts +47 -0
- package/dist/workflow.js +49 -0
- package/package.json +57 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Avram Aruh
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,143 @@
|
|
|
1
|
-
#
|
|
1
|
+
# n8n-docgen
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Readable n8n workflows in git.** Turn every exported workflow JSON into a Markdown page with a Mermaid diagram, its triggers, credentials and execution settings, and fail CI when the docs fall behind.
|
|
4
|
+
|
|
5
|
+
[](https://github.com/avramaruh92/n8n-docgen/actions/workflows/ci.yml)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
|
|
8
|
+
Runs locally and in GitHub Actions. No network, no telemetry, no n8n instance needed.
|
|
9
|
+
|
|
10
|
+
## Quick start
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
# Pages for every workflow JSON under workflows/, written to docs/workflows/
|
|
14
|
+
npx n8n-docgen workflows
|
|
15
|
+
|
|
16
|
+
# In CI: exit 1 when a page is missing or out of date
|
|
17
|
+
npx n8n-docgen workflows --check
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Needs Node 22.18 or newer. Works with the editor's "Download" file and with `n8n export:workflow` (one workflow per file, or `--all` in one file).
|
|
21
|
+
|
|
22
|
+
## What you get
|
|
23
|
+
|
|
24
|
+
One page per workflow and an index ([see the examples](examples/docs/README.md)). Here is the top of [`daily-sales-report.md`](examples/docs/daily-sales-report.md):
|
|
25
|
+
|
|
26
|
+
> # Daily sales report
|
|
27
|
+
>
|
|
28
|
+
> | | |
|
|
29
|
+
> | --- | --- |
|
|
30
|
+
> | Active | Yes |
|
|
31
|
+
> | Nodes | 7 |
|
|
32
|
+
> | Tags | reports, sales |
|
|
33
|
+
> | Error workflow | [Error alert](examples/docs/error-alert.md) |
|
|
34
|
+
|
|
35
|
+
```mermaid
|
|
36
|
+
flowchart LR
|
|
37
|
+
n1(["Every morning"])
|
|
38
|
+
n2["Get yesterday's orders"]
|
|
39
|
+
n3["Any orders?"]
|
|
40
|
+
n4["Sum by product"]
|
|
41
|
+
n5["Post to #35;sales"]
|
|
42
|
+
n6["Post #quot;no sales#quot;"]
|
|
43
|
+
n7["Old email report"]
|
|
44
|
+
n1 --> n2
|
|
45
|
+
n2 --> n3
|
|
46
|
+
n3 -->|true| n4
|
|
47
|
+
n3 -->|false| n6
|
|
48
|
+
n4 --> n5
|
|
49
|
+
n6 --> n7
|
|
50
|
+
classDef disabled stroke-dasharray: 5 5,opacity:0.5
|
|
51
|
+
class n7 disabled
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
> **Triggers:** Every morning (`scheduleTrigger`): Every day at 07:30; Cron `0 18 * * 5`
|
|
55
|
+
>
|
|
56
|
+
> | Credential | Type | Used by |
|
|
57
|
+
> | --- | --- | --- |
|
|
58
|
+
> | Shop API | `httpHeaderAuth` | Get yesterday's orders |
|
|
59
|
+
> | Team Slack | `slackApi` | Post to #sales, Post "no sales" |
|
|
60
|
+
|
|
61
|
+
Each page has:
|
|
62
|
+
|
|
63
|
+
- **Flow**: a Mermaid diagram GitHub and GitLab render in place. Triggers are rounded, IF branches say true and false, error outputs say error, AI models, memory and tools hang off their agent with dotted lines, disabled nodes are dashed.
|
|
64
|
+
- **Triggers**: schedules in plain words (`Every week on Mon, Thu at 09:00`), cron expressions, webhook methods and paths, polling intervals, chat, form, error and sub-workflow triggers.
|
|
65
|
+
- **Credentials**: name, type and the nodes that use them. Credential IDs are never read.
|
|
66
|
+
- **Error workflow**: set or not, linked to its page when it lives in the same folder.
|
|
67
|
+
- **Execution data**: saved successful, failed and manual runs, progress, timeout, timezone, who can call it.
|
|
68
|
+
- **Warnings**: pinned data and owner data in the export, both of which can carry real records and emails into git.
|
|
69
|
+
- **Nodes** with their types and notes, and the canvas's **sticky notes**.
|
|
70
|
+
|
|
71
|
+
## Your text stays
|
|
72
|
+
|
|
73
|
+
Generated text sits between two marker comments. Write above or below them: a runbook, links, who owns the workflow. Every run replaces only what is between the markers.
|
|
74
|
+
|
|
75
|
+
```markdown
|
|
76
|
+
## Runbook
|
|
77
|
+
Restart the shop sync first.
|
|
78
|
+
|
|
79
|
+
<!-- n8n-docgen:start (generated: edits between these markers are replaced) -->
|
|
80
|
+
...
|
|
81
|
+
<!-- n8n-docgen:end -->
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## GitHub Action
|
|
85
|
+
|
|
86
|
+
Fail the pull request when someone exports a workflow and forgets the docs:
|
|
87
|
+
|
|
88
|
+
```yaml
|
|
89
|
+
- uses: actions/checkout@v5
|
|
90
|
+
- uses: avramaruh92/n8n-docgen@v1.0.0
|
|
91
|
+
with:
|
|
92
|
+
path: workflows # where the JSON files are
|
|
93
|
+
out: docs/workflows # where the pages go
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Or write the pages in CI and commit them with the step of your choice:
|
|
97
|
+
|
|
98
|
+
```yaml
|
|
99
|
+
- uses: avramaruh92/n8n-docgen@v1.0.0
|
|
100
|
+
with:
|
|
101
|
+
path: workflows
|
|
102
|
+
mode: write
|
|
103
|
+
- uses: stefanzweifel/git-auto-commit-action@v5
|
|
104
|
+
with:
|
|
105
|
+
commit_message: "docs: update n8n workflow pages"
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Inputs: `path`, `out`, `mode` (`check` or `write`), `scrub`, `deny`, `keep`, `title`.
|
|
109
|
+
|
|
110
|
+
## Scrub before you publish
|
|
111
|
+
|
|
112
|
+
Public repo, private infrastructure? `--scrub` runs every page through [pasteproof](https://github.com/avramaruh92/pasteproof) and swaps hostnames, IPs, emails, URL tokens and webhook paths for consistent placeholders (`HOST_1`, `PRIVATE_2`). Add your own names and domains with `--deny`.
|
|
113
|
+
|
|
114
|
+
<!-- pasteproof-ignore-start -->
|
|
115
|
+
```bash
|
|
116
|
+
npx n8n-docgen workflows --scrub --deny mycompany,mydomain.com
|
|
117
|
+
```
|
|
118
|
+
<!-- pasteproof-ignore-end -->
|
|
119
|
+
|
|
120
|
+
In the Action, pass the deny list as a secret: `scrub: "true"` and `deny: ${{ secrets.DOCGEN_DENY }}`.
|
|
121
|
+
|
|
122
|
+
## Options
|
|
123
|
+
|
|
124
|
+
| Option | Default | |
|
|
125
|
+
| --- | --- | --- |
|
|
126
|
+
| `[folder]` | `.` | Folder with workflow JSON files, searched recursively. Other JSON files are skipped. |
|
|
127
|
+
| `-o, --out` | `docs/workflows` | Where the pages go. Subfolders are mirrored. |
|
|
128
|
+
| `--check` | | Write nothing; exit 1 when a page is missing or out of date. |
|
|
129
|
+
| `--format github` | | With `--check`: GitHub annotations. |
|
|
130
|
+
| `--scrub` | | Replace private identifiers (see above). |
|
|
131
|
+
| `--deny`, `--keep` | | With `--scrub`: words to replace, values to leave alone. |
|
|
132
|
+
| `--index` | `README.md` | Index page name, so GitHub shows it when you open the folder. |
|
|
133
|
+
| `--title` | `n8n workflows` | Index page title. |
|
|
134
|
+
|
|
135
|
+
Exit codes: 0 ok, 1 docs out of date, 2 usage or input error.
|
|
136
|
+
|
|
137
|
+
## Versions
|
|
138
|
+
|
|
139
|
+
Patch releases fix output, minor releases add sections, and only a major release changes flags, exit codes or the marker format. Pin a version in CI and update on your own schedule.
|
|
140
|
+
|
|
141
|
+
## Licence
|
|
142
|
+
|
|
143
|
+
MIT. n8n-docgen is an independent project, not affiliated with or endorsed by n8n GmbH.
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { relative } from "node:path";
|
|
4
|
+
import { plan, write } from "./generate.js";
|
|
5
|
+
const HELP = `n8n-docgen: readable n8n workflows in git
|
|
6
|
+
|
|
7
|
+
Usage:
|
|
8
|
+
n8n-docgen [folder] [options]
|
|
9
|
+
|
|
10
|
+
Writes a Markdown page with a Mermaid diagram for every exported workflow
|
|
11
|
+
JSON in [folder] (default: .), plus an index page. Text outside the
|
|
12
|
+
n8n-docgen marker comments is kept.
|
|
13
|
+
|
|
14
|
+
Options:
|
|
15
|
+
-o, --out <folder> Where pages go (default: docs/workflows)
|
|
16
|
+
--check Write nothing; exit 1 when pages are missing or out of date
|
|
17
|
+
--scrub Replace hostnames, IPs, emails, URL tokens and webhook
|
|
18
|
+
paths with placeholders (uses pasteproof)
|
|
19
|
+
--deny <a,b> With --scrub: more words to replace (names, domains)
|
|
20
|
+
--keep <a,b> With --scrub: values to leave alone
|
|
21
|
+
--index <file> Index page name inside --out (default: README.md)
|
|
22
|
+
--title <text> Index page title (default: n8n workflows)
|
|
23
|
+
--format github With --check: print GitHub annotations
|
|
24
|
+
-q, --quiet Print errors only
|
|
25
|
+
-h, --help Show this help
|
|
26
|
+
-v, --version Show the version
|
|
27
|
+
|
|
28
|
+
Exit codes: 0 ok, 1 docs out of date (--check), 2 usage or input error.
|
|
29
|
+
`;
|
|
30
|
+
function list(v) {
|
|
31
|
+
return v
|
|
32
|
+
.split(",")
|
|
33
|
+
.map((s) => s.trim())
|
|
34
|
+
.filter(Boolean);
|
|
35
|
+
}
|
|
36
|
+
function parse(argv) {
|
|
37
|
+
const a = {
|
|
38
|
+
input: ".",
|
|
39
|
+
out: "docs/workflows",
|
|
40
|
+
check: false,
|
|
41
|
+
scrub: false,
|
|
42
|
+
deny: [],
|
|
43
|
+
keep: [],
|
|
44
|
+
index: "README.md",
|
|
45
|
+
title: "n8n workflows",
|
|
46
|
+
github: false,
|
|
47
|
+
quiet: false,
|
|
48
|
+
};
|
|
49
|
+
let positional = 0;
|
|
50
|
+
for (let i = 0; i < argv.length; i++) {
|
|
51
|
+
const raw = argv[i];
|
|
52
|
+
const eq = raw.startsWith("--") ? raw.indexOf("=") : -1;
|
|
53
|
+
const flag = eq > 0 ? raw.slice(0, eq) : raw;
|
|
54
|
+
const value = () => {
|
|
55
|
+
if (eq > 0)
|
|
56
|
+
return raw.slice(eq + 1);
|
|
57
|
+
const v = argv[++i];
|
|
58
|
+
if (v === undefined)
|
|
59
|
+
throw new Error(`${flag} needs a value`);
|
|
60
|
+
return v;
|
|
61
|
+
};
|
|
62
|
+
switch (flag) {
|
|
63
|
+
case "-h":
|
|
64
|
+
case "--help":
|
|
65
|
+
return "help";
|
|
66
|
+
case "-v":
|
|
67
|
+
case "--version":
|
|
68
|
+
return "version";
|
|
69
|
+
case "-o":
|
|
70
|
+
case "--out":
|
|
71
|
+
a.out = value();
|
|
72
|
+
break;
|
|
73
|
+
case "--check":
|
|
74
|
+
a.check = true;
|
|
75
|
+
break;
|
|
76
|
+
case "--scrub":
|
|
77
|
+
a.scrub = true;
|
|
78
|
+
break;
|
|
79
|
+
case "--deny":
|
|
80
|
+
a.deny.push(...list(value()));
|
|
81
|
+
break;
|
|
82
|
+
case "--keep":
|
|
83
|
+
a.keep.push(...list(value()));
|
|
84
|
+
break;
|
|
85
|
+
case "--index":
|
|
86
|
+
a.index = value();
|
|
87
|
+
break;
|
|
88
|
+
case "--title":
|
|
89
|
+
a.title = value();
|
|
90
|
+
break;
|
|
91
|
+
case "--format": {
|
|
92
|
+
const f = value();
|
|
93
|
+
if (f !== "github" && f !== "text")
|
|
94
|
+
throw new Error(`unknown format: ${f}`);
|
|
95
|
+
a.github = f === "github";
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
case "-q":
|
|
99
|
+
case "--quiet":
|
|
100
|
+
a.quiet = true;
|
|
101
|
+
break;
|
|
102
|
+
default:
|
|
103
|
+
if (flag.startsWith("-"))
|
|
104
|
+
throw new Error(`unknown option: ${flag}`);
|
|
105
|
+
if (positional++ > 0)
|
|
106
|
+
throw new Error(`one folder only (got ${flag})`);
|
|
107
|
+
a.input = flag;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (process.env.N8N_DOCGEN_DENY)
|
|
111
|
+
a.deny.push(...list(process.env.N8N_DOCGEN_DENY));
|
|
112
|
+
if (!a.index || /[\\/]/.test(a.index))
|
|
113
|
+
throw new Error("--index is a file name, not a path");
|
|
114
|
+
return a;
|
|
115
|
+
}
|
|
116
|
+
function version() {
|
|
117
|
+
const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
118
|
+
return pkg.version;
|
|
119
|
+
}
|
|
120
|
+
async function main(argv) {
|
|
121
|
+
let args;
|
|
122
|
+
try {
|
|
123
|
+
args = parse(argv);
|
|
124
|
+
}
|
|
125
|
+
catch (e) {
|
|
126
|
+
process.stderr.write(`n8n-docgen: ${e.message}\nRun n8n-docgen --help for usage.\n`);
|
|
127
|
+
return 2;
|
|
128
|
+
}
|
|
129
|
+
if (args === "help") {
|
|
130
|
+
process.stdout.write(HELP);
|
|
131
|
+
return 0;
|
|
132
|
+
}
|
|
133
|
+
if (args === "version") {
|
|
134
|
+
process.stdout.write(`${version()}\n`);
|
|
135
|
+
return 0;
|
|
136
|
+
}
|
|
137
|
+
const log = (s) => {
|
|
138
|
+
if (!args.quiet)
|
|
139
|
+
process.stderr.write(`${s}\n`);
|
|
140
|
+
};
|
|
141
|
+
let p;
|
|
142
|
+
try {
|
|
143
|
+
p = await plan({
|
|
144
|
+
input: args.input,
|
|
145
|
+
out: args.out,
|
|
146
|
+
index: args.index,
|
|
147
|
+
title: args.title,
|
|
148
|
+
scrub: args.scrub,
|
|
149
|
+
deny: args.deny,
|
|
150
|
+
keep: args.keep,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
catch (e) {
|
|
154
|
+
process.stderr.write(`n8n-docgen: ${e.message}\n`);
|
|
155
|
+
return 2;
|
|
156
|
+
}
|
|
157
|
+
for (const err of p.errors)
|
|
158
|
+
process.stderr.write(`n8n-docgen: cannot read ${err}\n`);
|
|
159
|
+
if (p.skipped.length > 0)
|
|
160
|
+
log(`skipped ${p.skipped.length} JSON file${p.skipped.length === 1 ? "" : "s"} with no workflow`);
|
|
161
|
+
if (p.workflows === 0) {
|
|
162
|
+
process.stderr.write(`n8n-docgen: no workflow JSON found in ${args.input}\n`);
|
|
163
|
+
return 2;
|
|
164
|
+
}
|
|
165
|
+
const rel = (path) => relative(process.cwd(), path).split("\\").join("/");
|
|
166
|
+
const stale = p.files.filter((f) => f.state !== "same");
|
|
167
|
+
if (args.check) {
|
|
168
|
+
for (const f of stale) {
|
|
169
|
+
const why = f.state === "new" ? "missing" : "out of date";
|
|
170
|
+
if (args.github) {
|
|
171
|
+
process.stdout.write(`::error file=${rel(f.path)},title=n8n-docgen::${why}, run n8n-docgen to update\n`);
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
process.stdout.write(`${rel(f.path)}: ${why}\n`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (stale.length > 0) {
|
|
178
|
+
log(`${stale.length} of ${p.files.length} page${p.files.length === 1 ? "" : "s"} need an update: run n8n-docgen ${args.input} --out ${args.out}${args.scrub ? " --scrub" : ""}`);
|
|
179
|
+
return p.errors.length > 0 ? 2 : 1;
|
|
180
|
+
}
|
|
181
|
+
log(`${p.workflows} workflow${p.workflows === 1 ? "" : "s"}, docs up to date`);
|
|
182
|
+
return p.errors.length > 0 ? 2 : 0;
|
|
183
|
+
}
|
|
184
|
+
const written = write(p);
|
|
185
|
+
for (const f of stale)
|
|
186
|
+
log(`${f.state === "new" ? "wrote" : "updated"} ${rel(f.path)}`);
|
|
187
|
+
log(`${p.workflows} workflow${p.workflows === 1 ? "" : "s"}, ${written} page${written === 1 ? "" : "s"} written`);
|
|
188
|
+
return p.errors.length > 0 ? 2 : 0;
|
|
189
|
+
}
|
|
190
|
+
process.exitCode = await main(process.argv.slice(2));
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type N8nNode, type N8nWorkflow } from "./workflow.ts";
|
|
2
|
+
export interface TriggerInfo {
|
|
3
|
+
node: string;
|
|
4
|
+
type: string;
|
|
5
|
+
/** One line per schedule rule or webhook; empty for plain triggers. */
|
|
6
|
+
details: string[];
|
|
7
|
+
kind: "schedule" | "webhook" | "manual" | "workflow" | "error" | "chat" | "form" | "poll" | "event";
|
|
8
|
+
}
|
|
9
|
+
export interface CredentialInfo {
|
|
10
|
+
name: string;
|
|
11
|
+
type: string;
|
|
12
|
+
nodes: string[];
|
|
13
|
+
}
|
|
14
|
+
export interface SettingInfo {
|
|
15
|
+
label: string;
|
|
16
|
+
value: string;
|
|
17
|
+
}
|
|
18
|
+
export interface Description {
|
|
19
|
+
triggers: TriggerInfo[];
|
|
20
|
+
credentials: CredentialInfo[];
|
|
21
|
+
settings: SettingInfo[];
|
|
22
|
+
errorWorkflowId: string | undefined;
|
|
23
|
+
warnings: string[];
|
|
24
|
+
/** Webhook and form paths, for --scrub. */
|
|
25
|
+
webhookPaths: string[];
|
|
26
|
+
}
|
|
27
|
+
/** Schedule Trigger (`rule.interval`), n8n 1.x and later. */
|
|
28
|
+
export declare function describeScheduleRules(parameters: Record<string, unknown>): string[];
|
|
29
|
+
/** Legacy Cron node (`triggerTimes`) and polling triggers (`pollTimes`). */
|
|
30
|
+
export declare function describeTimeItems(times: unknown): string[];
|
|
31
|
+
export declare function triggers(wf: N8nWorkflow): TriggerInfo[];
|
|
32
|
+
export declare function isTriggerNode(node: N8nNode): boolean;
|
|
33
|
+
/** Names and types only. Credential IDs are never read. */
|
|
34
|
+
export declare function credentials(wf: N8nWorkflow): CredentialInfo[];
|
|
35
|
+
export declare function settings(wf: N8nWorkflow): SettingInfo[];
|
|
36
|
+
export declare function warnings(wf: N8nWorkflow): string[];
|
|
37
|
+
export declare function describe(wf: N8nWorkflow): Description;
|
package/dist/describe.js
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
// Turns a workflow into plain facts: triggers and schedules, credentials,
|
|
2
|
+
// execution settings and warnings. No Markdown here; render.ts formats it.
|
|
3
|
+
import { isStickyNote, shortType } from "./workflow.js";
|
|
4
|
+
const DAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
5
|
+
function str(v) {
|
|
6
|
+
if (typeof v === "string" && v.length > 0)
|
|
7
|
+
return v;
|
|
8
|
+
if (typeof v === "number" && Number.isFinite(v))
|
|
9
|
+
return String(v);
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
function num(v, fallback) {
|
|
13
|
+
if (typeof v === "number" && Number.isFinite(v))
|
|
14
|
+
return v;
|
|
15
|
+
if (typeof v === "string" && v.length > 0)
|
|
16
|
+
return v; // an expression
|
|
17
|
+
return fallback;
|
|
18
|
+
}
|
|
19
|
+
function time(hour, minute) {
|
|
20
|
+
const h = num(hour, 0);
|
|
21
|
+
const m = num(minute, 0);
|
|
22
|
+
if (typeof h === "number" && typeof m === "number") {
|
|
23
|
+
return `${String(h).padStart(2, "0")}:${String(m).padStart(2, "0")}`;
|
|
24
|
+
}
|
|
25
|
+
return `${h}:${m}`;
|
|
26
|
+
}
|
|
27
|
+
function every(n, unit) {
|
|
28
|
+
return n === 1 ? `Every ${unit}` : `Every ${n} ${unit}s`;
|
|
29
|
+
}
|
|
30
|
+
function weekdays(v) {
|
|
31
|
+
const list = Array.isArray(v) ? v : v === undefined ? [0] : [v];
|
|
32
|
+
return list.map((d) => (typeof d === "number" && DAYS[d] ? DAYS[d] : String(d))).join(", ");
|
|
33
|
+
}
|
|
34
|
+
function asRecord(v) {
|
|
35
|
+
return typeof v === "object" && v !== null && !Array.isArray(v) ? v : {};
|
|
36
|
+
}
|
|
37
|
+
/** Schedule Trigger (`rule.interval`), n8n 1.x and later. */
|
|
38
|
+
export function describeScheduleRules(parameters) {
|
|
39
|
+
const rule = asRecord(parameters.rule);
|
|
40
|
+
const intervals = Array.isArray(rule.interval) && rule.interval.length > 0 ? rule.interval : [{}];
|
|
41
|
+
return intervals.map((raw) => {
|
|
42
|
+
const r = asRecord(raw);
|
|
43
|
+
const field = str(r.field) ?? "days";
|
|
44
|
+
switch (field) {
|
|
45
|
+
case "seconds":
|
|
46
|
+
return every(num(r.secondsInterval, 30), "second");
|
|
47
|
+
case "minutes":
|
|
48
|
+
return every(num(r.minutesInterval, 5), "minute");
|
|
49
|
+
case "hours":
|
|
50
|
+
return `${every(num(r.hoursInterval, 1), "hour")} at minute ${num(r.triggerAtMinute, 0)}`;
|
|
51
|
+
case "days":
|
|
52
|
+
return `${every(num(r.daysInterval, 1), "day")} at ${time(r.triggerAtHour, r.triggerAtMinute)}`;
|
|
53
|
+
case "weeks":
|
|
54
|
+
return `${every(num(r.weeksInterval, 1), "week")} on ${weekdays(r.triggerAtDay)} at ${time(r.triggerAtHour, r.triggerAtMinute)}`;
|
|
55
|
+
case "months":
|
|
56
|
+
return `${every(num(r.monthsInterval, 1), "month")} on day ${num(r.triggerAtDayOfMonth, 1)} at ${time(r.triggerAtHour, r.triggerAtMinute)}`;
|
|
57
|
+
case "cronExpression":
|
|
58
|
+
return `Cron \`${str(r.expression) ?? "?"}\``;
|
|
59
|
+
default:
|
|
60
|
+
return `Every ${field}`;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/** Legacy Cron node (`triggerTimes`) and polling triggers (`pollTimes`). */
|
|
65
|
+
export function describeTimeItems(times) {
|
|
66
|
+
const items = asRecord(times).item;
|
|
67
|
+
if (!Array.isArray(items) || items.length === 0)
|
|
68
|
+
return [];
|
|
69
|
+
return items.map((raw) => {
|
|
70
|
+
const t = asRecord(raw);
|
|
71
|
+
const mode = str(t.mode) ?? "everyDay";
|
|
72
|
+
switch (mode) {
|
|
73
|
+
case "everyMinute":
|
|
74
|
+
return "Every minute";
|
|
75
|
+
case "everyHour":
|
|
76
|
+
return `Every hour at minute ${num(t.minute, 0)}`;
|
|
77
|
+
case "everyDay":
|
|
78
|
+
return `Every day at ${time(t.hour, t.minute)}`;
|
|
79
|
+
case "everyWeek":
|
|
80
|
+
return `Every week on ${weekdays(t.weekday ?? 1)} at ${time(t.hour, t.minute)}`;
|
|
81
|
+
case "everyMonth":
|
|
82
|
+
return `Every month on day ${num(t.dayOfMonth, 1)} at ${time(t.hour, t.minute)}`;
|
|
83
|
+
case "everyX": {
|
|
84
|
+
const unit = (str(t.unit) ?? "hours").replace(/s$/, "");
|
|
85
|
+
return every(num(t.value, 2), unit);
|
|
86
|
+
}
|
|
87
|
+
case "custom":
|
|
88
|
+
return `Cron \`${str(t.cronExpression) ?? "?"}\``;
|
|
89
|
+
default:
|
|
90
|
+
return mode;
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
function triggerOf(node) {
|
|
95
|
+
const type = shortType(node.type);
|
|
96
|
+
const p = node.parameters ?? {};
|
|
97
|
+
const base = { node: node.name, type };
|
|
98
|
+
switch (type) {
|
|
99
|
+
case "scheduleTrigger":
|
|
100
|
+
return { ...base, kind: "schedule", details: describeScheduleRules(p) };
|
|
101
|
+
case "cron":
|
|
102
|
+
return { ...base, kind: "schedule", details: describeTimeItems(p.triggerTimes) };
|
|
103
|
+
case "interval": {
|
|
104
|
+
const unit = (str(p.unit) ?? "seconds").replace(/s$/, "");
|
|
105
|
+
return { ...base, kind: "schedule", details: [every(num(p.interval, 1), unit)] };
|
|
106
|
+
}
|
|
107
|
+
case "webhook": {
|
|
108
|
+
const method = str(p.httpMethod) ?? "GET";
|
|
109
|
+
const path = str(p.path);
|
|
110
|
+
return { ...base, kind: "webhook", details: [path ? `${method} \`${path}\`` : method] };
|
|
111
|
+
}
|
|
112
|
+
case "formTrigger": {
|
|
113
|
+
const path = str(p.path);
|
|
114
|
+
return { ...base, kind: "form", details: path ? [`Form at \`${path}\``] : [] };
|
|
115
|
+
}
|
|
116
|
+
case "manualTrigger":
|
|
117
|
+
case "start":
|
|
118
|
+
return { ...base, kind: "manual", details: [] };
|
|
119
|
+
case "executeWorkflowTrigger":
|
|
120
|
+
return { ...base, kind: "workflow", details: ["Called by another workflow"] };
|
|
121
|
+
case "errorTrigger":
|
|
122
|
+
return { ...base, kind: "error", details: ["Runs when a workflow that names this one as its Error Workflow fails"] };
|
|
123
|
+
case "chatTrigger":
|
|
124
|
+
return { ...base, kind: "chat", details: ["Chat message"] };
|
|
125
|
+
default:
|
|
126
|
+
if (!/trigger$/i.test(type))
|
|
127
|
+
return undefined;
|
|
128
|
+
if (p.pollTimes !== undefined)
|
|
129
|
+
return { ...base, kind: "poll", details: describeTimeItems(p.pollTimes).map((d) => `Polls: ${d}`) };
|
|
130
|
+
return { ...base, kind: "event", details: ["Event"] };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
export function triggers(wf) {
|
|
134
|
+
const out = [];
|
|
135
|
+
for (const node of wf.nodes) {
|
|
136
|
+
if (isStickyNote(node))
|
|
137
|
+
continue;
|
|
138
|
+
const t = triggerOf(node);
|
|
139
|
+
if (t)
|
|
140
|
+
out.push(t);
|
|
141
|
+
}
|
|
142
|
+
return out;
|
|
143
|
+
}
|
|
144
|
+
export function isTriggerNode(node) {
|
|
145
|
+
return triggerOf(node) !== undefined;
|
|
146
|
+
}
|
|
147
|
+
/** Names and types only. Credential IDs are never read. */
|
|
148
|
+
export function credentials(wf) {
|
|
149
|
+
const byKey = new Map();
|
|
150
|
+
for (const node of wf.nodes) {
|
|
151
|
+
for (const [type, ref] of Object.entries(node.credentials ?? {})) {
|
|
152
|
+
const name = typeof ref?.name === "string" && ref.name ? ref.name : "(unnamed)";
|
|
153
|
+
const key = `${type}\u0000${name}`;
|
|
154
|
+
const entry = byKey.get(key) ?? { name, type, nodes: [] };
|
|
155
|
+
if (!entry.nodes.includes(node.name))
|
|
156
|
+
entry.nodes.push(node.name);
|
|
157
|
+
byKey.set(key, entry);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return [...byKey.values()].sort((a, b) => a.name.localeCompare(b.name) || a.type.localeCompare(b.type));
|
|
161
|
+
}
|
|
162
|
+
function saveSetting(v) {
|
|
163
|
+
if (v === "all" || v === true)
|
|
164
|
+
return "Saved";
|
|
165
|
+
if (v === "none" || v === false)
|
|
166
|
+
return "Not saved";
|
|
167
|
+
return "Instance default";
|
|
168
|
+
}
|
|
169
|
+
const CALLER_POLICY = {
|
|
170
|
+
any: "Any workflow",
|
|
171
|
+
none: "No workflows",
|
|
172
|
+
workflowsFromSameOwner: "Workflows from the same owner",
|
|
173
|
+
workflowsFromAList: "Selected workflows",
|
|
174
|
+
};
|
|
175
|
+
export function settings(wf) {
|
|
176
|
+
const s = wf.settings ?? {};
|
|
177
|
+
const timeout = s.executionTimeout;
|
|
178
|
+
const tz = str(s.timezone);
|
|
179
|
+
const caller = str(s.callerPolicy);
|
|
180
|
+
return [
|
|
181
|
+
{ label: "Successful runs", value: saveSetting(s.saveDataSuccessExecution) },
|
|
182
|
+
{ label: "Failed runs", value: saveSetting(s.saveDataErrorExecution) },
|
|
183
|
+
{ label: "Manual runs", value: saveSetting(s.saveManualExecutions) },
|
|
184
|
+
{ label: "Progress of each node", value: saveSetting(s.saveExecutionProgress) },
|
|
185
|
+
{
|
|
186
|
+
label: "Timeout",
|
|
187
|
+
value: typeof timeout === "number" && timeout > 0 ? `${timeout} s` : "None",
|
|
188
|
+
},
|
|
189
|
+
{ label: "Timezone", value: tz && tz !== "DEFAULT" ? tz : "Instance default" },
|
|
190
|
+
{ label: "Can be called by", value: caller ? (CALLER_POLICY[caller] ?? caller) : "Instance default" },
|
|
191
|
+
];
|
|
192
|
+
}
|
|
193
|
+
const OWNER_KEYS = ["shared", "homeProject", "ownedBy", "owner", "createdBy", "updatedBy"];
|
|
194
|
+
export function warnings(wf) {
|
|
195
|
+
const out = [];
|
|
196
|
+
const pinned = Object.keys(wf.pinData ?? {});
|
|
197
|
+
if (pinned.length > 0) {
|
|
198
|
+
out.push(`Pinned data on ${pinned.length} node${pinned.length === 1 ? "" : "s"} (${pinned.join(", ")}). Pinned test data is exported with the workflow and can hold real records. Unpin it before you commit.`);
|
|
199
|
+
}
|
|
200
|
+
const owner = OWNER_KEYS.filter((k) => {
|
|
201
|
+
const v = wf[k];
|
|
202
|
+
return v !== undefined && v !== null && !(Array.isArray(v) && v.length === 0);
|
|
203
|
+
});
|
|
204
|
+
if (owner.length > 0) {
|
|
205
|
+
out.push(`Owner data in the export (${owner.map((k) => `\`${k}\``).join(", ")}). It can hold user names and emails. Export without it or strip it before you commit.`);
|
|
206
|
+
}
|
|
207
|
+
return out;
|
|
208
|
+
}
|
|
209
|
+
export function describe(wf) {
|
|
210
|
+
const t = triggers(wf);
|
|
211
|
+
const webhookPaths = wf.nodes
|
|
212
|
+
.filter((n) => ["webhook", "formTrigger"].includes(shortType(n.type)))
|
|
213
|
+
.map((n) => str(n.parameters?.path))
|
|
214
|
+
.filter((p) => p !== undefined && !p.startsWith("="));
|
|
215
|
+
return {
|
|
216
|
+
triggers: t,
|
|
217
|
+
credentials: credentials(wf),
|
|
218
|
+
settings: settings(wf),
|
|
219
|
+
errorWorkflowId: str(wf.settings?.errorWorkflow),
|
|
220
|
+
warnings: warnings(wf),
|
|
221
|
+
webhookPaths,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export interface Options {
|
|
2
|
+
/** Folder with workflow JSON files (searched recursively). */
|
|
3
|
+
input: string;
|
|
4
|
+
/** Folder for the Markdown pages. */
|
|
5
|
+
out: string;
|
|
6
|
+
/** File name of the index page inside `out`. */
|
|
7
|
+
index?: string;
|
|
8
|
+
/** Title of the index page. */
|
|
9
|
+
title?: string;
|
|
10
|
+
/** Run generated text through pasteproof's scrubber. */
|
|
11
|
+
scrub?: boolean;
|
|
12
|
+
deny?: string[];
|
|
13
|
+
keep?: string[];
|
|
14
|
+
/** Base for paths shown on pages (default: the current folder). */
|
|
15
|
+
cwd?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface PlannedFile {
|
|
18
|
+
/** Absolute path. */
|
|
19
|
+
path: string;
|
|
20
|
+
content: string;
|
|
21
|
+
/** "new", "changed" or "same". */
|
|
22
|
+
state: "new" | "changed" | "same";
|
|
23
|
+
}
|
|
24
|
+
export interface Plan {
|
|
25
|
+
files: PlannedFile[];
|
|
26
|
+
workflows: number;
|
|
27
|
+
/** JSON files that hold no workflow, relative to cwd. */
|
|
28
|
+
skipped: string[];
|
|
29
|
+
/** Files that could not be parsed, with the reason. */
|
|
30
|
+
errors: string[];
|
|
31
|
+
}
|
|
32
|
+
export declare function findJsonFiles(dir: string, exclude?: string[]): string[];
|
|
33
|
+
export declare function slug(text: string): string;
|
|
34
|
+
export declare function plan(options: Options): Promise<Plan>;
|
|
35
|
+
export declare function write(p: Plan): number;
|
package/dist/generate.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// Reads a folder of workflow exports and works out every page to write.
|
|
2
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
4
|
+
import { describe } from "./describe.js";
|
|
5
|
+
import { merge } from "./markers.js";
|
|
6
|
+
import { renderIndex, renderPage } from "./render.js";
|
|
7
|
+
import { workflowsIn } from "./workflow.js";
|
|
8
|
+
const SKIP_DIRS = new Set(["node_modules", ".git"]);
|
|
9
|
+
function posix(p) {
|
|
10
|
+
return p.split(sep).join("/");
|
|
11
|
+
}
|
|
12
|
+
export function findJsonFiles(dir, exclude = []) {
|
|
13
|
+
const out = [];
|
|
14
|
+
const walk = (d) => {
|
|
15
|
+
for (const entry of readdirSync(d, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
16
|
+
const full = join(d, entry.name);
|
|
17
|
+
if (entry.isDirectory()) {
|
|
18
|
+
if (SKIP_DIRS.has(entry.name) || exclude.includes(resolve(full)))
|
|
19
|
+
continue;
|
|
20
|
+
walk(full);
|
|
21
|
+
}
|
|
22
|
+
else if (entry.isFile() && entry.name.toLowerCase().endsWith(".json")) {
|
|
23
|
+
out.push(full);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
walk(dir);
|
|
28
|
+
return out;
|
|
29
|
+
}
|
|
30
|
+
export function slug(text) {
|
|
31
|
+
return (text
|
|
32
|
+
.normalize("NFKD")
|
|
33
|
+
.replace(/[\u0300-\u036f]/g, "")
|
|
34
|
+
.toLowerCase()
|
|
35
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
36
|
+
.replace(/^-+|-+$/g, "") || "workflow");
|
|
37
|
+
}
|
|
38
|
+
async function makeScrubber(deny, keep) {
|
|
39
|
+
let mod;
|
|
40
|
+
try {
|
|
41
|
+
mod = await import("pasteproof");
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
throw new Error("--scrub needs the pasteproof package: npm install pasteproof");
|
|
45
|
+
}
|
|
46
|
+
const scrubber = new mod.Scrubber({ deny, keep });
|
|
47
|
+
return (text) => scrubber.scrub(text).text;
|
|
48
|
+
}
|
|
49
|
+
export async function plan(options) {
|
|
50
|
+
const cwd = options.cwd ?? process.cwd();
|
|
51
|
+
const input = resolve(cwd, options.input);
|
|
52
|
+
const out = resolve(cwd, options.out);
|
|
53
|
+
const indexName = options.index ?? "README.md";
|
|
54
|
+
if (!existsSync(input) || !statSync(input).isDirectory()) {
|
|
55
|
+
throw new Error(`not a folder: ${options.input}`);
|
|
56
|
+
}
|
|
57
|
+
const entries = [];
|
|
58
|
+
const skipped = [];
|
|
59
|
+
const errors = [];
|
|
60
|
+
const used = new Set([indexName.toLowerCase()]);
|
|
61
|
+
for (const file of findJsonFiles(input, [out])) {
|
|
62
|
+
const source = posix(relative(cwd, file));
|
|
63
|
+
let parsed;
|
|
64
|
+
try {
|
|
65
|
+
parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
66
|
+
}
|
|
67
|
+
catch (e) {
|
|
68
|
+
errors.push(`${source}: ${e.message}`);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const wfs = workflowsIn(parsed);
|
|
72
|
+
if (wfs.length === 0) {
|
|
73
|
+
skipped.push(source);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const base = posix(relative(input, file)).replace(/\.json$/i, "");
|
|
77
|
+
for (const wf of wfs) {
|
|
78
|
+
let page = wfs.length === 1 ? `${base}.md` : `${base}/${slug(wf.name)}.md`;
|
|
79
|
+
for (let n = 2; used.has(page.toLowerCase()); n++)
|
|
80
|
+
page = page.replace(/(-\d+)?\.md$/, `-${n}.md`);
|
|
81
|
+
used.add(page.toLowerCase());
|
|
82
|
+
entries.push({ wf, source, page });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const byId = new Map();
|
|
86
|
+
for (const e of entries) {
|
|
87
|
+
if (typeof e.wf.id === "string" || typeof e.wf.id === "number")
|
|
88
|
+
byId.set(String(e.wf.id), { name: e.wf.name, page: e.page });
|
|
89
|
+
}
|
|
90
|
+
const descriptions = entries.map((e) => describe(e.wf));
|
|
91
|
+
const scrub = options.scrub
|
|
92
|
+
? await makeScrubber([...(options.deny ?? []), ...new Set(descriptions.flatMap((d) => d.webhookPaths))], options.keep ?? [])
|
|
93
|
+
: (text) => text;
|
|
94
|
+
const files = [];
|
|
95
|
+
const add = (path, generated) => {
|
|
96
|
+
const existing = existsSync(path) ? readFileSync(path, "utf8") : undefined;
|
|
97
|
+
const content = merge(existing, generated);
|
|
98
|
+
files.push({ path, content, state: existing === undefined ? "new" : existing === content ? "same" : "changed" });
|
|
99
|
+
};
|
|
100
|
+
entries.forEach((e, i) => {
|
|
101
|
+
add(join(out, e.page), scrub(renderPage(e.wf, { source: e.source, page: e.page, byId }, descriptions[i])));
|
|
102
|
+
});
|
|
103
|
+
const rows = entries
|
|
104
|
+
.map((e, i) => ({ wf: e.wf, page: e.page, d: descriptions[i] }))
|
|
105
|
+
.sort((a, b) => a.wf.name.localeCompare(b.wf.name) || a.page.localeCompare(b.page));
|
|
106
|
+
if (entries.length > 0)
|
|
107
|
+
add(join(out, indexName), scrub(renderIndex(rows, options.title ?? "n8n workflows")));
|
|
108
|
+
return { files, workflows: entries.length, skipped, errors };
|
|
109
|
+
}
|
|
110
|
+
export function write(p) {
|
|
111
|
+
let n = 0;
|
|
112
|
+
for (const f of p.files) {
|
|
113
|
+
if (f.state === "same")
|
|
114
|
+
continue;
|
|
115
|
+
mkdirSync(dirname(f.path), { recursive: true });
|
|
116
|
+
writeFileSync(f.path, f.content);
|
|
117
|
+
n++;
|
|
118
|
+
}
|
|
119
|
+
return n;
|
|
120
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { plan, write, findJsonFiles, slug } from "./generate.ts";
|
|
2
|
+
export type { Options, Plan, PlannedFile } from "./generate.ts";
|
|
3
|
+
export { describe, triggers, credentials, settings, warnings, describeScheduleRules, describeTimeItems } from "./describe.ts";
|
|
4
|
+
export type { Description, TriggerInfo, CredentialInfo, SettingInfo } from "./describe.ts";
|
|
5
|
+
export { mermaid } from "./mermaid.ts";
|
|
6
|
+
export { renderPage, renderIndex } from "./render.ts";
|
|
7
|
+
export { merge, START, END } from "./markers.ts";
|
|
8
|
+
export { workflowsIn, isWorkflow, shortType } from "./workflow.ts";
|
|
9
|
+
export type { N8nWorkflow, N8nNode, N8nConnections } from "./workflow.ts";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { plan, write, findJsonFiles, slug } from "./generate.js";
|
|
2
|
+
export { describe, triggers, credentials, settings, warnings, describeScheduleRules, describeTimeItems } from "./describe.js";
|
|
3
|
+
export { mermaid } from "./mermaid.js";
|
|
4
|
+
export { renderPage, renderIndex } from "./render.js";
|
|
5
|
+
export { merge, START, END } from "./markers.js";
|
|
6
|
+
export { workflowsIn, isWorkflow, shortType } from "./workflow.js";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const START = "<!-- n8n-docgen:start (generated: edits between these markers are replaced) -->";
|
|
2
|
+
export declare const END = "<!-- n8n-docgen:end -->";
|
|
3
|
+
export declare function block(generated: string): string;
|
|
4
|
+
/**
|
|
5
|
+
* The new file text: `existing` with its marked block replaced, or with the
|
|
6
|
+
* block appended when it has no markers yet. A new file is the block alone.
|
|
7
|
+
*/
|
|
8
|
+
export declare function merge(existing: string | undefined, generated: string): string;
|
package/dist/markers.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Generated text lives between two marker comments. Everything outside them
|
|
2
|
+
// is yours and survives every run.
|
|
3
|
+
export const START = "<!-- n8n-docgen:start (generated: edits between these markers are replaced) -->";
|
|
4
|
+
export const END = "<!-- n8n-docgen:end -->";
|
|
5
|
+
const START_RE = /<!--\s*n8n-docgen:start\b[^>]*-->/;
|
|
6
|
+
const END_RE = /<!--\s*n8n-docgen:end\s*-->/;
|
|
7
|
+
export function block(generated) {
|
|
8
|
+
return `${START}\n${generated.trimEnd()}\n${END}`;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The new file text: `existing` with its marked block replaced, or with the
|
|
12
|
+
* block appended when it has no markers yet. A new file is the block alone.
|
|
13
|
+
*/
|
|
14
|
+
export function merge(existing, generated) {
|
|
15
|
+
const b = block(generated);
|
|
16
|
+
if (existing === undefined || existing.trim() === "")
|
|
17
|
+
return `${b}\n`;
|
|
18
|
+
const start = START_RE.exec(existing);
|
|
19
|
+
const end = END_RE.exec(existing);
|
|
20
|
+
if (start && end && end.index > start.index) {
|
|
21
|
+
return existing.slice(0, start.index) + b + existing.slice(end.index + end[0].length);
|
|
22
|
+
}
|
|
23
|
+
return `${existing.trimEnd()}\n\n${b}\n`;
|
|
24
|
+
}
|
package/dist/mermaid.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// Mermaid flowchart of a workflow's nodes and connections.
|
|
2
|
+
import { isTriggerNode } from "./describe.js";
|
|
3
|
+
import { isStickyNote, shortType } from "./workflow.js";
|
|
4
|
+
/** Mermaid entity codes, so any node name is a safe label. */
|
|
5
|
+
export function mermaidLabel(text) {
|
|
6
|
+
return text
|
|
7
|
+
.replace(/#/g, "#35;")
|
|
8
|
+
.replace(/"/g, "#quot;")
|
|
9
|
+
.replace(/&/g, "#amp;")
|
|
10
|
+
.replace(/</g, "#lt;")
|
|
11
|
+
.replace(/>/g, "#gt;")
|
|
12
|
+
.replace(/`/g, "#96;")
|
|
13
|
+
.replace(/[\r\n]+/g, " ");
|
|
14
|
+
}
|
|
15
|
+
function edgeLabel(text) {
|
|
16
|
+
return text.replace(/[|"<>#&`\r\n]/g, " ").trim();
|
|
17
|
+
}
|
|
18
|
+
function outputLabel(type, index, outputs, errorOutput) {
|
|
19
|
+
if (errorOutput && index === outputs - 1)
|
|
20
|
+
return "error";
|
|
21
|
+
if (type === "if")
|
|
22
|
+
return index === 0 ? "true" : index === 1 ? "false" : String(index);
|
|
23
|
+
if (type === "filter")
|
|
24
|
+
return index === 0 ? "kept" : "discarded";
|
|
25
|
+
if (outputs > 1)
|
|
26
|
+
return String(index);
|
|
27
|
+
return "";
|
|
28
|
+
}
|
|
29
|
+
export function mermaid(wf) {
|
|
30
|
+
const nodes = wf.nodes.filter((n) => !isStickyNote(n));
|
|
31
|
+
const ids = new Map();
|
|
32
|
+
nodes.forEach((n, i) => ids.set(n.name, `n${i + 1}`));
|
|
33
|
+
const lines = ["flowchart LR"];
|
|
34
|
+
for (const n of nodes) {
|
|
35
|
+
const id = ids.get(n.name);
|
|
36
|
+
const label = `"${mermaidLabel(n.name)}"`;
|
|
37
|
+
lines.push(isTriggerNode(n) ? ` ${id}([${label}])` : ` ${id}[${label}]`);
|
|
38
|
+
}
|
|
39
|
+
for (const n of nodes) {
|
|
40
|
+
const from = ids.get(n.name);
|
|
41
|
+
const byType = wf.connections[n.name] ?? {};
|
|
42
|
+
const type = shortType(n.type);
|
|
43
|
+
for (const connType of Object.keys(byType).sort((a, b) => (a === "main" ? -1 : b === "main" ? 1 : a.localeCompare(b)))) {
|
|
44
|
+
const outputs = byType[connType] ?? [];
|
|
45
|
+
outputs.forEach((targets, index) => {
|
|
46
|
+
for (const target of targets ?? []) {
|
|
47
|
+
const to = ids.get(target?.node);
|
|
48
|
+
if (!to)
|
|
49
|
+
continue;
|
|
50
|
+
if (connType === "main") {
|
|
51
|
+
const label = edgeLabel(outputLabel(type, index, outputs.length, n.onError === "continueErrorOutput"));
|
|
52
|
+
lines.push(label ? ` ${from} -->|${label}| ${to}` : ` ${from} --> ${to}`);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
// AI sub-nodes (model, memory, tools) plug into their parent node.
|
|
56
|
+
const label = edgeLabel(connType.replace(/^ai_/, "").replace(/([a-z])([A-Z])/g, "$1 $2").toLowerCase());
|
|
57
|
+
lines.push(` ${from} -. ${label} .-> ${to}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const disabled = nodes.filter((n) => n.disabled).map((n) => ids.get(n.name));
|
|
64
|
+
if (disabled.length > 0) {
|
|
65
|
+
lines.push(" classDef disabled stroke-dasharray: 5 5,opacity:0.5");
|
|
66
|
+
lines.push(` class ${disabled.join(",")} disabled`);
|
|
67
|
+
}
|
|
68
|
+
return lines.join("\n");
|
|
69
|
+
}
|
package/dist/render.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type Description } from "./describe.ts";
|
|
2
|
+
import { type N8nWorkflow } from "./workflow.ts";
|
|
3
|
+
export interface PageRef {
|
|
4
|
+
/** Workflow name. */
|
|
5
|
+
name: string;
|
|
6
|
+
/** Page path relative to the output folder, with forward slashes. */
|
|
7
|
+
page: string;
|
|
8
|
+
}
|
|
9
|
+
export interface RenderContext {
|
|
10
|
+
/** Source file, relative to where the command runs (shown on the page). */
|
|
11
|
+
source: string;
|
|
12
|
+
/** This page's path relative to the output folder. */
|
|
13
|
+
page: string;
|
|
14
|
+
/** Workflow ID -> page, to link Error Workflows found in the same folder. */
|
|
15
|
+
byId: Map<string, PageRef>;
|
|
16
|
+
}
|
|
17
|
+
/** Table cell text: one line, no pipe that would split the cell. */
|
|
18
|
+
export declare function cell(text: string): string;
|
|
19
|
+
/** Relative link from one page to another, both relative to the output folder. */
|
|
20
|
+
export declare function relativeLink(from: string, to: string): string;
|
|
21
|
+
export declare function renderPage(wf: N8nWorkflow, ctx: RenderContext, d?: Description): string;
|
|
22
|
+
export interface IndexRow {
|
|
23
|
+
wf: N8nWorkflow;
|
|
24
|
+
page: string;
|
|
25
|
+
d: Description;
|
|
26
|
+
}
|
|
27
|
+
export declare function renderIndex(rows: IndexRow[], title: string): string;
|
package/dist/render.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// Markdown for one workflow page and for the index.
|
|
2
|
+
import { describe } from "./describe.js";
|
|
3
|
+
import { mermaid } from "./mermaid.js";
|
|
4
|
+
import { isStickyNote, shortType, tagNames } from "./workflow.js";
|
|
5
|
+
/** Table cell text: one line, no pipe that would split the cell. */
|
|
6
|
+
export function cell(text) {
|
|
7
|
+
return text.replace(/\r?\n/g, " ").replace(/\|/g, "\\|").trim();
|
|
8
|
+
}
|
|
9
|
+
/** Relative link from one page to another, both relative to the output folder. */
|
|
10
|
+
export function relativeLink(from, to) {
|
|
11
|
+
const a = from.split("/").slice(0, -1);
|
|
12
|
+
const b = to.split("/");
|
|
13
|
+
let i = 0;
|
|
14
|
+
while (i < a.length && i < b.length - 1 && a[i] === b[i])
|
|
15
|
+
i++;
|
|
16
|
+
return [...a.slice(i).map(() => ".."), ...b.slice(i)].join("/");
|
|
17
|
+
}
|
|
18
|
+
function linkText(text) {
|
|
19
|
+
return text.replace(/([[\]\\])/g, "\\$1");
|
|
20
|
+
}
|
|
21
|
+
function errorWorkflowText(d, ctx) {
|
|
22
|
+
if (!d.errorWorkflowId)
|
|
23
|
+
return "**Not set**";
|
|
24
|
+
const ref = ctx.byId.get(d.errorWorkflowId);
|
|
25
|
+
if (!ref)
|
|
26
|
+
return "Set";
|
|
27
|
+
return `[${linkText(ref.name)}](${relativeLink(ctx.page, ref.page)})`;
|
|
28
|
+
}
|
|
29
|
+
function truncate(text, max) {
|
|
30
|
+
const one = text.replace(/\s+/g, " ").trim();
|
|
31
|
+
return one.length > max ? `${one.slice(0, max - 3)}...` : one;
|
|
32
|
+
}
|
|
33
|
+
export function renderPage(wf, ctx, d = describe(wf)) {
|
|
34
|
+
const nodes = wf.nodes.filter((n) => !isStickyNote(n));
|
|
35
|
+
const tags = tagNames(wf);
|
|
36
|
+
const out = [];
|
|
37
|
+
out.push(`# ${wf.name}`, "");
|
|
38
|
+
out.push(`Source: \`${ctx.source}\``, "");
|
|
39
|
+
out.push("| | |", "| --- | --- |");
|
|
40
|
+
out.push(`| Active | ${wf.active ? "Yes" : "No"} |`);
|
|
41
|
+
out.push(`| Nodes | ${nodes.length} |`);
|
|
42
|
+
if (tags.length > 0)
|
|
43
|
+
out.push(`| Tags | ${cell(tags.join(", "))} |`);
|
|
44
|
+
out.push(`| Error workflow | ${errorWorkflowText(d, ctx)} |`);
|
|
45
|
+
out.push("");
|
|
46
|
+
if (d.warnings.length > 0) {
|
|
47
|
+
out.push("> [!WARNING]");
|
|
48
|
+
d.warnings.forEach((w, i) => {
|
|
49
|
+
if (i > 0)
|
|
50
|
+
out.push(">");
|
|
51
|
+
out.push(`> ${w}`);
|
|
52
|
+
});
|
|
53
|
+
out.push("");
|
|
54
|
+
}
|
|
55
|
+
out.push("## Flow", "", "```mermaid", mermaid(wf), "```", "");
|
|
56
|
+
out.push("## Triggers", "");
|
|
57
|
+
if (d.triggers.length === 0) {
|
|
58
|
+
out.push("None. This workflow only runs when started by hand or by another workflow.", "");
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
for (const t of d.triggers) {
|
|
62
|
+
const what = t.details.length > 0 ? t.details.join("; ") : "Runs on start";
|
|
63
|
+
out.push(`- **${t.node}** (\`${t.type}\`): ${what}`);
|
|
64
|
+
}
|
|
65
|
+
out.push("");
|
|
66
|
+
}
|
|
67
|
+
out.push("## Credentials", "");
|
|
68
|
+
if (d.credentials.length === 0) {
|
|
69
|
+
out.push("None.", "");
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
out.push("| Credential | Type | Used by |", "| --- | --- | --- |");
|
|
73
|
+
for (const c of d.credentials)
|
|
74
|
+
out.push(`| ${cell(c.name)} | \`${c.type}\` | ${cell(c.nodes.join(", "))} |`);
|
|
75
|
+
out.push("");
|
|
76
|
+
}
|
|
77
|
+
out.push("## Execution data", "", "| Setting | Value |", "| --- | --- |");
|
|
78
|
+
for (const s of d.settings)
|
|
79
|
+
out.push(`| ${s.label} | ${cell(s.value)} |`);
|
|
80
|
+
out.push("");
|
|
81
|
+
out.push("## Nodes", "", "| Node | Type | Notes |", "| --- | --- | --- |");
|
|
82
|
+
for (const n of nodes) {
|
|
83
|
+
const notes = [n.disabled ? "Disabled" : "", n.notes ? truncate(n.notes, 120) : ""].filter(Boolean).join(". ");
|
|
84
|
+
out.push(`| ${cell(n.name)} | \`${shortType(n.type)}\` | ${cell(notes)} |`);
|
|
85
|
+
}
|
|
86
|
+
out.push("");
|
|
87
|
+
const stickies = wf.nodes
|
|
88
|
+
.filter(isStickyNote)
|
|
89
|
+
.map((n) => (typeof n.parameters?.content === "string" ? n.parameters.content.trim() : ""))
|
|
90
|
+
.filter(Boolean);
|
|
91
|
+
if (stickies.length > 0) {
|
|
92
|
+
out.push("## Sticky notes", "");
|
|
93
|
+
for (const s of stickies) {
|
|
94
|
+
out.push(...s.split(/\r?\n/).map((l) => (l ? `> ${l}` : ">")), "");
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return out.join("\n").trimEnd();
|
|
98
|
+
}
|
|
99
|
+
export function renderIndex(rows, title) {
|
|
100
|
+
const out = [`# ${title}`, ""];
|
|
101
|
+
out.push(`${rows.length} workflow${rows.length === 1 ? "" : "s"}.`, "");
|
|
102
|
+
out.push("| Workflow | Active | Triggers | Nodes | Error workflow | Warnings |", "| --- | --- | --- | --- | --- | --- |");
|
|
103
|
+
for (const { wf, page, d } of rows) {
|
|
104
|
+
const nodes = wf.nodes.filter((n) => !isStickyNote(n)).length;
|
|
105
|
+
const triggers = [...new Set(d.triggers.map((t) => t.kind))].join(", ") || "None";
|
|
106
|
+
out.push(`| [${linkText(cell(wf.name))}](${page}) | ${wf.active ? "Yes" : "No"} | ${triggers} | ${nodes} | ${d.errorWorkflowId ? "Set" : "**Not set**"} | ${d.warnings.length > 0 ? `**${d.warnings.length}**` : ""} |`);
|
|
107
|
+
}
|
|
108
|
+
return out.join("\n");
|
|
109
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export interface N8nCredentialRef {
|
|
2
|
+
id?: string;
|
|
3
|
+
name?: string;
|
|
4
|
+
}
|
|
5
|
+
export interface N8nNode {
|
|
6
|
+
id?: string;
|
|
7
|
+
name: string;
|
|
8
|
+
type: string;
|
|
9
|
+
typeVersion?: number;
|
|
10
|
+
parameters?: Record<string, unknown>;
|
|
11
|
+
credentials?: Record<string, N8nCredentialRef>;
|
|
12
|
+
disabled?: boolean;
|
|
13
|
+
notes?: string;
|
|
14
|
+
onError?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface N8nConnectionTarget {
|
|
17
|
+
node: string;
|
|
18
|
+
type?: string;
|
|
19
|
+
index?: number;
|
|
20
|
+
}
|
|
21
|
+
/** source node name -> connection type (main, ai_tool, ...) -> output index -> targets */
|
|
22
|
+
export type N8nConnections = Record<string, Record<string, Array<N8nConnectionTarget[] | null> | undefined>>;
|
|
23
|
+
export interface N8nWorkflow {
|
|
24
|
+
id?: string;
|
|
25
|
+
name: string;
|
|
26
|
+
active?: boolean;
|
|
27
|
+
nodes: N8nNode[];
|
|
28
|
+
connections: N8nConnections;
|
|
29
|
+
settings?: Record<string, unknown>;
|
|
30
|
+
pinData?: Record<string, unknown>;
|
|
31
|
+
tags?: Array<string | {
|
|
32
|
+
name?: string;
|
|
33
|
+
}>;
|
|
34
|
+
[key: string]: unknown;
|
|
35
|
+
}
|
|
36
|
+
export declare function isWorkflow(value: unknown): value is N8nWorkflow;
|
|
37
|
+
/**
|
|
38
|
+
* Workflows in a parsed JSON file. A file holds one workflow (the editor's
|
|
39
|
+
* download, `n8n export:workflow --separate`) or an array of them
|
|
40
|
+
* (`n8n export:workflow --all` without `--separate`). Anything else is not a
|
|
41
|
+
* workflow file and gives an empty list.
|
|
42
|
+
*/
|
|
43
|
+
export declare function workflowsIn(parsed: unknown): N8nWorkflow[];
|
|
44
|
+
/** `n8n-nodes-base.httpRequest` -> `httpRequest`; community nodes keep their package. */
|
|
45
|
+
export declare function shortType(type: string): string;
|
|
46
|
+
export declare function isStickyNote(node: N8nNode): boolean;
|
|
47
|
+
export declare function tagNames(wf: N8nWorkflow): string[];
|
package/dist/workflow.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// The parts of an exported n8n workflow that n8n-docgen reads. Exports carry
|
|
2
|
+
// more fields; anything not listed here is ignored.
|
|
3
|
+
export function isWorkflow(value) {
|
|
4
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
5
|
+
return false;
|
|
6
|
+
const v = value;
|
|
7
|
+
return (Array.isArray(v.nodes) &&
|
|
8
|
+
v.nodes.every((n) => typeof n === "object" &&
|
|
9
|
+
n !== null &&
|
|
10
|
+
typeof n.name === "string" &&
|
|
11
|
+
typeof n.type === "string") &&
|
|
12
|
+
(v.connections === undefined || (typeof v.connections === "object" && v.connections !== null)));
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Workflows in a parsed JSON file. A file holds one workflow (the editor's
|
|
16
|
+
* download, `n8n export:workflow --separate`) or an array of them
|
|
17
|
+
* (`n8n export:workflow --all` without `--separate`). Anything else is not a
|
|
18
|
+
* workflow file and gives an empty list.
|
|
19
|
+
*/
|
|
20
|
+
export function workflowsIn(parsed) {
|
|
21
|
+
const items = Array.isArray(parsed) ? parsed : [parsed];
|
|
22
|
+
const out = [];
|
|
23
|
+
for (const item of items) {
|
|
24
|
+
if (!isWorkflow(item))
|
|
25
|
+
continue;
|
|
26
|
+
out.push({
|
|
27
|
+
...item,
|
|
28
|
+
name: typeof item.name === "string" && item.name.trim() ? item.name : "Untitled workflow",
|
|
29
|
+
connections: item.connections ?? {},
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
const PREFIXES = ["n8n-nodes-base.", "@n8n/n8n-nodes-langchain.", "n8n-nodes-langchain."];
|
|
35
|
+
/** `n8n-nodes-base.httpRequest` -> `httpRequest`; community nodes keep their package. */
|
|
36
|
+
export function shortType(type) {
|
|
37
|
+
for (const p of PREFIXES)
|
|
38
|
+
if (type.startsWith(p))
|
|
39
|
+
return type.slice(p.length);
|
|
40
|
+
return type;
|
|
41
|
+
}
|
|
42
|
+
export function isStickyNote(node) {
|
|
43
|
+
return shortType(node.type) === "stickyNote";
|
|
44
|
+
}
|
|
45
|
+
export function tagNames(wf) {
|
|
46
|
+
return (wf.tags ?? [])
|
|
47
|
+
.map((t) => (typeof t === "string" ? t : t?.name))
|
|
48
|
+
.filter((t) => typeof t === "string" && t.length > 0);
|
|
49
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "n8n-docgen",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Readable n8n workflows in git: a Markdown page with a Mermaid diagram for every exported workflow, checked in CI.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Avram Aruh",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/avramaruh92/n8n-docgen.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/avramaruh92/n8n-docgen#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/avramaruh92/n8n-docgen/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"bin": {
|
|
17
|
+
"n8n-docgen": "dist/cli.js"
|
|
18
|
+
},
|
|
19
|
+
"main": "dist/index.js",
|
|
20
|
+
"types": "dist/index.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"default": "./dist/index.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=22.18"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsc -p tsconfig.json",
|
|
35
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
36
|
+
"test": "node --test test/*.test.ts",
|
|
37
|
+
"examples": "node src/cli.ts examples/workflows --out examples/docs --title \"Example workflows\"",
|
|
38
|
+
"prepublishOnly": "npm run build && npm test"
|
|
39
|
+
},
|
|
40
|
+
"keywords": [
|
|
41
|
+
"n8n",
|
|
42
|
+
"workflow",
|
|
43
|
+
"documentation",
|
|
44
|
+
"docs",
|
|
45
|
+
"mermaid",
|
|
46
|
+
"markdown",
|
|
47
|
+
"gitops",
|
|
48
|
+
"github-action",
|
|
49
|
+
"automation",
|
|
50
|
+
"cli"
|
|
51
|
+
],
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"pasteproof": "1.0.0"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@types/node": "22.20.5",
|
|
57
|
+
"typescript": "7.0.2"
|
|
58
|
+
}
|
|
59
|
+
}
|