@nifrajs/cli 5.12.1 → 5.13.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/CHANGELOG.md +45 -0
- package/dist/cli.d.ts +9 -2
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +140 -19
- package/dist/cli.js.map +1 -1
- package/dist/command-catalog.d.ts +29 -1
- package/dist/command-catalog.d.ts.map +1 -1
- package/dist/command-catalog.js +102 -4
- package/dist/command-catalog.js.map +1 -1
- package/dist/deployment-adapters.d.ts +12 -0
- package/dist/deployment-adapters.d.ts.map +1 -0
- package/dist/deployment-adapters.js +92 -0
- package/dist/deployment-adapters.js.map +1 -0
- package/dist/dev-bun-config.d.ts +4 -3
- package/dist/dev-bun-config.d.ts.map +1 -1
- package/dist/dev-bun-config.js +14 -4
- package/dist/dev-bun-config.js.map +1 -1
- package/dist/init-agents.d.ts +5 -0
- package/dist/init-agents.d.ts.map +1 -1
- package/dist/init-agents.js +1 -1
- package/dist/init-agents.js.map +1 -1
- package/dist/load.d.ts +12 -3
- package/dist/load.d.ts.map +1 -1
- package/dist/load.js +19 -0
- package/dist/load.js.map +1 -1
- package/dist/mcp-http.js +1 -1
- package/dist/mcp-root.d.ts.map +1 -1
- package/dist/mcp-root.js +21 -3
- package/dist/mcp-root.js.map +1 -1
- package/dist/port.d.ts +6 -3
- package/dist/port.d.ts.map +1 -1
- package/dist/port.js +19 -7
- package/dist/port.js.map +1 -1
- package/dist/recipes/5.13.0.d.ts +9 -0
- package/dist/recipes/5.13.0.d.ts.map +1 -0
- package/dist/recipes/5.13.0.js +40 -0
- package/dist/recipes/5.13.0.js.map +1 -0
- package/dist/recipes/index.d.ts.map +1 -1
- package/dist/recipes/index.js +2 -0
- package/dist/recipes/index.js.map +1 -1
- package/dist/rules/routes.js +2 -2
- package/dist/shell-files.d.ts +44 -0
- package/dist/shell-files.d.ts.map +1 -0
- package/dist/shell-files.js +201 -0
- package/dist/shell-files.js.map +1 -0
- package/dist/shell-init.d.ts +33 -0
- package/dist/shell-init.d.ts.map +1 -0
- package/dist/shell-init.js +99 -0
- package/dist/shell-init.js.map +1 -0
- package/docs/examples.json +97 -1
- package/docs/llms-full.txt +989 -40
- package/docs/types.json +606 -36
- package/package.json +11 -11
- package/src/cli.ts +185 -35
- package/src/command-catalog.ts +144 -7
- package/src/deployment-adapters.ts +107 -0
- package/src/dev-bun-config.ts +21 -2
- package/src/init-agents.ts +1 -1
- package/src/load.ts +36 -3
- package/src/mcp-http.ts +1 -1
- package/src/mcp-root.ts +21 -3
- package/src/port.ts +25 -8
- package/src/recipes/5.13.0.ts +41 -0
- package/src/recipes/index.ts +2 -0
- package/src/rules/routes.ts +2 -2
- package/src/shell-files.ts +225 -0
- package/src/shell-init.ts +139 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a Tauri 2 shell project is made of, as text: the transport module the pages reach their server
|
|
3
|
+
* through and the `src-tauri/` project that wraps the static build. Pure and import-free, so
|
|
4
|
+
* `create-nifra` carries a byte-identical copy (`bun run gen:vendored`) and a scaffolded app gets what
|
|
5
|
+
* `nifra shell tauri` writes.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export const TRANSPORT_PATH = "frontend/shell-transport.ts"
|
|
9
|
+
export const SHELL_DIST = "dist-shell"
|
|
10
|
+
export const SHELL_DEV_PORT = 3112
|
|
11
|
+
export const DEFAULT_SHELL_ORIGIN = "http://127.0.0.1:3111"
|
|
12
|
+
|
|
13
|
+
/** A name safe as a crate and a library: lower-case letters, digits and `-`, starting with a letter. */
|
|
14
|
+
export function crateName(raw: string): string {
|
|
15
|
+
const base = raw
|
|
16
|
+
.replace(/^@[^/]+\//, "")
|
|
17
|
+
.toLowerCase()
|
|
18
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
19
|
+
.replace(/^-|-$/g, "")
|
|
20
|
+
if (base === "") return "app"
|
|
21
|
+
return /^[a-z]/.test(base) ? base : `app-${base}`
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** `https://api.example.com/x` -> `https://api.example.com`; throws on anything but an http(s) origin. */
|
|
25
|
+
export function originOf(raw: string): string {
|
|
26
|
+
let url: URL
|
|
27
|
+
try {
|
|
28
|
+
url = new URL(raw)
|
|
29
|
+
} catch {
|
|
30
|
+
throw new Error(`[nifra] --origin must be a URL such as https://api.example.com, got "${raw}"`)
|
|
31
|
+
}
|
|
32
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
33
|
+
throw new Error(`[nifra] --origin must be http or https, got "${raw}"`)
|
|
34
|
+
}
|
|
35
|
+
return url.origin
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function transportSource(origin: string): string {
|
|
39
|
+
return `import { serverFetch } from "@nifrajs/web/shell"
|
|
40
|
+
|
|
41
|
+
// The pages ship inside the shell; the server is another origin. PUBLIC_SHELL_ORIGIN overrides it per
|
|
42
|
+
// build (an Android emulator reaches the host's loopback as 10.0.2.2, the iOS Simulator as 127.0.0.1);
|
|
43
|
+
// the content security policy in src-tauri/tauri.conf.json must then list that origin under connect-src.
|
|
44
|
+
function shellOrigin(): string | undefined {
|
|
45
|
+
try {
|
|
46
|
+
return process.env.PUBLIC_SHELL_ORIGIN
|
|
47
|
+
} catch {
|
|
48
|
+
return undefined
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default serverFetch({ origin: shellOrigin() ?? ${JSON.stringify(origin)} })
|
|
53
|
+
`
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const libName = (crate: string): string => `${crate.replace(/-/g, "_")}_lib`
|
|
57
|
+
|
|
58
|
+
export function cargoToml(crate: string): string {
|
|
59
|
+
return `[package]
|
|
60
|
+
name = ${JSON.stringify(crate)}
|
|
61
|
+
version = "0.1.0"
|
|
62
|
+
edition = "2021"
|
|
63
|
+
|
|
64
|
+
[lib]
|
|
65
|
+
name = ${JSON.stringify(libName(crate))}
|
|
66
|
+
crate-type = ["staticlib", "cdylib", "rlib"]
|
|
67
|
+
|
|
68
|
+
[build-dependencies]
|
|
69
|
+
tauri-build = { version = "2", features = [] }
|
|
70
|
+
|
|
71
|
+
[dependencies]
|
|
72
|
+
tauri = { version = "2", features = [] }
|
|
73
|
+
|
|
74
|
+
[profile.release]
|
|
75
|
+
opt-level = "s"
|
|
76
|
+
lto = true
|
|
77
|
+
codegen-units = 1
|
|
78
|
+
panic = "abort"
|
|
79
|
+
strip = true
|
|
80
|
+
`
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export const BUILD_RS = `fn main() {
|
|
84
|
+
tauri_build::build()
|
|
85
|
+
}
|
|
86
|
+
`
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The window opens hidden and shows when the page has painted, so the user never sees the blank
|
|
90
|
+
* window between the web view appearing and the first paint. A timer shows it anyway if no paint
|
|
91
|
+
* is reported, so a page that fails to load cannot leave the app invisible.
|
|
92
|
+
*/
|
|
93
|
+
export const LIB_RS = `use tauri::{WebviewUrl, WebviewWindowBuilder};
|
|
94
|
+
|
|
95
|
+
const SHOW_WHEN_PAINTED: &str = r#"
|
|
96
|
+
(function () {
|
|
97
|
+
try {
|
|
98
|
+
new PerformanceObserver(function (list) {
|
|
99
|
+
for (var entry of list.getEntries()) {
|
|
100
|
+
if (entry.name === 'first-contentful-paint') {
|
|
101
|
+
window.__TAURI_INTERNALS__.invoke('shell_painted');
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}).observe({ type: 'paint', buffered: true });
|
|
105
|
+
} catch (_) {}
|
|
106
|
+
})();
|
|
107
|
+
"#;
|
|
108
|
+
|
|
109
|
+
#[tauri::command]
|
|
110
|
+
fn shell_painted(window: tauri::WebviewWindow) {
|
|
111
|
+
let _ = window.show();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
|
115
|
+
pub fn run() {
|
|
116
|
+
tauri::Builder::default()
|
|
117
|
+
.invoke_handler(tauri::generate_handler![shell_painted])
|
|
118
|
+
.setup(|app| {
|
|
119
|
+
let builder = WebviewWindowBuilder::new(app, "main", WebviewUrl::App("index.html".into()))
|
|
120
|
+
.initialization_script(SHOW_WHEN_PAINTED);
|
|
121
|
+
#[cfg(desktop)]
|
|
122
|
+
let builder = builder
|
|
123
|
+
.title(app.config().product_name.clone().unwrap_or_default())
|
|
124
|
+
.inner_size(900.0, 640.0)
|
|
125
|
+
.visible(false);
|
|
126
|
+
#[allow(unused_variables)]
|
|
127
|
+
let window = builder.build()?;
|
|
128
|
+
#[cfg(desktop)]
|
|
129
|
+
std::thread::spawn(move || {
|
|
130
|
+
std::thread::sleep(std::time::Duration::from_secs(3));
|
|
131
|
+
let _ = window.show();
|
|
132
|
+
});
|
|
133
|
+
Ok(())
|
|
134
|
+
})
|
|
135
|
+
.run(tauri::generate_context!())
|
|
136
|
+
.expect("error while running the app");
|
|
137
|
+
}
|
|
138
|
+
`
|
|
139
|
+
|
|
140
|
+
export function mainRs(crate: string): string {
|
|
141
|
+
return `// Keeps a release build on Windows from opening a console window.
|
|
142
|
+
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
|
143
|
+
|
|
144
|
+
fn main() {
|
|
145
|
+
${libName(crate)}::run()
|
|
146
|
+
}
|
|
147
|
+
`
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export const CAPABILITY = `{
|
|
151
|
+
"identifier": "default",
|
|
152
|
+
"windows": ["main"],
|
|
153
|
+
"permissions": ["core:default"]
|
|
154
|
+
}
|
|
155
|
+
`
|
|
156
|
+
|
|
157
|
+
export function tauriConf(name: string, id: string, origin: string): string {
|
|
158
|
+
const socket = origin.replace(/^http/, "ws")
|
|
159
|
+
const conf = {
|
|
160
|
+
$schema: "https://schema.tauri.app/config/2",
|
|
161
|
+
productName: name,
|
|
162
|
+
version: "0.1.0",
|
|
163
|
+
identifier: id,
|
|
164
|
+
build: {
|
|
165
|
+
frontendDist: `../${SHELL_DIST}`,
|
|
166
|
+
devUrl: `http://localhost:${SHELL_DEV_PORT}`,
|
|
167
|
+
beforeDevCommand: `nifra dev --port ${SHELL_DEV_PORT} --transport ./${TRANSPORT_PATH}`,
|
|
168
|
+
beforeBuildCommand: `nifra build --target static --out ${SHELL_DIST} --transport ./${TRANSPORT_PATH} --prerender /`,
|
|
169
|
+
},
|
|
170
|
+
app: {
|
|
171
|
+
windows: [],
|
|
172
|
+
security: {
|
|
173
|
+
csp: `default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; connect-src 'self' ${origin} ${socket}`,
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
bundle: { active: true, targets: "all" },
|
|
177
|
+
}
|
|
178
|
+
return `${JSON.stringify(conf, null, 2)}\n`
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export const SHELL_GITIGNORE = `/target/
|
|
182
|
+
/gen/schemas
|
|
183
|
+
/gen/android/app/build
|
|
184
|
+
/gen/android/.gradle
|
|
185
|
+
`
|
|
186
|
+
|
|
187
|
+
export interface ShellSpec {
|
|
188
|
+
/** The Cargo package name. */
|
|
189
|
+
readonly crate: string
|
|
190
|
+
readonly origin: string
|
|
191
|
+
readonly name: string
|
|
192
|
+
readonly id: string
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Validate the options and fill the defaults; `packageName` is the app's `package.json` name. */
|
|
196
|
+
export function resolveShellSpec(options: {
|
|
197
|
+
readonly origin?: string | undefined
|
|
198
|
+
readonly name?: string | undefined
|
|
199
|
+
readonly id?: string | undefined
|
|
200
|
+
readonly packageName?: string | undefined
|
|
201
|
+
}): ShellSpec {
|
|
202
|
+
const origin = originOf(options.origin ?? DEFAULT_SHELL_ORIGIN)
|
|
203
|
+
const raw = options.name ?? options.packageName ?? "app"
|
|
204
|
+
const crate = crateName(raw)
|
|
205
|
+
const name = options.name ?? raw.replace(/^@[^/]+\//, "")
|
|
206
|
+
const id = options.id ?? `com.example.${crate.replace(/-/g, "")}`
|
|
207
|
+
if (!/^[A-Za-z][A-Za-z0-9-]*(\.[A-Za-z][A-Za-z0-9-]*)+$/.test(id)) {
|
|
208
|
+
throw new Error(`[nifra] --id must look like com.example.app, got "${id}"`)
|
|
209
|
+
}
|
|
210
|
+
return { crate, origin, name, id }
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Every file of the shell, by project-relative path. */
|
|
214
|
+
export function shellFiles(spec: ShellSpec): readonly (readonly [string, string])[] {
|
|
215
|
+
return [
|
|
216
|
+
[TRANSPORT_PATH, transportSource(spec.origin)],
|
|
217
|
+
["src-tauri/Cargo.toml", cargoToml(spec.crate)],
|
|
218
|
+
["src-tauri/build.rs", BUILD_RS],
|
|
219
|
+
["src-tauri/tauri.conf.json", tauriConf(spec.name, spec.id, spec.origin)],
|
|
220
|
+
["src-tauri/capabilities/default.json", CAPABILITY],
|
|
221
|
+
["src-tauri/src/lib.rs", LIB_RS],
|
|
222
|
+
["src-tauri/src/main.rs", mainRs(spec.crate)],
|
|
223
|
+
["src-tauri/.gitignore", SHELL_GITIGNORE],
|
|
224
|
+
]
|
|
225
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `nifra shell tauri` - add a Tauri 2 shell to an existing app: the transport module the pages reach
|
|
3
|
+
* their server through, and the `src-tauri/` project that wraps the static build.
|
|
4
|
+
*
|
|
5
|
+
* Like `init-agents` it writes files only, never overwrites one without `--force`, and touches neither
|
|
6
|
+
* `package.json` nor `nifra.config.ts`: the shell build passes `--transport` itself, so the web build of
|
|
7
|
+
* the same app keeps its own fetch.
|
|
8
|
+
*/
|
|
9
|
+
import { existsSync } from "node:fs"
|
|
10
|
+
import { readFile } from "node:fs/promises"
|
|
11
|
+
import { join } from "node:path"
|
|
12
|
+
import { type InitAgentsFileResult, writeOwned } from "./init-agents.ts"
|
|
13
|
+
import { resolveShellSpec, SHELL_DIST, shellFiles } from "./shell-files.ts"
|
|
14
|
+
|
|
15
|
+
export interface ShellInitOptions {
|
|
16
|
+
/** The server the pages talk to. */
|
|
17
|
+
readonly origin?: string | undefined
|
|
18
|
+
/** The app's display name. Default: `package.json`'s `name`. */
|
|
19
|
+
readonly name?: string | undefined
|
|
20
|
+
/** The bundle identifier. Default `com.example.<name>`. */
|
|
21
|
+
readonly id?: string | undefined
|
|
22
|
+
readonly force?: boolean
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ShellInitResult {
|
|
26
|
+
readonly files: readonly InitAgentsFileResult[]
|
|
27
|
+
readonly origin: string
|
|
28
|
+
readonly id: string
|
|
29
|
+
readonly name: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function packageName(cwd: string): Promise<string | undefined> {
|
|
33
|
+
try {
|
|
34
|
+
const parsed: unknown = JSON.parse(await readFile(join(cwd, "package.json"), "utf8"))
|
|
35
|
+
const name = typeof parsed === "object" && parsed !== null ? Reflect.get(parsed, "name") : null
|
|
36
|
+
return typeof name === "string" && name !== "" ? name : undefined
|
|
37
|
+
} catch {
|
|
38
|
+
return undefined
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export async function runShellInit(
|
|
43
|
+
cwd: string,
|
|
44
|
+
options: ShellInitOptions = {},
|
|
45
|
+
): Promise<ShellInitResult> {
|
|
46
|
+
const pkg = await packageName(cwd)
|
|
47
|
+
const spec = resolveShellSpec({ ...options, packageName: pkg })
|
|
48
|
+
const force = options.force === true
|
|
49
|
+
const files: InitAgentsFileResult[] = []
|
|
50
|
+
for (const [path, content] of shellFiles(spec)) {
|
|
51
|
+
files.push(await writeOwned(cwd, path, content, force))
|
|
52
|
+
}
|
|
53
|
+
return { files, origin: spec.origin, id: spec.id, name: spec.name }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** What the developer does after the files are written, as the CLI prints it and the JSON carries it. */
|
|
57
|
+
export function shellNextSteps(): readonly string[] {
|
|
58
|
+
return [
|
|
59
|
+
"bun add -d @tauri-apps/cli",
|
|
60
|
+
"bunx tauri icon <a square PNG, 1024x1024 recommended>: the app icon set Tauri needs to build",
|
|
61
|
+
"list tauri://localhost and http://tauri.localhost on the server: CORS, route allowedOrigins, serverFunctions origins, live trustedOrigins (docs/native-shells)",
|
|
62
|
+
"the shell build prerenders / (--prerender in src-tauri/tauri.conf.json's beforeBuildCommand): list more pages there, comma-separated",
|
|
63
|
+
`bunx tauri dev, or bunx tauri build (the page build lands in ${SHELL_DIST}/)`,
|
|
64
|
+
]
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export type MobilePlatform = "android" | "ios"
|
|
68
|
+
|
|
69
|
+
export interface MobileInitResult {
|
|
70
|
+
readonly platform: MobilePlatform
|
|
71
|
+
readonly ok: boolean
|
|
72
|
+
/** Why it did not run or what it said when it failed; empty on success. */
|
|
73
|
+
readonly note: string
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const MOBILE_TIMEOUT_MS = 300_000
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* `tauri <platform> init` for each platform, with the project's own Tauri CLI. It generates the native
|
|
80
|
+
* project under `src-tauri/gen/`, which needs the platform's toolchain (Android SDK and NDK, Xcode), so a
|
|
81
|
+
* failure is reported per platform rather than thrown and the files already written stay.
|
|
82
|
+
*/
|
|
83
|
+
export async function initMobile(
|
|
84
|
+
cwd: string,
|
|
85
|
+
platforms: readonly MobilePlatform[],
|
|
86
|
+
host: NodeJS.Platform = process.platform,
|
|
87
|
+
): Promise<MobileInitResult[]> {
|
|
88
|
+
const bin = join(
|
|
89
|
+
cwd,
|
|
90
|
+
"node_modules",
|
|
91
|
+
".bin",
|
|
92
|
+
process.platform === "win32" ? "tauri.cmd" : "tauri",
|
|
93
|
+
)
|
|
94
|
+
const results: MobileInitResult[] = []
|
|
95
|
+
for (const platform of platforms) {
|
|
96
|
+
if (platform === "ios" && host !== "darwin") {
|
|
97
|
+
results.push({ platform, ok: false, note: "an iOS project needs macOS and Xcode" })
|
|
98
|
+
continue
|
|
99
|
+
}
|
|
100
|
+
if (!existsSync(bin)) {
|
|
101
|
+
results.push({
|
|
102
|
+
platform,
|
|
103
|
+
ok: false,
|
|
104
|
+
note:
|
|
105
|
+
"the Tauri CLI is not installed: run `bun add -d @tauri-apps/cli`, then `bunx tauri " +
|
|
106
|
+
`${platform} init\``,
|
|
107
|
+
})
|
|
108
|
+
continue
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
const child = Bun.spawn([bin, platform, "init", "--ci"], {
|
|
112
|
+
cwd,
|
|
113
|
+
stdout: "pipe",
|
|
114
|
+
stderr: "pipe",
|
|
115
|
+
stdin: "ignore",
|
|
116
|
+
})
|
|
117
|
+
const timer = setTimeout(() => child.kill(), MOBILE_TIMEOUT_MS)
|
|
118
|
+
try {
|
|
119
|
+
const [out, err, code] = await Promise.all([
|
|
120
|
+
new Response(child.stdout).text(),
|
|
121
|
+
new Response(child.stderr).text(),
|
|
122
|
+
child.exited,
|
|
123
|
+
])
|
|
124
|
+
const tail = `${out}\n${err}`.trim().split("\n").slice(-4).join(" | ")
|
|
125
|
+
results.push({ platform, ok: code === 0, note: code === 0 ? "" : tail || `exited ${code}` })
|
|
126
|
+
} finally {
|
|
127
|
+
clearTimeout(timer)
|
|
128
|
+
}
|
|
129
|
+
} catch (error) {
|
|
130
|
+
// A CLI that exists but cannot run (no execute bit, a broken shim) is that platform's failure.
|
|
131
|
+
results.push({
|
|
132
|
+
platform,
|
|
133
|
+
ok: false,
|
|
134
|
+
note: error instanceof Error ? error.message : String(error),
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return results
|
|
139
|
+
}
|