Skip to content

Release 2.1.0 #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/config-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:

strategy:
matrix:
node-version: ['12', '14', '16']
node-version: ['18']

steps:
- name: Checkout repository
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
17
18
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ $ npm install -g @hackmd/hackmd-cli
$ hackmd-cli COMMAND
running command...
$ hackmd-cli (-v|--version|version)
@hackmd/hackmd-cli/2.0.2 darwin-arm64 node-v17.7.0
@hackmd/hackmd-cli/2.1.0 darwin-arm64 node-v17.9.1
$ hackmd-cli --help [COMMAND]
USAGE
$ hackmd-cli COMMAND
Expand Down Expand Up @@ -131,7 +131,7 @@ EXAMPLE
# A note to be exported
```

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

## `hackmd-cli help [COMMAND]`

Expand Down Expand Up @@ -177,7 +177,7 @@ EXAMPLE
BnC6gN0_TfStV2KKmPPXeg Welcome to your team's workspace null CLI-test
```

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

## `hackmd-cli login`

Expand All @@ -198,7 +198,7 @@ EXAMPLE
Login successfully
```

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

## `hackmd-cli logout`

Expand All @@ -217,7 +217,7 @@ EXAMPLE
You've logged out successfully
```

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

## `hackmd-cli notes`

Expand Down Expand Up @@ -246,7 +246,7 @@ EXAMPLE
raUuSTetT5uQbqQfLnz9lA CLI test note gvfz2UB5THiKABQJQnLs6Q null
```

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

## `hackmd-cli notes:create`

Expand All @@ -257,6 +257,7 @@ USAGE
$ hackmd-cli notes:create

OPTIONS
-e, --editor create note with $EDITOR
-h, --help Show CLI help.
-x, --extended show extra columns
--columns=columns only show provided columns (comma-separated)
Expand All @@ -281,7 +282,7 @@ EXAMPLES
cat README.md | hackmd-cli notes create
```

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

## `hackmd-cli notes:delete`

Expand All @@ -299,7 +300,7 @@ EXAMPLE
$ hackmd-cli notes delete --noteId=WNkLM6gkS0Cg2cQ8rv7bYA
```

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

## `hackmd-cli notes:update`

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

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

## `hackmd-cli team-notes`

Expand Down Expand Up @@ -348,7 +349,7 @@ EXAMPLE
BnC6gN0_TfStV2KKmPPXeg Welcome to your team's workspace null CLI-test
```

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

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

Expand Down Expand Up @@ -385,7 +386,7 @@ EXAMPLES
cat README.md | hackmd-cli notes create --teamPath=CLI-test
```

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

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

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

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

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

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

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

## `hackmd-cli teams`

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

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

## `hackmd-cli version`

Expand Down Expand Up @@ -489,7 +490,7 @@ EXAMPLE
82f7f3d9-4079-4c78-8a00-14094272ece9 Ming-Hsiu Tsai null gvfz2UB5THiKABQJQnLs6Q
```

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

## License
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hackmd/hackmd-cli",
"version": "2.0.2",
"version": "2.1.0",
"author": "HackMD Team",
"bin": {
"hackmd-cli": "./bin/run"
Expand Down
70 changes: 50 additions & 20 deletions src/commands/notes/create.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import {CommentPermissionType, CreateNoteOptions, NotePermissionRole} from '@hackmd/api/dist/type'
import {
CommentPermissionType,
CreateNoteOptions,
NotePermissionRole,
} from '@hackmd/api/dist/type'
import {CliUx, Flags} from '@oclif/core'
import * as fs from 'fs'

import HackMDCommand from '../../command'
import {commentPermission, noteContent, notePermission, noteTitle} from '../../flags'
import {safeStdinRead} from '../../utils'
import {
commentPermission,
noteContent,
notePermission,
noteTitle,
} from '../../flags'
import openEditor from '../../open-editor'
import {safeStdinRead, temporaryMD} from '../../utils'

export default class Create extends HackMDCommand {
export default class CreateCommand extends HackMDCommand {
static description = 'Create a note'

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

'Or you can pipe content via Unix pipeline:',
'cat README.md | hackmd-cli notes create'
'cat README.md | hackmd-cli notes create',
]

static flags = {
Expand All @@ -26,40 +37,59 @@ raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q
readPermission: notePermission(),
writePermission: notePermission(),
commentPermission: commentPermission(),
editor: Flags.boolean({
char: 'e',
description: 'create note with $EDITOR',
}),
...CliUx.ux.table.flags(),
}

async run() {
const {flags} = await this.parse(Create)
const {flags} = await this.parse(CreateCommand)
const pipeString = safeStdinRead()

const options: CreateNoteOptions = {
title: flags.title,
content: pipeString || flags.content,
readPermission: flags.readPermission as NotePermissionRole,
writePermission: flags.writePermission as NotePermissionRole,
commentPermission: flags.commentPermission as CommentPermissionType
commentPermission: flags.commentPermission as CommentPermissionType,
}

if (flags.editor) {
try {
const mdFile = temporaryMD()
await openEditor(mdFile)

options.content = fs.readFileSync(mdFile).toString()
} catch (e) {
this.error(e as Error)
}
}

try {
const APIClient = await this.getAPIClient()
const note = await APIClient.createNote(options)

CliUx.ux.table([note], {
id: {
header: 'ID',
},
title: {},
userPath: {
header: 'User path'
CliUx.ux.table(
[note],
{
id: {
header: 'ID',
},
title: {},
userPath: {
header: 'User path',
},
teamPath: {
header: 'Team path',
},
},
teamPath: {
header: 'Team path'
{
printLine: this.log.bind(this),
...flags,
}
}, {
printLine: this.log.bind(this),
...flags
})
)
} catch (e) {
this.log('Create note failed')
this.error(e as Error)
Expand Down
43 changes: 43 additions & 0 deletions src/open-editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {ChildProcess, spawn} from 'child_process'

interface EditorOptions {
editor?: string
}

export function openEditor(
file: string,
opts: EditorOptions = {}
): Promise<void> {
return new Promise((resolve, reject) => {
const editor = getEditor(opts.editor)
const args = editor.split(/\s+/)
const bin = args.shift()

if (!bin) {
reject(new Error('Editor binary not found'))
return
}

const ps: ChildProcess = spawn(bin, [...args, file], {stdio: 'inherit'})

ps.on('exit', () => {
resolve()
})

ps.on('error', (err: Error) => {
reject(err)
})
})
}

function getEditor(editor?: string): string {
return (
editor || process.env.VISUAL || process.env.EDITOR || getDefaultEditor()
)
}

function getDefaultEditor(): string {
return /^win/.test(process.platform) ? 'notepad' : 'vim'
}

export default openEditor
25 changes: 20 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs'
import {homedir} from 'os'
import fs from 'fs-extra'
import {homedir, tmpdir} from 'os'
import * as path from 'path'

export function getConfigFilePath() {
Expand All @@ -10,7 +10,14 @@ export function getConfigFilePath() {
configDir = path.join(homedir(), '.hackmd')
}

return path.join(configDir, 'config.json')
const configPath = path.join(configDir, 'config.json')

if (!fs.existsSync(configDir)) {
fs.ensureFileSync(configPath)
fs.writeFileSync(configPath, JSON.stringify({}))
}

return configPath
}

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

export function safeStdinRead() {
let result
const STDIN_FD = 0
try {
result = fs.readFileSync(STDIN_FD).toString()
result = fs.readFileSync(process.stdin.fd).toString()
} catch {}
return result
}

// generate temporary markdown file in /tmp directory
export function temporaryMD() {
const tmpDir = tmpdir()
const filename = `temp_${Math.random().toString(36).substring(2)}.md`
const filePath = path.join(tmpDir, filename)

return filePath
}