Skip to content

Commit 5959ddc

Browse files
committed
fix: tslint
1 parent b21fcbc commit 5959ddc

File tree

4 files changed

+77
-79
lines changed

4 files changed

+77
-79
lines changed

src/commands/notes/create.ts

+30-30
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ import {
22
CommentPermissionType,
33
CreateNoteOptions,
44
NotePermissionRole,
5-
} from "@hackmd/api/dist/type";
6-
import { CliUx, Flags } from "@oclif/core";
7-
import * as fs from "fs";
5+
} from '@hackmd/api/dist/type'
6+
import {CliUx, Flags} from '@oclif/core'
7+
import * as fs from 'fs'
88

9-
import HackMDCommand from "../../command";
9+
import HackMDCommand from '../../command'
1010
import {
1111
commentPermission,
1212
noteContent,
1313
notePermission,
1414
noteTitle,
15-
} from "../../flags";
16-
import { safeStdinRead, temporaryMD } from "../../utils";
17-
import openEditor from "../../openEditor";
15+
} from '../../flags'
16+
import openEditor from '../../open-editor'
17+
import {safeStdinRead, temporaryMD} from '../../utils'
1818

1919
export default class CreateCommand extends HackMDCommand {
20-
static description = "Create a note";
20+
static description = 'Create a note'
2121

2222
static examples = [
2323
"notes create --content='# A new note' --readPermission=owner --writePermission=owner --commentPermission=disabled",
@@ -26,73 +26,73 @@ export default class CreateCommand extends HackMDCommand {
2626
────────────────────── ──────────────────────────────── ────────────────────── ────────
2727
raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q null`,
2828

29-
"Or you can pipe content via Unix pipeline:",
30-
"cat README.md | hackmd-cli notes create",
31-
];
29+
'Or you can pipe content via Unix pipeline:',
30+
'cat README.md | hackmd-cli notes create',
31+
]
3232

3333
static flags = {
34-
help: Flags.help({ char: "h" }),
34+
help: Flags.help({char: 'h'}),
3535
title: noteTitle(),
3636
content: noteContent(),
3737
readPermission: notePermission(),
3838
writePermission: notePermission(),
3939
commentPermission: commentPermission(),
4040
editor: Flags.boolean({
41-
char: "e",
42-
description: "create note with $EDITOR",
41+
char: 'e',
42+
description: 'create note with $EDITOR',
4343
}),
4444
...CliUx.ux.table.flags(),
45-
};
45+
}
4646

4747
async run() {
48-
const { flags } = await this.parse(CreateCommand);
49-
const pipeString = safeStdinRead();
48+
const {flags} = await this.parse(CreateCommand)
49+
const pipeString = safeStdinRead()
5050

5151
const options: CreateNoteOptions = {
5252
title: flags.title,
5353
content: pipeString || flags.content,
5454
readPermission: flags.readPermission as NotePermissionRole,
5555
writePermission: flags.writePermission as NotePermissionRole,
5656
commentPermission: flags.commentPermission as CommentPermissionType,
57-
};
57+
}
5858

5959
if (flags.editor) {
6060
try {
61-
const mdFile = temporaryMD();
62-
await openEditor(mdFile);
61+
const mdFile = temporaryMD()
62+
await openEditor(mdFile)
6363

64-
options.content = fs.readFileSync(mdFile).toString();
64+
options.content = fs.readFileSync(mdFile).toString()
6565
} catch (e) {
66-
this.error(e as Error);
66+
this.error(e as Error)
6767
}
6868
}
6969

7070
try {
71-
const APIClient = await this.getAPIClient();
72-
const note = await APIClient.createNote(options);
71+
const APIClient = await this.getAPIClient()
72+
const note = await APIClient.createNote(options)
7373

7474
CliUx.ux.table(
7575
[note],
7676
{
7777
id: {
78-
header: "ID",
78+
header: 'ID',
7979
},
8080
title: {},
8181
userPath: {
82-
header: "User path",
82+
header: 'User path',
8383
},
8484
teamPath: {
85-
header: "Team path",
85+
header: 'Team path',
8686
},
8787
},
8888
{
8989
printLine: this.log.bind(this),
9090
...flags,
9191
}
92-
);
92+
)
9393
} catch (e) {
94-
this.log("Create note failed");
95-
this.error(e as Error);
94+
this.log('Create note failed')
95+
this.error(e as Error)
9696
}
9797
}
9898
}

src/open-editor.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {ChildProcess, spawn} from 'child_process'
2+
3+
interface EditorOptions {
4+
editor?: string
5+
}
6+
7+
export function openEditor(
8+
file: string,
9+
opts: EditorOptions = {}
10+
): Promise<void> {
11+
return new Promise((resolve, reject) => {
12+
const editor = getEditor(opts.editor)
13+
const args = editor.split(/\s+/)
14+
const bin = args.shift()
15+
16+
if (!bin) {
17+
reject(new Error('Editor binary not found'))
18+
return
19+
}
20+
21+
const ps: ChildProcess = spawn(bin, [...args, file], {stdio: 'inherit'})
22+
23+
ps.on('exit', () => {
24+
resolve()
25+
})
26+
27+
ps.on('error', (err: Error) => {
28+
reject(err)
29+
})
30+
})
31+
}
32+
33+
function getEditor(editor?: string): string {
34+
return (
35+
editor || process.env.VISUAL || process.env.EDITOR || getDefaultEditor()
36+
)
37+
}
38+
39+
function getDefaultEditor(): string {
40+
return /^win/.test(process.platform) ? 'notepad' : 'vim'
41+
}
42+
43+
export default openEditor

src/openEditor.ts

-45
This file was deleted.

src/utils.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ export function safeStdinRead() {
4141

4242
// generate temporary markdown file in /tmp directory
4343
export function temporaryMD() {
44-
const tmpDir = tmpdir();
45-
const filename = `temp_${Math.random().toString(36).substring(2)}.md`;
46-
const filePath = path.join(tmpDir, filename);
44+
const tmpDir = tmpdir()
45+
const filename = `temp_${Math.random().toString(36).substring(2)}.md`
46+
const filePath = path.join(tmpDir, filename)
4747

48-
return filePath;
48+
return filePath
4949
}

0 commit comments

Comments
 (0)