fix(cli): fix computation of Angular version in sb init

`packageManager.getVersion` was fetching the latest version from npm
registry instead of checking the version installed locally.
This commit is contained in:
Gaëtan Maisse 2021-05-14 21:01:58 +02:00
parent 5de8ca626e
commit 1497a4773e
No known key found for this signature in database
GPG Key ID: D934C0EF3714A8A8
2 changed files with 16 additions and 1 deletions

View File

@ -33,7 +33,9 @@ const generator: Generator = async (packageManager, npmOptions, options) => {
'Could not find a default project in your Angular workspace.\nSet a defaultProject in your angular.json and re-run the installation.'
);
}
const angularVersion = semver.coerce(await packageManager.getVersion('@angular/core'))?.version;
const angularVersion = semver.coerce(
packageManager.retrievePackageJson().dependencies['@angular/core']
)?.version;
const isWebpack5 = semver.gte(angularVersion, '12.0.0');
const updatedOptions = isWebpack5 ? { ...options, builder: CoreBuilder.Webpack5 } : options;

View File

@ -55,6 +55,10 @@ export abstract class JsPackageManager {
done();
}
/**
* Read the `package.json` file available in the directory the command was call from
* If there is no `package.json` it will create one.
*/
public retrievePackageJson(): PackageJsonWithDepsAndDevDeps {
let packageJson = readPackageJson();
if (!packageJson) {
@ -151,6 +155,15 @@ export abstract class JsPackageManager {
return Promise.all(packageNames.map((packageName) => this.getVersion(packageName)));
}
/**
* Return the latest version of the input package available on npmjs registry.
* If constraint are provided it return the latest version matching the constraints.
*
* For `@storybook/*` packages the latest version is retrieved from `cli/src/versions.json` file directly
*
* @param packageName The name of the package
* @param constraint A valid semver constraint, example: '1.x || >=2.5.0 || 5.0.0 - 7.2.3'
*/
public async getVersion(packageName: string, constraint?: string): Promise<string> {
let current: string;