Skip to content

Commit

Permalink
Refoctor util export
Browse files Browse the repository at this point in the history
  • Loading branch information
godu committed May 30, 2016
1 parent 5fee449 commit 82d28f3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
12 changes: 9 additions & 3 deletions src/util/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {get$, set$, delRecursive$, del$, mkdir$, rmdirRecursive$} from './etcd';
import makeDebug from 'debug';
const debug = makeDebug('squirrel:util:cli');

export const sync$ = (client, pathFS, pathETCD) => {
const sync$ = (client, pathFS, pathETCD) => {
debug('sync', pathFS, pathETCD);
return isDirectory$(pathFS).flatMap(isDirectory => {
if (isDirectory)
Expand All @@ -17,7 +17,7 @@ export const sync$ = (client, pathFS, pathETCD) => {
);
};

export const syncDirectory$ = (client, pathFS, pathETCD) => {
const syncDirectory$ = (client, pathFS, pathETCD) => {
debug('syncDirectory', pathFS, pathETCD);
return get$(client, pathETCD).catch(err => {
if (err.errorCode === 100)
Expand Down Expand Up @@ -45,7 +45,7 @@ export const syncDirectory$ = (client, pathFS, pathETCD) => {
}).map(entry => relative(pathFS, entry));
};

export const syncFile$ = (client, pathFS, pathETCD) => {
const syncFile$ = (client, pathFS, pathETCD) => {
debug('syncFile', pathFS, pathETCD);
const entry$ = readFileUTF8$(pathFS);

Expand All @@ -61,3 +61,9 @@ export const syncFile$ = (client, pathFS, pathETCD) => {
return set$(client, pathETCD, file);
}).combineAll().ignoreElements();
};

export {
sync$,
syncDirectory$,
syncFile$
};
36 changes: 25 additions & 11 deletions src/util/etcd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,60 @@ import {Observable} from 'rxjs';
import makeDebug from 'debug';
const debug = makeDebug('squirrel:util:etcd');

export const compareAndSwap$ = (client, ...argz) =>
const compareAndSwap$ = (client, ...argz) =>
debug('compareAndSwap', ...argz) ||
Observable.bindNodeCallback(client.compareAndSwap.bind(client))(...argz).pluck(0);

export const del$ = (client, ...argz) =>
const del$ = (client, ...argz) =>
debug('del', ...argz) ||
Observable.bindNodeCallback(client.del.bind(client))(...argz).pluck(0);

export const delRecursive$ = (client, key) =>
const delRecursive$ = (client, key) =>
debug('delRecursive', key) ||
del$(client, key, {recursive: true});

export const get$ = (client, ...argz) =>
const get$ = (client, ...argz) =>
debug('get', ...argz) ||
Observable.bindNodeCallback(client.get.bind(client))(...argz).pluck(0);

export const getRecursive$ = (client, key) =>
const getRecursive$ = (client, key) =>
debug('getRecursive', key) ||
get$(client, key, {recursive: true});

export const mkdir$ = (client, ...argz) =>
const mkdir$ = (client, ...argz) =>
debug('mkdir', ...argz) ||
Observable.bindNodeCallback(client.mkdir.bind(client))(...argz).pluck(0);

export const rmdir$ = (client, ...argz) =>
const rmdir$ = (client, ...argz) =>
debug('rmdir', ...argz) ||
Observable.bindNodeCallback(client.rmdir.bind(client))(...argz).pluck(0);

export const rmdirRecursive$ = (client, key) =>
const rmdirRecursive$ = (client, key) =>
debug('rmdirRecursive', key) ||
rmdir$(client, key, {recursive: true});

export const set$ = (client, ...argz) =>
const set$ = (client, ...argz) =>
debug('set', ...argz) ||
Observable.bindNodeCallback(client.set.bind(client))(...argz).pluck(0);

export const isDirectory = node =>
const isDirectory = node =>
debug('isDirectory', node) ||
!!(node && node.dir);

export const isFile = node =>
const isFile = node =>
debug('isFile', node) ||
!!(node && !node.dir);

export {
compareAndSwap$,
del$,
delRecursive$,
get$,
getRecursive$,
mkdir$,
rmdir$,
rmdirRecursive$,
set$,
isDirectory,
isFile
};
24 changes: 17 additions & 7 deletions src/util/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,33 @@ import {Observable} from 'rxjs';
import {stat, readdir, readFile} from 'fs';
import {join} from 'path';

export const stat$ =
const stat$ =
Observable.bindNodeCallback(stat);

const _readdir$ = Observable.bindNodeCallback(readdir);
export const readdir$ = pathFS => _readdir$(pathFS).flatMap(Observable.from).map(entry => join(pathFS, entry));
const readdir$ = pathFS => _readdir$(pathFS).flatMap(Observable.from).map(entry => join(pathFS, entry));

export const readFile$ = Observable.bindNodeCallback(readFile);
export const readFileUTF8$ = file => readFile$(file, {encoding: 'UTF8'});
const readFile$ = Observable.bindNodeCallback(readFile);
const readFileUTF8$ = file => readFile$(file, {encoding: 'UTF8'});

export const isFile$ = pathFS =>
const isFile$ = pathFS =>
stat$(pathFS).map(stat => stat.isFile());

export const isDirectory$ = pathFS =>
const isDirectory$ = pathFS =>
stat$(pathFS).map(stat => stat.isDirectory());

export const filter$ = curry((predicate, value) =>
const filter$ = curry((predicate, value) =>
predicate(value)
.filter(Boolean)
.map(() => value)
);

export {
stat$,
readdir$,
readFile$,
readFileUTF8$,
isFile$,
isDirectory$,
filter$
};

0 comments on commit 82d28f3

Please sign in to comment.