Last active
August 16, 2017 12:53
-
-
Save masonkmeyer/340c251b46440839f80e372b4f59ed09 to your computer and use it in GitHub Desktop.
cp with RxJS and TS
This file contains 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
import * as rx from 'rxjs'; | |
import * as fs from 'fs'; | |
import * as path from 'path'; | |
function cp(from: string, to: string) { | |
return stat(from) | |
.flatMap((s) => { | |
return s.isDirectory() ? copyDir(from, to) : copyFile(from, to) | |
}) | |
} | |
function cpIf(from: string, to: string, filter: (x: string) => boolean) { | |
return expand(from) | |
.filter(filter) | |
.flatMap((x: string) => { | |
return rx.Observable.combineLatest(stat(x), rx.Observable.of(x), (x, y) => { return { stat: x, file: y } }); | |
}) | |
.flatMap((s) => { | |
let newPath = s.file.replace(from, to); | |
return s.stat.isFile() ? copyFile(s.file, newPath) : mkdir(newPath); | |
}); | |
} | |
function copyFile(from: string, to: string) { | |
return readFile(from).flatMap((x) => { | |
return writeFile(to, x); | |
}); | |
} | |
function copyDir(from: string, to: string) { | |
return expand(from) | |
.flatMap((x: string) => { | |
let newPath = x.replace(from, to); | |
return rx.Observable.combineLatest(stat(x), rx.Observable.of(x), rx.Observable.of(newPath), (x, y, z) => { return { stat: x, from: y, to: z } }); | |
}).flatMap((s) => { | |
return s.stat.isFile() ? copyFile(s.from, s.to) : mkdir(s.to); | |
}); | |
} | |
function exists(p: string) { | |
let e = rx.Observable.bindNodeCallback(fs.exists); | |
return e; | |
} | |
function expand(dir: string): rx.Observable<string | {}> { | |
return ls(dir) | |
.expand((file: string) => { | |
return stat(file).flatMap(s => { | |
return s.isDirectory() ? ls(file) : rx.Observable.empty(); | |
}) | |
}); | |
} | |
function ls(dir: string): rx.Observable<string> { | |
const read = rx.Observable.bindNodeCallback(fs.readdir) | |
return read(dir) | |
.flatMap((files: string[]) => { | |
return rx.Observable.from(files); | |
}) | |
.map((x: string) => { | |
return path.join(dir, x); | |
}); | |
} | |
function mkdir(p: string): rx.Observable<{}> { | |
let dir = rx.Observable.bindNodeCallback(fs.mkdir); | |
let exists = fs.existsSync(p); | |
return exists ? rx.Observable.of({}) : dir(p); | |
} | |
function stat(aPath: string): rx.Observable<fs.Stats> { | |
var stat = rx.Observable.bindNodeCallback(fs.stat); | |
return stat(aPath); | |
} | |
function readFile(file: string): rx.Observable<fs.ReadStream> { | |
let s = fs.createReadStream(file, {}); | |
rx.Observable.fromEvent(s, "error").subscribe(x => { console.log(x); }); | |
return rx.Observable.fromEvent(s, 'open') | |
.map(x => { | |
return s; | |
}); | |
} | |
function writeFile(path: string, b: fs.ReadStream): rx.Observable<fs.WriteStream> { | |
let s = fs.createWriteStream(path, {}); | |
b.pipe(s); | |
rx.Observable.fromEvent(s, "error").subscribe(x => { console.log(x); }) | |
return rx.Observable.fromEvent(s, 'close'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: The read and write file need some work. It was just an example for @CrowderSoup