Skip to content

Commit c53794b

Browse files
Copilotkermanxantfu
authored
fix(cli): avoid prompting if not in tty (#2344)
Co-authored-by: kermanx <63178754+kermanx@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: _Kerman <kermanx@qq.com> Co-authored-by: Anthony Fu <github@antfu.me>
1 parent 5d331b3 commit c53794b

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

packages/slidev/node/resolver.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ export async function findPkgRoot(dep: string, parent: string, ensure = false) {
6868
export async function findGlobalPkgRoot(name: string, ensure: true): Promise<string>
6969
export async function findGlobalPkgRoot(name: string, ensure?: boolean): Promise<string | undefined>
7070
export async function findGlobalPkgRoot(name: string, ensure = false) {
71+
const localPath = await findDepPkgJsonPath(name, cliRoot)
72+
if (localPath)
73+
return dirname(localPath)
7174
const yarnPath = join(globalDirs.yarn.packages, name)
7275
if (existsSync(`${yarnPath}/package.json`))
7376
return yarnPath
@@ -83,6 +86,11 @@ export async function resolveEntry(entryRaw: string) {
8386
entryRaw += '.md'
8487
const entry = resolve(entryRaw)
8588
if (!existsSync(entry)) {
89+
// Check if stdin is available for prompts (i.e., is a TTY)
90+
if (!process.stdin.isTTY) {
91+
console.error(`Entry file "${entry}" does not exist and cannot prompt for confirmation`)
92+
process.exit(1)
93+
}
8694
const { create } = await prompts({
8795
name: 'create',
8896
type: 'confirm',
@@ -102,6 +110,12 @@ export async function resolveEntry(entryRaw: string) {
102110
*/
103111
export function createResolver(type: 'theme' | 'addon', officials: Record<string, string>) {
104112
async function promptForInstallation(pkgName: string) {
113+
// Check if stdin is available for prompts (i.e., is a TTY)
114+
if (!process.stdin.isTTY) {
115+
console.error(`The ${type} "${pkgName}" was not found and cannot prompt for installation`)
116+
process.exit(1)
117+
}
118+
105119
const { confirm } = await prompts({
106120
name: 'confirm',
107121
initial: 'Y',
@@ -132,21 +146,16 @@ export function createResolver(type: 'theme' | 'addon', officials: Record<string
132146
if (name[0] === '.' || (name[0] !== '@' && name.includes('/')))
133147
return [name, resolve(dirname(importer), name)]
134148

135-
// definitely a package name
136-
if (name.startsWith(`@slidev/${type}-`) || name.startsWith(`slidev-${type}-`)) {
137-
const pkgRoot = await findPkgRoot(name, importer)
138-
if (!pkgRoot)
139-
await promptForInstallation(name)
140-
return [name, await findPkgRoot(name, importer, true)]
141-
}
142-
143149
// search for local packages first
144150
{
145-
const possiblePkgNames = [
146-
`@slidev/${type}-${name}`,
147-
`slidev-${type}-${name}`,
148-
name,
149-
]
151+
const possiblePkgNames = [name]
152+
153+
if (!name.includes('/') && !name.startsWith('@')) {
154+
possiblePkgNames.push(
155+
`@slidev/${type}-${name}`,
156+
`slidev-${type}-${name}`,
157+
)
158+
}
150159

151160
for (const pkgName of possiblePkgNames) {
152161
const pkgRoot = await findPkgRoot(pkgName, importer)

0 commit comments

Comments
 (0)