-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
recursively search upwards for pnp.cjs
- Loading branch information
1 parent
01e94a7
commit 775314a
Showing
5 changed files
with
184 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import findUp from 'find-up' | ||
import path from 'path' | ||
import fs from 'fs-extra' | ||
import Debug from 'debug' | ||
const debug = Debug('cypress:scaffold-config:searchUtils') | ||
|
||
const ROOT_PATHS = [ | ||
'.git', | ||
|
||
// https://pnpm.io/workspaces | ||
'pnpm-workspace.yaml', | ||
|
||
// https://rushjs.io/pages/advanced/config_files/ | ||
'rush.json', | ||
|
||
// https://nx.dev/deprecated/workspace-json#workspace.json | ||
// https://nx.dev/reference/nx-json#nx.json | ||
'workspace.json', | ||
'nx.json', | ||
|
||
// https://lerna.js.org/docs/api-reference/configuration | ||
'lerna.json', | ||
] | ||
|
||
async function hasWorkspacePackageJson (directory: string) { | ||
try { | ||
const pkg = await fs.readJson(path.join(directory, 'package.json')) | ||
|
||
debug('package file for %s: %o', directory, pkg) | ||
|
||
return !!pkg.workspaces | ||
} catch (e) { | ||
debug('error reading package.json in %s. this is not the repository root', directory) | ||
|
||
return false | ||
} | ||
} | ||
|
||
export async function isRepositoryRoot (directory: string) { | ||
if (ROOT_PATHS.some((rootPath) => fs.existsSync(path.join(directory, rootPath)))) { | ||
return true | ||
} | ||
|
||
return hasWorkspacePackageJson(directory) | ||
} | ||
|
||
/** | ||
* Recursing search upwards from projectPath until the repository root looking for .pnp.cjs. | ||
* If `.pnp.cjs` is found, return it | ||
*/ | ||
export async function tryToFindPnpFile (projectPath: string): Promise<string | undefined> { | ||
return findUp(async (directory: string) => { | ||
const isCurrentRepositoryRoot = await isRepositoryRoot(directory) | ||
|
||
const file = path.join(directory, '.pnp.cjs') | ||
const hasPnpCjs = await fs.pathExists(file) | ||
|
||
if (hasPnpCjs) { | ||
return file | ||
} | ||
|
||
if (isCurrentRepositoryRoot) { | ||
debug('stopping search at %s because it is believed to be the repository root', directory) | ||
|
||
return findUp.stop | ||
} | ||
|
||
// Return undefined to keep searching | ||
return undefined | ||
}, { cwd: projectPath }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import fs from 'fs-extra' | ||
import path from 'path' | ||
import { expect } from 'chai' | ||
import os from 'os' | ||
import { isRepositoryRoot, tryToFindPnpFile } from '../../src/searchUtils' | ||
import dedent from 'dedent' | ||
|
||
const TEMP_DIR = path.join(os.tmpdir(), 'is-repository-root-tmp') | ||
|
||
beforeEach(async () => { | ||
await fs.mkdir(TEMP_DIR) | ||
}) | ||
|
||
afterEach(async () => { | ||
await fs.rm(TEMP_DIR, { recursive: true }) | ||
}) | ||
|
||
describe('isRepositoryRoot', () => { | ||
it('returns false if there is nothing in the directory', async () => { | ||
const isCurrentRepositoryRoot = await isRepositoryRoot(TEMP_DIR) | ||
|
||
expect(isCurrentRepositoryRoot).to.be.false | ||
}) | ||
|
||
it('returns true if there is a Git directory', async () => { | ||
await fs.mkdir(path.join(TEMP_DIR, '.git')) | ||
|
||
const isCurrentRepositoryRoot = await isRepositoryRoot(TEMP_DIR) | ||
|
||
expect(isCurrentRepositoryRoot).to.be.true | ||
}) | ||
|
||
it('returns false if there is a package.json without workspaces field', async () => { | ||
await fs.writeFile(path.join(TEMP_DIR, 'package.json'), `{ | ||
"name": "@packages/foo", | ||
"private": true, | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT" | ||
} | ||
`) | ||
|
||
const isCurrentRepositoryRoot = await isRepositoryRoot(TEMP_DIR) | ||
|
||
expect(isCurrentRepositoryRoot).to.be.false | ||
}) | ||
|
||
it('returns true if there is a package.json with workspaces field', async () => { | ||
await fs.writeFile(path.join(TEMP_DIR, 'package.json'), `{ | ||
"name": "monorepo-repo", | ||
"private": true, | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"workspaces": [ | ||
"packages/*" | ||
] | ||
} | ||
`) | ||
|
||
const isCurrentRepositoryRoot = await isRepositoryRoot(TEMP_DIR) | ||
|
||
expect(isCurrentRepositoryRoot).to.be.true | ||
}) | ||
}) | ||
|
||
describe('tryToFindPnpFile', () => { | ||
it('finds pnp.cjs at repo root', async () => { | ||
const projectPath = path.join(TEMP_DIR, 'packages', 'tests') | ||
const pnpcjs = path.join(TEMP_DIR, '.pnp.cjs') | ||
|
||
await Promise.all([ | ||
fs.ensureFile(path.join(projectPath, 'package.json')), | ||
fs.writeFile(pnpcjs, '/* pnp api */'), | ||
fs.writeFile(path.join(TEMP_DIR, 'package.json'), dedent` | ||
{ | ||
"workspaces": [ | ||
"packages/*" | ||
] | ||
} | ||
`), | ||
]) | ||
|
||
const pnpPath = await tryToFindPnpFile(projectPath) | ||
|
||
expect(pnpPath).to.eq(pnpcjs) | ||
}) | ||
|
||
it('does not find pnp.cjs at repo root', async () => { | ||
const projectPath = path.join(TEMP_DIR, 'packages', 'tests') | ||
|
||
await fs.ensureFile(path.join(projectPath, 'package.json')) | ||
await fs.writeFile(path.join(TEMP_DIR, 'package.json'), dedent` | ||
{ | ||
"workspaces": [ | ||
"packages/*" | ||
] | ||
} | ||
`) | ||
|
||
const pnpPath = await tryToFindPnpFile(projectPath) | ||
|
||
expect(pnpPath).to.eq(undefined) | ||
}) | ||
}) |