vite-plugin-windmill 1.687.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 +164 -0
- package/bin/vite-plugin-windmill +3 -0
- package/dist/cli.d.mts +4 -0
- package/dist/cli.mjs +475 -0
- package/dist/index.d.mts +128 -0
- package/dist/index.mjs +1044 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +77 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jerry
|
|
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
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# vite-plugin-windmill
|
|
2
|
+
|
|
3
|
+
Vite plugin and deploy tooling for Windmill raw apps.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- infers a Windmill raw-app path and `base` from a `.raw_app` or `__raw_app` directory
|
|
8
|
+
- injects a virtual `wmill` runtime for local dev and production builds
|
|
9
|
+
- configures authenticated `/api` proxying for Vite dev and preview when a Windmill URL and token are available
|
|
10
|
+
- assembles raw-app payloads from `raw_app.yaml`, `backend/`, and tracked app files
|
|
11
|
+
- honors repo `wmill.yaml` exclude globs when collecting app files for deploy
|
|
12
|
+
- optionally deploys the built raw app after `vite build`
|
|
13
|
+
|
|
14
|
+
## Versioning
|
|
15
|
+
|
|
16
|
+
This package version tracks Windmill minor releases. New package versions are published for new Windmill minor lines; patch releases within a line are absorbed by the `windmill-client` range and the pinned generated runtime source.
|
|
17
|
+
|
|
18
|
+
<!-- windmill-release:compat-start -->
|
|
19
|
+
|
|
20
|
+
Current release line: `1.687.x`
|
|
21
|
+
|
|
22
|
+
It currently depends on `windmill-client@^1.687.0` and bundles `rawAppWmillTs.ts` generated from `windmill-labs/windmill@v1.687.0`.
|
|
23
|
+
|
|
24
|
+
<!-- windmill-release:compat-end -->
|
|
25
|
+
|
|
26
|
+
## Install
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pnpm add -D vite vite-plugin-windmill
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
`vite` is a peer dependency. `windmill-client` and `yaml` are bundled runtime dependencies. The package currently targets Node.js 22+.
|
|
33
|
+
|
|
34
|
+
## Usage
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { defineConfig } from "vite";
|
|
38
|
+
import windmill from "vite-plugin-windmill";
|
|
39
|
+
|
|
40
|
+
export default defineConfig({
|
|
41
|
+
plugins: [windmill({ entry: "src/main.tsx" })],
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Expected project shape:
|
|
46
|
+
|
|
47
|
+
```text
|
|
48
|
+
f/example/app.raw_app/
|
|
49
|
+
raw_app.yaml
|
|
50
|
+
index.tsx
|
|
51
|
+
backend/
|
|
52
|
+
hello.py
|
|
53
|
+
hello.yaml
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
The plugin resolves the raw-app directory from `dir` or by searching upward for `raw_app.yaml`. It infers the Windmill app path from the raw-app folder name, so `f/example/app.raw_app` becomes `f/example/app`.
|
|
57
|
+
|
|
58
|
+
## Options
|
|
59
|
+
|
|
60
|
+
`windmill(options)` accepts:
|
|
61
|
+
|
|
62
|
+
- `dir`: raw-app directory. Defaults to the nearest ancestor containing `raw_app.yaml`.
|
|
63
|
+
- `entry`: entry file relative to `dir`. Defaults to `index.ts` or `index.tsx`.
|
|
64
|
+
- `path`: override the inferred Windmill app path.
|
|
65
|
+
- `base`: override the inferred raw-app base URL.
|
|
66
|
+
- `root`: workspace root used for path inference and `wmill.yaml` discovery.
|
|
67
|
+
- `workspace`: Windmill workspace override.
|
|
68
|
+
- `token`: Windmill API token override.
|
|
69
|
+
- `url`: Windmill base URL override.
|
|
70
|
+
- `proxy`: `false` to disable proxying, or an object with `context`, `target`, `token`, and any Vite proxy options.
|
|
71
|
+
- `deploy`: `true`, `false`, or `{ dry?: boolean; message?: string }`.
|
|
72
|
+
- `nonDotted`: override `wmill.yaml` `nonDottedPaths`.
|
|
73
|
+
- `ts`: default TypeScript runnable language override.
|
|
74
|
+
|
|
75
|
+
Connection resolution order:
|
|
76
|
+
|
|
77
|
+
- `workspace`: plugin option `workspace`, then `WM_WORKSPACE`
|
|
78
|
+
- `token`: plugin option `token`, then `WM_TOKEN`
|
|
79
|
+
- `url`: plugin option `url`, then `BASE_INTERNAL_URL`, then `BASE_URL`
|
|
80
|
+
|
|
81
|
+
By default, the plugin adds a shared `/api` proxy for both `vite dev` and `vite preview` when it can resolve a Windmill URL and token from plugin options or from `BASE_INTERNAL_URL` or `BASE_URL` plus `WM_TOKEN`.
|
|
82
|
+
|
|
83
|
+
Use `proxy: false` to disable it, or pass overrides such as `proxy: { context: '/wm-api', target: 'https://windmill.internal', token: process.env.WM_TOKEN }`.
|
|
84
|
+
|
|
85
|
+
## Deploy After Build
|
|
86
|
+
|
|
87
|
+
The plugin can deploy the built raw-app bundle after `vite build`.
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
windmill({
|
|
91
|
+
deploy: true,
|
|
92
|
+
entry: "src/main.tsx",
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
`deploy` also accepts an options object:
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
windmill({
|
|
100
|
+
deploy: {
|
|
101
|
+
dry: true,
|
|
102
|
+
message: "preview release",
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Behavior:
|
|
108
|
+
|
|
109
|
+
- `deploy: true`: deploy after build
|
|
110
|
+
- `deploy: { dry: true }`: validate the local deploy payload without contacting or mutating Windmill
|
|
111
|
+
- `deploy: { message: '...' }`: override the deployment message sent to Windmill
|
|
112
|
+
- explicit `deploy` config wins over `WM_DEPLOY`
|
|
113
|
+
|
|
114
|
+
If you prefer environment-driven deploys, leave `deploy` unset and use `WM_DEPLOY`:
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
WM_DEPLOY=true vite build
|
|
118
|
+
WM_DEPLOY=dry vite build
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Accepted `WM_DEPLOY` values:
|
|
122
|
+
|
|
123
|
+
- truthy: `true`, `1`, `yes`, `on`, empty string
|
|
124
|
+
- falsy: `false`, `0`, `no`, `off`
|
|
125
|
+
- dry-run: `dry`, `check`
|
|
126
|
+
|
|
127
|
+
## Deploy CLI
|
|
128
|
+
|
|
129
|
+
The package also exposes a deploy CLI:
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
vite-plugin-windmill --dir ./f/example/app.raw_app --dry
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Use `vite-plugin-windmill --help` for all flags.
|
|
136
|
+
|
|
137
|
+
## Programmatic Deploy
|
|
138
|
+
|
|
139
|
+
You can also deploy outside the Vite plugin lifecycle:
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
import { deploy } from "vite-plugin-windmill";
|
|
143
|
+
|
|
144
|
+
await deploy({
|
|
145
|
+
dir: "./f/example/app.raw_app",
|
|
146
|
+
dry: true,
|
|
147
|
+
workspace: process.env.WM_WORKSPACE,
|
|
148
|
+
token: process.env.WM_TOKEN,
|
|
149
|
+
url: process.env.BASE_URL,
|
|
150
|
+
});
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
`deploy()` resolves the same raw-app metadata as the plugin and accepts optional `bundles`, `js`, and `css` overrides when you want to supply build artifacts explicitly.
|
|
154
|
+
|
|
155
|
+
## Example Scripts
|
|
156
|
+
|
|
157
|
+
```json
|
|
158
|
+
{
|
|
159
|
+
"scripts": {
|
|
160
|
+
"bundle": "vite build",
|
|
161
|
+
"deploy": "WM_DEPLOY=\"${WM_DEPLOY:-true}\" vite build"
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
```
|
package/dist/cli.d.mts
ADDED
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { parseArgs } from "node:util";
|
|
3
|
+
import { access, readFile, readdir } from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { ApiError, AppService, setClient } from "windmill-client";
|
|
6
|
+
import { createHash } from "node:crypto";
|
|
7
|
+
import { parse } from "yaml";
|
|
8
|
+
//#region src/project.ts
|
|
9
|
+
const RAW_APP_FOLDER_SUFFIXES = [".raw_app", "__raw_app"];
|
|
10
|
+
const RAW_APP_FILE_NAME = "raw_app.yaml";
|
|
11
|
+
const WMILL_CONFIG_FILE_NAME = "wmill.yaml";
|
|
12
|
+
const DEPLOY_IGNORED_FILE_NAMES = new Set([
|
|
13
|
+
"AGENTS.md",
|
|
14
|
+
"DATATABLES.md",
|
|
15
|
+
"package-lock.json",
|
|
16
|
+
"raw_app.yaml",
|
|
17
|
+
"wmill.d.ts"
|
|
18
|
+
]);
|
|
19
|
+
const DEPLOY_IGNORED_DIRECTORIES = new Set([
|
|
20
|
+
".claude",
|
|
21
|
+
"backend",
|
|
22
|
+
"dist",
|
|
23
|
+
"node_modules",
|
|
24
|
+
"sql_to_apply"
|
|
25
|
+
]);
|
|
26
|
+
const LANGUAGE_BY_EXTENSION = {
|
|
27
|
+
"bq.sql": "bigquery",
|
|
28
|
+
"bun.ts": "bun",
|
|
29
|
+
cs: "csharp",
|
|
30
|
+
"deno.ts": "deno",
|
|
31
|
+
"duckdb.sql": "duckdb",
|
|
32
|
+
"frontend.js": "frontend",
|
|
33
|
+
go: "go",
|
|
34
|
+
gql: "graphql",
|
|
35
|
+
java: "java",
|
|
36
|
+
"ms.sql": "mssql",
|
|
37
|
+
"my.sql": "mysql",
|
|
38
|
+
"native.ts": "nativets",
|
|
39
|
+
nu: "nu",
|
|
40
|
+
"odb.sql": "oracledb",
|
|
41
|
+
"pg.sql": "postgresql",
|
|
42
|
+
php: "php",
|
|
43
|
+
"playbook.yml": "ansible",
|
|
44
|
+
ps1: "powershell",
|
|
45
|
+
py: "python3",
|
|
46
|
+
rb: "ruby",
|
|
47
|
+
rs: "rust",
|
|
48
|
+
"sf.sql": "snowflake",
|
|
49
|
+
sh: "bash",
|
|
50
|
+
ts: "bun"
|
|
51
|
+
};
|
|
52
|
+
const isNodeError = (value) => value instanceof Error && "code" in value;
|
|
53
|
+
const pathExists = async (filePath) => {
|
|
54
|
+
try {
|
|
55
|
+
await access(filePath);
|
|
56
|
+
return true;
|
|
57
|
+
} catch {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const normalizeToPosix = (value) => value.split(path.sep).join("/");
|
|
62
|
+
const dirnameIfPossible = (value) => {
|
|
63
|
+
const parent = path.dirname(value);
|
|
64
|
+
return parent === value ? void 0 : parent;
|
|
65
|
+
};
|
|
66
|
+
const findUp = async (startDir, fileName) => {
|
|
67
|
+
let currentDir = path.resolve(startDir);
|
|
68
|
+
while (true) {
|
|
69
|
+
const candidate = path.join(currentDir, fileName);
|
|
70
|
+
if (await pathExists(candidate)) return candidate;
|
|
71
|
+
const parentDir = dirnameIfPossible(currentDir);
|
|
72
|
+
if (!parentDir) return void 0;
|
|
73
|
+
currentDir = parentDir;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const parseYamlFile = async (filePath) => {
|
|
77
|
+
return parse(await readFile(filePath, "utf8"));
|
|
78
|
+
};
|
|
79
|
+
const resolveRoot = async (explicitRoot, dir) => {
|
|
80
|
+
if (explicitRoot) {
|
|
81
|
+
const root = path.resolve(explicitRoot);
|
|
82
|
+
return {
|
|
83
|
+
path: await pathExists(path.join(root, WMILL_CONFIG_FILE_NAME)) ? path.join(root, WMILL_CONFIG_FILE_NAME) : void 0,
|
|
84
|
+
root
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
const configPath = await findUp(dir, WMILL_CONFIG_FILE_NAME);
|
|
88
|
+
return {
|
|
89
|
+
path: configPath,
|
|
90
|
+
root: configPath ? path.dirname(configPath) : dir
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
const resolveDir = async (explicitDir) => {
|
|
94
|
+
const candidate = path.resolve(explicitDir ?? process.cwd());
|
|
95
|
+
const rawAppPath = await findUp(candidate, RAW_APP_FILE_NAME);
|
|
96
|
+
if (!rawAppPath) throw new Error(`Could not find ${RAW_APP_FILE_NAME} from ${candidate}`);
|
|
97
|
+
return path.dirname(rawAppPath);
|
|
98
|
+
};
|
|
99
|
+
const stripRawAppSuffix = (folderName) => {
|
|
100
|
+
for (const suffix of RAW_APP_FOLDER_SUFFIXES) if (folderName.endsWith(suffix)) return folderName.slice(0, -suffix.length);
|
|
101
|
+
};
|
|
102
|
+
const inferPath = (dir, root) => {
|
|
103
|
+
const segments = normalizeToPosix(path.relative(root, dir)).split("/").filter(Boolean);
|
|
104
|
+
if (segments.length === 0) throw new Error(`Could not infer a Windmill app path from ${dir}`);
|
|
105
|
+
const lastSegment = segments.at(-1);
|
|
106
|
+
if (!lastSegment) throw new Error(`Could not infer a Windmill app path from ${dir}`);
|
|
107
|
+
const strippedSegment = stripRawAppSuffix(lastSegment);
|
|
108
|
+
if (!strippedSegment) throw new Error(`Expected ${dir} to end in .raw_app or __raw_app so the app path can be inferred`);
|
|
109
|
+
return [...segments.slice(0, -1), strippedSegment].join("/");
|
|
110
|
+
};
|
|
111
|
+
const inferBase = (pathValue) => `/apps_raw/get/${pathValue}/`;
|
|
112
|
+
const resolveEntry = async (dir, value) => {
|
|
113
|
+
if (value) {
|
|
114
|
+
const entry = path.resolve(dir, value);
|
|
115
|
+
if (!await pathExists(entry)) throw new Error(`Entry file does not exist: ${entry}`);
|
|
116
|
+
return entry;
|
|
117
|
+
}
|
|
118
|
+
const tsEntry = path.join(dir, "index.ts");
|
|
119
|
+
if (await pathExists(tsEntry)) return tsEntry;
|
|
120
|
+
const tsxEntry = path.join(dir, "index.tsx");
|
|
121
|
+
if (await pathExists(tsxEntry)) return tsxEntry;
|
|
122
|
+
throw new Error(`Could not find index.ts or index.tsx inside ${dir}`);
|
|
123
|
+
};
|
|
124
|
+
const resolveEnv = (options) => ({
|
|
125
|
+
url: options.url ?? process.env.BASE_INTERNAL_URL ?? process.env.BASE_URL,
|
|
126
|
+
token: options.token ?? process.env.WM_TOKEN,
|
|
127
|
+
workspace: options.workspace ?? process.env.WM_WORKSPACE
|
|
128
|
+
});
|
|
129
|
+
const resolveProject = async (options = {}) => {
|
|
130
|
+
const dir = await resolveDir(options.dir);
|
|
131
|
+
const { path: config, root } = await resolveRoot(options.root, dir);
|
|
132
|
+
const wmillConfig = config ? await parseYamlFile(config) : {};
|
|
133
|
+
const pathValue = options.path ?? inferPath(dir, root);
|
|
134
|
+
const entry = await resolveEntry(dir, options.entry);
|
|
135
|
+
const { url, token, workspace } = resolveEnv(options);
|
|
136
|
+
return {
|
|
137
|
+
base: options.base ?? inferBase(pathValue),
|
|
138
|
+
config,
|
|
139
|
+
dir,
|
|
140
|
+
entry,
|
|
141
|
+
nonDotted: options.nonDotted ?? wmillConfig.nonDottedPaths ?? false,
|
|
142
|
+
path: pathValue,
|
|
143
|
+
root,
|
|
144
|
+
syncExcludes: wmillConfig.excludes ?? [],
|
|
145
|
+
ts: options.ts ?? wmillConfig.defaultTs ?? "bun",
|
|
146
|
+
workspace,
|
|
147
|
+
token,
|
|
148
|
+
url,
|
|
149
|
+
yaml: path.join(dir, RAW_APP_FILE_NAME)
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
const collectStaticFields = (fields) => Object.fromEntries(Object.entries(fields ?? {}).filter(([, field]) => field.type === "static").map(([name, field]) => [name, field.value]));
|
|
153
|
+
const createRawscriptHash = (content) => createHash("sha256").update(content ?? "").digest("hex");
|
|
154
|
+
const resolveTriggerableEntry = async (runnableId, runnable) => {
|
|
155
|
+
const staticInputs = collectStaticFields(runnable.fields);
|
|
156
|
+
const allowUserResources = Object.entries(runnable.fields ?? {}).filter(([, field]) => field.allowUserResources).map(([name]) => name);
|
|
157
|
+
if (runnable.inlineScript) return [`${runnableId}:rawscript/${createRawscriptHash(runnable.inlineScript.content)}`, {
|
|
158
|
+
allow_user_resources: allowUserResources,
|
|
159
|
+
one_of_inputs: {},
|
|
160
|
+
static_inputs: staticInputs
|
|
161
|
+
}];
|
|
162
|
+
if (runnable.path && runnable.runType) return [`${runnableId}:${runnable.runType === "hubscript" ? "script" : runnable.runType}/${runnable.path}`, {
|
|
163
|
+
allow_user_resources: allowUserResources,
|
|
164
|
+
one_of_inputs: {},
|
|
165
|
+
static_inputs: staticInputs
|
|
166
|
+
}];
|
|
167
|
+
};
|
|
168
|
+
const generateRawAppPolicy = async (runnables, policy, isPublic) => {
|
|
169
|
+
const resolvedTriggerableEntries = (await Promise.all(Object.entries(runnables).map(async ([runnableId, runnable]) => resolveTriggerableEntry(runnableId, runnable)))).filter((entry) => entry !== void 0);
|
|
170
|
+
return {
|
|
171
|
+
...policy,
|
|
172
|
+
execution_mode: isPublic ? "anonymous" : "publisher",
|
|
173
|
+
triggerables_v2: Object.fromEntries(resolvedTriggerableEntries)
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
const resolveRunnableLanguage = (extension, ts) => {
|
|
177
|
+
const language = LANGUAGE_BY_EXTENSION[extension];
|
|
178
|
+
if (!language) return void 0;
|
|
179
|
+
return extension === "ts" ? ts : language;
|
|
180
|
+
};
|
|
181
|
+
const findRunnableContentFile = async (backendDir, runnableId, allFileNames) => {
|
|
182
|
+
for (const fileName of allFileNames) {
|
|
183
|
+
if (fileName.endsWith(".yaml") || fileName.endsWith(".lock")) continue;
|
|
184
|
+
if (!fileName.startsWith(`${runnableId}.`)) continue;
|
|
185
|
+
const extension = fileName.slice(runnableId.length + 1);
|
|
186
|
+
if (!resolveRunnableLanguage(extension, "bun")) continue;
|
|
187
|
+
return {
|
|
188
|
+
content: await readFile(path.join(backendDir, fileName), "utf8"),
|
|
189
|
+
extension
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
const getRunnableIdFromCodeFile = (fileName) => {
|
|
194
|
+
if (fileName.endsWith(".yaml") || fileName.endsWith(".lock")) return void 0;
|
|
195
|
+
for (const extension of Object.keys(LANGUAGE_BY_EXTENSION)) if (fileName.endsWith(`.${extension}`)) return fileName.slice(0, -(extension.length + 1));
|
|
196
|
+
};
|
|
197
|
+
const inlinePathPrefix = "!inline ";
|
|
198
|
+
const dereferenceInlineValue = async (value, localPath) => {
|
|
199
|
+
if (typeof value !== "string" || !value.startsWith(inlinePathPrefix)) return value;
|
|
200
|
+
const relativePath = value.slice(8);
|
|
201
|
+
return readFile(path.join(localPath, relativePath), "utf8");
|
|
202
|
+
};
|
|
203
|
+
const cloneRunnable = async (value, localPath) => {
|
|
204
|
+
if (Array.isArray(value)) return Promise.all(value.map(async (item) => cloneRunnable(item, localPath)));
|
|
205
|
+
if (typeof value !== "object" || value === null) return dereferenceInlineValue(value, localPath);
|
|
206
|
+
const entries = await Promise.all(Object.entries(value).map(async ([key, entryValue]) => [key, await cloneRunnable(entryValue, localPath)]));
|
|
207
|
+
return Object.fromEntries(entries);
|
|
208
|
+
};
|
|
209
|
+
const loadRunnablesFromBackend = async (backendDir, ts = "bun") => {
|
|
210
|
+
const runnables = {};
|
|
211
|
+
try {
|
|
212
|
+
const allFileNames = (await readdir(backendDir, { withFileTypes: true })).filter((entry) => entry.isFile()).map((entry) => entry.name);
|
|
213
|
+
const processedIds = /* @__PURE__ */ new Set();
|
|
214
|
+
for (const fileName of allFileNames) {
|
|
215
|
+
if (!fileName.endsWith(".yaml")) continue;
|
|
216
|
+
const runnableId = fileName.slice(0, -5);
|
|
217
|
+
processedIds.add(runnableId);
|
|
218
|
+
const runnable = await parseYamlFile(path.join(backendDir, fileName));
|
|
219
|
+
if (runnable.type === "inline") {
|
|
220
|
+
const contentFile = await findRunnableContentFile(backendDir, runnableId, allFileNames);
|
|
221
|
+
if (contentFile) {
|
|
222
|
+
const lockPath = path.join(backendDir, `${runnableId}.lock`);
|
|
223
|
+
let lock;
|
|
224
|
+
try {
|
|
225
|
+
lock = await readFile(lockPath, "utf8");
|
|
226
|
+
} catch (error) {
|
|
227
|
+
if (!isNodeError(error) || error.code !== "ENOENT") throw error;
|
|
228
|
+
}
|
|
229
|
+
runnable.inlineScript = {
|
|
230
|
+
...runnable.inlineScript,
|
|
231
|
+
content: contentFile.content,
|
|
232
|
+
language: resolveRunnableLanguage(contentFile.extension, ts),
|
|
233
|
+
...lock ? { lock } : {}
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
} else if (runnable.type === "flow" || runnable.type === "hubscript" || runnable.type === "script") {
|
|
237
|
+
const { type, schema: _schema, ...rest } = runnable;
|
|
238
|
+
runnables[runnableId] = {
|
|
239
|
+
...rest,
|
|
240
|
+
runType: type,
|
|
241
|
+
type: "path"
|
|
242
|
+
};
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
runnables[runnableId] = runnable;
|
|
246
|
+
}
|
|
247
|
+
for (const fileName of allFileNames) {
|
|
248
|
+
const runnableId = getRunnableIdFromCodeFile(fileName);
|
|
249
|
+
if (!runnableId || processedIds.has(runnableId)) continue;
|
|
250
|
+
processedIds.add(runnableId);
|
|
251
|
+
const contentFile = await findRunnableContentFile(backendDir, runnableId, allFileNames);
|
|
252
|
+
if (!contentFile) continue;
|
|
253
|
+
const lockPath = path.join(backendDir, `${runnableId}.lock`);
|
|
254
|
+
let lock;
|
|
255
|
+
try {
|
|
256
|
+
lock = await readFile(lockPath, "utf8");
|
|
257
|
+
} catch (error) {
|
|
258
|
+
if (!isNodeError(error) || error.code !== "ENOENT") throw error;
|
|
259
|
+
}
|
|
260
|
+
runnables[runnableId] = {
|
|
261
|
+
inlineScript: {
|
|
262
|
+
content: contentFile.content,
|
|
263
|
+
language: resolveRunnableLanguage(contentFile.extension, ts),
|
|
264
|
+
...lock ? { lock } : {}
|
|
265
|
+
},
|
|
266
|
+
type: "inline"
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
} catch (error) {
|
|
270
|
+
if (!isNodeError(error) || error.code !== "ENOENT") throw error;
|
|
271
|
+
}
|
|
272
|
+
return runnables;
|
|
273
|
+
};
|
|
274
|
+
const matchesSyncExclude = (relativePath, excludes) => excludes.some((pattern) => path.posix.matchesGlob(relativePath, pattern));
|
|
275
|
+
const collectAppFiles = async (dir, options = {}) => {
|
|
276
|
+
const files = {};
|
|
277
|
+
const root = options.root ? path.resolve(options.root) : dir;
|
|
278
|
+
const excludes = options.excludes ?? [];
|
|
279
|
+
const walk = async (currentDir, relativeDir = "/") => {
|
|
280
|
+
const entries = await readdir(currentDir, { withFileTypes: true });
|
|
281
|
+
for (const entry of entries) {
|
|
282
|
+
const fullPath = path.join(currentDir, entry.name);
|
|
283
|
+
const relativePath = `${relativeDir}${entry.name}`;
|
|
284
|
+
const relativeToRoot = normalizeToPosix(path.relative(root, fullPath));
|
|
285
|
+
if (entry.isDirectory()) {
|
|
286
|
+
if (DEPLOY_IGNORED_DIRECTORIES.has(entry.name)) continue;
|
|
287
|
+
await walk(fullPath, `${relativePath}/`);
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
if (DEPLOY_IGNORED_FILE_NAMES.has(entry.name)) continue;
|
|
291
|
+
if (matchesSyncExclude(relativeToRoot, excludes)) continue;
|
|
292
|
+
files[relativePath] = await readFile(fullPath, "utf8");
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
await walk(dir);
|
|
296
|
+
return files;
|
|
297
|
+
};
|
|
298
|
+
const loadRawAppProject = async (project) => {
|
|
299
|
+
const config = await parseYamlFile(project.yaml);
|
|
300
|
+
const backendDir = path.join(project.dir, "backend");
|
|
301
|
+
const backendRunnables = await loadRunnablesFromBackend(backendDir, project.ts);
|
|
302
|
+
const runnables = await cloneRunnable(Object.keys(backendRunnables).length > 0 ? backendRunnables : config.runnables ?? {}, backendDir);
|
|
303
|
+
const files = await collectAppFiles(project.dir, {
|
|
304
|
+
excludes: project.syncExcludes,
|
|
305
|
+
root: project.root
|
|
306
|
+
});
|
|
307
|
+
return {
|
|
308
|
+
config,
|
|
309
|
+
files,
|
|
310
|
+
policy: await generateRawAppPolicy(runnables, config.policy, Boolean(config.public)),
|
|
311
|
+
runnables,
|
|
312
|
+
value: {
|
|
313
|
+
...config.data !== void 0 ? { data: config.data } : {},
|
|
314
|
+
files,
|
|
315
|
+
runnables
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
};
|
|
319
|
+
//#endregion
|
|
320
|
+
//#region src/deploy.ts
|
|
321
|
+
const defaultDeploymentMessage = () => {
|
|
322
|
+
const sha = process.env.GITHUB_SHA;
|
|
323
|
+
return sha ? `vite-plugin-windmill deploy ${sha}` : "vite-plugin-windmill deploy";
|
|
324
|
+
};
|
|
325
|
+
const requireProjectConnection = (project) => {
|
|
326
|
+
if (!project.workspace) throw new Error("Missing Windmill workspace. Set `workspace` or `WM_WORKSPACE`.");
|
|
327
|
+
if (!project.token) throw new Error("Missing Windmill token. Set `token` or `WM_TOKEN`.");
|
|
328
|
+
if (!project.url) throw new Error("Missing Windmill URL. Set `url`, `BASE_INTERNAL_URL`, or `BASE_URL`.");
|
|
329
|
+
return {
|
|
330
|
+
url: project.url,
|
|
331
|
+
token: project.token,
|
|
332
|
+
workspace: project.workspace
|
|
333
|
+
};
|
|
334
|
+
};
|
|
335
|
+
const readBundleFile = async (filePath, fallback = "") => {
|
|
336
|
+
try {
|
|
337
|
+
return await readFile(filePath, "utf8");
|
|
338
|
+
} catch (error) {
|
|
339
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") return fallback;
|
|
340
|
+
throw error;
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
const readBundleContents = async (dir, js = path.join(dir, "dist/windmill/bundle.js"), css = path.join(dir, "dist/windmill/bundle.css")) => ({
|
|
344
|
+
css: await readBundleFile(css, ""),
|
|
345
|
+
js: await readBundleFile(js)
|
|
346
|
+
});
|
|
347
|
+
const findExistingRawApp = async (workspace, pathValue) => {
|
|
348
|
+
try {
|
|
349
|
+
return await AppService.getAppByPath({
|
|
350
|
+
workspace,
|
|
351
|
+
path: pathValue
|
|
352
|
+
});
|
|
353
|
+
} catch (error) {
|
|
354
|
+
if (error instanceof ApiError && error.status === 404) return void 0;
|
|
355
|
+
throw error;
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
/**
|
|
359
|
+
* Deploys a raw app to Windmill using `windmill-client` instead of shelling out to the CLI.
|
|
360
|
+
*/
|
|
361
|
+
const deploy = async (options) => {
|
|
362
|
+
const dir = options.dir ?? process.cwd();
|
|
363
|
+
const project = await resolveProject({
|
|
364
|
+
...options,
|
|
365
|
+
dir
|
|
366
|
+
});
|
|
367
|
+
const rawAppProject = await loadRawAppProject(project);
|
|
368
|
+
const connection = requireProjectConnection(project);
|
|
369
|
+
const bundles = options.bundles ?? await readBundleContents(dir, options.js, options.css);
|
|
370
|
+
if (!bundles.js) throw new Error("Cannot deploy a Windmill raw app without a JavaScript bundle");
|
|
371
|
+
if (options.dry) return {
|
|
372
|
+
action: "dry-run",
|
|
373
|
+
base: project.base,
|
|
374
|
+
path: project.path,
|
|
375
|
+
workspace: connection.workspace
|
|
376
|
+
};
|
|
377
|
+
setClient(connection.token, connection.url);
|
|
378
|
+
const existingApp = await findExistingRawApp(connection.workspace, project.path);
|
|
379
|
+
if (existingApp && !existingApp.raw_app) throw new Error(`${project.path} exists remotely but is not a raw app`);
|
|
380
|
+
const message = options.message ?? defaultDeploymentMessage();
|
|
381
|
+
const appPayload = {
|
|
382
|
+
...rawAppProject.config.custom_path ? { custom_path: rawAppProject.config.custom_path } : {},
|
|
383
|
+
deployment_message: message,
|
|
384
|
+
path: project.path,
|
|
385
|
+
policy: rawAppProject.policy,
|
|
386
|
+
summary: rawAppProject.config.summary,
|
|
387
|
+
value: rawAppProject.value
|
|
388
|
+
};
|
|
389
|
+
if (existingApp) {
|
|
390
|
+
await AppService.updateAppRaw({
|
|
391
|
+
workspace: connection.workspace,
|
|
392
|
+
path: project.path,
|
|
393
|
+
formData: {
|
|
394
|
+
app: appPayload,
|
|
395
|
+
css: bundles.css,
|
|
396
|
+
js: bundles.js
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
return {
|
|
400
|
+
action: "update",
|
|
401
|
+
base: project.base,
|
|
402
|
+
path: project.path,
|
|
403
|
+
workspace: connection.workspace
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
await AppService.createAppRaw({
|
|
407
|
+
workspace: connection.workspace,
|
|
408
|
+
formData: {
|
|
409
|
+
app: appPayload,
|
|
410
|
+
css: bundles.css,
|
|
411
|
+
js: bundles.js
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
return {
|
|
415
|
+
action: "create",
|
|
416
|
+
base: project.base,
|
|
417
|
+
path: project.path,
|
|
418
|
+
workspace: connection.workspace
|
|
419
|
+
};
|
|
420
|
+
};
|
|
421
|
+
//#endregion
|
|
422
|
+
//#region src/cli.ts
|
|
423
|
+
const main = async () => {
|
|
424
|
+
const { values } = parseArgs({ options: {
|
|
425
|
+
base: { type: "string" },
|
|
426
|
+
css: { type: "string" },
|
|
427
|
+
dir: { type: "string" },
|
|
428
|
+
dry: { type: "boolean" },
|
|
429
|
+
entry: { type: "string" },
|
|
430
|
+
js: { type: "string" },
|
|
431
|
+
message: { type: "string" },
|
|
432
|
+
path: { type: "string" },
|
|
433
|
+
root: { type: "string" },
|
|
434
|
+
token: { type: "string" },
|
|
435
|
+
url: { type: "string" },
|
|
436
|
+
workspace: { type: "string" },
|
|
437
|
+
help: {
|
|
438
|
+
type: "boolean",
|
|
439
|
+
short: "h"
|
|
440
|
+
}
|
|
441
|
+
} });
|
|
442
|
+
if (values.help) return console.log(`
|
|
443
|
+
Usage: vite-plugin-windmill [options]
|
|
444
|
+
|
|
445
|
+
Deploy a built Windmill raw app bundle.
|
|
446
|
+
|
|
447
|
+
Options:
|
|
448
|
+
-h, --help Show this help message
|
|
449
|
+
--base <path> Override the raw-app base path
|
|
450
|
+
--css <path> Path to the CSS file to include in the deployment
|
|
451
|
+
--dir <path> Raw-app directory to resolve and deploy from
|
|
452
|
+
--dry Perform a dry run without making any changes
|
|
453
|
+
--entry <path> Override the raw-app entry file
|
|
454
|
+
--js <path> Path to the JavaScript file to include in the deployment
|
|
455
|
+
--message <text> Deployment message or description
|
|
456
|
+
--path <path> Override the inferred Windmill raw-app path
|
|
457
|
+
--root <path> Override the workspace root used to infer the app path
|
|
458
|
+
--token <token> Windmill API token for authentication
|
|
459
|
+
--url <url> Windmill instance URL
|
|
460
|
+
--workspace <name> Windmill workspace name
|
|
461
|
+
|
|
462
|
+
Connection resolution order:
|
|
463
|
+
workspace: --workspace, WM_WORKSPACE
|
|
464
|
+
token: --token, WM_TOKEN
|
|
465
|
+
url: --url, BASE_INTERNAL_URL, BASE_URL
|
|
466
|
+
`.trim());
|
|
467
|
+
const result = await deploy(values);
|
|
468
|
+
console.log(JSON.stringify(result, null, 2));
|
|
469
|
+
};
|
|
470
|
+
main().catch((error) => {
|
|
471
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
472
|
+
process.exitCode = 1;
|
|
473
|
+
});
|
|
474
|
+
//#endregion
|
|
475
|
+
export { main };
|