UNPKG

@sprucelabs/spruce-cli

Version:

Command line interface for building Spruce skills.

216 lines • 8.93 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 spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils"); const test_utils_1 = require("@sprucelabs/test-utils"); const AbstractCliTest_1 = __importDefault(require("../../../tests/AbstractCliTest")); class WatchingForChangesTest extends AbstractCliTest_1.default { static async beforeEach() { await super.beforeEach(); spruce_skill_utils_1.diskUtil.createDir(this.resolvePath('dist')); spruce_skill_utils_1.diskUtil.createDir(this.resolvePath('build')); } static async installsWatchers() { await this.installWatch(); await this.assertIsFeatureInstalled('watch'); } static async notWatchingAtTheStart() { const cli = await this.installWatch(); const isWatching = await cli.getFeature('watch').isWatching(); test_utils_1.assert.isFalse(isWatching); } static async canStartAndStopWatch() { const cli = await this.installWatch(); const feature = cli.getFeature('watch'); void feature.startWatching(); let isWatching = await feature.isWatching(); test_utils_1.assert.isTrue(isWatching); await feature.stopWatching(); isWatching = await feature.isWatching(); test_utils_1.assert.isFalse(isWatching); } static async watchesInCorrectDir(path, shouldFire) { const cli = await this.installWatch(); const feature = cli.getFeature('watch'); let fireCount = 0; void cli.on('watcher.did-detect-change', () => { fireCount++; }); await this.startWatching(feature); spruce_skill_utils_1.diskUtil.writeFile(this.resolvePath(path, 'test.ts'), 'console.log("hello world")'); await this.stopWatching(feature); test_utils_1.assert.isEqual(fireCount, shouldFire ? 1 : 0); } static async watcherFiresEventWhenASrcFileChanges(changeCount) { let fireCount = 0; const cli = await this.installWatch(); const feature = cli.getFeature('watch'); let payloadChanges = {}; void cli.on('watcher.did-detect-change', (payload) => { fireCount++; payloadChanges = payload.changes; }); spruce_skill_utils_1.diskUtil.createDir(this.cwd); await this.startWatching(feature); const expectedChanges = []; for (let idx = 0; idx < changeCount; idx++) { const filename = `index-${idx}.js`; spruce_skill_utils_1.diskUtil.writeFile(this.resolvePath('build', filename), 'console.log("hello world")'); expectedChanges.push({ id: 'generatedFile', values: { action: 'generated', path: this.resolvePath('build', filename), name: filename, }, }); } await this.stopWatching(feature); test_utils_1.assert.isEqual(fireCount, 1); test_utils_1.assert.isTrue(payloadChanges.length >= changeCount); for (const expected of expectedChanges) { test_utils_1.assert.doesInclude(payloadChanges, expected); } } static async canTrackAddingDir() { await this.watchRunStop(async () => { const newDirDest = this.resolvePath('build', 'new_dir'); spruce_skill_utils_1.diskUtil.createDir(newDirDest); const expected = [ { id: 'generatedDir', values: { action: 'generated', name: 'new_dir', path: this.resolvePath('build', 'new_dir'), }, }, ]; return expected; }); } static async canTrackDeletingDir() { const newDirDest = this.resolvePath('build', 'new_dir'); spruce_skill_utils_1.diskUtil.createDir(newDirDest); await this.watchRunStop(async () => { spruce_skill_utils_1.diskUtil.deleteDir(newDirDest); const expected = [ { id: 'generatedDir', values: { action: 'deleted', name: 'new_dir', path: this.resolvePath('build', 'new_dir'), }, }, ]; return expected; }); } static async canTrackDeletingFile() { const newFile = this.resolvePath('build', 'test.js'); spruce_skill_utils_1.diskUtil.writeFile(newFile, 'test'); await this.watchRunStop(async () => { spruce_skill_utils_1.diskUtil.deleteFile(newFile); const expected = [ { id: 'generatedFile', values: { action: 'deleted', name: 'test.js', path: this.resolvePath('build', 'test.js'), }, }, ]; return expected; }); } static async canTrackDeletingFileInDirectory() { const newDirDest = this.resolvePath('build', 'new_dir'); spruce_skill_utils_1.diskUtil.createDir(newDirDest); const newFile = this.resolvePath('build', 'new_dir', 'test.js'); spruce_skill_utils_1.diskUtil.writeFile(newFile, 'test'); await this.watchRunStop(async () => { spruce_skill_utils_1.diskUtil.deleteFile(newFile); const expected = [ { id: 'generatedFile', values: { action: 'deleted', name: 'test.js', path: this.resolvePath('build', 'new_dir', 'test.js'), }, }, ]; return expected; }); } static async stopWatching(feature) { await this.wait(3000); await feature.stopWatching(); } static async startWatching(feature) { void feature.startWatching({ delay: 0 }); await this.wait(1000); } static async watchRunStop(runner) { const cli = await this.installWatch(); const feature = cli.getFeature('watch'); let payloadChanges = {}; void cli.on('watcher.did-detect-change', (payload) => { payloadChanges = payload.changes; }); await this.startWatching(feature); const expected = await runner(); await this.stopWatching(feature); for (const e of expected) { test_utils_1.assert.doesInclude(payloadChanges, e); } } static async installWatch() { const fixture = this.FeatureFixture(); const cli = await fixture.installFeatures([{ code: 'watch' }]); return cli; } } exports.default = WatchingForChangesTest; __decorate([ (0, test_utils_1.test)() ], WatchingForChangesTest, "installsWatchers", null); __decorate([ (0, test_utils_1.test)() ], WatchingForChangesTest, "notWatchingAtTheStart", null); __decorate([ (0, test_utils_1.test)() ], WatchingForChangesTest, "canStartAndStopWatch", null); __decorate([ (0, test_utils_1.test)('does not watch src', 'src', false), (0, test_utils_1.test)('does watch build', 'build', true), (0, test_utils_1.test)('does watch dist', 'dist', true) ], WatchingForChangesTest, "watchesInCorrectDir", null); __decorate([ (0, test_utils_1.test)('fires once with one change', 1), (0, test_utils_1.test)('fires once with two changes', 2), (0, test_utils_1.test)('fires once with three changes', 3) ], WatchingForChangesTest, "watcherFiresEventWhenASrcFileChanges", null); __decorate([ (0, test_utils_1.test)() ], WatchingForChangesTest, "canTrackAddingDir", null); __decorate([ (0, test_utils_1.test)() ], WatchingForChangesTest, "canTrackDeletingDir", null); __decorate([ (0, test_utils_1.test)() ], WatchingForChangesTest, "canTrackDeletingFile", null); __decorate([ (0, test_utils_1.test)() ], WatchingForChangesTest, "canTrackDeletingFileInDirectory", null); //# sourceMappingURL=WatchingForChanges.test.js.map