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

74 lines
3.3 KiB
JavaScript

import {Host} from "../../dist";
async function testWatch(){
let host = new Host({workingDirectory: __dirname});
let initialized = false;
// Initialize
await host.remove("src", {recursive: true});// Make sure dir is removed and we have clean start (could have the user pass a force flag here..
await host.remove("dist", {recursive: true});// Make sure dir is removed and we have clean start (could have the user pass a force flag here..
let srcHost = await host.cwd('src');
await srcHost.write('main.js', "//I represent a main-file");
await srcHost.write('assets/icon.svg', "<svg>I'm an example icon</svg>");
await srcHost.write('assets/logo.png', "I'm to lazy to make a miniature example image'");
await srcHost.write('common/another-source-file.js', "//I represent a service or common part of sorts");
await srcHost.write('.babelrc', "//I represent another source-file to be excluded from being copied");
await srcHost.write('index.html', "<html><body>I represent the entry-html file!</body></html>");
initialized = true;
console.log("Initialized, starting sync");
// Syntax like this for the moment! (this might change in the future...)
let syncSub = host.watch("src").glob("**/*.!(js|babelrc|json|scss)").sync("dist").subscribe(sync=>{
let changes = sync.changes;
console.log(`----------\nSYNCED Changes:\n${
changes.map(x=>x.event.toUpperCase() + ": " + x.file).join('\n')
}\n`);
});
let watchSub = host.watch("src").glob("**/*.!(js|babelrc|json|scss)").subscribe((event)=>{
let {changes, files} = event;
console.log(`----------\nOBSERVED Changes:\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("** Removing assets/icon.svg");
await srcHost.remove('assets/icon.svg');
console.log("** Removed assets/icon.svg\n");
await new Promise((resolve,reject)=>setTimeout(()=>resolve(),1000));
// Watch an add having occurred
console.log("** Adding assets/icon2.svg");
await srcHost.write('assets/icon2.svg', "<svg>I'm an new example icon</svg>");
console.log("** Added assets/icon2.svg\n");
await new Promise((resolve,reject)=>setTimeout(()=>resolve(),1000));
// Watch an edit having occurred
console.log("** Writing index.html");
await srcHost.write('index.html', "<html><body>I am no longer the same entry-html file!</body></html>");
console.log("** Written index.html\n");
await new Promise((resolve,reject)=>setTimeout(()=>resolve(), 1000));// clearly our current process is slow
// Test an unwatched edit having occurred
console.log("** Writing .babelrc");
await srcHost.write('.babelrc', "//I represent another source-file whose edit shouldn't have been seen!");
console.log("** Written babelrc\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();
syncSub.unsubscribe();
console.log("DONE!");
}
testWatch();