Last active
March 9, 2016 23:02
-
-
Save DrMabuse23/077350b8253cf855c2d0 to your computer and use it in GitHub Desktop.
watcher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gulp = require('gulp'); | |
var fs = require('fs'); | |
var join = require('path').join; | |
var util = require('gulp-util'); | |
var _file = null; | |
var rootdir = __dirname; | |
var SRC = null; | |
const DEST = 'dist'; | |
function writeinfile(file, cb) { | |
fs.stat(file, function(err, stats) { | |
if (err) return cb(err); | |
cb(null, stats); | |
}) | |
} | |
function prepare(file) { | |
var absolutePath = join(rootdir, file); | |
writeinfile(absolutePath, function(err, stats) { | |
if (err) { | |
process.exit(); | |
} | |
util.log("'" + util.colors.cyan(file) + "'", util.colors.red(JSON.stringify(stats, null, 2))); | |
util.log(util.colors.cyan("'isfile'"), util.colors.magenta(stats.isFile()) ); | |
util.log(util.colors.cyan("'isDirectory'"), util.colors.magenta(stats.isDirectory())); | |
if (stats.isFile()) { | |
SRC = file; | |
gulp.start('copyProjectFile') | |
} | |
}) | |
} | |
/** | |
* @link https://www.npmjs.com/package/gulp-chokidar | |
*/ | |
gulp.task('watch', function() { | |
var watch = require('gulp-chokidar'); | |
watch(['./test/**/*.*', './test/**'], function(event) { | |
}) | |
.on('all', function(eventType) { | |
//console.log('all eventtype: ', eventType); | |
var text = null; | |
switch(eventType) { | |
case 'change': | |
text = 'was changed'; | |
break; | |
case 'add': | |
text = 'was added'; | |
break; | |
case 'delete': | |
text = 'was deleted'; | |
break; | |
case 'error': | |
text = 'has an error'; | |
break; | |
}; | |
if(text) { | |
util.log("'" + util.colors.cyan(eventType) + "'", text); | |
} | |
}) | |
.on('ready', function(test) { | |
util.log(util.colors.yellow('multi client is activated')); | |
}) | |
.on('add', function(filepath) { | |
prepare(filepath); | |
}) | |
.on('change', function(filepath) { | |
prepare(filepath); | |
}) | |
.on('delete', function(filepath) { | |
util.log("'" + util.colors.red('deleted') + "'", filepath); | |
}) | |
.on('error', function(err) { | |
util.log(util.colors.red(err)); | |
}); | |
}); | |
gulp.task('copyProjectFile', function (){ | |
util.log(util.colors.red('copy from'), SRC); | |
return gulp.src(SRC).pipe( | |
gulp.dest(function(file) { | |
console.log(file.base); | |
var dest = file.base.replace('/test/', '/'+DEST+'/'); | |
util.log(util.colors.red('copy to'), dest); | |
return dest; | |
})); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
let gulp = require('gulp'); | |
let fs = require('fs'); | |
let join = require('path').join; | |
let chokidar = require('chokidar'); | |
let Rx = require('rxjs'); | |
let util = require('gulp-util'); | |
let rootdir = __dirname; | |
const DEST = `dist`; | |
class Project{ | |
constructor(folder){ | |
this.path = join(rootdir, folder); | |
this.folder = folder; | |
this.watcher = chokidar.watch(this.path,{ | |
persistent: true | |
}); | |
} | |
event(type){ | |
return Rx.Observable.fromEvent(this.watcher, type).delay(500); | |
} | |
copy(file, folder){ | |
gulp.src(file).pipe(gulp.dest( | |
(file) => { | |
// console.log(JSON.stringify(file, null, 2)); | |
let dest = file.base.replace(`/${folder}/`, `/${DEST}/`); | |
this.log(`copy from ${util.colors.white(file.base)} to `, dest, 'cyan', 'yellow'); | |
return dest; | |
} | |
)); | |
} | |
stop(){ | |
this.watcher.close(); | |
} | |
log(first, second, colorFirst, colorSecond){ | |
let d = { | |
colorFirst: colorFirst||'cyan', | |
first: first||'', | |
second: second||'', | |
colorSecond: colorSecond||'magenta' | |
}; | |
util.log(util.colors[d.colorFirst](d.first), util.colors[d.colorSecond](d.second)); | |
} | |
error(err){ | |
this.log(err, this.path, 'red'); | |
} | |
complete(){ | |
this.stop(); | |
this.log('complete', this.path, 'green'); | |
} | |
add(file){ | |
this.log(this.folder, file); | |
this.copy(file, this.folder); | |
} | |
} | |
class Client extends Project { | |
constructor(folder){ | |
super(folder); | |
this.listener(); | |
} | |
listener(){ | |
this.event('add').subscribe( | |
this.add.bind(this), | |
this.error.bind(this), | |
this.complete.bind(this) | |
); | |
this.event('change').subscribe( | |
this.add.bind(this), | |
this.error.bind(this), | |
this.complete.bind(this) | |
); | |
} | |
} | |
class Common extends Project{ | |
constructor(){ | |
super(`common`); | |
this.listener(); | |
} | |
listener(){ | |
this.event('add').subscribe( | |
this.add.bind(this), | |
this.error.bind(this), | |
this.complete.bind(this) | |
); | |
this.event('unlink').subscribe( | |
(path) => { | |
this.log(`File ${path} has been removed`); | |
}, | |
this.error.bind(this), | |
this.complete.bind(this) | |
); | |
this.event('change').subscribe( | |
this.add.bind(this), | |
this.error.bind(this), | |
this.complete.bind(this) | |
); | |
} | |
} | |
gulp.task('clientStart', () => { | |
let common = new Common(); | |
Rx.Observable.fromEvent(common.watcher, 'ready').subscribe(() => { | |
let client = new Client('test'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment