host/tests/manual/watch.dev.js
2019-12-23 13:06:33 +01:00

58 lines
2.5 KiB
JavaScript

import {Host} from "../../dist";
async function testWatch(){
let host = new Host({workingDirectory: __dirname});
await host.remove("test", {recursive: true});// Make sure dir is removed and we have clean start (could have the user pass a force flag here..
let initialized = false;
let testHost = await host.cwd('test');
await testHost.write('test-file.out', "I am the first basic test-file!");
await testHost.write('dist/test-file.txt', "I am a very basic test-file yo!");
await testHost.write('dist/other/another-file.txt', "I serve to fill a tree of files");
await testHost.write('dist/other/more-files.out', "I also serve to fill a tree of files!");
initialized = true;
// TODO starting a watch on a dir before it is created does not work yet!!
let watchSub = host.watch("test").subscribe(({changes, files})=>{
console.log(`----------\nChanges:\n${
changes.map(x=>x.event.toUpperCase() + ": " + x.file).join('\n')
}\nFiles:\n${files.join("\n")}\n----------`);
});
await new Promise((resolve,reject)=>setTimeout(()=>resolve(),1000));// clearly our current process is slow
// Watch a remove having occurred
console.log("** Removing dist/test-file.txt");
await testHost.remove('dist/test-file.txt');
console.log("** Removed dist/test-file.txt\n");
await new Promise((resolve,reject)=>setTimeout(()=>resolve(),1000));// clearly our current process is slow
// Watch an add having occurred
console.log("** Adding dist/test-file2.txt");
await testHost.write('dist/test-file2.txt', 'I am a new test-file!');
console.log("** Added dist/test-file2.txt\n");
await new Promise((resolve,reject)=>setTimeout(()=>resolve(),1000));// clearly our current process is slow
// Watch a move having occurred TODO we don't have this feature yet!
//await testHost.move('dist/test-file2.txt', 'dist2/test-file.txt');
// Watch an edit having occurred
console.log("** Writing dist/other/another-file.txt");
await testHost.write('dist/other/another-file.txt', 'I am no longer the same file i used to be!');
console.log("** Written dist/other/another-file.txt\n");
await new Promise((resolve,reject)=>setTimeout(()=>resolve(), 3000));// clearly our current process is slow
console.log("UNSUBSCRIBING!");
// No longer watching should close all file handles!
watchSub.unsubscribe();
console.log("DONE!");
}
testWatch();