@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
88 lines • 3.32 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils");
const isCi_1 = __importDefault(require("../utilities/isCi"));
class NodePackageManager {
pkgService;
cwd;
commandService;
constructor(options) {
const { pkgService, commandService, cwd } = options;
this.pkgService = pkgService;
this.commandService = commandService;
this.cwd = cwd;
}
async installDependencies(pkg, options) {
const shouldCleanupLockFiles = options?.shouldCleanupLockFiles !== false;
const deleteLockFile = shouldCleanupLockFiles
? () => this.pkgService.deleteLockFile()
: () => { };
if (!pkg) {
await this.commandService.execute('yarn', { args: ['install'] });
deleteLockFile();
return { totalInstalled: -1, totalSkipped: -1 };
}
deleteLockFile();
const packages = Array.isArray(pkg) ? pkg : [pkg];
const toInstall = [];
const labsModules = [];
let totalInstalled = 0;
let totalSkipped = 0;
for (const thisPackage of packages) {
const isInstalled = !options?.shouldForceInstall &&
this.pkgService.isInstalled(thisPackage);
if (thisPackage.startsWith('@sprucelabs/') || !isInstalled) {
toInstall.push(this.pkgService.stripLatest(thisPackage));
totalInstalled++;
}
else {
totalSkipped++;
}
}
if (totalInstalled > 0) {
const { executable, args } = this.buildCommandAndArgs(toInstall, options);
const isInWorkspace = this.pkgService.get('workspaces')?.length > 0;
if (isInWorkspace) {
args.push('-W');
}
await this.commandService.execute(executable, {
args,
});
}
else if (!spruce_skill_utils_1.diskUtil.doesDirExist(path_1.default.join(this.cwd, 'node_modules'))) {
await this.commandService.execute('yarn', { args: ['install'] });
}
deleteLockFile();
return {
totalInstalled: totalInstalled + labsModules.length,
totalSkipped,
};
}
async uninstallDependencies(pkg) {
const packages = Array.isArray(pkg) ? pkg : [pkg];
const args = ['uninstall', ...packages];
await this.commandService.execute('npm', {
args,
});
await this.installDependencies();
}
buildCommandAndArgs(toInstall, options) {
const args = [
(0, isCi_1.default)() && '--cache-folder',
(0, isCi_1.default)() && spruce_skill_utils_1.diskUtil.createRandomTempDir(),
'add',
...toInstall,
].filter((a) => !!a);
if (options?.isDev) {
args.push('-D');
}
const executable = 'yarn';
return { executable, args };
}
}
exports.default = NodePackageManager;
//# sourceMappingURL=NodePackageManager.js.map