From d69ff8470373f50741b1cc7c42400e28d9f6e7a7 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Fri, 28 Apr 2023 21:15:48 +0800 Subject: [PATCH 1/2] feat: support --editor option for team note creation --- src/commands/notes/create.ts | 6 ++---- src/commands/team-notes/create.ts | 18 ++++++++++++++++-- src/flags.ts | 5 +++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/commands/notes/create.ts b/src/commands/notes/create.ts index 635697e..42ad492 100644 --- a/src/commands/notes/create.ts +++ b/src/commands/notes/create.ts @@ -12,6 +12,7 @@ import { noteContent, notePermission, noteTitle, + editor, } from '../../flags' import openEditor from '../../open-editor' import {safeStdinRead, temporaryMD} from '../../utils' @@ -37,10 +38,7 @@ raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q readPermission: notePermission(), writePermission: notePermission(), commentPermission: commentPermission(), - editor: Flags.boolean({ - char: 'e', - description: 'create note with $EDITOR', - }), + editor, ...CliUx.ux.table.flags(), } diff --git a/src/commands/team-notes/create.ts b/src/commands/team-notes/create.ts index 08938a3..c214467 100644 --- a/src/commands/team-notes/create.ts +++ b/src/commands/team-notes/create.ts @@ -1,9 +1,11 @@ import {CommentPermissionType, CreateNoteOptions, NotePermissionRole} from '@hackmd/api/dist/type' import {CliUx, Flags} from '@oclif/core' +import fs from 'fs' import HackMDCommand from '../../command' -import {commentPermission, noteContent, notePermission, noteTitle, teamPath} from '../../flags' -import {safeStdinRead} from '../../utils' +import {commentPermission, noteContent, notePermission, noteTitle, teamPath, editor} from '../../flags' +import {safeStdinRead, temporaryMD} from '../../utils' +import openEditor from '../../open-editor' export default class Create extends HackMDCommand { static description = 'Create a team note' @@ -26,6 +28,7 @@ raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q n readPermission: notePermission(), writePermission: notePermission(), commentPermission: commentPermission(), + editor, ...CliUx.ux.table.flags(), } @@ -46,6 +49,17 @@ raUuSTetT5uQbqQfLnz9lA A new note gvfz2UB5THiKABQJQnLs6Q n this.error('Flag teamPath could not be empty') } + 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.createTeamNote(teamPath, options) diff --git a/src/flags.ts b/src/flags.ts index c65634b..200f78c 100644 --- a/src/flags.ts +++ b/src/flags.ts @@ -22,3 +22,8 @@ export const notePermission = Flags.build({ export const commentPermission = Flags.build({ description: 'set comment permission: disabled, forbidden, owners, signed_in_users, everyone' }) + +export const editor = Flags.boolean({ + char: 'e', + description: 'create note with $EDITOR', + }) From 86aecfd9f9a9adcf8414ee69660fe0bcb282cc89 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Fri, 28 Apr 2023 21:26:29 +0800 Subject: [PATCH 2/2] fix: lint --- src/commands/notes/create.ts | 2 +- src/commands/team-notes/create.ts | 4 ++-- src/flags.ts | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/commands/notes/create.ts b/src/commands/notes/create.ts index 42ad492..788dc19 100644 --- a/src/commands/notes/create.ts +++ b/src/commands/notes/create.ts @@ -9,10 +9,10 @@ import * as fs from 'fs' import HackMDCommand from '../../command' import { commentPermission, + editor, noteContent, notePermission, noteTitle, - editor, } from '../../flags' import openEditor from '../../open-editor' import {safeStdinRead, temporaryMD} from '../../utils' diff --git a/src/commands/team-notes/create.ts b/src/commands/team-notes/create.ts index c214467..537a4ae 100644 --- a/src/commands/team-notes/create.ts +++ b/src/commands/team-notes/create.ts @@ -3,9 +3,9 @@ import {CliUx, Flags} from '@oclif/core' import fs from 'fs' import HackMDCommand from '../../command' -import {commentPermission, noteContent, notePermission, noteTitle, teamPath, editor} from '../../flags' -import {safeStdinRead, temporaryMD} from '../../utils' +import {commentPermission, editor, noteContent, notePermission, noteTitle, teamPath} from '../../flags' import openEditor from '../../open-editor' +import {safeStdinRead, temporaryMD} from '../../utils' export default class Create extends HackMDCommand { static description = 'Create a team note' diff --git a/src/flags.ts b/src/flags.ts index 200f78c..944cd2f 100644 --- a/src/flags.ts +++ b/src/flags.ts @@ -24,6 +24,6 @@ export const commentPermission = Flags.build({ }) export const editor = Flags.boolean({ - char: 'e', - description: 'create note with $EDITOR', - }) + char: 'e', + description: 'create note with $EDITOR', +})