Skip to content

Commit 02e1ea3

Browse files
authored
Merge pull request #71 from hackmdio/develop
Release 2.1.0
2 parents 8c8ff81 + c8c0bb4 commit 02e1ea3

File tree

8 files changed

+134
-45
lines changed

8 files changed

+134
-45
lines changed

.github/workflows/config-test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99

1010
strategy:
1111
matrix:
12-
node-version: ['12', '14', '16']
12+
node-version: ['18']
1313

1414
steps:
1515
- name: Checkout repository

.nvmrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
17
1+
18

README.md

+16-15
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ $ npm install -g @hackmd/hackmd-cli
2828
$ hackmd-cli COMMAND
2929
running command...
3030
$ hackmd-cli (-v|--version|version)
31-
@hackmd/hackmd-cli/2.0.2 darwin-arm64 node-v17.7.0
31+
@hackmd/hackmd-cli/2.1.0 darwin-arm64 node-v17.9.1
3232
$ hackmd-cli --help [COMMAND]
3333
USAGE
3434
$ hackmd-cli COMMAND
@@ -131,7 +131,7 @@ EXAMPLE
131131
# A note to be exported
132132
```
133133

134-
_See code: [src/commands/export.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.0.2/src/commands/export.ts)_
134+
_See code: [src/commands/export.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.1.0/src/commands/export.ts)_
135135

136136
## `hackmd-cli help [COMMAND]`
137137

@@ -177,7 +177,7 @@ EXAMPLE
177177
BnC6gN0_TfStV2KKmPPXeg Welcome to your team's workspace null CLI-test
178178
```
179179

180-
_See code: [src/commands/history.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.0.2/src/commands/history.ts)_
180+
_See code: [src/commands/history.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.1.0/src/commands/history.ts)_
181181

182182
## `hackmd-cli login`
183183

@@ -198,7 +198,7 @@ EXAMPLE
198198
Login successfully
199199
```
200200

201-
_See code: [src/commands/login.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.0.2/src/commands/login.ts)_
201+
_See code: [src/commands/login.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.1.0/src/commands/login.ts)_
202202

203203
## `hackmd-cli logout`
204204

@@ -217,7 +217,7 @@ EXAMPLE
217217
You've logged out successfully
218218
```
219219

220-
_See code: [src/commands/logout.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.0.2/src/commands/logout.ts)_
220+
_See code: [src/commands/logout.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.1.0/src/commands/logout.ts)_
221221

222222
## `hackmd-cli notes`
223223

@@ -246,7 +246,7 @@ EXAMPLE
246246
raUuSTetT5uQbqQfLnz9lA CLI test note gvfz2UB5THiKABQJQnLs6Q null
247247
```
248248

249-
_See code: [src/commands/notes/index.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.0.2/src/commands/notes/index.ts)_
249+
_See code: [src/commands/notes/index.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.1.0/src/commands/notes/index.ts)_
250250

251251
## `hackmd-cli notes:create`
252252

@@ -257,6 +257,7 @@ USAGE
257257
$ hackmd-cli notes:create
258258
259259
OPTIONS
260+
-e, --editor create note with $EDITOR
260261
-h, --help Show CLI help.
261262
-x, --extended show extra columns
262263
--columns=columns only show provided columns (comma-separated)
@@ -281,7 +282,7 @@ EXAMPLES
281282
cat README.md | hackmd-cli notes create
282283
```
283284

284-
_See code: [src/commands/notes/create.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.0.2/src/commands/notes/create.ts)_
285+
_See code: [src/commands/notes/create.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.1.0/src/commands/notes/create.ts)_
285286

286287
## `hackmd-cli notes:delete`
287288

@@ -299,7 +300,7 @@ EXAMPLE
299300
$ hackmd-cli notes delete --noteId=WNkLM6gkS0Cg2cQ8rv7bYA
300301
```
301302

302-
_See code: [src/commands/notes/delete.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.0.2/src/commands/notes/delete.ts)_
303+
_See code: [src/commands/notes/delete.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.1.0/src/commands/notes/delete.ts)_
303304

304305
## `hackmd-cli notes:update`
305306

@@ -318,7 +319,7 @@ EXAMPLE
318319
$ hackmd-cli notes update --noteId=WNkLM6gkS0Cg2cQ8rv7bYA --content='# A new title'
319320
```
320321

321-
_See code: [src/commands/notes/update.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.0.2/src/commands/notes/update.ts)_
322+
_See code: [src/commands/notes/update.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.1.0/src/commands/notes/update.ts)_
322323

323324
## `hackmd-cli team-notes`
324325

@@ -348,7 +349,7 @@ EXAMPLE
348349
BnC6gN0_TfStV2KKmPPXeg Welcome to your team's workspace null CLI-test
349350
```
350351

351-
_See code: [src/commands/team-notes/index.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.0.2/src/commands/team-notes/index.ts)_
352+
_See code: [src/commands/team-notes/index.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.1.0/src/commands/team-notes/index.ts)_
352353

353354
## `hackmd-cli team-notes:create`
354355

@@ -385,7 +386,7 @@ EXAMPLES
385386
cat README.md | hackmd-cli notes create --teamPath=CLI-test
386387
```
387388

388-
_See code: [src/commands/team-notes/create.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.0.2/src/commands/team-notes/create.ts)_
389+
_See code: [src/commands/team-notes/create.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.1.0/src/commands/team-notes/create.ts)_
389390

390391
## `hackmd-cli team-notes:delete`
391392

@@ -404,7 +405,7 @@ EXAMPLE
404405
$ hackmd-cli team-notes delete --teamPath=CLI-test --noteId=WNkLM6gkS0Cg2cQ8rv7bYA
405406
```
406407

407-
_See code: [src/commands/team-notes/delete.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.0.2/src/commands/team-notes/delete.ts)_
408+
_See code: [src/commands/team-notes/delete.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.1.0/src/commands/team-notes/delete.ts)_
408409

409410
## `hackmd-cli team-notes:update`
410411

@@ -424,7 +425,7 @@ EXAMPLE
424425
$ hackmd-cli team-notes update --teamPath=CLI-test --noteId=WNkLM6gkS0Cg2cQ8rv7bYA --content='# A new title'
425426
```
426427

427-
_See code: [src/commands/team-notes/update.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.0.2/src/commands/team-notes/update.ts)_
428+
_See code: [src/commands/team-notes/update.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.1.0/src/commands/team-notes/update.ts)_
428429

429430
## `hackmd-cli teams`
430431

@@ -452,7 +453,7 @@ EXAMPLE
452453
f76308a6-d77a-41f6-86d0-8ada426a6fb4 CLI test team CLI-test 82f7f3d9-4079-4c78-8a00-14094272ece9
453454
```
454455

455-
_See code: [src/commands/teams.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.0.2/src/commands/teams.ts)_
456+
_See code: [src/commands/teams.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.1.0/src/commands/teams.ts)_
456457

457458
## `hackmd-cli version`
458459

@@ -489,7 +490,7 @@ EXAMPLE
489490
82f7f3d9-4079-4c78-8a00-14094272ece9 Ming-Hsiu Tsai null gvfz2UB5THiKABQJQnLs6Q
490491
```
491492

492-
_See code: [src/commands/whoami.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.0.2/src/commands/whoami.ts)_
493+
_See code: [src/commands/whoami.ts](https://github.com/hackmdio/hackmd-cli/blob/v2.1.0/src/commands/whoami.ts)_
493494
<!-- commandsstop -->
494495

495496
## License

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hackmd/hackmd-cli",
3-
"version": "2.0.2",
3+
"version": "2.1.0",
44
"author": "HackMD Team",
55
"bin": {
66
"hackmd-cli": "./bin/run"

src/commands/notes/create.ts

+50-20
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
import {CommentPermissionType, CreateNoteOptions, NotePermissionRole} from '@hackmd/api/dist/type'
1+
import {
2+
CommentPermissionType,
3+
CreateNoteOptions,
4+
NotePermissionRole,
5+
} from '@hackmd/api/dist/type'
26
import {CliUx, Flags} from '@oclif/core'
7+
import * as fs from 'fs'
38

49
import HackMDCommand from '../../command'
5-
import {commentPermission, noteContent, notePermission, noteTitle} from '../../flags'
6-
import {safeStdinRead} from '../../utils'
10+
import {
11+
commentPermission,
12+
noteContent,
13+
notePermission,
14+
noteTitle,
15+
} from '../../flags'
16+
import openEditor from '../../open-editor'
17+
import {safeStdinRead, temporaryMD} from '../../utils'
718

8-
export default class Create extends HackMDCommand {
19+
export default class CreateCommand extends HackMDCommand {
920
static description = 'Create a note'
1021

1122
static examples = [
@@ -16,7 +27,7 @@ export default class Create extends HackMDCommand {
1627
raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q null`,
1728

1829
'Or you can pipe content via Unix pipeline:',
19-
'cat README.md | hackmd-cli notes create'
30+
'cat README.md | hackmd-cli notes create',
2031
]
2132

2233
static flags = {
@@ -26,40 +37,59 @@ raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q
2637
readPermission: notePermission(),
2738
writePermission: notePermission(),
2839
commentPermission: commentPermission(),
40+
editor: Flags.boolean({
41+
char: 'e',
42+
description: 'create note with $EDITOR',
43+
}),
2944
...CliUx.ux.table.flags(),
3045
}
3146

3247
async run() {
33-
const {flags} = await this.parse(Create)
48+
const {flags} = await this.parse(CreateCommand)
3449
const pipeString = safeStdinRead()
3550

3651
const options: CreateNoteOptions = {
3752
title: flags.title,
3853
content: pipeString || flags.content,
3954
readPermission: flags.readPermission as NotePermissionRole,
4055
writePermission: flags.writePermission as NotePermissionRole,
41-
commentPermission: flags.commentPermission as CommentPermissionType
56+
commentPermission: flags.commentPermission as CommentPermissionType,
57+
}
58+
59+
if (flags.editor) {
60+
try {
61+
const mdFile = temporaryMD()
62+
await openEditor(mdFile)
63+
64+
options.content = fs.readFileSync(mdFile).toString()
65+
} catch (e) {
66+
this.error(e as Error)
67+
}
4268
}
4369

4470
try {
4571
const APIClient = await this.getAPIClient()
4672
const note = await APIClient.createNote(options)
4773

48-
CliUx.ux.table([note], {
49-
id: {
50-
header: 'ID',
51-
},
52-
title: {},
53-
userPath: {
54-
header: 'User path'
74+
CliUx.ux.table(
75+
[note],
76+
{
77+
id: {
78+
header: 'ID',
79+
},
80+
title: {},
81+
userPath: {
82+
header: 'User path',
83+
},
84+
teamPath: {
85+
header: 'Team path',
86+
},
5587
},
56-
teamPath: {
57-
header: 'Team path'
88+
{
89+
printLine: this.log.bind(this),
90+
...flags,
5891
}
59-
}, {
60-
printLine: this.log.bind(this),
61-
...flags
62-
})
92+
)
6393
} catch (e) {
6494
this.log('Create note failed')
6595
this.error(e as Error)

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/utils.ts

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import fs from 'fs'
2-
import {homedir} from 'os'
1+
import fs from 'fs-extra'
2+
import {homedir, tmpdir} from 'os'
33
import * as path from 'path'
44

55
export function getConfigFilePath() {
@@ -10,7 +10,14 @@ export function getConfigFilePath() {
1010
configDir = path.join(homedir(), '.hackmd')
1111
}
1212

13-
return path.join(configDir, 'config.json')
13+
const configPath = path.join(configDir, 'config.json')
14+
15+
if (!fs.existsSync(configDir)) {
16+
fs.ensureFileSync(configPath)
17+
fs.writeFileSync(configPath, JSON.stringify({}))
18+
}
19+
20+
return configPath
1421
}
1522

1623
export function setAccessTokenConfig(token: string) {
@@ -26,9 +33,17 @@ export function setAccessTokenConfig(token: string) {
2633

2734
export function safeStdinRead() {
2835
let result
29-
const STDIN_FD = 0
3036
try {
31-
result = fs.readFileSync(STDIN_FD).toString()
37+
result = fs.readFileSync(process.stdin.fd).toString()
3238
} catch {}
3339
return result
3440
}
41+
42+
// generate temporary markdown file in /tmp directory
43+
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)
47+
48+
return filePath
49+
}

0 commit comments

Comments
 (0)