fix: throw an exception if the workspace manifest is named incorrectly

PR #3039
This commit is contained in:
Zoltan Kochan
2020-12-22 23:50:31 +02:00
committed by GitHub
parent 440771947d
commit ec37069f2a
7 changed files with 54 additions and 13 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/find-workspace-dir": major
---
Throw an exception if the workspace manifest is created with the wrong extension.

View File

@@ -28,6 +28,7 @@
},
"homepage": "https://github.com/pnpm/pnpm/blob/master/packages/find-workspace-dir#readme",
"dependencies": {
"@pnpm/error": "workspace:^1.4.0",
"find-up": "^5.0.0"
},
"funding": "https://opencollective.com/pnpm"

View File

@@ -1,9 +1,13 @@
import PnpmError from '@pnpm/error'
import path = require('path')
import findUp = require('find-up')
const WORKSPACE_MANIFEST_FILENAME = 'pnpm-workspace.yaml'
export default async function findWorkspaceDir (cwd: string) {
const workspaceManifestLocation = await findUp(WORKSPACE_MANIFEST_FILENAME, { cwd })
const workspaceManifestLocation = await findUp([WORKSPACE_MANIFEST_FILENAME, 'pnpm-workspace.yml'], { cwd })
if (workspaceManifestLocation?.endsWith('.yml')) {
throw new PnpmError('BAD_WORKSPACE_MANIFEST_NAME', `The workspace manifest file should be named "pnpm-workspace.yaml". File found: ${workspaceManifestLocation}`)
}
return workspaceManifestLocation && path.dirname(workspaceManifestLocation)
}

View File

@@ -8,5 +8,9 @@
"src/**/*.ts",
"../../typings/**/*.d.ts"
],
"references": []
"references": [
{
"path": "../error"
}
]
}

View File

@@ -10,6 +10,7 @@ import { scopeLogger } from '@pnpm/core-loggers'
import { filterPackages } from '@pnpm/filter-workspace-packages'
import findWorkspacePackages from '@pnpm/find-workspace-packages'
import logger from '@pnpm/logger'
import { ParsedCliArgs } from '@pnpm/parse-cli-args'
import checkForUpdates from './checkForUpdates'
import pnpmCmds, { getRCOptionsTypes } from './cmd'
import { formatUnknownOptionsError } from './formatError'
@@ -41,6 +42,14 @@ const DEPRECATED_OPTIONS = new Set([
])
export default async function run (inputArgv: string[]) {
let parsedCliArgs!: ParsedCliArgs
try {
parsedCliArgs = await parseCliArgs(inputArgv)
} catch (err) {
// Reporting is not initialized at this point, so just printing the error
printError(err.message, err['hint'])
process.exit(1)
}
const {
argv,
params: cliParams,
@@ -48,10 +57,9 @@ export default async function run (inputArgv: string[]) {
cmd,
unknownOptions,
workspaceDir,
} = await parseCliArgs(inputArgv)
} = parsedCliArgs
if (cmd !== null && !pnpmCmds[cmd]) {
console.error(`${chalk.bgRed.black('\u2009ERROR\u2009')} ${chalk.red(`Unknown command '${cmd}'`)}`)
console.log('For help, run: pnpm help')
printError(`Unknown command '${cmd}'`, 'For help, run: pnpm help')
process.exit(1)
}
@@ -66,8 +74,7 @@ export default async function run (inputArgv: string[]) {
}
console.log(deprecationMsg)
} else {
console.error(formatUnknownOptionsError(unknownOptions))
console.log(`For help, run: pnpm help${cmd ? ` ${cmd}` : ''}`)
printError(formatUnknownOptionsError(unknownOptions), `For help, run: pnpm help${cmd ? ` ${cmd}` : ''}`)
process.exit(1)
}
}
@@ -91,12 +98,8 @@ export default async function run (inputArgv: string[]) {
config.argv = argv
} catch (err) {
// Reporting is not initialized at this point, so just printing the error
console.error(`${chalk.bgRed.black('\u2009ERROR\u2009')} ${chalk.red(err.message)}`)
if (err['hint']) {
console.log(err['hint'])
} else {
console.log(`For help, run: pnpm help${cmd ? ` ${cmd}` : ''}`)
}
const hint = err['hint'] ? err['hint'] : `For help, run: pnpm help${cmd ? ` ${cmd}` : ''}`
printError(err.message, hint)
process.exit(1)
}
@@ -247,3 +250,10 @@ export default async function run (inputArgv: string[]) {
process.exit(exitCode)
}
}
function printError (message: string, hint?: string) {
console.error(`${chalk.bgRed.black('\u2009ERROR\u2009')} ${chalk.red(message)}`)
if (hint) {
console.log(hint)
}
}

View File

@@ -52,6 +52,21 @@ test('no projects found', async () => {
}
})
test('incorrect workspace manifest', async () => {
preparePackages([
{
name: 'project',
version: '1.0.0',
},
])
await writeYamlFile('pnpm-workspace.yml', { packages: ['**', '!store/**'] })
const { status, stderr } = execPnpmSync(['install'])
expect(stderr.toString()).toMatch(/The workspace manifest file should be named "pnpm-workspace.yaml"/)
expect(status).toBe(1)
})
test('linking a package inside a monorepo', async () => {
const projects = preparePackages([
{

2
pnpm-lock.yaml generated
View File

@@ -544,8 +544,10 @@ importers:
p-filter: ^2.1.0
packages/find-workspace-dir:
dependencies:
'@pnpm/error': 'link:../error'
find-up: 5.0.0
specifiers:
'@pnpm/error': 'workspace:^1.4.0'
find-up: ^5.0.0
packages/find-workspace-packages:
dependencies: