UNPKG

l7note

Version:

Access your notion notes quick

114 lines (98 loc) 2.6 kB
import { Client } from '@notionhq/client'; import { CreatePageParameters, CreatePageResponse, } from '@notionhq/client/build/src/api-endpoints'; import chalk from 'chalk'; import { exit } from 'process'; import prompts, { PromptObject } from 'prompts'; import { getSettingValueByName } from '../helper/getSettingValueByName.js'; import { globalConfig } from '../setup.js'; type NoteData = { title: string; text: string; tags: string; }; const getDateString = (): string => { const date = new Date(); let dateStr = ('00' + (date.getMonth() + 1)).slice(-2) + '/' + ('00' + date.getDate()).slice(-2) + '/' + date.getFullYear() + ' ' + ('00' + date.getHours()).slice(-2) + ':' + ('00' + date.getMinutes()).slice(-2) + ':' + ('00' + date.getSeconds()).slice(-2); return dateStr; }; const addQuestions: PromptObject[] = [ { type: 'text', name: 'title', message: 'Title of note', initial: 'Note from ' + getDateString(), }, { type: 'text', name: 'text', message: 'Text of note', }, { type: 'text', name: 'tags', message: 'Tags of note', hint: 'Seperate tags with spaces', }, ]; const addList = async () => { const notion = new Client({ auth: globalConfig.token }); let noteData: NoteData = { title: '', text: '', tags: '' }; if (globalConfig.optionalArgs.length == 0) { noteData = (await prompts(addQuestions)) as NoteData; } else { noteData.title = getSettingValueByName('-h'); noteData.text = getSettingValueByName('-d'); noteData.tags = getSettingValueByName('-t'); } const databaseId = globalConfig.dbId ?? ''; if (!noteData.title) { console.log(chalk.red('There was an error creating your note. Sorry')); exit(1); } let reqBody: CreatePageParameters = { parent: { database_id: databaseId, }, properties: { Title: { title: [{ text: { content: noteData.title } }] }, }, }; if (noteData.text) { reqBody.properties.Text = { rich_text: [{ text: { content: noteData.text } }], }; } if (noteData.tags) { reqBody.properties.Tags = { multi_select: noteData.tags.split(' ').map(tag => { return { name: tag }; }), }; } try { const response: any = await notion.pages.create(reqBody); if (process.platform != 'android') { const ncp = require('copy-paste'); ncp.copy(response.url); } console.log(chalk.green(response.url)); } catch (err) { console.log(chalk.red('There was an error creating your note. Sorry')); exit(1); } }; export { addList };