11import type { RootsInfo } from '@slidev/types'
2- import * as fs from 'node:fs'
2+ import { existsSync } from 'node:fs'
3+ import { copyFile , readFile } from 'node:fs/promises'
34import { dirname , join , relative , resolve } from 'node:path'
45import process from 'node:process'
56import { fileURLToPath } from 'node:url'
@@ -68,28 +69,28 @@ export async function findGlobalPkgRoot(name: string, ensure: true): Promise<str
6869export async function findGlobalPkgRoot ( name : string , ensure ?: boolean ) : Promise < string | undefined >
6970export async function findGlobalPkgRoot ( name : string , ensure = false ) {
7071 const yarnPath = join ( globalDirs . yarn . packages , name )
71- if ( fs . existsSync ( `${ yarnPath } /package.json` ) )
72+ if ( existsSync ( `${ yarnPath } /package.json` ) )
7273 return yarnPath
7374 const npmPath = join ( globalDirs . npm . packages , name )
74- if ( fs . existsSync ( `${ npmPath } /package.json` ) )
75+ if ( existsSync ( `${ npmPath } /package.json` ) )
7576 return npmPath
7677 if ( ensure )
7778 throw new Error ( `Failed to resolve global package "${ name } "` )
7879}
7980
8081export async function resolveEntry ( entryRaw : string ) {
81- if ( ! fs . existsSync ( entryRaw ) && ! entryRaw . endsWith ( '.md' ) && ! / [ / \\ ] / . test ( entryRaw ) )
82+ if ( ! existsSync ( entryRaw ) && ! entryRaw . endsWith ( '.md' ) && ! / [ / \\ ] / . test ( entryRaw ) )
8283 entryRaw += '.md'
8384 const entry = resolve ( entryRaw )
84- if ( ! fs . existsSync ( entry ) ) {
85+ if ( ! existsSync ( entry ) ) {
8586 const { create } = await prompts ( {
8687 name : 'create' ,
8788 type : 'confirm' ,
8889 initial : 'Y' ,
8990 message : `Entry file ${ yellow ( `"${ entry } "` ) } does not exist, do you want to create it?` ,
9091 } )
9192 if ( create )
92- fs . copyFileSync ( resolve ( cliRoot , 'template.md' ) , entry )
93+ await copyFile ( resolve ( cliRoot , 'template.md' ) , entry )
9394 else
9495 process . exit ( 0 )
9596 }
@@ -161,24 +162,20 @@ export function createResolver(type: 'theme' | 'addon', officials: Record<string
161162 }
162163}
163164
164- function getUserPkgJson ( userRoot : string ) {
165+ async function getUserPkgJson ( userRoot : string ) {
165166 const path = resolve ( userRoot , 'package.json' )
166- if ( fs . existsSync ( path ) )
167- return JSON . parse ( fs . readFileSync ( path , 'utf-8' ) ) as Record < string , any >
167+ if ( existsSync ( path ) )
168+ return JSON . parse ( await readFile ( path , 'utf-8' ) ) as Record < string , any >
168169 return { }
169170}
170171
171172// npm: https://docs.npmjs.com/cli/v7/using-npm/workspaces#installing-workspaces
172173// yarn: https://classic.yarnpkg.com/en/docs/workspaces/#toc-how-to-use-it
173- function hasWorkspacePackageJSON ( root : string ) : boolean {
174+ async function hasWorkspacePackageJSON ( root : string ) : Promise < boolean > {
174175 const path = join ( root , 'package.json' )
175- try {
176- fs . accessSync ( path , fs . constants . R_OK )
177- }
178- catch {
176+ if ( ! existsSync ( path ) )
179177 return false
180- }
181- const content = JSON . parse ( fs . readFileSync ( path , 'utf-8' ) ) || { }
178+ const content = JSON . parse ( await readFile ( path , 'utf-8' ) ) || { }
182179 return ! ! content . workspaces
183180}
184181
@@ -198,19 +195,19 @@ function hasRootFile(root: string): boolean {
198195 // 'nx.json'
199196 ]
200197
201- return ROOT_FILES . some ( file => fs . existsSync ( join ( root , file ) ) )
198+ return ROOT_FILES . some ( file => existsSync ( join ( root , file ) ) )
202199}
203200
204201/**
205202 * Search up for the nearest workspace root
206203 */
207- function searchForWorkspaceRoot (
204+ async function searchForWorkspaceRoot (
208205 current : string ,
209206 root = current ,
210- ) : string {
207+ ) : Promise < string > {
211208 if ( hasRootFile ( current ) )
212209 return current
213- if ( hasWorkspacePackageJSON ( current ) )
210+ if ( await hasWorkspacePackageJSON ( current ) )
214211 return current
215212
216213 const dir = dirname ( current )
@@ -234,8 +231,8 @@ export async function getRoots(entry?: string): Promise<RootsInfo> {
234231 || ( await import ( 'is-installed-globally' ) ) . default
235232 const clientRoot = await findPkgRoot ( '@slidev/client' , cliRoot , true )
236233 const closestPkgRoot = dirname ( await findClosestPkgJsonPath ( userRoot ) || userRoot )
237- const userPkgJson = getUserPkgJson ( closestPkgRoot )
238- const userWorkspaceRoot = searchForWorkspaceRoot ( closestPkgRoot )
234+ const userPkgJson = await getUserPkgJson ( closestPkgRoot )
235+ const userWorkspaceRoot = await searchForWorkspaceRoot ( closestPkgRoot )
239236 rootsInfo = {
240237 cliRoot,
241238 clientRoot,
@@ -245,3 +242,21 @@ export async function getRoots(entry?: string): Promise<RootsInfo> {
245242 }
246243 return rootsInfo
247244}
245+
246+ export function resolveSourceFiles (
247+ roots : string [ ] ,
248+ subpath : string ,
249+ extensions = [ '.mjs' , '.js' , '.mts' , '.ts' ] , // The same order as https://vite.dev/config/shared-options#resolve-extensions
250+ ) {
251+ const results : string [ ] = [ ]
252+ for ( const root of roots ) {
253+ for ( const ext of extensions ) {
254+ const fullPath = join ( root , subpath + ext )
255+ if ( existsSync ( fullPath ) ) {
256+ results . push ( fullPath )
257+ break
258+ }
259+ }
260+ }
261+ return results
262+ }
0 commit comments