UNPKG

@sprucelabs/spruce-cli

Version:

Command line interface for building Spruce skills.

181 lines • 8.32 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const test_utils_1 = require("@sprucelabs/test-utils"); const ActionFactory_1 = __importDefault(require("../../../features/ActionFactory")); const CommandService_1 = __importDefault(require("../../../services/CommandService")); const LintService_1 = __importDefault(require("../../../services/LintService")); const ServiceFactory_1 = __importDefault(require("../../../services/ServiceFactory")); const AbstractCliTest_1 = __importDefault(require("../../../tests/AbstractCliTest")); class UpgradingASkill5Test extends AbstractCliTest_1.default { static invocationLog = []; static async beforeEach() { await super.beforeEach(); this.invocationLog = []; } static async upgradeResetsEventCache() { await this.installSetListenerCacheAndBlockExecute(); await test_utils_1.assert.doesThrowAsync(() => this.upgrade()); const value = this.Settings().getListenerCache(); test_utils_1.assert.isFalsy(value); } static async doesNotResetEventCacheWithOtherAction() { await this.installSetListenerCacheAndBlockExecute(); await test_utils_1.assert.doesThrowAsync(() => this.Action('schema', 'sync').execute({})); const value = this.Settings().getListenerCache(); test_utils_1.assert.isEqualDeep(value, { shouldBeDeleted: true }); } static async shouldSyncSchemasIfSchemasIsInstalledAndSchemaFolderExists(shouldCreateSchema) { await this.FeatureFixture().installCachedFeatures('schemas'); CommandService_1.default.fakeCommand(new RegExp(/yarn/gis), { code: 0, }); if (shouldCreateSchema) { await this.Action('schema', 'create').execute({ nameReadable: 'Test schema!', namePascal: 'AnotherTest', nameCamel: 'anotherTest', description: 'this is so great!', }); } const emitter = this.emitter; let wasHit = false; await emitter.on('feature.will-execute', (payload) => { if (payload.featureCode === 'schema' && payload.actionCode === 'sync') { wasHit = true; } return {}; }); await this.upgrade(false); test_utils_1.assert.isTrue(wasHit === shouldCreateSchema); } static async modulesMovedFromDevToProdDependenciesStayThere() { await this.installSkillsSkill(); await this.moveDependencyToProd('eslint'); await this.moveDependencyToDev('@sprucelabs/error'); let wasMovedBackToDev = false; let wasMovedBackToProd = false; CommandService_1.default.fakeCommand(new RegExp(/yarn/gis), { code: 0, callback: (_, args) => { if (args.indexOf('-D') > -1 && args.indexOf('eslint') > -1) { wasMovedBackToDev = true; } else if (args.indexOf('-D') === -1 && args.indexOf('@sprucelabs/error') > -1) { wasMovedBackToProd = true; } }, }); await this.upgrade(); test_utils_1.assert.isFalse(wasMovedBackToDev, 'dependency moved back to dev'); test_utils_1.assert.isFalse(wasMovedBackToProd, 'dependency moved back to prod'); } static async resolvePathsMovedToDevDependencies() { await this.installSkillsSkill(); this.assertResolvePathAliasesInDevDependencies(); await this.moveDependencyToProd('@sprucelabs/resolve-path-aliases'); await this.upgrade(); this.assertResolvePathAliasesInDevDependencies(); } static async runsFixLintAfterUpgrade() { ActionFactory_1.default.setActionClass('node', 'updateDependencies', SpyUpdateDependenciesAction); ServiceFactory_1.default.setFactoryClass('lint', SpyLintService); CommandService_1.default.fakeCommand(/.*/gi, { code: 0, callback: (command, args) => { this.invocationLog.push([command, ...args].join(' ')); }, }); await this.installSkillsSkill(); await this.upgrade(); const lastIdx = this.invocationLog.length - 1; const secondToLastIdx = lastIdx - 1; test_utils_1.assert.isEqual(this.invocationLog[lastIdx], 'yarn build.dev'); test_utils_1.assert.isEqual(this.invocationLog[secondToLastIdx], 'yarn fix.lint **/*.ts'); } static assertResolvePathAliasesInDevDependencies() { const pkg = this.Service('pkg'); const pkgContents = pkg.readPackage(); test_utils_1.assert.isFalsy(pkgContents.dependencies['@sprucelabs/resolve-path-aliases'], 'resolve-path-aliases not should be in dependencies'); test_utils_1.assert.isTruthy(pkgContents.devDependencies['@sprucelabs/resolve-path-aliases'], 'resolve-path-aliases should be in devDependencies'); } static async upgrade(shouldCheckForErrors = true) { const results = await this.Action('node', 'upgrade').execute({}); if (shouldCheckForErrors) { test_utils_1.assert.isFalsy(results.errors, 'errors should be empty'); } } static async installSkillsSkill() { await this.FeatureFixture().installCachedFeatures('skills'); } static async moveDependencyToDev(name) { const pkg = this.Service('pkg'); await pkg.uninstall(name); await pkg.install(name, { isDev: true }); } static async moveDependencyToProd(name) { const pkg = this.Service('pkg'); await pkg.uninstall(name); await pkg.install(name); } static async installSetListenerCacheAndBlockExecute() { await this.FeatureFixture().installCachedFeatures('events'); const settings = this.Settings(); settings.setListenerCache({ shouldBeDeleted: true }); const emitter = this.emitter; void emitter.on('feature.will-execute', () => { throw new Error('Stop!'); }); } static Settings() { return this.Service('eventCache'); } } exports.default = UpgradingASkill5Test; __decorate([ (0, test_utils_1.test)() ], UpgradingASkill5Test, "upgradeResetsEventCache", null); __decorate([ (0, test_utils_1.test)() ], UpgradingASkill5Test, "doesNotResetEventCacheWithOtherAction", null); __decorate([ (0, test_utils_1.test)('syncs schemas when schemas installed and schemas folder exists', true), (0, test_utils_1.test)('does not syncs schemas when schemas installed but schemas folder does not exist', false) ], UpgradingASkill5Test, "shouldSyncSchemasIfSchemasIsInstalledAndSchemaFolderExists", null); __decorate([ (0, test_utils_1.test)() ], UpgradingASkill5Test, "modulesMovedFromDevToProdDependenciesStayThere", null); __decorate([ (0, test_utils_1.test)() ], UpgradingASkill5Test, "resolvePathsMovedToDevDependencies", null); __decorate([ (0, test_utils_1.test)() ], UpgradingASkill5Test, "runsFixLintAfterUpgrade", null); class SpyUpdateDependenciesAction { optionsSchema; commandAliases = []; invocationMessage = 'Nothing'; async execute() { UpgradingASkill5Test.invocationLog.push('updateDependencies'); return {}; } } class SpyLintService extends LintService_1.default { static fixPattern; fix = async (pattern) => { SpyLintService.fixPattern = pattern; UpgradingASkill5Test.invocationLog.push('yarn fix.lint ' + pattern); return []; }; } //# sourceMappingURL=UpgradingASkill5.test.js.map