UNPKG

@sprucelabs/spruce-cli

Version:

Command line interface for building Spruce skills.

207 lines 7.5 kB
"use strict"; 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 Cli_1 = __importDefault(require("../../cli/Cli")); const test_utility_1 = __importDefault(require("../utilities/test.utility")); class FeatureFixture { cwd; installedSkills = {}; serviceFactory; static linkedUtils = false; static dirsToDelete = []; ui; generateCacheIfMissing = false; apiClientFactory; emitter; featureInstaller; constructor(options) { if (options.cwd.search('packages/spruce-cli') > -1) { throw new Error("You can't run FeatureFixture in the cli directory."); } this.cwd = options.cwd; this.serviceFactory = options.serviceFactory; this.ui = options.ui; this.generateCacheIfMissing = !!options.shouldGenerateCacheIfMissing; this.apiClientFactory = options.apiClientFactory; this.emitter = options.emitter; this.featureInstaller = options.featureInstaller; } static deleteOldSkillDirs() { for (const dir of this.dirsToDelete) { try { spruce_skill_utils_1.diskUtil.deleteDir(dir); } catch { } } this.dirsToDelete = []; } Service(type, cwd) { return this.serviceFactory.Service(cwd ?? this.cwd, type); } async Cli(options) { await this.linkWorkspacePackages(); const cli = await Cli_1.default.Boot({ cwd: this.cwd, graphicsInterface: this.ui, apiClientFactory: this.apiClientFactory, emitter: this.emitter, featureInstaller: this.featureInstaller, ...(options ?? {}), }); return cli; } async linkWorkspacePackages() { if (!FeatureFixture.linkedUtils) { FeatureFixture.linkedUtils = true; // const expectedLinkedDir = pathUtil.join( // os.homedir(), // '.config', // 'yarn', // 'link', // '@sprucelabs', // 'spruce-skill-utils' // ) // if (!fsUtil.existsSync(expectedLinkedDir)) { // const command = this.Service('command') // try { // await command.execute( // `cd ${pathUtil.join( // __dirname, // '..', // '..', // '..', // 'spruce-skill-utils' // )} && yarn link` // ) // } catch (err) { // if (fsUtil.existsSync(expectedLinkedDir)) { // log.warn(`Symlink ${expectedLinkedDir} already exists`) // } else { // log.warn( // `Symlink ${expectedLinkedDir} failed, but the check thinks it is missing` // ) // } // } // } } } async installCachedFeatures(cacheKey, bootOptions) { return this.installFeatures([], cacheKey, bootOptions); } async installFeatures(features, cacheKey, bootOptions) { if (this.isCached(cacheKey)) { return this.installedSkills[cacheKey].cli; } let isCached = false; if (cacheKey && test_utility_1.default.isCacheEnabled()) { isCached = this.doesCacheExist(cacheKey); if (!isCached && !this.generateCacheIfMissing) { throw new Error(`Cached skill not found, make sure\n\n"${cacheKey}"\n\nis in your package.json under "testSkillCache" and run\n\n\`yarn cache.tests\``); } if (isCached) { await this.copyCachedSkillToCwd(cacheKey); } else { this.removeCwdFromCacheTracker(cacheKey); } } const cli = await this.Cli(bootOptions); if (!isCached) { await cli.installFeatures({ features, }); } if (cacheKey && test_utility_1.default.isCacheEnabled()) { !isCached && this.addCwdToCacheTracker(cacheKey); this.cacheCli(cacheKey, cli); } await this.linkLocalPackages(); return cli; } isCached(cacheKey) { return (cacheKey && this.installedSkills[cacheKey] && test_utility_1.default.isCacheEnabled()); } async linkLocalPackages() { // const command = this.Service('command') // await command.execute(`yarn link @sprucelabs/spruce-skill-utils`) } async copyCachedSkillToCwd(cacheKey) { let isCached = this.doesCacheExist(cacheKey); if (isCached) { let settings = this.loadCacheTracker(); await spruce_skill_utils_1.diskUtil.copyDir(settings[cacheKey], this.cwd); if (process.env.TEST_HOST) { this.Service('env').set('HOST', process.env.TEST_HOST); } FeatureFixture.dirsToDelete.push(this.cwd); } } addCwdToCacheTracker(cacheKey) { let settings = this.loadCacheTracker(); if (!settings) { settings = {}; } if (!settings[cacheKey]) { settings[cacheKey] = this.cwd; this.writeCacheSettings(settings); } return settings; } removeCwdFromCacheTracker(cacheKey) { let settings = this.loadCacheTracker(); if (!settings) { settings = {}; } if (settings[cacheKey]) { delete settings[cacheKey]; this.writeCacheSettings(settings); } } writeCacheSettings(settings) { const settingsFile = this.getTestCacheTrackerFilePath(); const settingsFolder = path_1.default.dirname(settingsFile); !spruce_skill_utils_1.diskUtil.doesDirExist(settingsFolder) && spruce_skill_utils_1.diskUtil.createDir(settingsFolder); spruce_skill_utils_1.diskUtil.writeFile(settingsFile, JSON.stringify(settings, null, 2)); } doesCacheExist(cacheKey) { let alreadyInstalled = false; const settings = this.loadCacheTracker(); if (settings?.[cacheKey]) { alreadyInstalled = true; } if (alreadyInstalled) { alreadyInstalled = spruce_skill_utils_1.diskUtil.doesDirExist(spruce_skill_utils_1.diskUtil.resolvePath(settings[cacheKey], 'node_modules')); } return alreadyInstalled; } loadCacheTracker() { const settingsFile = this.getTestCacheTrackerFilePath(); const exists = spruce_skill_utils_1.diskUtil.doesFileExist(settingsFile); let settingsObject = {}; try { settingsObject = exists ? JSON.parse(spruce_skill_utils_1.diskUtil.readFile(settingsFile)) : {}; } catch { } return settingsObject; } getTestCacheTrackerFilePath() { return spruce_skill_utils_1.diskUtil.resolveHashSprucePath(__dirname, 'tmp', `cached-skills.json`); } cacheCli(cacheKey, cli) { this.installedSkills[cacheKey] = { cwd: this.cwd, cli, }; } } exports.default = FeatureFixture; //# sourceMappingURL=FeatureFixture.js.map