@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
73 lines • 2.73 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 chokidar_1 = __importDefault(require("chokidar"));
const AbstractFeature_1 = __importDefault(require("../AbstractFeature"));
class WatchFeature extends AbstractFeature_1.default {
description = 'Watches for changes on the file system and emits app level events for other features to respond to.';
code = 'watch';
nameReadable = 'Watch';
_isWatching = false;
watcher;
timeoutId;
changesSinceLastChange = [];
async isWatching() {
return this._isWatching;
}
async startWatching(options) {
this._isWatching = true;
const watchDir = spruce_skill_utils_1.diskUtil.resolvePath(this.cwd, options?.sourceDir ?? '');
this.watcher = chokidar_1.default.watch([
spruce_skill_utils_1.diskUtil.resolvePath(watchDir, 'build'),
spruce_skill_utils_1.diskUtil.resolvePath(watchDir, 'dist'),
], {
ignoreInitial: true,
});
this.watcher.on('all', async (action, path) => {
this.changesSinceLastChange.push({
id: this.mapChokidarActionToSchemaId(action),
values: {
action: this.mapChokidarActionToGeneratedAction(action),
path,
name: path_1.default.basename(path),
},
});
if (this.timeoutId) {
clearTimeout(this.timeoutId);
}
this.timeoutId = setTimeout(async () => {
await this.fireChange();
}, options?.delay ?? 500);
});
}
mapChokidarActionToSchemaId(action) {
return action.search(/dir/gi) > -1 ? 'generatedDir' : 'generatedFile';
}
mapChokidarActionToGeneratedAction(chokidar) {
const map = {
add: 'generated',
addDir: 'generated',
change: 'updated',
unlink: 'deleted',
unlinkDir: 'deleted',
};
return map[chokidar];
}
async fireChange() {
const changes = this.changesSinceLastChange;
this.changesSinceLastChange = [];
await this.emitter.emitAndFlattenResponses('watcher.did-detect-change', {
changes,
});
}
async stopWatching() {
this._isWatching = false;
await this.watcher?.close();
}
}
exports.default = WatchFeature;
//# sourceMappingURL=WatchFeature.js.map
;