Commit 1958bf1f authored by Steve Streza's avatar Steve Streza
Browse files

More cli

parent f499578a
import DataStore from "./src/data/database";
import FolderAccessor from "./src/data/accessors/folder";
import * as Yargs from 'yargs'
const accessor = new FolderAccessor("/tmp/data");
let foo = new DataStore(accessor);
foo = foo;
foo.accessor
.create("horse", {
date: new Date(),
data: {
message: "Hello world"
}
Yargs
.usage("$0 <command> [args]")
.command("create", "creates a new journal entry", yargs => yargs
.positional("message", {
alias: "m",
type: "string",
describe: "The message to post"
})
.positional("journal", {
alias: "j",
type: "string",
default: "default",
describe: "The name of the journal (defaults to \"default\")"
})
.demandOption(["message"])
, (argv: { [key: string]: any }) => {
const message: string = argv.message
const journal: string = argv.journal
if (message && journal) {
foo.accessor
.create(journal, {
date: new Date(),
data: {
message
}
})
.subscribe({
error: error => {
console.error("ERROR: ", error);
process.exit(1)
}
});
}
})
.subscribe({
next: data => {
console.log("Donezo: " + data);
.command("journals", "lists journals", yargs => yargs, () => {
foo.accessor.listJournals()
.subscribe({
next: j => {
console.log("" + j.slug)
}
})
})
.command("list [journal]", "lists the contents of the journal", yargs => yargs
.positional("journal", {
type: "string",
default: "default",
describe: "The name of the journal (defaults to \"default\")"
})
, (argv: any) => {
const journal: string = argv.journal
if (journal) {
foo.accessor.list(journal)
.subscribe({
next: entry => {
if (entry.date) {
console.log("" + entry.date.toLocaleDateString() + " " + entry.date.toLocaleTimeString() + ": " + entry.data.message)
}
else {
console.log("" + entry.data.message)
}
}
})
}
});
})
.help()
.argv
This diff is collapsed.
......@@ -4,7 +4,7 @@
"description": "",
"main": "index.ts",
"scripts": {
"build": "tsc --outDir bin index.ts",
"build": "tsc -p .",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
......@@ -13,6 +13,7 @@
"@types/bluebird": "^3.5.25",
"@types/mkdirp": "^0.5.2",
"@types/node": "^10.12.18",
"@types/yargs": "^12.0.5",
"ts-node": "^7.0.1",
"tslint": "^5.12.0",
"typescript": "^3.2.2"
......@@ -21,6 +22,7 @@
"bluebird": "^3.5.3",
"mkdirp": "^0.5.1",
"readdir-rx": "0.0.6",
"rxjs": "^6.3.3"
"rxjs": "^6.3.3",
"yargs": "^12.0.5"
}
}
import * as Bluebird from "bluebird";
import { Observable, from, empty } from "rxjs";
import { map, flatMap, concatMap, tap, filter } from "rxjs/operators";
import { map, flatMap, concatMap, filter } from "rxjs/operators";
import { Record, Accessor } from "../accessor";
......@@ -54,15 +54,9 @@ export default class FolderAccessor implements Accessor {
create(journalSlug: string, record: Record): Observable<boolean> {
const recordPath = this.path(journalSlug, record);
return this.initializer.pipe(
tap(() => console.log("Initialized")),
flatMap(() => mkdirp(this.journalPath(journalSlug), null)),
tap(() =>
console.log("Created journal path ", this.journalPath(journalSlug))
),
flatMap(() =>
writeFile(recordPath, JSON.stringify(record)).then(() => true)
),
tap(() => console.log("Created record file ", recordPath))
flatMap(() => writeFile(recordPath, JSON.stringify(record))),
map(() => true)
);
}
......@@ -75,7 +69,11 @@ export default class FolderAccessor implements Accessor {
return timestamp > 1000 && timestamp <= start;
}),
concatMap(result => readFile(result.file)),
map(data => JSON.parse(data.toString("utf8")))
map(data => JSON.parse(data.toString("utf8"))),
map(json => ({
...json,
date: new Date(json.date),
}) as Record)
);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment