This commit is contained in:
Philipp Kunz 2016-09-22 16:33:44 +02:00
commit aaa7ee9ea9
15 changed files with 340 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
node_modules/
pages/
public/
coverage/

36
dist/index.d.ts vendored Normal file
View File

@ -0,0 +1,36 @@
/// <reference types="chokidar" />
/// <reference types="q" />
import * as plugins from './smartchok.plugins';
import { Stringmap } from 'lik';
export declare type TSmartchokStatus = 'idle' | 'starting' | 'watching';
export declare type TFsEvent = 'add' | 'addDir' | 'change' | 'error' | 'unlink' | 'unlinkDir' | 'ready' | 'raw';
export declare class Smartchok {
watchStringmap: Stringmap;
chokidarOptions: plugins.chokidar.WatchOptions;
status: TSmartchokStatus;
private watcher;
private watchingDeferred;
private eventObservablemap;
constructor(watchArrayArg: string[], optionsArg?: plugins.chokidar.WatchOptions);
/**
* adds files to the list of watched files
*/
add(pathArrayArg: string[]): void;
/**
* removes files from the list of watched files
*/
remove(pathArg: string): void;
/**
* gets an observable for a certain event
*/
getObservableFor(fsEvent: TFsEvent): plugins.q.Promise<plugins.rx.Observable<any>>;
/**
* starts the watcher
* @returns Promise<void>
*/
start(): plugins.q.Promise<void>;
/**
* stop the watcher process if watching
*/
stop(): void;
}

81
dist/index.js vendored Normal file
View File

@ -0,0 +1,81 @@
"use strict";
const plugins = require("./smartchok.plugins");
const lik_1 = require("lik");
class Smartchok {
constructor(watchArrayArg, optionsArg = {}) {
this.watchStringmap = new lik_1.Stringmap();
this.watchingDeferred = plugins.q.defer(); // used to run things when watcher is initialized
this.eventObservablemap = new plugins.lik.Observablemap(); // register one observable per event
this.watchStringmap.addStringArray(watchArrayArg);
this.chokidarOptions = optionsArg;
}
/**
* adds files to the list of watched files
*/
add(pathArrayArg) {
this.watchStringmap.addStringArray(pathArrayArg);
this.watchingDeferred.promise.then(() => {
this.watcher.add(pathArrayArg);
});
}
/**
* removes files from the list of watched files
*/
remove(pathArg) {
this.watchStringmap.removeString(''); // TODO
this.watchingDeferred.promise.then(() => {
this.watcher.unwatch(pathArg);
});
}
/**
* gets an observable for a certain event
*/
getObservableFor(fsEvent) {
let done = plugins.q.defer();
this.watchingDeferred.promise.then(() => {
let eventObservable = this.eventObservablemap.getObservableForEmitterEvent(this.watcher, fsEvent);
done.resolve(eventObservable);
});
return done.promise;
}
/**
* starts the watcher
* @returns Promise<void>
*/
start() {
let done = plugins.q.defer();
this.status = 'starting';
this.watcher = plugins.chokidar.watch(this.watchStringmap.getStringArray(), this.chokidarOptions);
this.watcher.on('ready', () => {
this.status = 'watching';
this.watchingDeferred.resolve();
done.resolve();
});
return done.promise;
}
/**
* stop the watcher process if watching
*/
stop() {
let closeWatcher = () => {
let watchedFiles = this.watcher.getWatched();
for (let key in watchedFiles) {
let files = watchedFiles[key];
for (let file in files) {
let filePath = plugins.path.join(key, file);
this.watcher.unwatch(filePath);
}
}
this.watcher.close();
};
if (this.status === 'watching') {
console.log('closing while watching');
closeWatcher();
}
else if (this.status === 'starting') {
this.watchingDeferred.promise.then(() => { closeWatcher(); });
}
}
}
exports.Smartchok = Smartchok;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsK0NBQThDO0FBQzlDLDZCQUErQjtBQUsvQjtJQVFJLFlBQVksYUFBdUIsRUFBRSxVQUFVLEdBQWtDLEVBQUU7UUFQbkYsbUJBQWMsR0FBRyxJQUFJLGVBQVMsRUFBRSxDQUFBO1FBSXhCLHFCQUFnQixHQUFHLE9BQU8sQ0FBQyxDQUFDLENBQUMsS0FBSyxFQUFRLENBQUEsQ0FBQyxpREFBaUQ7UUFDNUYsdUJBQWtCLEdBQUcsSUFBSSxPQUFPLENBQUMsR0FBRyxDQUFDLGFBQWEsRUFBRSxDQUFBLENBQUMsb0NBQW9DO1FBRzdGLElBQUksQ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FBQVEsR0FBRyxPQUFPLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQyxHQUFHLEVBQUMsSUFBSSxDQUFDLENBQUE7b0JBQzFDLElBQUksQ0FBQyxPQUFPLENBQUMsT0FBTyxDQUFDLFFBQVEsQ0FBQyxDQUFBO2dCQUNsQyxDQUFDO1lBQ0wsQ0FBQztZQUNELElBQUksQ0FBQyxPQUFPLENBQUMsS0FBSyxFQUFFLENBQUE7UUFDeEIsQ0FBQyxDQUFBO1FBQ0QsRUFBRSxDQUFDLENBQUMsSUFBSSxDQUFDLE1BQU0sS0FBSyxVQUFVLENBQUMsQ0FBQyxDQUFDO1lBQzdCLE9BQU8sQ0FBQyxHQUFHLENBQUMsd0JBQXdCLENBQUMsQ0FBQTtZQUNyQyxZQUFZLEVBQUUsQ0FBQTtRQUNsQixDQUFDO1FBQUMsSUFBSSxDQUFDLEVBQUUsQ0FBQyxDQUFDLElBQUksQ0FBQyxNQUFNLEtBQUssVUFBVSxDQUFDLENBQUMsQ0FBQztZQUNwQyxJQUFJLENBQUMsZ0JBQWdCLENBQUMsT0FBTyxDQUFDLElBQUksQ0FBQyxRQUFRLFlBQVksRUFBRSxDQUFBLENBQUMsQ0FBQyxDQUFDLENBQUE7UUFDaEUsQ0FBQztJQUNMLENBQUM7Q0FDSjtBQW5GRCw4QkFtRkMifQ==

6
dist/smartchok.plugins.d.ts vendored Normal file
View File

@ -0,0 +1,6 @@
import 'typings-global';
export import chokidar = require('chokidar');
export import lik = require('lik');
export import path = require('path');
export import q = require('q');
export import rx = require('rxjs/Rx');

8
dist/smartchok.plugins.js vendored Normal file
View File

@ -0,0 +1,8 @@
"use strict";
require("typings-global");
exports.chokidar = require("chokidar");
exports.lik = require("lik");
exports.path = require("path");
exports.q = require("q");
exports.rx = require("rxjs/Rx");
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjaG9rLnBsdWdpbnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGNob2sucGx1Z2lucy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBQ3ZCLHVDQUE0QztBQUM1Qyw2QkFBa0M7QUFDbEMsK0JBQW9DO0FBQ3BDLHlCQUE4QjtBQUM5QixnQ0FBcUMifQ==

5
npmextra.json Normal file
View File

@ -0,0 +1,5 @@
{
"npmts": {
"mode": "default"
}
}

36
package.json Normal file
View File

@ -0,0 +1,36 @@
{
"name": "smartchok",
"version": "1.0.0",
"description": "smart wrapper for chokidar",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"scripts": {
"test": "(npm run prepareTest && npmts)",
"prepareTest":"(rm -f ./test/assets/hi.txt)"
},
"repository": {
"type": "git",
"url": "git+ssh://git@gitlab.com/pushrocks/smartchok.git"
},
"author": "Lossless GmbH",
"license": "MIT",
"bugs": {
"url": "https://gitlab.com/pushrocks/smartchok/issues"
},
"homepage": "https://gitlab.com/pushrocks/smartchok#README",
"dependencies": {
"@types/chokidar": "^1.4.29",
"@types/q": "0.0.31",
"chokidar": "^1.6.0",
"lik": "^1.0.20",
"q": "^1.4.1",
"rxjs": "^5.0.0-beta.12",
"typings-global": "^1.0.14"
},
"devDependencies": {
"@types/should": "^8.1.30",
"should": "^11.1.0",
"smartfile": "^4.0.20",
"typings-test": "^1.0.3"
}
}

1
test/assets/hi.txt Normal file
View File

@ -0,0 +1 @@
HI

1
test/assets/test1.txt Normal file
View File

@ -0,0 +1 @@
# This is a textfile that will be cloned

1
test/test.d.ts vendored Normal file
View File

@ -0,0 +1 @@
import 'typings-test';

35
test/test.js Normal file
View File

@ -0,0 +1,35 @@
"use strict";
require("typings-test");
const should = require("should");
const smartfile = require("smartfile");
const smartchok = require("../dist/index");
describe('smartchok', function () {
let testSmartchok;
let testAddObservable;
let testSubscription;
it('should create a new instance', function () {
testSmartchok = new smartchok.Smartchok([]);
should(testSmartchok).be.instanceof(smartchok.Smartchok);
});
it('should add some files to watch and start', function (done) {
testSmartchok.add(['./test/assets/**/*.txt']);
testSmartchok.start().then(() => { done(); }).catch(err => { console.log(err); });
});
it('should get an observable for a certain event', function (done) {
testSmartchok.getObservableFor('add').then((observableArg) => {
testAddObservable = observableArg;
done();
}).catch(err => { console.log(err); });
});
it('should register an add operation', function (done) {
this.timeout(5000);
testSubscription = testAddObservable.subscribe(x => {
done();
});
smartfile.memory.toFs('HI', './test/assets/hi.txt');
});
it('should stop the watch process', function () {
testSmartchok.stop();
});
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUNyQixpQ0FBZ0M7QUFDaEMsdUNBQXNDO0FBRXRDLDJDQUEwQztBQUUxQyxRQUFRLENBQUMsV0FBVyxFQUFDO0lBQ2pCLElBQUksYUFBa0MsQ0FBQTtJQUN0QyxJQUFJLGlCQUFxQyxDQUFBO0lBQ3pDLElBQUksZ0JBQWlDLENBQUE7SUFDckMsRUFBRSxDQUFDLDhCQUE4QixFQUFDO1FBQzlCLGFBQWEsR0FBRyxJQUFJLFNBQVMsQ0FBQyxTQUFTLENBQUMsRUFBRSxDQUFDLENBQUE7UUFDM0MsTUFBTSxDQUFDLGFBQWEsQ0FBQyxDQUFDLEVBQUUsQ0FBQyxVQUFVLENBQUMsU0FBUyxDQUFDLFNBQVMsQ0FBQyxDQUFBO0lBQzVELENBQUMsQ0FBQyxDQUFBO0lBQ0YsRUFBRSxDQUFDLDBDQUEwQyxFQUFDLFVBQVMsSUFBSTtRQUN2RCxhQUFhLENBQUMsR0FBRyxDQUFDLENBQUMsd0JBQXdCLENBQUMsQ0FBQyxDQUFBO1FBQzdDLGFBQWEsQ0FBQyxLQUFLLEVBQUUsQ0FBQyxJQUFJLENBQUMsUUFBUSxJQUF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as should from 'should'
import * as smartfile from 'smartfile'
import * as rx from 'rxjs/Rx'
import * as smartchok from '../dist/index'
describe('smartchok',function(){
let testSmartchok: smartchok.Smartchok
let testAddObservable: rx.Observable<any>
let testSubscription: rx.Subscription
it('should create a new instance',function(){
testSmartchok = new smartchok.Smartchok([])
should(testSmartchok).be.instanceof(smartchok.Smartchok)
})
it('should add some files to watch and start',function(done){
testSmartchok.add(['./test/assets/**/*.txt'])
testSmartchok.start().then(() => { done() }).catch(err => { console.log(err) })
})
it('should get an observable for a certain event',function(done){
testSmartchok.getObservableFor('add').then((observableArg) => {
testAddObservable = observableArg
done()
}).catch(err => { console.log(err) })
})
it('should register an add operation',function(done){
this.timeout(5000)
testSubscription = testAddObservable.subscribe(x => {
done()
})
smartfile.memory.toFs('HI','./test/assets/hi.txt')
})
it('should stop the watch process',function() {
testSmartchok.stop()
})
})

82
ts/index.ts Normal file
View File

@ -0,0 +1,82 @@
import * as plugins from './smartchok.plugins'
import { Stringmap } from 'lik'
export type TSmartchokStatus = 'idle' | 'starting' | 'watching'
export type TFsEvent = 'add' | 'addDir' | 'change' | 'error' | 'unlink' | 'unlinkDir' | 'ready' | 'raw'
export class Smartchok {
watchStringmap = new Stringmap()
chokidarOptions: plugins.chokidar.WatchOptions
status: TSmartchokStatus
private watcher
private watchingDeferred = plugins.q.defer<void>() // used to run things when watcher is initialized
private eventObservablemap = new plugins.lik.Observablemap() // register one observable per event
constructor(watchArrayArg: string[], optionsArg: plugins.chokidar.WatchOptions = {}) {
this.watchStringmap.addStringArray(watchArrayArg)
this.chokidarOptions = optionsArg
}
/**
* adds files to the list of watched files
*/
add(pathArrayArg: string[]) {
this.watchStringmap.addStringArray(pathArrayArg)
this.watchingDeferred.promise.then(() => {
this.watcher.add(pathArrayArg)
})
}
/**
* removes files from the list of watched files
*/
remove(pathArg: string) {
this.watchStringmap.removeString('') // TODO
this.watchingDeferred.promise.then(() => {
this.watcher.unwatch(pathArg)
})
}
/**
* gets an observable for a certain event
*/
getObservableFor(fsEvent: TFsEvent): plugins.q.Promise<plugins.rx.Observable<any>> {
let done = plugins.q.defer<plugins.rx.Observable<any>>()
this.watchingDeferred.promise.then(() => {
let eventObservable = this.eventObservablemap.getObservableForEmitterEvent(this.watcher, fsEvent)
done.resolve(eventObservable)
})
return done.promise
}
/**
* starts the watcher
* @returns Promise<void>
*/
start(): plugins.q.Promise<void> {
let done = plugins.q.defer<void>()
this.status = 'starting'
this.watcher = plugins.chokidar.watch(this.watchStringmap.getStringArray(), this.chokidarOptions)
this.watcher.on('ready', () => {
this.status = 'watching'
this.watchingDeferred.resolve()
done.resolve()
})
return done.promise
}
/**
* stop the watcher process if watching
*/
stop() {
let closeWatcher = () => {
this.watcher.close()
}
if (this.status === 'watching') {
console.log('closing while watching')
closeWatcher()
} else if (this.status === 'starting') {
this.watchingDeferred.promise.then(() => { closeWatcher() })
}
}
}

6
ts/smartchok.plugins.ts Normal file
View File

@ -0,0 +1,6 @@
import 'typings-global'
export import chokidar = require('chokidar')
export import lik = require('lik')
export import path = require('path')
export import q = require('q')
export import rx = require('rxjs/Rx')

3
tslint.json Normal file
View File

@ -0,0 +1,3 @@
{
"extends": "tslint-config-standard"
}