Skip to content

Commit

Permalink
fix: searchForWorkspaceRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Sep 17, 2021
1 parent 38513ca commit 4dc2494
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/slidev/node/plugins/extendConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { dirname, join } from 'path'
import { InlineConfig, mergeConfig, Plugin } from 'vite'
import isInstalledGlobally from 'is-installed-globally'
import resolveGlobal from 'resolve-global'
import { uniq } from '@antfu/utils'
import { getIndexHtml } from '../common'
import { dependencies } from '../../../client/package.json'
import { ResolvedSlidevOptions } from '../options'
import { resolveImportPath, toAtFS } from '../utils'
import { searchForWorkspaceRoot } from '../vite/searchRoot'

const EXCLUDE = [
'@slidev/shared',
Expand Down Expand Up @@ -48,15 +50,15 @@ export function createConfigPlugin(options: ResolvedSlidevOptions): Plugin {
server: {
fs: {
strict: true,
allow: [
dirname(options.userRoot),
options.cliRoot,
allow: uniq([
searchForWorkspaceRoot(options.userRoot),
searchForWorkspaceRoot(options.cliRoot),
...(
isInstalledGlobally
? [dirname(resolveGlobal('@slidev/client/package.json'))]
: []
),
],
]),
},
},
}
Expand Down
70 changes: 70 additions & 0 deletions packages/slidev/node/vite/searchRoot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import fs from 'fs'
import { dirname, join } from 'path'

// https://github.com/vitejs/vite/issues/2820#issuecomment-812495079
const ROOT_FILES = [
// '.git',

// https://pnpm.js.org/workspaces/
'pnpm-workspace.yaml',

// https://rushjs.io/pages/advanced/config_files/
// 'rush.json',

// https://nx.dev/latest/react/getting-started/nx-setup
// 'workspace.json',
// 'nx.json'
]

// npm: https://docs.npmjs.com/cli/v7/using-npm/workspaces#installing-workspaces
// yarn: https://classic.yarnpkg.com/en/docs/workspaces/#toc-how-to-use-it
function hasWorkspacePackageJSON(root: string): boolean {
const path = join(root, 'package.json')
try {
fs.accessSync(path, fs.constants.R_OK)
}
catch {
return false
}
const content = JSON.parse(fs.readFileSync(path, 'utf-8')) || {}
return !!content.workspaces
}

function hasRootFile(root: string): boolean {
return ROOT_FILES.some(file => fs.existsSync(join(root, file)))
}

function hasPackageJSON(root: string) {
const path = join(root, 'package.json')
return fs.existsSync(path)
}

/**
* Search up for the nearest `package.json`
*/
export function searchForPackageRoot(current: string, root = current): string {
if (hasPackageJSON(current)) return current

const dir = dirname(current)
// reach the fs root
if (!dir || dir === current) return root

return searchForPackageRoot(dir, root)
}

/**
* Search up for the nearest workspace root
*/
export function searchForWorkspaceRoot(
current: string,
root = searchForPackageRoot(current),
): string {
if (hasRootFile(current)) return current
if (hasWorkspacePackageJSON(current)) return current

const dir = dirname(current)
// reach the fs root
if (!dir || dir === current) return root

return searchForWorkspaceRoot(dir, root)
}

0 comments on commit 4dc2494

Please sign in to comment.