Skip to content

Commit

Permalink
Merge pull request #14 from faissaloux/refactor-package-manager
Browse files Browse the repository at this point in the history
Refactor package manager
  • Loading branch information
faissaloux authored Sep 15, 2024
2 parents 2f5b2fc + 4bf5ccc commit e24bdd8
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 66 deletions.
3 changes: 1 addition & 2 deletions src/interfaces/package_manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export interface PackageManager {
rootPath: string;
getInstalled(packageName: string): Promise<any>
getLockPath(): Promise<string>
getLockPath(): string
}
17 changes: 17 additions & 0 deletions src/package_manager/language_package_manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as vscode from 'vscode';

export class LanguagePackageManager {
constructor(protected rootPath: string) {}

async lockFileContent(): Promise<string> {
const lockPath = this.getLockPath();
const lockFile = vscode.Uri.file(lockPath);
const lockFileContent = await vscode.workspace.fs.readFile(lockFile);

return lockFileContent.toString();
}

getLockPath(): string {
throw new Error("Not Implemented!");
}
}
37 changes: 19 additions & 18 deletions src/package_manager/package_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@ import * as vscode from 'vscode';
import { Javascript } from './package_managers/javascript';
import { Php } from './package_managers/php';
import { Ruby } from './package_managers/ruby';
import path = require('path');

export class PackageManager {
private editorFileName: string;
private packageManager: string = '';
private languagesPackagesFiles: {[key: string]: string} = {
'php': 'composer.json',
'javascript': 'package.json',
'ruby': 'Gemfile',
};
private packageManagers = {
'php': Php,
'javascript': Javascript,
'ruby': Ruby,
};

constructor(private readonly editor: vscode.TextEditor) {
this.editorFileName = this.editor.document.fileName;
Expand All @@ -14,28 +25,18 @@ export class PackageManager {
}

get(): this {
if (this.editorFileName.endsWith('composer.json')){
this.packageManager = 'php';
} else if (this.editorFileName.endsWith('package.json')) {
this.packageManager = 'javascript';
} else if (this.editorFileName.endsWith('Gemfile')) {
this.packageManager = 'ruby';
for (const [language, packagesFile] of Object.entries(this.languagesPackagesFiles)) {
if (this.editorFileName.endsWith(packagesFile)){
this.packageManager = language;
break;
}
}

return this;
}

async getInstalled(packageName: string): Promise<any> {
let installedPackage;

if (this.packageManager === 'php') {
installedPackage = await new Php(this.editorFileName).getInstalled(packageName);
} else if (this.packageManager === 'javascript') {
installedPackage = await new Javascript(this.editorFileName).getInstalled(packageName);
} else if (this.packageManager === 'ruby') {
installedPackage = await new Ruby(this.editorFileName).getInstalled(packageName);
}

return installedPackage;
// @ts-ignore
return await new this.packageManagers[this.packageManager](path.dirname(this.editorFileName)).getInstalled(packageName);
}
}
26 changes: 10 additions & 16 deletions src/package_manager/package_managers/javascript.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as vscode from 'vscode';
import { Parser } from '../../parser/parser';
import { PackageManager } from '../../interfaces/package_manager';
import { LanguagePackageManager } from '../language_package_manager';
import { pathJoin } from '../../util/globals';

export class Javascript implements PackageManager {
rootPath: string;
export class Javascript extends LanguagePackageManager implements PackageManager {
packageManager: string = 'npm';
locks: {[key: string]: string} = {
'npm': 'package-lock.json',
Expand All @@ -16,16 +17,9 @@ export class Javascript implements PackageManager {
'pnpm': '/packageName/',
};

constructor(packageJsonFilePath: string) {
this.rootPath = packageJsonFilePath.replace(new RegExp('package.json$'), '');
}

async getInstalled(packageName: string): Promise<any> {
const lockPath = await this.getLockPath();
const lockFile = vscode.Uri.file(lockPath);
const lockFileContent = await vscode.workspace.fs.readFile(lockFile);

const installedPackages = new Parser(this.packageManager).parse(lockFileContent.toString());
this.packageManager = await this.getPackageManager();
const installedPackages = new Parser(this.packageManager).parse(await this.lockFileContent());

if (this.packageManager === 'pnpm') {
this.appendVersion(installedPackages);
Expand All @@ -34,21 +28,21 @@ export class Javascript implements PackageManager {
return Object.entries(installedPackages).find(([title, details]) => title.startsWith(this.lockPackageStartsWith(packageName)))?.[1];
}

async getLockPath(): Promise<string> {
this.packageManager = await this.getPackageManager();

return this.rootPath + this.locks[this.packageManager];
override getLockPath(): string {
return pathJoin(this.rootPath, this.locks[this.packageManager]);
}

async getPackageManager(): Promise<string> {
for (const lock in this.locks) {
let lockFile = vscode.Uri.file(this.rootPath + this.locks[lock]);
let lockFile = vscode.Uri.file(pathJoin(this.rootPath, this.locks[lock]));

try{
await vscode.workspace.fs.readFile(lockFile);

return lock;
} catch (error) {}
}

return Object.keys(this.locks)[0];
}

Expand Down
19 changes: 4 additions & 15 deletions src/package_manager/package_managers/php.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
import * as vscode from 'vscode';
import { pathJoin } from '../../util/globals';
import * as types from '../../types/types';
import { Parser } from '../../parser/parser';
import { PackageManager } from '../../interfaces/package_manager';
import { LanguagePackageManager } from '../language_package_manager';

export class Php implements PackageManager {
rootPath: string;

constructor(packageJsonFilePath: string) {
this.rootPath = packageJsonFilePath.replace(new RegExp('composer.json$'), '');
}

export class Php extends LanguagePackageManager implements PackageManager {
async getInstalled(packageName: string): Promise<any> {
const lockPath = await this.getLockPath();

const lockFile = vscode.Uri.file(lockPath);
const lockFileContent = await vscode.workspace.fs.readFile(lockFile);

const installedPackages = new Parser("composer").parse(lockFileContent.toString());
const installedPackages = new Parser("composer").parse(await this.lockFileContent());

return installedPackages.find((pkg: types.ComposerInstalledPackage) => pkg.name === packageName);
}

async getLockPath(): Promise<string> {
override getLockPath(): string {
return pathJoin(this.rootPath, 'vendor', 'composer', 'installed.json');
}
}
20 changes: 5 additions & 15 deletions src/package_manager/package_managers/ruby.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import * as vscode from 'vscode';
import { pathJoin } from '../../util/globals';
import { Parser } from '../../parser/parser';
import { PackageManager } from '../../interfaces/package_manager';
import { LanguagePackageManager } from '../language_package_manager';

export class Ruby implements PackageManager {
rootPath: string;

constructor(packageJsonFilePath: string) {
this.rootPath = packageJsonFilePath.replace(new RegExp('Gemfile$'), '');
}

export class Ruby extends LanguagePackageManager implements PackageManager {
async getInstalled(packageName: string): Promise<{[key: string]: string}| undefined> {
const lockPath = await this.getLockPath();
const lockFile = vscode.Uri.file(lockPath);
const lockFileContent = await vscode.workspace.fs.readFile(lockFile);

const installedPackages = new Parser("rubygems").parse(lockFileContent.toString());
const installedPackages = new Parser("rubygems").parse(await this.lockFileContent());

const packageFound = Object.keys(installedPackages.GEM.specs).find((pkg: string) => pkg.startsWith(packageName));

Expand All @@ -27,13 +17,13 @@ export class Ruby implements PackageManager {
return {
"name": packageAndVersion[0],
"version": matches[1],
};
};
}

return;
}

async getLockPath(): Promise<string> {
override getLockPath(): string {
return pathJoin(this.rootPath, 'Gemfile.lock');
}
}

0 comments on commit e24bdd8

Please sign in to comment.