@filemonger/main
Advanced tools
Comparing version
@@ -1,2 +0,2 @@ | ||
import { AbsolutePath, FullPath, VoidStream } from "@filemonger/types"; | ||
export default function symlinkFile(srcFile: FullPath<AbsolutePath>, outFile: FullPath<AbsolutePath>): VoidStream; | ||
import { AbsolutePath, FullPath, VoidStream, Directory } from "@filemonger/types"; | ||
export default function symlinkFile(srcFile: FullPath<AbsolutePath> | Directory<AbsolutePath>, outFile: FullPath<AbsolutePath> | Directory<AbsolutePath>): VoidStream; |
@@ -1,2 +0,2 @@ | ||
import { FileStream, RelativePath, Directory, AbsolutePath } from "@filemonger/types"; | ||
export default function tmp(fn: (tmpDir: Directory<AbsolutePath>) => FileStream<RelativePath>): FileStream<RelativePath>; | ||
import { Directory, AbsolutePath, OpaqueStream } from "@filemonger/types"; | ||
export default function tmp(fn: (tmpDir: Directory<AbsolutePath>) => OpaqueStream): OpaqueStream; |
@@ -10,3 +10,3 @@ "use strict"; | ||
function tmp(fn) { | ||
return createTmpDir().flatMap(tmpDir => fn(tmpDir).do({ | ||
return createTmpDir().mergeMap(tmpDir => fn(tmpDir).do({ | ||
complete() { | ||
@@ -13,0 +13,0 @@ rimraf(tmpDir, err => { |
@@ -1,5 +0,5 @@ | ||
import { Filemonger, IDict, Transform } from "@filemonger/types"; | ||
import { Filemonger, IDict, Transform, IFilemonger } from "@filemonger/types"; | ||
import * as helpers from "./helpers"; | ||
export { helpers }; | ||
export declare function makeFilemonger<Opts extends IDict<any>>(transform: Transform<Opts>): Filemonger<Opts>; | ||
export declare const passthrumonger: Filemonger<{}>; | ||
export declare const make: <Opts extends IDict<any>>(transform: Transform<Opts>) => Filemonger<Opts>; | ||
export declare const merge: (...instances: IFilemonger[]) => IFilemonger; |
@@ -7,56 +7,26 @@ "use strict"; | ||
const rxjs_1 = require("rxjs"); | ||
const { f, tmp, symlinkFile, filesInDir } = helpers; | ||
function makeFilemonger(transform) { | ||
return (patternOrFileStream = "**/*.*", opts = {}) => { | ||
const unit = (srcDir, destDir) => { | ||
const resolvedSrcDir = f.dir(f.abs(path_1.resolve(process.cwd(), srcDir))); | ||
const finalDestDir = f.dir(f.abs(path_1.resolve(process.cwd(), destDir))); | ||
const file$ = typeof patternOrFileStream === "string" | ||
? filesInDir(resolvedSrcDir, f.pat(patternOrFileStream)) | ||
: patternOrFileStream; | ||
return transform(file$, resolvedSrcDir, finalDestDir, opts); | ||
}; | ||
const run = (srcDir, destDir, complete) => { | ||
return unit(srcDir, destDir) | ||
.toArray() | ||
.first() | ||
.subscribe({ | ||
next(files) { | ||
complete(undefined, files); | ||
}, | ||
error(err) { | ||
complete(err, []); | ||
} | ||
}); | ||
}; | ||
const bind = mongerFactory => { | ||
const bindingmonger = makeFilemonger((_, srcDir, destDir) => tmp(tmpDir => mongerFactory(unit(srcDir, tmpDir)).unit(tmpDir, destDir))); | ||
return bindingmonger(); | ||
}; | ||
const bypass = (predicate, fn) => { | ||
return multicast(f$ => fn(f$.filter(predicate)), f$ => exports.passthrumonger(f$.filter(f => !predicate(f)))); | ||
}; | ||
const merge = (...others) => { | ||
const mergemonger = makeFilemonger((_, srcDir, destDir) => { | ||
const thisFile$ = unit(srcDir, destDir); | ||
const otherFile$s = others.map(o => o.unit(srcDir, destDir)); | ||
return rxjs_1.Observable.merge(thisFile$, ...otherFile$s); | ||
}); | ||
return mergemonger(); | ||
}; | ||
const multicast = (...sinkFactories) => { | ||
const multicastmonger = makeFilemonger((file$, srcDir, destDir) => file$.multicast(() => new rxjs_1.Subject(), file$ => rxjs_1.Observable.merge(...sinkFactories.map(sF => sF(file$).unit(srcDir, destDir))))); | ||
return bind(multicastmonger); | ||
}; | ||
return { | ||
bind, | ||
bypass, | ||
multicast, | ||
merge, | ||
unit, | ||
run | ||
}; | ||
const { f, tmp } = helpers; | ||
const isSubscribable = (thing) => thing && thing.subscribe; | ||
const isPromise = (thing) => thing && thing.then; | ||
exports.make = (transform) => (srcDir = "", opts = Object.create({})) => { | ||
const writeTo = (destDir) => { | ||
const resolvedSrcDir = f.dir(f.abs(path_1.resolve(srcDir))); | ||
const resolvedDestDir = f.dir(f.abs(path_1.resolve(destDir))); | ||
const result = transform(resolvedSrcDir, resolvedDestDir, opts); | ||
if (isSubscribable(result)) { | ||
return result.map(() => { }); | ||
} | ||
if (isPromise(result)) { | ||
return rxjs_1.Observable.fromPromise(result).map(() => { }); | ||
} | ||
return rxjs_1.Observable.of(result).map(() => { }); | ||
}; | ||
} | ||
exports.makeFilemonger = makeFilemonger; | ||
exports.passthrumonger = makeFilemonger((file$, srcDir, destDir) => file$.delayWhen(file => symlinkFile(f.fullPath(f.abs(path_1.join(srcDir, file))), f.fullPath(f.abs(path_1.join(destDir, file)))))); | ||
const run = (destDir, done = () => { }) => writeTo(destDir).subscribe({ complete: done, error: done }); | ||
const bind = factory => exports.make((_, destDir) => tmp(tmpDir => writeTo(tmpDir).mergeMap(() => factory(tmpDir).writeTo(destDir))))(); | ||
return { | ||
bind, | ||
writeTo, | ||
run | ||
}; | ||
}; | ||
exports.merge = (...instances) => exports.make((_, destDir) => rxjs_1.Observable.forkJoin(instances.map(m => m.writeTo(destDir))))(); |
{ | ||
"name": "@filemonger/main", | ||
"version": "0.4.2", | ||
"version": "0.5.0", | ||
"description": "Process files with streams", | ||
@@ -24,3 +24,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@filemonger/types": "^0.3.0", | ||
"@filemonger/types": "^0.5.0", | ||
"@types/fs-extra": "^5.0.0", | ||
@@ -31,3 +31,3 @@ "fs-extra": "^5.0.0", | ||
"devDependencies": { | ||
"@filemonger/cli": "^0.2.3", | ||
"@filemonger/filtermonger": "^0.5.0", | ||
"@types/babel-core": "^6.25.3", | ||
@@ -34,0 +34,0 @@ "@types/chai": "^4.1.2", |
9822
-11.55%208
-12.61%+ Added
- Removed
Updated