genezio
Version:
Command line utility to interact with Genezio infrastructure.
68 lines (67 loc) • 2.54 kB
JavaScript
import { cmp } from "semver";
import { $ } from "execa";
import { UserError } from "../errors.js";
export default class YarnPackageManager {
constructor() {
this.command = "yarn";
}
async install(packages = [], cwd, args = []) {
// Yarn has two different commands for installing packages:
// - `yarn install` will install all packages from the lockfile
// - `yarn add` will install the specified packages and update the lockfile
if (packages.length === 0) {
await $({ cwd }) `yarn install ${args}`;
return;
}
await $({ cwd }) `yarn add ${args} ${packages}`;
}
async cleanInstall(cwd, args = []) {
await $({ cwd }) `yarn install --frozen-lockfile ${args}`;
}
installSync(packages = [], cwd) {
// Yarn has two different commands for installing packages:
// - `yarn install` will install all packages from the lockfile
// - `yarn add` will install the specified packages and update the lockfile
if (packages.length === 0) {
$({ cwd }) `yarn install`;
return;
}
$({ cwd }) `yarn add ${packages}`;
}
async link(packages = [], cwd) {
await $({ cwd }) `yarn link ${packages}`;
}
async publish(cwd) {
await $({ cwd }) `yarn publish`;
}
async addScopedRegistry(scope, url, authToken) {
if (cmp(await this.getVersion(), "<", "2.0.0")) {
throw new UserError("yarn v1 (classic) is not supported. Please update yarn to v2.0.0 or above.");
}
const scopeConfig = {
npmRegistryServer: url,
npmAuthToken: authToken,
npmAlwaysAuth: true,
};
await $ `yarn config set --home npmScopes.${scope} --json '${JSON.stringify(scopeConfig)}'`;
}
async removeScopedRegistry(scope) {
if (cmp(await this.getVersion(), "<", "2.0.0")) {
throw new UserError("yarn v1 (classic) is not supported. Please update yarn to v2.0.0 or above.");
}
await $ `yarn config unset --home npmScopes.${scope}`;
}
async getVersion() {
// Check if the version is already cached
if (this.version !== undefined) {
return this.version;
}
const { stdout } = await $ `yarn --version`;
this.version = stdout.trim();
return this.version;
}
async pack(cwd, _destination) {
const { stdout } = await $({ cwd }) `yarn pack`;
return stdout.trim();
}
}