-
Notifications
You must be signed in to change notification settings - Fork 394
/
package-manager.factory.ts
41 lines (39 loc) · 1.43 KB
/
package-manager.factory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { readdir } from 'fs';
import { AbstractPackageManager } from './abstract.package-manager';
import { NpmPackageManager } from './npm.package-manager';
import { PackageManager } from './package-manager';
import { YarnPackageManager } from './yarn.package-manager';
import { PnpmPackageManager } from './pnpm.package-manager';
export class PackageManagerFactory {
public static create(name: PackageManager | string): AbstractPackageManager {
switch (name) {
case PackageManager.NPM:
return new NpmPackageManager();
case PackageManager.YARN:
return new YarnPackageManager();
case PackageManager.PNPM:
return new PnpmPackageManager();
default:
throw new Error(`Package manager ${name} is not managed.`);
}
}
public static async find(): Promise<AbstractPackageManager> {
return new Promise<AbstractPackageManager>((resolve) => {
readdir(process.cwd(), (error, files) => {
if (error) {
resolve(this.create(PackageManager.NPM));
} else {
if (files.findIndex((filename) => filename === 'yarn.lock') > -1) {
resolve(this.create(PackageManager.YARN));
} else if (
files.findIndex((filename) => filename === 'pnpm-lock.yaml') > -1
) {
resolve(this.create(PackageManager.PNPM));
} else {
resolve(this.create(PackageManager.NPM));
}
}
});
});
}
}