-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcreate.ts
65 lines (58 loc) · 1.8 KB
/
create.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { Command, flags } from "@oclif/command";
import * as fs from "fs";
import * as path from "path";
import { bearExec } from "../utils/bear-exec";
import { NoteId } from "../types";
import { logNoteId } from "../utils/log";
import cmdFlags from "../utils/flags";
import { argsWithPipe } from "../utils/read-pipe";
export default class Create extends Command {
static description = [
"Create a new note. Empty notes are not allowed.",
"Returns unique note identifier of new note."
].join("\n");
static flags = {
edit: cmdFlags.edit,
file: cmdFlags.file,
filename: cmdFlags.filename,
help: cmdFlags.help,
"new-window": cmdFlags["new-window"],
"open-note": cmdFlags["open-note"],
pin: cmdFlags.pin,
"show-window": cmdFlags["show-window"],
tag: flags.string({
char: "t",
description: "tag for note",
multiple: true
}),
timestamp: cmdFlags.timestamp,
title: cmdFlags.title
};
static args = [
{
name: "text",
description: "note body"
}
];
async run() {
const { flags, args: cmdArgs } = this.parse(Create);
const args = await argsWithPipe(Create.args, cmdArgs);
const { tag = [], file, ...restFlags } = flags;
type Params = typeof restFlags & { file?: string; tags: string };
const params: Params = { ...args, ...restFlags, tags: tag.join(",") };
// bear requires base64 encoding of file attachements
if (file) {
try {
const fileContents = fs.readFileSync(
path.join(process.cwd(), file),
"utf8"
);
params.file = Buffer.from(fileContents).toString("base64");
} catch (error) {
this.error("There was an error accessing that file");
}
}
const result = await bearExec<NoteId>("create", params);
logNoteId(result);
}
}