Skip to content

Commit

Permalink
Refactor package version retrieval in JsPackageManager
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Jan 31, 2024
1 parent 66010a2 commit 5c368f2
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export abstract class JsPackageManager {
basePath?: string
): Promise<PackageJson | null>;

public abstract getPackageVersion(packageName: string, basePath?: string): Promise<string | null>;
async getPackageVersion(packageName: string, basePath = this.cwd): Promise<string | null> {
const packageJSON = await this.getPackageJSON(packageName, basePath);
return packageJSON ? packageJSON.version ?? null : null;
}

// NOTE: for some reason yarn prefers the npm registry in
// local development, so always use npm
Expand Down
6 changes: 0 additions & 6 deletions code/lib/core-common/src/js-package-manager/NPMProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import dedent from 'ts-dedent';
import { sync as findUpSync } from 'find-up';
import { existsSync, readFileSync } from 'fs';
import path from 'path';
import semver from 'semver';
import { logger } from '@storybook/node-logger';
import { JsPackageManager } from './JsPackageManager';
import type { PackageJson } from './PackageJson';
Expand Down Expand Up @@ -102,11 +101,6 @@ export class NPMProxy extends JsPackageManager {
return packageJson;
}

public async getPackageVersion(packageName: string, basePath = this.cwd): Promise<string | null> {
const packageJson = await this.getPackageJSON(packageName, basePath);
return packageJson ? semver.coerce(packageJson.version)?.version ?? null : null;
}

getInstallArgs(): string[] {
if (!this.installArgs) {
this.installArgs = [];
Expand Down
7 changes: 0 additions & 7 deletions code/lib/core-common/src/js-package-manager/PNPMProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import dedent from 'ts-dedent';
import { sync as findUpSync } from 'find-up';
import path from 'path';
import fs from 'fs';
import semver from 'semver';
import { JsPackageManager } from './JsPackageManager';
import type { PackageJson } from './PackageJson';
import type { InstallationMetadata, PackageMetadata } from './types';
Expand Down Expand Up @@ -159,12 +158,6 @@ export class PNPMProxy extends JsPackageManager {
return JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
}

async getPackageVersion(packageName: string, basePath = this.cwd): Promise<string | null> {
const packageJSON = await this.getPackageJSON(packageName, basePath);

return packageJSON ? semver.coerce(packageJSON.version)?.version ?? null : null;
}

protected getResolutions(packageJson: PackageJson, versions: Record<string, string>) {
return {
overrides: {
Expand Down
6 changes: 0 additions & 6 deletions code/lib/core-common/src/js-package-manager/Yarn1Proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import dedent from 'ts-dedent';
import { sync as findUpSync } from 'find-up';
import { existsSync, readFileSync } from 'fs';
import path from 'path';
import semver from 'semver';
import { createLogStream } from '../utils/cli';
import { JsPackageManager } from './JsPackageManager';
import type { PackageJson } from './PackageJson';
Expand Down Expand Up @@ -82,11 +81,6 @@ export class Yarn1Proxy extends JsPackageManager {
return JSON.parse(readFileSync(packageJsonPath, 'utf-8')) as Record<string, any>;
}

public async getPackageVersion(packageName: string, basePath = this.cwd): Promise<string | null> {
const packageJson = await this.getPackageJSON(packageName, basePath);
return packageJson ? semver.coerce(packageJson.version)?.version ?? null : null;
}

public async findInstallations(pattern: string[]) {
const commandResult = await this.executeCommand({
command: 'yarn',
Expand Down
6 changes: 0 additions & 6 deletions code/lib/core-common/src/js-package-manager/Yarn2Proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { existsSync, readFileSync } from 'fs';
import path from 'path';
import { PosixFS, VirtualFS, ZipOpenFS } from '@yarnpkg/fslib';
import { getLibzipSync } from '@yarnpkg/libzip';
import semver from 'semver';
import { createLogStream } from '../utils/cli';
import { JsPackageManager } from './JsPackageManager';
import type { PackageJson } from './PackageJson';
Expand Down Expand Up @@ -174,11 +173,6 @@ export class Yarn2Proxy extends JsPackageManager {
return packageJson;
}

async getPackageVersion(packageName: string, basePath = this.cwd): Promise<string | null> {
const packageJSON = await this.getPackageJSON(packageName, basePath);
return packageJSON ? semver.coerce(packageJSON.version)?.version ?? null : null;
}

protected getResolutions(packageJson: PackageJson, versions: Record<string, string>) {
return {
resolutions: {
Expand Down

0 comments on commit 5c368f2

Please sign in to comment.