UNPKG

@roots/dependencies

Version:

Automated package installation

38 lines (37 loc) 1.12 kB
import { realpath } from 'node:fs/promises'; import { join } from 'node:path'; import { Npm } from './command/index.js'; import { Yarn } from './command/index.js'; export class Dependencies { path; onMessage; onError; constructor(path, onMessage, onError) { this.path = path; this.onMessage = onMessage; this.onError = onError; } async getClient() { return await this.isYarn().then(isYarn => { return isYarn ? new Yarn(this.path, this.onMessage, this.onError) : new Npm(this.path, this.onMessage, this.onError); }); } /** * Get the latest version of a package from the npm registry * * @returns - Package version */ async getLatestVersion(signifier) { return await this.getClient().then(async (client) => await client.getLatestVersion(signifier)); } async isYarn() { try { return await realpath(join(this.path, `yarn.lock`)).then(result => typeof result === `string`); } catch (e) { return false; } } }