How to use watchFile method in stryker-parent

Best JavaScript code snippet using stryker-parent

watchFile.js

Source:watchFile.js Github

copy

Full Screen

1/**2 * 1.监听文件变动3 */4const fs = require("fs");5const path = require("path");6const tools = require("./tools");7 8 9 /**10 * 事件回调11 *12 * @callback WatchEventCallback13 * @param {string} eventName - 事件名 create | delete | change | init14 * @param {Array<string>} files - 改变的文件15 */16 17 /**18 * 监听事件19 */20 class WatchFile {21 /** 22 * @param {WatchFile} watchObj23 * @param {string} pathName 24 * @param {WatchEventCallback} eventCallback 25 */26 constructor(watchObj,pathName,eventCallback){27 this.pathName = pathName;28 this.eventCallback = eventCallback;29 this._watchObj = watchObj;30 /** @type Object<string,fs.Stats> */31 this._files = {}32 this._isChecking = false;33 this._isInit = false;34 this._isExistPath = false;35 this.check();36 }37 38 stop(){39 this._watchObj.removeWatch(this.eventCallback);40 }41 42 /**43 * 更新文件状态44 */45 async check(){46 if(this._isChecking){47 return;48 }49 let pathStat = fs.existsSync(this.pathName) ? fs.statSync(this.pathName) : undefined;50 if(!pathStat){51 // 监听的目录被删除52 if(this._isExistPath) setTimeout(this._removeAllFile.bind(this),1);53 this._isExistPath = false;54 return ;55 }56 this._isExistPath = true;57 this._isChecking = true;58 let newFiles = {}59 if(pathStat && pathStat.isDirectory())60 {61 newFiles = await getDirAllFiles(this.pathName,newFiles)62 this._update(newFiles)63 }else if(pathStat.isFile())64 {65 newFiles[this.pathName] = pathStat;66 // 延迟执行防止循环调用67 setTimeout(()=>this._update(newFiles),1) 68 }69 }70 71 /**72 * 更新状态73 * @param {Object<string,fs.Stats>} newFiles 74 */75 _update(newFiles){76 let deleteFiles = [];77 let changeFiles = [];78 let createFiles = [];79 for (const filePath in this._files) {80 const oldFileStats = this._files[filePath];81 const newFileStats = newFiles[filePath]82 if(newFileStats == null){83 // 文件被删84 deleteFiles.push(filePath);85 }else if(newFileStats.mtimeMs != oldFileStats.mtimeMs){86 // 文件被修改87 changeFiles.push(filePath);88 }89 }90 91 for (const filePath in newFiles) {92 /** @type fs.Stats */93 const oldFileStats = this._files[filePath];94 if(oldFileStats == null){95 // 新创文件96 createFiles.push(filePath);97 }98 }99 let isInit = this._isInit;100 this._files = newFiles;101 this._isChecking = false;102 this._isInit = true;103 104 // 调用事件105 if(this.eventCallback){106 if(!isInit){107 this.eventCallback('init',createFiles);108 }else{109 if(deleteFiles.length) this.eventCallback('delete',deleteFiles);110 if(changeFiles.length) this.eventCallback('change',changeFiles);111 if(createFiles.length) this.eventCallback('create',createFiles);112 }113 }114 }115 _removeAllFile(){116 let deleteFiles = [];117 for (const filePath in this._files) {118 deleteFiles.push(filePath);119 }120 this._files = {};121 if(deleteFiles.length) this.eventCallback('delete',deleteFiles);122 }123 }124 125 /**126 * 监听文件类127 */128 class WatchMgr{129 constructor(){130 /** @type [WatchFile] */131 this.eventListens = []132 this.watchFileBuffs = {}133 }134 135 /**136 * 137 * @param {string} pathName 138 * @param {WatchEventCallback} eventCallback 139 * @returns {WatchFile}140 */141 addWatchPath(pathName,eventCallback) {142 let watchFile = new WatchFile(this,pathName,eventCallback);143 this.eventListens.push(watchFile);144 return watchFile;145 }146 147 checkAll() {148 for (let i = 0; i < this.eventListens.length; i++) {149 const watchFile = this.eventListens[i];150 watchFile.check();151 }152 }153 154 check(pathName) {155 for (let i = 0; i < this.eventListens.length; i++) {156 const watchFile = this.eventListens[i];157 if(watchFile.pathName == pathName){158 watchFile.check();159 break160 }161 }162 }163 164 removeWatch(pathName) {165 for (let i = 0; i < this.eventListens.length; i++) {166 const watchFile = this.eventListens[i];167 if(watchFile.pathName == pathName){168 this.eventListens.splice(i,1);169 break170 }171 }172 }173 }174 175 176async function getDirAllFiles(dirPath ,result = {}) {177 return new Promise((resolve, reject )=> 178 {179 fs.readdir(dirPath,(err,files)=>180 {181 if(err) return reject(err);182 let len = files.length;183 if(len == 0){184 resolve(result); // 没有文件185 return;186 }187 let cur_ind = 0;188 files.forEach((val) => {189 let fPath = path.join(dirPath, val);190 fs.stat(fPath,async (err,stat)=>191 {192 if(err) return reject(err);193 if (stat.isDirectory()) {194 result = await getDirAllFiles(fPath,result);195 } else if (stat.isFile()) {196 result[fPath] = stat ;197 }198 cur_ind ++;199 if(cur_ind == len) {200 resolve(result)201 }202 })203 });204 });205 })206 }207 208 209 exports.WatchMgr = WatchMgr;...

Full Screen

Full Screen

livereload.js

Source:livereload.js Github

copy

Full Screen

1var livereload = require('livereload');2var argv = require('yargs').argv;3var path = require("path");4var watchFile = {5 image: ["png,jpg"],6 js: ["js"],7 css: ["css","less","styl","sass"],8 html: ["html","tpl"]9};10//watch type11var t = argv.t || "all";12//watch path13var p = path.join(__dirname,"src",argv.p||"");14var exts = (function(){15 var type = argv.t || "all";16 var list = []17 if(type == "js") {18 list = list.concat(watchFile.js);19 }20 if(type == "css") {21 list = list.concat(watchFile.css);22 }23 if(type == "html") {24 list = list.concat(watchFile.html);25 }26 if(type == "all") {27 list = list.concat(watchFile.js,watchFile.css,watchFile.html);28 }29 return list.concat(watchFile.image);30})();31var server = livereload.createServer({exts: exts})...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var fs = require('fs');2fs.watchFile('test.js', function (curr, prev) {3 console.log('the current mtime is: ' + curr.mtime);4 console.log('the previous mtime was: ' + prev.mtime);5});6var fs = require('fs');7fs.watch('test.js', function (event, filename) {8 console.log('event is: ' + event);9 if (filename) {10 console.log('filename provided: ' + filename);11 } else {12 console.log('filename not provided');13 }14});15var fs = require('fs');16fs.watch('test.js', function (event, filename) {17 console.log('event is: ' + event);18 if (filename) {19 console.log('filename provided: ' + filename);20 } else {21 console.log('filename not provided');22 }23});24the current mtime is: Sat May 30 2020 17:13:50 GMT+0530 (India Standard Time)25the previous mtime was: Sat May 30 2020 17:13:25 GMT+0530 (India Standard Time)26the current mtime is: Sat May 30 2020 17:13:55 GMT+0530 (India Standard Time)27the previous mtime was: Sat May 30 2020 17:13:50 GMT+0530 (India Standard Time)

Full Screen

Using AI Code Generation

copy

Full Screen

1const strykerParent = require('stryker-parent');2strykerParent.watchFile('test.js', (curr, prev) => {3 console.log(`the current mtime is: ${curr.mtime}`);4 console.log(`the previous mtime was: ${prev.mtime}`);5});6const strykerParent = require('stryker-parent');7strykerParent.watchFile('test.js', (curr, prev) => {8 console.log(`the current mtime is: ${curr.mtime}`);9 console.log(`the previous mtime was: ${prev.mtime}`);10});11const strykerParent = require('stryker-parent');12strykerParent.watchFile('test.js', (curr, prev) => {13 console.log(`the current mtime is: ${curr.mtime}`);14 console.log(`the previous mtime was: ${prev.mtime}`);15});16const strykerParent = require('stryker-parent');17strykerParent.watchFile('test.js', (curr, prev) => {18 console.log(`the current mtime is: ${curr.mtime}`);19 console.log(`the previous mtime was: ${prev.mtime}`);20});21const strykerParent = require('stryker-parent');22strykerParent.watchFile('test.js', (curr, prev) => {23 console.log(`the current mtime is: ${curr.mtime}`);24 console.log(`the previous mtime was: ${prev.mtime}`);25});26const strykerParent = require('stryker-parent');27strykerParent.watchFile('test.js', (curr, prev) => {28 console.log(`the current mtime is: ${curr.mtime}`);29 console.log(`the previous mtime was: ${prev.mtime}`);30});31const strykerParent = require('stryker-parent');32strykerParent.watchFile('test.js', (curr, prev) => {

Full Screen

Using AI Code Generation

copy

Full Screen

1var fs = require('fs');2var path = require('path');3var StrykerParent = require('stryker-parent');4var strykerParent = new StrykerParent();5var testFile = path.resolve(__dirname, 'test.js');6var log = console.log.bind(console);7strykerParent.watchFile(testFile, log, log);8fs.writeFileSync(testFile, 'console.log("watched file changed");');9### new StrykerParent()10### StrykerParent#watchFile(file, log, error)

Full Screen

Using AI Code Generation

copy

Full Screen

1var parent = require('stryker-parent');2var path = require('path');3var file = path.resolve('test.js');4var callback = function (curr, prev) {5 console.log('the current mtime is: ' + curr.mtime);6 console.log('the previous mtime was: ' + prev.mtime);7};8parent.watchFile(file, callback);9var parent = require('stryker-parent');10var path = require('path');11var file = path.resolve('test.js');12var callback = function (curr, prev) {13 console.log('the current mtime is: ' + curr.mtime);14 console.log('the previous mtime was: ' + prev.mtime);15};16parent.unwatchFile(file, callback);17var parent = require('stryker-parent');18var path = require('path');19var file = path.resolve('test.js');20var callback = function (event, filename) {21 console.log('event is: ' + event);22 if (filename) {23 console.log('filename provided: ' + filename);24 } else {25 console.log('filename not provided');26 }27};28parent.watch(file, callback);29var parent = require('stryker-parent');30var path = require('path');31var file = path.resolve('test.js');32var callback = function (event, filename) {33 console.log('event is: ' + event);34 if (filename) {35 console.log('filename provided: ' + filename);36 } else {37 console.log('filename not provided');38 }39};40parent.unwatch(file, callback);41var parent = require('stryker-parent');42var path = require('path');43var file = path.resolve('test.js');44var callback = function (event, filename) {45 console.log('event is: ' + event);46 if (filename) {47 console.log('filename provided: ' + filename);48 } else {49 console.log('filename not provided');50 }51};52parent.watch(file, callback);53parent.unwatch(file, callback);

Full Screen

Using AI Code Generation

copy

Full Screen

1var stryker = require('stryker-parent');2var file = 'test.js';3stryker.watchFile(file, function (event, filename) {4 console.log('event is: ' + event);5 console.log('filename provided: ' + filename);6});7var stryker = require('stryker-parent');8var file = 'test.js';9stryker.watchFile(file, function (event, filename) {10 console.log('event is: ' + event);11 console.log('filename provided: ' + filename);12});13var stryker = require('stryker-parent');14var file = 'test.js';15stryker.watchFile(file, function (event, filename) {16 console.log('event is: ' + event);17 console.log('filename provided: ' + filename);18});19var stryker = require('stryker-parent');20var file = 'test.js';21stryker.watchFile(file, function (event, filename) {22 console.log('event is: ' + event);23 console.log('filename provided: ' + filename);24});25var stryker = require('stryker-parent');26var file = 'test.js';27stryker.watchFile(file, function (event, filename) {28 console.log('event is: ' + event);29 console.log('filename provided: ' + filename);30});31var stryker = require('stryker-parent');32var file = 'test.js';33stryker.watchFile(file, function (event, filename) {34 console.log('event is: ' + event);35 console.log('filename provided: ' + filename);36});37var stryker = require('stryker-parent');38var file = 'test.js';39stryker.watchFile(file, function (event, filename) {40 console.log('event is: ' + event);41 console.log('filename provided: ' + filename);42});

Full Screen

Using AI Code Generation

copy

Full Screen

1const watchFile = require('stryker-parent').watchFile;2watchFile('path/to/file', (current, previous) => {3});4module.exports = {5};6module.exports = watchFile;7function watchFile() {8}9I have a similar issue. I am trying to use this package in a monorepo and I want to use the watchFile method from stryker-parent. I am not able to import it. I tried the following approaches:10const watchFile = require('stryker-parent/lib/watchFile');

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run stryker-parent automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful