62 lines
2.6 KiB
JavaScript
62 lines
2.6 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").glob("**/*.txt").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));
|
|
|
|
// Watch a remove having occurred
|
|
console.log("\n** 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));
|
|
|
|
// Watch an add having occurred
|
|
console.log("\n** 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));
|
|
|
|
// Watch a copy having occurred
|
|
console.log("\n** Copying dist/test-file2.txt to dist2/test-file.txt");
|
|
await testHost.copy('dist/test-file2.txt', "dist2/test-file.txt");
|
|
console.log("** Copied dist2/test-file.txt\n");
|
|
|
|
await new Promise((resolve,reject)=>setTimeout(()=>resolve(), 1000));// clearly our current process is slow
|
|
|
|
// Watch an edit having occurred
|
|
console.log("\n** 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(); |