webnek 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.
- package/README.md +5 -13
- package/bin/cli.mjs +51 -95
- package/package.json +5 -2
- package/template/.cargo/config.toml +2 -0
- package/template/Cargo.toml +2 -1
- package/template/README.md +3 -3
- package/template/build.rs +42 -65
package/README.md
CHANGED
|
@@ -1,23 +1,15 @@
|
|
|
1
1
|
# webnek
|
|
2
2
|
|
|
3
3
|
```bash
|
|
4
|
-
|
|
4
|
+
bun create webnek@latest my-app
|
|
5
5
|
cd my-app
|
|
6
|
-
|
|
6
|
+
cargo dev
|
|
7
7
|
```
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Create compiles the app (first time on a machine can take a minute). After that `cargo dev` starts the server.
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
Same create command: `npm create webnek@latest my-app` or `pnpm create webnek@latest my-app`.
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
## Commands
|
|
16
|
-
|
|
17
|
-
| Command | What it does |
|
|
18
|
-
|---|---|
|
|
19
|
-
| `bunx webnek@latest [dir]` | Scaffold a project |
|
|
20
|
-
| `bunx webnek@latest dev` | Start the app |
|
|
21
|
-
| `bunx webnek@latest add <pkg>` | Add a crate or npm package |
|
|
13
|
+
Generated apps have no `package.json` and no `node_modules`. `cargo build` and `cargo test` work as usual.
|
|
22
14
|
|
|
23
15
|
Requires [Rust](https://rustup.rs/).
|
package/bin/cli.mjs
CHANGED
|
@@ -15,7 +15,6 @@ import { fileURLToPath } from "node:url";
|
|
|
15
15
|
const here = dirname(fileURLToPath(import.meta.url));
|
|
16
16
|
const pkgRoot = resolve(here, "..");
|
|
17
17
|
const templateRoot = join(pkgRoot, "template");
|
|
18
|
-
const GIT = "https://github.com/Grenish/webnek";
|
|
19
18
|
const VERSION = JSON.parse(readFileSync(join(pkgRoot, "package.json"), "utf8")).version;
|
|
20
19
|
|
|
21
20
|
const args = process.argv.slice(2);
|
|
@@ -41,16 +40,15 @@ const IGNORE_WHEN_EMPTY = new Set([
|
|
|
41
40
|
]);
|
|
42
41
|
|
|
43
42
|
function help() {
|
|
44
|
-
const run = invokeCmd();
|
|
45
43
|
console.log(`
|
|
46
44
|
webnek ${VERSION}
|
|
47
45
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
46
|
+
bun create webnek@latest [dir]
|
|
47
|
+
npm create webnek@latest [dir]
|
|
48
|
+
pnpm create webnek@latest [dir]
|
|
49
|
+
|
|
50
|
+
Then:
|
|
51
|
+
cargo dev
|
|
54
52
|
`);
|
|
55
53
|
}
|
|
56
54
|
|
|
@@ -86,7 +84,6 @@ function frameworkDep(appDir) {
|
|
|
86
84
|
const fw = resolve(root, "crates/framework");
|
|
87
85
|
let rel = relative(appDir, fw);
|
|
88
86
|
if (!rel.startsWith(".") && !rel.startsWith("/")) rel = `./${rel}`;
|
|
89
|
-
// Cargo.toml paths work with forward slashes on all platforms.
|
|
90
87
|
rel = rel.replaceAll("\\", "/");
|
|
91
88
|
return `webnek = { path = "${rel}", features = ["ssr"] }`;
|
|
92
89
|
}
|
|
@@ -138,10 +135,35 @@ function stripNodeManifests(appDir) {
|
|
|
138
135
|
if (existsSync(nm)) rmSync(nm, { recursive: true, force: true });
|
|
139
136
|
}
|
|
140
137
|
|
|
138
|
+
function which(bin) {
|
|
139
|
+
const r = spawnSync("sh", ["-c", `command -v ${bin}`], { encoding: "utf8" });
|
|
140
|
+
return r.status === 0 ? r.stdout.trim() : "";
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function cargoBuild(appDir) {
|
|
144
|
+
if (!which("cargo")) {
|
|
145
|
+
console.error(`
|
|
146
|
+
✖ cargo is not installed. Install Rust first:
|
|
147
|
+
|
|
148
|
+
https://rustup.rs/
|
|
149
|
+
`);
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
console.log(" webnek compiling (first time on this computer can take a minute) …\n");
|
|
153
|
+
const r = spawnSync("cargo", ["build"], {
|
|
154
|
+
cwd: appDir,
|
|
155
|
+
stdio: "inherit",
|
|
156
|
+
env: { ...process.env, CARGO_TERM_COLOR: process.env.CARGO_TERM_COLOR || "always" },
|
|
157
|
+
});
|
|
158
|
+
return r.status === 0;
|
|
159
|
+
}
|
|
160
|
+
|
|
141
161
|
function scaffold(targetArg) {
|
|
142
162
|
const appDir = resolve(process.cwd(), targetArg || ".");
|
|
143
163
|
const pkgName = rustPackageName(
|
|
144
|
-
appDir === process.cwd()
|
|
164
|
+
appDir === process.cwd()
|
|
165
|
+
? process.cwd().split(/[/\\]/).pop() || "app"
|
|
166
|
+
: targetArg.replace(/\/+$/, "").split(/[/\\]/).pop()
|
|
145
167
|
);
|
|
146
168
|
|
|
147
169
|
if (existsSync(join(appDir, "Cargo.toml"))) {
|
|
@@ -157,101 +179,32 @@ function scaffold(targetArg) {
|
|
|
157
179
|
|
|
158
180
|
mkdirSync(appDir, { recursive: true });
|
|
159
181
|
stripNodeManifests(appDir);
|
|
160
|
-
|
|
182
|
+
copyTemplate(templateRoot, appDir, {
|
|
161
183
|
PACKAGE_NAME: pkgName,
|
|
162
184
|
FRAMEWORK_DEP: frameworkDep(appDir),
|
|
163
185
|
PROJECT_NAME: pkgName,
|
|
164
|
-
|
|
165
|
-
};
|
|
166
|
-
copyTemplate(templateRoot, appDir, vars);
|
|
186
|
+
});
|
|
167
187
|
stripNodeManifests(appDir);
|
|
168
188
|
|
|
169
189
|
const rel = relative(process.cwd(), appDir) || ".";
|
|
170
|
-
const
|
|
171
|
-
const lines = [` webnek ${VERSION} created ${rel}`, ``, ` Next:`];
|
|
172
|
-
if (rel !== ".") {
|
|
173
|
-
lines.push(` cd ${rel}`);
|
|
174
|
-
}
|
|
175
|
-
lines.push(` ${run} dev`);
|
|
176
|
-
console.log(`\n${lines.join("\n")}\n`);
|
|
177
|
-
}
|
|
190
|
+
const built = cargoBuild(appDir);
|
|
178
191
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
function invokeCmd() {
|
|
185
|
-
const ua = process.env.npm_config_user_agent || "";
|
|
186
|
-
const argv0 = process.argv[0] || "";
|
|
187
|
-
const argv1 = process.argv[1] || "";
|
|
188
|
-
if (ua.includes("bun") || argv0.includes("bun") || argv1.includes("bun")) {
|
|
189
|
-
return "bunx webnek@latest";
|
|
190
|
-
}
|
|
191
|
-
if (ua.includes("npx") || argv1.includes("npx") || argv1.includes("/npm/")) {
|
|
192
|
-
return "npx webnek@latest";
|
|
192
|
+
const lines = [` webnek ${VERSION} ${built ? "ready" : "created"} ${rel}`, ``, ` Next:`];
|
|
193
|
+
if (rel !== ".") lines.push(` cd ${rel}`);
|
|
194
|
+
lines.push(` cargo dev`);
|
|
195
|
+
if (!built) {
|
|
196
|
+
lines.push(``, ` cargo build failed. Fix that, then cargo dev.`);
|
|
193
197
|
}
|
|
194
|
-
|
|
195
|
-
return "bunx webnek@latest";
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
function cargoWebnekOk() {
|
|
199
|
-
const probe = spawnSync("cargo", ["webnek", "--help"], {
|
|
200
|
-
encoding: "utf8",
|
|
201
|
-
stdio: "pipe",
|
|
202
|
-
});
|
|
203
|
-
return probe.status === 0 || Boolean(which("cargo-webnek"));
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
function installCargoWebnek() {
|
|
207
|
-
const root = findFrameworkRoot();
|
|
208
|
-
if (root && existsSync(join(root, "crates/cli/Cargo.toml"))) {
|
|
209
|
-
console.log(" webnek installing cargo-webnek …");
|
|
210
|
-
const ins = spawnSync(
|
|
211
|
-
"cargo",
|
|
212
|
-
["install", "--path", join(root, "crates/cli"), "--force"],
|
|
213
|
-
{ stdio: "inherit" }
|
|
214
|
-
);
|
|
215
|
-
return ins.status === 0;
|
|
216
|
-
}
|
|
217
|
-
console.log(" webnek installing cargo-webnek (once) …");
|
|
218
|
-
const ins = spawnSync(
|
|
219
|
-
"cargo",
|
|
220
|
-
["install", "webnek-cli"],
|
|
221
|
-
{ stdio: "inherit" }
|
|
222
|
-
);
|
|
223
|
-
return ins.status === 0;
|
|
198
|
+
console.log(`\n${lines.join("\n")}\n`);
|
|
224
199
|
}
|
|
225
200
|
|
|
226
|
-
function
|
|
227
|
-
if (cargoWebnekOk()) return;
|
|
228
|
-
|
|
201
|
+
function cargo(args) {
|
|
229
202
|
if (!which("cargo")) {
|
|
230
|
-
console.error(
|
|
231
|
-
✖ cargo is not installed. Install Rust first:
|
|
232
|
-
|
|
233
|
-
https://rustup.rs/
|
|
234
|
-
`);
|
|
203
|
+
console.error(" ✖ cargo is not installed. https://rustup.rs/");
|
|
235
204
|
process.exit(1);
|
|
236
205
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
console.error(`
|
|
240
|
-
✖ could not install cargo-webnek.
|
|
241
|
-
|
|
242
|
-
cargo install webnek-cli
|
|
243
|
-
`);
|
|
244
|
-
process.exit(1);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
function proxyCargoWeb(forward) {
|
|
249
|
-
ensureCargoWeb();
|
|
250
|
-
const result = spawnSync("cargo", ["webnek", ...forward], {
|
|
251
|
-
stdio: "inherit",
|
|
252
|
-
cwd: process.cwd(),
|
|
253
|
-
});
|
|
254
|
-
process.exit(result.status ?? 1);
|
|
206
|
+
const r = spawnSync("cargo", args, { stdio: "inherit", cwd: process.cwd() });
|
|
207
|
+
process.exit(r.status ?? 1);
|
|
255
208
|
}
|
|
256
209
|
|
|
257
210
|
const createInvoked =
|
|
@@ -269,12 +222,15 @@ if (args.length === 0 && createInvoked) {
|
|
|
269
222
|
help();
|
|
270
223
|
} else if (cmd === "new" || cmd === "create" || cmd === "init") {
|
|
271
224
|
scaffold(cmd === "init" ? args[1] || "." : args[1] || ".");
|
|
272
|
-
} else if (
|
|
273
|
-
|
|
225
|
+
} else if (cmd === "dev") {
|
|
226
|
+
cargo(["run", ...args.slice(1)]);
|
|
227
|
+
} else if (cmd === "build") {
|
|
228
|
+
cargo(["build", "--release", ...args.slice(1)]);
|
|
229
|
+
} else if (cmd === "test") {
|
|
230
|
+
cargo(["test", ...args.slice(1)]);
|
|
274
231
|
} else if (cmd.startsWith("-")) {
|
|
275
232
|
help();
|
|
276
233
|
process.exit(1);
|
|
277
234
|
} else {
|
|
278
|
-
// `webnek my-app`
|
|
279
235
|
scaffold(cmd);
|
|
280
236
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webnek",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Webnek app
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"description": "Create a Webnek app. bun create webnek@latest my-app && cargo dev",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT OR Apache-2.0",
|
|
7
7
|
"author": "Grenish Rai <mrcoder2033d@gmail.com>",
|
|
@@ -22,6 +22,9 @@
|
|
|
22
22
|
"framework",
|
|
23
23
|
"create-webnek"
|
|
24
24
|
],
|
|
25
|
+
"exports": {
|
|
26
|
+
"./bin/cli.mjs": "./bin/cli.mjs"
|
|
27
|
+
},
|
|
25
28
|
"engines": {
|
|
26
29
|
"node": ">=18"
|
|
27
30
|
},
|
package/template/Cargo.toml
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
name = "{{PACKAGE_NAME}}"
|
|
3
3
|
version = "0.1.0"
|
|
4
4
|
edition = "2021"
|
|
5
|
+
default-run = "{{PACKAGE_NAME}}"
|
|
5
6
|
|
|
6
7
|
[workspace]
|
|
7
8
|
|
|
@@ -15,4 +16,4 @@ path = "api/index.rs"
|
|
|
15
16
|
|
|
16
17
|
[package.metadata.npm]
|
|
17
18
|
# Frontend NPM packages installed to ~/.cargo/webnek/cache/
|
|
18
|
-
# Add
|
|
19
|
+
# JS packages: bunx <pkg> is not required. Add crates with cargo add.
|
package/template/README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# {{PROJECT_NAME}}
|
|
2
2
|
|
|
3
3
|
```bash
|
|
4
|
-
|
|
4
|
+
cargo dev
|
|
5
5
|
```
|
|
6
6
|
|
|
7
|
-
- Edit `src/styles.css` (Tailwind v4).
|
|
7
|
+
- Edit `src/styles.css` (Tailwind v4).
|
|
8
8
|
- `public/` is for files you add (favicon, images).
|
|
9
|
-
-
|
|
9
|
+
- `cargo build`, `cargo test`, and `cargo run` work as usual.
|
|
10
10
|
|
|
11
11
|
## Deploy
|
|
12
12
|
|
package/template/build.rs
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
//! Compile CSS and client JS into OUT_DIR
|
|
2
|
-
//!
|
|
3
|
-
//! Local `cargo webnek dev` still serves from `target/web/` (hot reload).
|
|
4
|
-
//! On Vercel, `npx` compiles Tailwind and esbuild.
|
|
1
|
+
//! Compile CSS and client JS into OUT_DIR on every `cargo build`.
|
|
5
2
|
|
|
6
3
|
use std::env;
|
|
7
4
|
use std::fs;
|
|
@@ -18,88 +15,68 @@ fn main() {
|
|
|
18
15
|
}
|
|
19
16
|
|
|
20
17
|
fn write_css(dest: &Path) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
18
|
+
let out = dest.to_str().unwrap_or("styles.css");
|
|
19
|
+
if run(
|
|
20
|
+
"bunx",
|
|
21
|
+
&[
|
|
22
|
+
"@tailwindcss/cli@4.3.3",
|
|
23
|
+
"-i",
|
|
24
|
+
"src/styles.css",
|
|
25
|
+
"-o",
|
|
26
|
+
out,
|
|
27
|
+
"--minify",
|
|
28
|
+
],
|
|
29
|
+
) || run(
|
|
30
|
+
"npx",
|
|
31
|
+
&[
|
|
32
|
+
"--yes",
|
|
33
|
+
"@tailwindcss/cli@4.3.3",
|
|
34
|
+
"-i",
|
|
35
|
+
"src/styles.css",
|
|
36
|
+
"-o",
|
|
37
|
+
out,
|
|
38
|
+
"--minify",
|
|
39
|
+
],
|
|
40
|
+
) {
|
|
33
41
|
return;
|
|
34
42
|
}
|
|
35
|
-
let fallback = fs::read_to_string("src/styles.css")
|
|
36
|
-
"/* styles.css
|
|
37
|
-
});
|
|
43
|
+
let fallback = fs::read_to_string("src/styles.css")
|
|
44
|
+
.unwrap_or_else(|_| "/* styles.css: install bun or npx to compile Tailwind */\n".into());
|
|
38
45
|
let _ = fs::write(dest, fallback);
|
|
39
46
|
}
|
|
40
47
|
|
|
41
48
|
fn write_js(dest: &Path) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
&& npx_ok(&[
|
|
47
|
-
"--yes",
|
|
49
|
+
let out = dest.to_str().unwrap_or("client.js");
|
|
50
|
+
if run(
|
|
51
|
+
"bunx",
|
|
52
|
+
&[
|
|
48
53
|
"esbuild",
|
|
49
54
|
"src/client/index.ts",
|
|
50
55
|
"--bundle",
|
|
51
56
|
"--format=esm",
|
|
52
57
|
"--outfile",
|
|
53
|
-
|
|
54
|
-
]
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if on_vercel()
|
|
59
|
-
&& npx_ok(&[
|
|
58
|
+
out,
|
|
59
|
+
],
|
|
60
|
+
) || run(
|
|
61
|
+
"npx",
|
|
62
|
+
&[
|
|
60
63
|
"--yes",
|
|
61
64
|
"esbuild",
|
|
62
65
|
"src/client/index.ts",
|
|
66
|
+
"--bundle",
|
|
63
67
|
"--format=esm",
|
|
64
68
|
"--outfile",
|
|
65
|
-
|
|
66
|
-
]
|
|
67
|
-
{
|
|
69
|
+
out,
|
|
70
|
+
],
|
|
71
|
+
) {
|
|
68
72
|
return;
|
|
69
73
|
}
|
|
70
|
-
let fallback = fs::read_to_string("src/client/index.ts")
|
|
71
|
-
.unwrap_or_else(|_| "// client\n".to_string());
|
|
74
|
+
let fallback = fs::read_to_string("src/client/index.ts").unwrap_or_else(|_| "// client\n".into());
|
|
72
75
|
let _ = fs::write(dest, fallback);
|
|
73
76
|
}
|
|
74
77
|
|
|
75
|
-
fn
|
|
76
|
-
|
|
77
|
-
Ok(p) => p,
|
|
78
|
-
Err(_) => return false,
|
|
79
|
-
};
|
|
80
|
-
let manifest = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".into()));
|
|
81
|
-
let mut candidates = Vec::new();
|
|
82
|
-
if let Ok(td) = env::var("CARGO_TARGET_DIR") {
|
|
83
|
-
candidates.push(PathBuf::from(td).join("web").join(&pkg).join(relative));
|
|
84
|
-
}
|
|
85
|
-
candidates.push(manifest.join("target/web").join(&pkg).join(relative));
|
|
86
|
-
if let Some(parent) = manifest.parent() {
|
|
87
|
-
candidates.push(parent.join("target/web").join(&pkg).join(relative));
|
|
88
|
-
}
|
|
89
|
-
for src in candidates {
|
|
90
|
-
if src.is_file() && fs::copy(&src, dest).is_ok() {
|
|
91
|
-
return true;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
false
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
fn on_vercel() -> bool {
|
|
98
|
-
env::var_os("VERCEL").is_some()
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
fn npx_ok(args: &[&str]) -> bool {
|
|
102
|
-
Command::new("npx")
|
|
78
|
+
fn run(bin: &str, args: &[&str]) -> bool {
|
|
79
|
+
Command::new(bin)
|
|
103
80
|
.args(args)
|
|
104
81
|
.status()
|
|
105
82
|
.map(|s| s.success())
|