@timo9378/flow2code 0.1.2 → 0.1.3

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.
Files changed (47) hide show
  1. package/CHANGELOG.md +56 -56
  2. package/LICENSE +21 -21
  3. package/README.md +230 -223
  4. package/dist/cli.js +61 -2
  5. package/dist/server.d.ts +27 -27
  6. package/dist/server.js +876 -4
  7. package/out/404.html +1 -1
  8. package/out/__next.__PAGE__.txt +4 -4
  9. package/out/__next._full.txt +13 -13
  10. package/out/__next._head.txt +4 -4
  11. package/out/__next._index.txt +6 -6
  12. package/out/__next._tree.txt +2 -2
  13. package/out/_next/static/chunks/{41ec06cb3e44c96a.js → 5f1a9fec0e69c483.js} +2 -2
  14. package/out/_next/static/chunks/6b84376656bd9887.js +1 -0
  15. package/out/_next/static/chunks/993eabba22c07d39.js +54 -0
  16. package/out/_next/static/chunks/a6dad97d9634a72d.js.map +1 -1
  17. package/out/_next/static/chunks/ab8888d4b78b94be.js +5 -0
  18. package/out/_next/static/chunks/acf223168ac429f7.js +1 -0
  19. package/out/_next/static/chunks/{8486af13ea541d4d.js → b112c2f519e4b429.js} +1 -1
  20. package/out/_next/static/chunks/{4b43c69e91d19eee.js → b163b5d7cccbcf42.js} +1 -1
  21. package/out/_next/static/chunks/b6e8711267bccbbd.js +1 -0
  22. package/out/_next/static/chunks/c8a26302d935bf6e.css +1 -0
  23. package/out/_next/static/chunks/fbca595129527827.js +1 -0
  24. package/out/_next/static/chunks/{turbopack-b95472a0b02a2729.js → turbopack-576234c945ffdc44.js} +1 -1
  25. package/out/_not-found/__next._full.txt +11 -11
  26. package/out/_not-found/__next._head.txt +4 -4
  27. package/out/_not-found/__next._index.txt +6 -6
  28. package/out/_not-found/{__next._not-found.__PAGE__.txt → __next._not-found/__PAGE__.txt} +2 -2
  29. package/out/_not-found/__next._not-found.txt +3 -3
  30. package/out/_not-found/__next._tree.txt +2 -2
  31. package/out/_not-found.html +1 -1
  32. package/out/_not-found.txt +11 -11
  33. package/out/index.html +2 -2
  34. package/out/index.txt +13 -13
  35. package/package.json +123 -121
  36. package/scripts/postinstall.cjs +23 -0
  37. package/out/_next/static/chunks/324e22de375c241a.css +0 -1
  38. package/out/_next/static/chunks/3638bc7cb3d72bbe.js +0 -379
  39. package/out/_next/static/chunks/46e882944a7f3aed.js +0 -1
  40. package/out/_next/static/chunks/59b058cf94cc555e.js +0 -1
  41. package/out/_next/static/chunks/6e36daee8960e188.js +0 -5
  42. package/out/_next/static/chunks/80a5431d4e78c469.js +0 -1
  43. package/out/_next/static/chunks/8a38426c5ed81587.js +0 -1
  44. package/out/_next/static/chunks/edbd80df6004e249.js +0 -52
  45. /package/out/_next/static/{dSD-EjLFfv3PUm7xo33fg → gs3QpnA696kN6tOSlwt6o}/_buildManifest.js +0 -0
  46. /package/out/_next/static/{dSD-EjLFfv3PUm7xo33fg → gs3QpnA696kN6tOSlwt6o}/_clientMiddlewareManifest.json +0 -0
  47. /package/out/_next/static/{dSD-EjLFfv3PUm7xo33fg → gs3QpnA696kN6tOSlwt6o}/_ssgManifest.js +0 -0
package/CHANGELOG.md CHANGED
@@ -1,56 +1,56 @@
1
- # Changelog
2
-
3
- All notable changes to this project will be documented in this file.
4
-
5
- The format is based on [Keep a Changelog](https://keepachangelog.com/),
6
- and this project adheres to [Semantic Versioning](https://semver.org/).
7
-
8
- ## [0.1.0] — 2026-02-27
9
-
10
- ### Added
11
-
12
- #### Core
13
- - **Visual AST Compiler** — Flow-based IR → production-ready TypeScript
14
- - **Headless compiler** — `import { compile } from "@timo9378/flow2code/compiler"` can be used standalone
15
- - **CLI** — `flow2code compile`, `flow2code watch`, `flow2code migrate` commands
16
- - **Standalone dev server** — Zero-dependency HTTP server (`flow2code serve`)
17
- - **Plugin system** — `NodePlugin` interface + `PluginRegistry` factory pattern
18
- - **15 built-in node types** — HTTP Webhook, Cron Job, Fetch API, SQL Query, Redis Cache, Custom Code, If/Else, For Loop, Try/Catch, Return Response, Declare Variable, Transform, Call Subflow, Manual Trigger
19
-
20
- #### Platforms
21
- - **Next.js** platform adapter (App Router)
22
- - **Express** platform adapter
23
- - **Cloudflare Workers** platform adapter
24
- - Extensible `PlatformAdapter` interface, supports third-party platform registration
25
-
26
- #### AI Features
27
- - AI Flow Generator (OpenAI-compatible endpoints)
28
- - Streaming support (SSE) + retry mechanism + token budget management
29
- - Custom AI endpoint management (copilot-api / Gemini / Ollama)
30
- - AI Code Review (automatic audit of generated IR)
31
- - **IR Security Validator** — Scans AI-generated code for malicious patterns (eval, child_process, fs, etc.)
32
-
33
- #### DX Improvements
34
- - **Runtime Error Tracer** — Reverse-lookup from Error.stack via Source Map to canvas node
35
- - **Git-Native Split Storage** — YAML directory format, supports git diff
36
- - **Edit-Time Type Inference** — Real-time inference of upstream node flowState types
37
- - **Dynamic Node Registry** — Extensible node definition system
38
- - **Expression Autocomplete** — Expression input auto-completion (flowState fields + methods)
39
- - **Decompiler** — TypeScript → FlowIR reverse parser (ts-morph AST analysis)
40
-
41
- #### Quality
42
- - TypeScript strict mode
43
- - ESLint v10 flat config (0 errors)
44
- - 354 tests / 31 test files (vitest)
45
- - GitHub Actions CI (Node.js 20/22 matrix)
46
- - `.husky/pre-push` CI guard
47
- - Content-Security-Policy headers
48
- - TypeDoc API reference generation
49
- - Zustand undo/redo isolated slice
50
-
51
- ### Security
52
- - IR Security Validator (`validateIRSecurity()`) three-tier threat detection
53
- - Custom Code dangerous API compile-time warnings
54
- - CSP / X-Content-Type-Options / X-Frame-Options / X-XSS-Protection headers
55
- - 2MB body size limit
56
- - IR structural validation (cycle detection, orphan node detection)
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/).
7
+
8
+ ## [0.1.0] — 2026-02-27
9
+
10
+ ### Added
11
+
12
+ #### Core
13
+ - **Visual AST Compiler** — Flow-based IR → production-ready TypeScript
14
+ - **Headless compiler** — `import { compile } from "@timo9378/flow2code/compiler"` can be used standalone
15
+ - **CLI** — `flow2code compile`, `flow2code watch`, `flow2code migrate` commands
16
+ - **Standalone dev server** — Zero-dependency HTTP server (`flow2code serve`)
17
+ - **Plugin system** — `NodePlugin` interface + `PluginRegistry` factory pattern
18
+ - **15 built-in node types** — HTTP Webhook, Cron Job, Fetch API, SQL Query, Redis Cache, Custom Code, If/Else, For Loop, Try/Catch, Return Response, Declare Variable, Transform, Call Subflow, Manual Trigger
19
+
20
+ #### Platforms
21
+ - **Next.js** platform adapter (App Router)
22
+ - **Express** platform adapter
23
+ - **Cloudflare Workers** platform adapter
24
+ - Extensible `PlatformAdapter` interface, supports third-party platform registration
25
+
26
+ #### AI Features
27
+ - AI Flow Generator (OpenAI-compatible endpoints)
28
+ - Streaming support (SSE) + retry mechanism + token budget management
29
+ - Custom AI endpoint management (copilot-api / Gemini / Ollama)
30
+ - AI Code Review (automatic audit of generated IR)
31
+ - **IR Security Validator** — Scans AI-generated code for malicious patterns (eval, child_process, fs, etc.)
32
+
33
+ #### DX Improvements
34
+ - **Runtime Error Tracer** — Reverse-lookup from Error.stack via Source Map to canvas node
35
+ - **Git-Native Split Storage** — YAML directory format, supports git diff
36
+ - **Edit-Time Type Inference** — Real-time inference of upstream node flowState types
37
+ - **Dynamic Node Registry** — Extensible node definition system
38
+ - **Expression Autocomplete** — Expression input auto-completion (flowState fields + methods)
39
+ - **Decompiler** — TypeScript → FlowIR reverse parser (ts-morph AST analysis)
40
+
41
+ #### Quality
42
+ - TypeScript strict mode
43
+ - ESLint v10 flat config (0 errors)
44
+ - 354 tests / 31 test files (vitest)
45
+ - GitHub Actions CI (Node.js 20/22 matrix)
46
+ - `.husky/pre-push` CI guard
47
+ - Content-Security-Policy headers
48
+ - TypeDoc API reference generation
49
+ - Zustand undo/redo isolated slice
50
+
51
+ ### Security
52
+ - IR Security Validator (`validateIRSecurity()`) three-tier threat detection
53
+ - Custom Code dangerous API compile-time warnings
54
+ - CSP / X-Content-Type-Options / X-Frame-Options / X-XSS-Protection headers
55
+ - 2MB body size limit
56
+ - IR structural validation (cycle detection, orphan node detection)
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2025-2026 flow2code contributors
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.
1
+ MIT License
2
+
3
+ Copyright (c) 2025-2026 flow2code contributors
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,223 +1,230 @@
1
- # Flow2Code
2
-
3
- **The Visual AST Compiler & Code Audit Tool for Backend APIs.**
4
-
5
- > AI generates the code, Flow2Code decompiles it into a visual flow for you to audit, or compiles your visual flow into production-ready TypeScript.
6
-
7
- [![CI](https://github.com/timo9378/flow2code/actions/workflows/ci.yml/badge.svg)](https://github.com/timo9378/flow2code/actions/workflows/ci.yml)
8
- [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
9
- [![npm version](https://img.shields.io/npm/v/@timo9378/flow2code.svg)](https://www.npmjs.com/package/@timo9378/flow2code)
10
- [![npm downloads](https://img.shields.io/npm/dm/@timo9378/flow2code.svg)](https://www.npmjs.com/package/@timo9378/flow2code)
11
-
12
- ```
13
- ┌──────────────────────────────────┐
14
- │ flow2code │
15
- │ │
16
- TS Code ──┤► decompile() → FlowIR → Canvas │ ← AI Code Audit
17
- │ │
18
- Canvas ──┤► FlowIR → compile() → TS Code │ ← Visual Compiler
19
- └──────────────────────────────────┘
20
- ```
21
-
22
- ## Why Flow2Code?
23
-
24
- | Traditional Low-Code | Flow2Code |
25
- |---|---|
26
- | Vendor-locked Runtime | **Zero-dependency** — outputs native TypeScript, deploy anywhere |
27
- | Black-box nodes | **AST Compilation** — ts-morph generates syntactically correct code |
28
- | Single platform | **Multi-platform** — Next.js, Express, Cloudflare Workers |
29
- | Can't version control | **Git-friendly** — IR is JSON/YAML, diffable in PRs |
30
- | Developers don't trust it | **Visual Audit** — bidirectional canvas ↔ code mapping |
31
-
32
- ## Core Features
33
-
34
- - **AST Compilation, not interpretation** — Uses ts-morph to generate syntactically correct TypeScript. No string concatenation.
35
- - **Zero-dependency output** — Generated code has no runtime dependency. Deploy directly to Vercel / AWS Lambda / Cloudflare.
36
- - **Multi-platform output** — Same flow compiles to Next.js, Express, or Cloudflare Workers.
37
- - **Per-instance Plugin System** — Node logic is extensible via plugins. Compile sessions are isolated.
38
- - **flowState + Type Inference** — Cross-node data passing with TypeScript type safety.
39
- - **Auto concurrency detection** — Topological sort identifies independent nodes, auto-generates `Promise.allSettled`.
40
- - **Environment variable protection** — Secrets auto-converted to `process.env.XXX`.
41
- - **Expression Parser** — Recursive Descent Parser for `$input` / `$trigger` / `$node.xxx` template syntax.
42
- - **Decompiler** — TypeScript → FlowIR reverse parser for code auditing.
43
- - **Semantic Diff** — Structural comparison of two IR versions.
44
-
45
- ## Tech Stack
46
-
47
- | Layer | Technology |
48
- |-------|-----------|
49
- | Visual Canvas | Next.js 16 + React 19 + React Flow (@xyflow/react v12) |
50
- | State Management | Zustand 5 |
51
- | IR Specification | Custom JSON Schema + TypeScript Types |
52
- | AST Engine | ts-morph 27 (TypeScript Compiler API Wrapper) |
53
- | Platform Adapters | NextjsPlatform / ExpressPlatform / CloudflarePlatform |
54
- | Plugin System | `createPluginRegistry()` factory (per-instance) |
55
- | CLI | Commander.js + Chokidar |
56
- | Testing | Vitest 4 — 354 tests across 31 test files |
57
- | CI | GitHub Actions (Node 20/22 matrix) |
58
-
59
- ## Quick Start
60
-
61
- ### Install
62
-
63
- ```bash
64
- npm install @timo9378/flow2code
65
- ```
66
-
67
- ### CLI Usage
68
-
69
- ```bash
70
- # Initialize flow2code in your project
71
- npx @timo9378/flow2code init
72
-
73
- # Compile a .flow.json to TypeScript
74
- npx @timo9378/flow2code compile .flow2code/flows/hello.flow.json --dry-run
75
-
76
- # Compile with output
77
- npx @timo9378/flow2code compile .flow2code/flows/hello.flow.json -o src/app/api/hello/route.ts
78
-
79
- # Audit (decompile) any TypeScript file into a visual FlowIR
80
- npx @timo9378/flow2code audit src/app/api/users/route.ts
81
-
82
- # Start the visual editor
83
- npx @timo9378/flow2code dev
84
-
85
- # Watch mode (auto-compile on file change)
86
- npx @timo9378/flow2code watch .flow2code/flows/
87
-
88
- # Source Map trace — find which canvas node generated a line
89
- npx @timo9378/flow2code trace src/app/api/hello/route.ts 15
90
- ```
91
-
92
- ### Library Usage
93
-
94
- ```ts
95
- import { compile, validate, decompile } from "@timo9378/flow2code";
96
-
97
- // Compile: FlowIR → TypeScript
98
- const result = compile(ir, { platform: "nextjs" });
99
-
100
- // Validate: Check IR structure
101
- const check = validate(ir);
102
-
103
- // Decompile: TypeScript → FlowIR (code audit)
104
- const audit = decompile(code);
105
- ```
106
-
107
- ## Development
108
-
109
- ```bash
110
- # Clone and install
111
- git clone https://github.com/timo9378/flow2code.git
112
- cd flow2code && pnpm install
113
-
114
- # Start dev server (visual canvas)
115
- pnpm dev
116
-
117
- # Run tests
118
- pnpm test:run
119
- ```
120
-
121
- ## Headless Usage (No UI Required)
122
-
123
- ```ts
124
- import { compile } from "@timo9378/flow2code/compiler";
125
-
126
- const ir = JSON.parse(fs.readFileSync("my-api.flow.json", "utf-8"));
127
- const result = compile(ir, { platform: "express" });
128
-
129
- if (result.success) {
130
- fs.writeFileSync(result.filePath!, result.code!);
131
- }
132
- ```
133
-
134
- ## Decompiler (TypeScript Visual Flow)
135
-
136
- ```ts
137
- import { decompile } from "@timo9378/flow2code/compiler";
138
-
139
- const code = fs.readFileSync("route.ts", "utf-8");
140
- const result = decompile(code);
141
-
142
- if (result.success) {
143
- console.log(JSON.stringify(result.ir, null, 2));
144
- console.log(`Confidence: ${result.confidence}`);
145
- }
146
- ```
147
-
148
- ## Project Structure
149
-
150
- ```
151
- flow2code/
152
- ├── src/
153
- │ ├── app/ # Next.js App Router (UI)
154
- │ ├── components/ # Visual canvas components
155
- │ ├── store/ # Zustand canvas state management
156
- │ ├── lib/
157
- │ │ ├── index.ts # Headless Compiler public API
158
- │ │ ├── ir/
159
- │ │ │ ├── types.ts # IR Schema + TypeScript types
160
- │ │ ├── validator.ts # IR validator
161
- │ │ └── topological-sort.ts # Topological sort + concurrency detection
162
- ├── compiler/
163
- │ │ ├── compiler.ts # AST compiler core
164
- │ │ ├── decompiler.ts # TS FlowIR reverse parser
165
- │ │ ├── expression-parser.ts # Recursive Descent Parser
166
- │ │ │ ├── type-inference.ts # Type inference engine
167
- │ │ │ ├── symbol-table.ts # Human-readable variable naming
168
- │ │ │ ├── plugins/ # Plugin system (extensible)
169
- │ │ │ │ ├── index.ts # Plugin registry factory + exports
170
- │ │ │ ├── types.ts # PluginRegistry interface
171
- │ │ │ │ └── builtin.ts # 15 built-in plugins
172
- │ │ │ └── platforms/ # Platform adapters
173
- │ │ │ ├── types.ts # PlatformAdapter interface
174
- │ │ │ ├── nextjs.ts # Next.js App Router
175
- │ │ │ ├── express.ts # Express.js
176
- │ │ │ └── cloudflare.ts # Cloudflare Workers
177
- │ │ ├── diff/ # Semantic Diff
178
- │ │ └── storage/ # .flow.json split/merge
179
- ├── cli/ # CLI (compile/watch/init)
180
- └── server/ # Standalone HTTP Server
181
- ├── tests/ # 354 tests (Vitest)
182
- ├── .github/workflows/ci.yml # GitHub Actions CI
183
- ├── CONTRIBUTING.md
184
- └── vitest.config.ts
185
- ```
186
-
187
- ## Node Types
188
-
189
- | Category | Node | Compiled Output |
190
- |----------|------|-----------------|
191
- | ⚡ Trigger | HTTP Webhook | `export async function POST(req)` |
192
- | ⚡ Trigger | Cron Job | Scheduled function |
193
- | ⚡ Trigger | Manual | Exported async function |
194
- | 🔧 Action | Fetch API | `await fetch(...)` + try/catch |
195
- | 🔧 Action | SQL Query | Drizzle / Prisma / Raw SQL |
196
- | 🔧 Action | Redis Cache | Redis get/set/del |
197
- | 🔧 Action | Custom Code | Inline TypeScript |
198
- | 🔧 Action | Call Subflow | `await importedFunction(...)` |
199
- | 🔀 Logic | If/Else | `if (...) { } else { }` |
200
- | 🔀 Logic | For Loop | `for (const item of ...)` |
201
- | 🔀 Logic | Try/Catch | `try { } catch (e) { }` |
202
- | 🔀 Logic | Promise.all | `await Promise.allSettled([...])` |
203
- | 📦 Variable | Declare | `const x = ...` |
204
- | 📦 Variable | Transform | Expression transform |
205
- | 📤 Output | Return Response | Platform-specific Response |
206
-
207
- ## Platform Support
208
-
209
- | Platform | Trigger Init | Response | CLI Flag |
210
- |----------|-------------|----------|----------|
211
- | **Next.js** (default) | `req.nextUrl.searchParams` / `req.json()` | `NextResponse.json()` | `--platform nextjs` |
212
- | **Express** | `req.query` / `req.body` | `res.status().json()` | `--platform express` |
213
- | **Cloudflare Workers** | `new URL(request.url)` / `request.json()` | `new Response()` | `--platform cloudflare` |
214
-
215
- ## Contributing
216
-
217
- See [CONTRIBUTING.md](CONTRIBUTING.md).
218
-
219
- For detailed usage examples and workflows, see [USAGE.md](USAGE.md).
220
-
221
- ## License
222
-
223
- MIT
1
+ # Flow2Code
2
+
3
+ **The Visual AST Compiler & Code Audit Tool for Backend APIs.**
4
+
5
+ > AI generates the code, Flow2Code decompiles it into a visual flow for you to audit, or compiles your visual flow into production-ready TypeScript.
6
+
7
+ [![CI](https://github.com/timo9378/flow2code/actions/workflows/ci.yml/badge.svg)](https://github.com/timo9378/flow2code/actions/workflows/ci.yml)
8
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
9
+ [![npm version](https://img.shields.io/npm/v/@timo9378/flow2code.svg)](https://www.npmjs.com/package/@timo9378/flow2code)
10
+ [![npm downloads](https://img.shields.io/npm/dm/@timo9378/flow2code.svg)](https://www.npmjs.com/package/@timo9378/flow2code)
11
+
12
+ ```
13
+ ┌──────────────────────────────────┐
14
+ │ flow2code │
15
+ │ │
16
+ TS Code ──┤► decompile() → FlowIR → Canvas │ ← AI Code Audit
17
+ │ │
18
+ Canvas ──┤► FlowIR → compile() → TS Code │ ← Visual Compiler
19
+ └──────────────────────────────────┘
20
+ ```
21
+
22
+ ## Why Flow2Code?
23
+
24
+ | Traditional Low-Code | Flow2Code |
25
+ |---|---|
26
+ | Vendor-locked Runtime | **Zero-dependency** — outputs native TypeScript, deploy anywhere |
27
+ | Black-box nodes | **AST Compilation** — ts-morph generates syntactically correct code |
28
+ | Single platform | **Multi-platform** — Next.js, Express, Cloudflare Workers |
29
+ | Can't version control | **Git-friendly** — IR is JSON/YAML, diffable in PRs |
30
+ | Developers don't trust it | **Visual Audit** — bidirectional canvas ↔ code mapping |
31
+
32
+ ## Core Features
33
+
34
+ - **AST Compilation, not interpretation** — Uses ts-morph to generate syntactically correct TypeScript. No string concatenation.
35
+ - **Zero-dependency output** — Generated code has no runtime dependency. Deploy directly to Vercel / AWS Lambda / Cloudflare.
36
+ - **Multi-platform output** — Same flow compiles to Next.js, Express, or Cloudflare Workers.
37
+ - **Per-instance Plugin System** — Node logic is extensible via plugins. Compile sessions are isolated.
38
+ - **flowState + Type Inference** — Cross-node data passing with TypeScript type safety.
39
+ - **Auto concurrency detection** — Topological sort identifies independent nodes, auto-generates `Promise.allSettled`.
40
+ - **Environment variable protection** — Secrets auto-converted to `process.env.XXX`.
41
+ - **Expression Parser** — Recursive Descent Parser for `$input` / `$trigger` / `$node.xxx` template syntax.
42
+ - **Decompiler** — TypeScript → FlowIR reverse parser for code auditing.
43
+ - **Semantic Diff** — Structural comparison of two IR versions.
44
+
45
+ ## Tech Stack
46
+
47
+ | Layer | Technology |
48
+ |-------|-----------|
49
+ | Visual Canvas | Next.js 16 + React 19 + React Flow (@xyflow/react v12) |
50
+ | State Management | Zustand 5 |
51
+ | IR Specification | Custom JSON Schema + TypeScript Types |
52
+ | AST Engine | ts-morph 27 (TypeScript Compiler API Wrapper) |
53
+ | Platform Adapters | NextjsPlatform / ExpressPlatform / CloudflarePlatform |
54
+ | Plugin System | `createPluginRegistry()` factory (per-instance) |
55
+ | CLI | Commander.js + Chokidar |
56
+ | Testing | Vitest 4 — 354 tests across 31 test files |
57
+ | CI | GitHub Actions (Node 20/22 matrix) |
58
+
59
+ ## Quick Start
60
+
61
+ ### Install
62
+
63
+ ```bash
64
+ # npm
65
+ npm install @timo9378/flow2code
66
+
67
+ # pnpm
68
+ pnpm add @timo9378/flow2code
69
+
70
+ # yarn
71
+ yarn add @timo9378/flow2code
72
+ ```
73
+
74
+ ### CLI Usage
75
+
76
+ ```bash
77
+ # Initialize flow2code in your project
78
+ npx @timo9378/flow2code init
79
+
80
+ # Compile a .flow.json to TypeScript
81
+ npx @timo9378/flow2code compile .flow2code/flows/hello.flow.json --dry-run
82
+
83
+ # Compile with output
84
+ npx @timo9378/flow2code compile .flow2code/flows/hello.flow.json -o src/app/api/hello/route.ts
85
+
86
+ # Audit (decompile) any TypeScript file into a visual FlowIR
87
+ npx @timo9378/flow2code audit src/app/api/users/route.ts
88
+
89
+ # Start the visual editor
90
+ npx @timo9378/flow2code dev
91
+
92
+ # Watch mode (auto-compile on file change)
93
+ npx @timo9378/flow2code watch .flow2code/flows/
94
+
95
+ # Source Map trace find which canvas node generated a line
96
+ npx @timo9378/flow2code trace src/app/api/hello/route.ts 15
97
+ ```
98
+
99
+ ### Library Usage
100
+
101
+ ```ts
102
+ import { compile, validate, decompile } from "@timo9378/flow2code";
103
+
104
+ // Compile: FlowIR → TypeScript
105
+ const result = compile(ir, { platform: "nextjs" });
106
+
107
+ // Validate: Check IR structure
108
+ const check = validate(ir);
109
+
110
+ // Decompile: TypeScript → FlowIR (code audit)
111
+ const audit = decompile(code);
112
+ ```
113
+
114
+ ## Development
115
+
116
+ ```bash
117
+ # Clone and install
118
+ git clone https://github.com/timo9378/flow2code.git
119
+ cd flow2code && pnpm install
120
+
121
+ # Start dev server (visual canvas)
122
+ pnpm dev
123
+
124
+ # Run tests
125
+ pnpm test:run
126
+ ```
127
+
128
+ ## Headless Usage (No UI Required)
129
+
130
+ ```ts
131
+ import { compile } from "@timo9378/flow2code/compiler";
132
+
133
+ const ir = JSON.parse(fs.readFileSync("my-api.flow.json", "utf-8"));
134
+ const result = compile(ir, { platform: "express" });
135
+
136
+ if (result.success) {
137
+ fs.writeFileSync(result.filePath!, result.code!);
138
+ }
139
+ ```
140
+
141
+ ## Decompiler (TypeScript → Visual Flow)
142
+
143
+ ```ts
144
+ import { decompile } from "@timo9378/flow2code/compiler";
145
+
146
+ const code = fs.readFileSync("route.ts", "utf-8");
147
+ const result = decompile(code);
148
+
149
+ if (result.success) {
150
+ console.log(JSON.stringify(result.ir, null, 2));
151
+ console.log(`Confidence: ${result.confidence}`);
152
+ }
153
+ ```
154
+
155
+ ## Project Structure
156
+
157
+ ```
158
+ flow2code/
159
+ ├── src/
160
+ │ ├── app/ # Next.js App Router (UI)
161
+ ├── components/ # Visual canvas components
162
+ │ ├── store/ # Zustand canvas state management
163
+ │ ├── lib/
164
+ │ │ ├── index.ts # Headless Compiler public API
165
+ │ │ ├── ir/
166
+ │ │ │ ├── types.ts # IR Schema + TypeScript types
167
+ │ │ │ ├── validator.ts # IR validator
168
+ │ │ │ └── topological-sort.ts # Topological sort + concurrency detection
169
+ │ │ ├── compiler/
170
+ │ │ │ ├── compiler.ts # AST compiler core
171
+ │ │ │ ├── decompiler.ts # TS FlowIR reverse parser
172
+ │ │ │ ├── expression-parser.ts # Recursive Descent Parser
173
+ │ │ │ ├── type-inference.ts # Type inference engine
174
+ │ │ │ ├── symbol-table.ts # Human-readable variable naming
175
+ │ │ │ ├── plugins/ # Plugin system (extensible)
176
+ │ │ │ │ ├── index.ts # Plugin registry factory + exports
177
+ │ │ │ │ ├── types.ts # PluginRegistry interface
178
+ │ │ │ │ └── builtin.ts # 15 built-in plugins
179
+ │ │ └── platforms/ # Platform adapters
180
+ │ │ ├── types.ts # PlatformAdapter interface
181
+ │ │ │ ├── nextjs.ts # Next.js App Router
182
+ │ │ │ ├── express.ts # Express.js
183
+ │ │ │ └── cloudflare.ts # Cloudflare Workers
184
+ │ │ ├── diff/ # Semantic Diff
185
+ │ │ └── storage/ # .flow.json split/merge
186
+ │ ├── cli/ # CLI (compile/watch/init)
187
+ │ └── server/ # Standalone HTTP Server
188
+ ├── tests/ # 354 tests (Vitest)
189
+ ├── .github/workflows/ci.yml # GitHub Actions CI
190
+ ├── CONTRIBUTING.md
191
+ └── vitest.config.ts
192
+ ```
193
+
194
+ ## Node Types
195
+
196
+ | Category | Node | Compiled Output |
197
+ |----------|------|-----------------|
198
+ | Trigger | HTTP Webhook | `export async function POST(req)` |
199
+ | Trigger | Cron Job | Scheduled function |
200
+ | Trigger | Manual | Exported async function |
201
+ | 🔧 Action | Fetch API | `await fetch(...)` + try/catch |
202
+ | 🔧 Action | SQL Query | Drizzle / Prisma / Raw SQL |
203
+ | 🔧 Action | Redis Cache | Redis get/set/del |
204
+ | 🔧 Action | Custom Code | Inline TypeScript |
205
+ | 🔧 Action | Call Subflow | `await importedFunction(...)` |
206
+ | 🔀 Logic | If/Else | `if (...) { } else { }` |
207
+ | 🔀 Logic | For Loop | `for (const item of ...)` |
208
+ | 🔀 Logic | Try/Catch | `try { } catch (e) { }` |
209
+ | 🔀 Logic | Promise.all | `await Promise.allSettled([...])` |
210
+ | 📦 Variable | Declare | `const x = ...` |
211
+ | 📦 Variable | Transform | Expression transform |
212
+ | 📤 Output | Return Response | Platform-specific Response |
213
+
214
+ ## Platform Support
215
+
216
+ | Platform | Trigger Init | Response | CLI Flag |
217
+ |----------|-------------|----------|----------|
218
+ | **Next.js** (default) | `req.nextUrl.searchParams` / `req.json()` | `NextResponse.json()` | `--platform nextjs` |
219
+ | **Express** | `req.query` / `req.body` | `res.status().json()` | `--platform express` |
220
+ | **Cloudflare Workers** | `new URL(request.url)` / `request.json()` | `new Response()` | `--platform cloudflare` |
221
+
222
+ ## Contributing
223
+
224
+ See [CONTRIBUTING.md](CONTRIBUTING.md).
225
+
226
+ For detailed usage examples and workflows, see [USAGE.md](USAGE.md).
227
+
228
+ ## License
229
+
230
+ MIT