-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): keep version for third party preset (#23284)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> ## Current Behavior <!-- This is the behavior we have today --> ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #23174
- Loading branch information
Showing
4 changed files
with
68 additions
and
50 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
21 changes: 21 additions & 0 deletions
21
packages/create-nx-workspace/src/utils/preset/get-third-party-preset.spec.ts
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,21 @@ | ||
import { getPackageNameFromThirdPartyPreset } from './get-third-party-preset'; | ||
|
||
describe('getPackageNameFromThirdPartyPreset', () => { | ||
it('should throw an error if preset is invalid', () => { | ||
expect(() => getPackageNameFromThirdPartyPreset('_random')).toThrow(); | ||
}); | ||
|
||
it('should return undefined if preset is known nx preset', () => { | ||
expect(getPackageNameFromThirdPartyPreset('react')).toBeUndefined(); | ||
expect(getPackageNameFromThirdPartyPreset('angular')).toBeUndefined(); | ||
}); | ||
|
||
it('should return package name if it is valid package', () => { | ||
expect(getPackageNameFromThirdPartyPreset('@nx-go/nx-go')).toEqual( | ||
'@nx-go/nx-go' | ||
); | ||
expect(getPackageNameFromThirdPartyPreset('@nx-go/[email protected]')).toEqual( | ||
'@nx-go/nx-go' | ||
); | ||
}); | ||
}); |
47 changes: 21 additions & 26 deletions
47
packages/create-nx-workspace/src/utils/preset/get-third-party-preset.ts
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 |
---|---|---|
@@ -1,36 +1,31 @@ | ||
import { output } from '../output'; | ||
import { validateNpmPackage } from '../validate-npm-package'; | ||
import { isKnownPreset } from './preset'; | ||
|
||
/** | ||
* This function is used to check if a preset is a third party preset. | ||
* @param preset | ||
* @returns null if the preset is a known Nx preset or preset does not exist, the package name of preset otherwise. | ||
* @returns | ||
* - undefined if the preset is a known Nx preset or invalid. | ||
* - packageName if the preset is a third party preset. | ||
* - throws an error if the preset is invalid. | ||
*/ | ||
export async function getThirdPartyPreset( | ||
export function getPackageNameFromThirdPartyPreset( | ||
preset?: string | ||
): Promise<string | null> { | ||
if (preset && !isKnownPreset(preset)) { | ||
// extract the package name from the preset | ||
const packageName = preset.match(/.+@/) | ||
? preset[0] + preset.substring(1).split('@')[0] | ||
: preset; | ||
const validateResult = validateNpmPackage(packageName); | ||
if (validateResult.validForNewPackages) { | ||
return Promise.resolve(packageName); | ||
} else { | ||
//! Error here | ||
output.error({ | ||
title: 'Invalid preset npm package', | ||
bodyLines: [ | ||
`There was an error with the preset npm package you provided:`, | ||
'', | ||
...(validateResult.errors ?? []), | ||
], | ||
}); | ||
throw new Error('Invalid preset npm package'); | ||
} | ||
} else { | ||
return Promise.resolve(null); | ||
): string | undefined { | ||
if (!preset || isKnownPreset(preset)) { | ||
return; | ||
} | ||
// extract the package name from the preset | ||
const packageName = preset.match(/.+@/) | ||
? preset[0] + preset.substring(1).split('@')[0] | ||
: preset; | ||
|
||
const validateResult = validateNpmPackage(packageName); | ||
if (!validateResult.validForNewPackages) { | ||
throw new Error( | ||
`Invalid preset npm package ${packageName}. There was an error with the preset npm package you provided: ` + | ||
(validateResult.errors ?? []).join('\n') | ||
); | ||
} | ||
return packageName; | ||
} |