Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] How to use shell plugin to call npx vitepress init #2309

Closed
xiaohulu opened this issue Jan 12, 2025 · 10 comments
Closed

[question] How to use shell plugin to call npx vitepress init #2309

xiaohulu opened this issue Jan 12, 2025 · 10 comments
Labels
plugin: shell question Further information is requested

Comments

@xiaohulu
Copy link

Vitepress use clack/prompts to ask user input params step by step.
https://vitepress.dev/guide/getting-started#setup-wizard

const command = Command.create(
    "bun-init-vitepress", 
    ["bun", "vitepress", "init"], 
    {cwd: dir}
  );

  command.on("close", data => {
    console.log("closed", data);
  })
  command.on("error", error => {
    console.log("error", error);
  })
  command.stderr.on("data", line => {
    console.log("command.stderr", line);
  })
  command.stdout.on("data", line => {
    console.log("command.stdout", line);
  })
  const child = await command.spawn();
  console.log(child);

I use this code to call npx vitepress init, but throw errors

command.stdout 
index.vue:156 command.stdout �[90m┌�[39m  �[1m�[36mWelcome to VitePress!�[39m�[22m
index.vue:151 command.stderr node:internal/errors:496
index.vue:151 command.stderr     ErrorCaptureStackTrace(err);
index.vue:151 command.stderr     ^
index.vue:151 command.stderr 
index.vue:151 command.stderr SystemError [ERR_TTY_INIT_FAILED]: TTY initialization failed: uv_tty_init returned EBADF (bad file descriptor)
index.vue:151 command.stderr     at new SystemError (node:internal/errors:256:5)
index.vue:151 command.stderr     at new NodeError (node:internal/errors:367:7)
index.vue:151 command.stderr     at new WriteStream (node:tty:93:11)
index.vue:151 command.stderr     at TD.prompt (file:///D:/temp/demo/b/node_modules/vitepress/dist/node/chunk-DMuPggCS.js:49491:863)
index.vue:151 command.stderr     at te (file:///D:/temp/demo/b/node_modules/vitepress/dist/node/chunk-DMuPggCS.js:49506:7)
index.vue:151 command.stderr     at root (file:///D:/temp/demo/b/node_modules/vitepress/dist/node/chunk-DMuPggCS.js:51627:16)
index.vue:151 command.stderr     at he (file:///D:/temp/demo/b/node_modules/vitepress/dist/node/chunk-DMuPggCS.js:49523:95)
index.vue:151 command.stderr     at init (file:///D:/temp/demo/b/node_modules/vitepress/dist/node/chunk-DMuPggCS.js:51623:25)
index.vue:151 command.stderr     at file:///D:/temp/demo/b/node_modules/vitepress/dist/node/cli.js:448:3
index.vue:151 command.stderr     at ModuleJob.run (node:internal/modules/esm/module_job:195:25) {
index.vue:151 command.stderr   code: 'ERR_TTY_INIT_FAILED',
index.vue:151 command.stderr   info: {
index.vue:151 command.stderr     errno: -4083,
index.vue:151 command.stderr     code: 'EBADF',
index.vue:151 command.stderr     message: 'bad file descriptor',
index.vue:151 command.stderr     syscall: 'uv_tty_init'
index.vue:151 command.stderr   },
index.vue:151 command.stderr   errno: [Getter/Setter],
index.vue:151 command.stderr   syscall: [Getter/Setter]
index.vue:151 command.stderr }
index.vue:151 command.stderr 
index.vue:151 command.stderr Node.js v18.19.0
@FabianLars
Copy link
Member

FabianLars commented Jan 12, 2025

oof that will be tricky. interacting with tty based cli programs in commands is always pretty hard (outside of tauri as well).

What exactly is behind bun-init-vitepress? Is it powershell or cmd.exe directly?

@FabianLars FabianLars added question Further information is requested plugin: shell labels Jan 12, 2025
@xiaohulu
Copy link
Author

@FabianLars
Copy link
Member

i mean, what did you configure in src-tauri/capabilities? I assume bun-init-vitepress is not literally bun vitepress init because then your code would end up calling bun vitepress init bun vitepress init

@xiaohulu
Copy link
Author

xiaohulu commented Jan 12, 2025

"identifier": "shell:allow-spawn",
"allow":[{
  "name": "bun-init-vitepress",
  "cmd": "bun",
  "args": ["vitepress", "init"],
  "sidecar": false
}]

@FabianLars
Copy link
Member

FabianLars commented Jan 12, 2025

okay, and what's the situation if you change that to

"identifier": "shell:allow-spawn",
"allow":[{
  "name": "bun-init-vitepress",
  "cmd": "powershell",
  "args": ["bun", "vitepress", "init"],
  "sidecar": false
}]

(or cmd.exe instead of powershell)

@xiaohulu
Copy link
Author

xiaohulu commented Jan 12, 2025

use powershell

throw the same error

"identifier": "shell:allow-spawn",
"allow":[{
  "name": "bun-init-vitepress",
  "cmd": "powershell",
  "args": ["bun", "vitepress", "init"],
  "sidecar": false
}]
const command = Command.create(
    "bun-init-vitepress", 
    ["powershell", "bun", "vitepress", "init"], 
    {cwd: dir, encoding: "utf-8"}
  );

use cmd.exe

"identifier": "shell:allow-spawn",
"allow":[{
  "name": "bun-init-vitepress",
  "cmd": "cmd.exe",
  "args": ["bun", "vitepress", "init"],
  "sidecar": false
}]
const command = Command.create(
    "bun-init-vitepress", 
    ["cmd.exe", "bun", "vitepress", "init"], 
    {cwd: dir, encoding: "utf-8"}
  );

print

command.stdout Microsoft Windows [version 10.0.22631.4602]
index.vue:155 command.stdout (c) Microsoft Corporation. 
index.vue:155 command.stdout 

not throw errors, not execute vitepress init yet

@FabianLars
Copy link
Member

Sorry my last comment didn't make sense, try this please

"identifier": "shell:allow-spawn",
"allow":[{
  "name": "bun-init-vitepress",
  "cmd": "powershell",
  "args": ["-C", "bun vitepress init"],
  "sidecar": false
}]
const command = Command.create(
    "bun-init-vitepress", 
    ["-C", "bun vitepress init"], 
    {cwd: dir, encoding: "utf-8"}
  );

I'm not 100% sure but "bun vitepress init" may need another quote like "'bun vitepress init'".

For cmd you need to replace -C with /C

@xiaohulu
Copy link
Author

throw the same error.

@FabianLars
Copy link
Member

In that case i think you can't use the Command api.

The init.ts file you shared above seems to be simple enough to actually do the same logic in your code directly which would be much easier than trying to make a tty connector that works with clack.

@FabianLars FabianLars closed this as not planned Won't fix, can't repro, duplicate, stale Jan 12, 2025
@xiaohulu
Copy link
Author

@FabianLars ok, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: shell question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants