From 647ad2dac55d9ebc08cb75f46ebb851472eb74c1 Mon Sep 17 00:00:00 2001 From: Santiago Souza Date: Sun, 19 Feb 2023 00:10:34 -0300 Subject: [PATCH 1/2] Fix event parsing locales and error handling --- package.json | 2 +- src/actions/googleSheet/loadDocument.ts | 20 +++++++++++++------ src/actions/init.ts | 20 ++++++++++++++----- src/module/actions/fetchEvents/fetchEvents.ts | 8 +++----- .../parseEvents/byDefinedColumns/index.ts | 4 ++-- src/utils/parseEvents/byRead/index.ts | 7 ++++--- 6 files changed, 39 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index 95408ae..1a7cc8e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gsheet-to-calendar", - "version": "0.0.5-1", + "version": "0.0.5-5", "license": "MIT", "repository": "git@github.com:makis-san/gsheet-to-calendar.git", "description": "CLI app and package that uses google spreadsheet api to export calendar data directly into google agenda or other supported methods :)", diff --git a/src/actions/googleSheet/loadDocument.ts b/src/actions/googleSheet/loadDocument.ts index 7cc87e4..ce50473 100644 --- a/src/actions/googleSheet/loadDocument.ts +++ b/src/actions/googleSheet/loadDocument.ts @@ -1,16 +1,24 @@ import chalk from 'chalk' import { GoogleSpreadsheet } from 'google-spreadsheet' -export default async (docId: string, callback: (msg: string) => void) => { +export default async ( + docId: string, + callback: (msg: string) => void, + orFail: boolean = false +) => { const doc = new GoogleSpreadsheet(docId) doc.useApiKey(process.env.DOC_API_KEY as string) - await doc.loadInfo().catch((error) => { + try { + await doc.loadInfo() + return doc + } catch (error) { if (error) { callback(`Unable to locate document ${chalk.bold(docId)}`) - process.exit(1) + if (orFail) { + process.exit(1) + } } - }) - - return doc + return undefined + } } diff --git a/src/actions/init.ts b/src/actions/init.ts index e3fe0ae..10b546c 100644 --- a/src/actions/init.ts +++ b/src/actions/init.ts @@ -17,10 +17,16 @@ const fetchSheetInformation = async (args?: { const spinner = useSpinner('Trying to fetch spreadsheet...') if (!isSilent) spinner.start() - const document = await googleSheet.loadDocument(docId, (msg) => { - spinner.text = msg - spinner.fail() - }) + const document = await googleSheet.loadDocument( + docId, + (msg) => { + spinner.text = msg + spinner.fail() + }, + true + ) + + if (!document) return spinner.text = `Succesfully loaded ${document.title}` spinner.succeed() @@ -38,11 +44,15 @@ const fetchSheetInformation = async (args?: { export default async (args: CLIArguments) => { args = await args - const { document, sheetId } = await fetchSheetInformation({ + const documentResponse = await fetchSheetInformation({ docId: args.docId, sheetId: args.sheetId }).catch(() => process.exit(1)) + if (!documentResponse) return + + const { document, sheetId } = documentResponse + const spinner = useSpinner('Trying to fetch calendar...') if (!isSilent) spinner.start() diff --git a/src/module/actions/fetchEvents/fetchEvents.ts b/src/module/actions/fetchEvents/fetchEvents.ts index 188341e..f138a2d 100644 --- a/src/module/actions/fetchEvents/fetchEvents.ts +++ b/src/module/actions/fetchEvents/fetchEvents.ts @@ -7,11 +7,9 @@ import { saveToGoogle } from '../saveToGoogle/saveToGoogle' import { FetchEventsFN } from './fetchEvents.types' export const fetchEvents: FetchEventsFN = async (props) => { - const { - document, - options = { ...defaultFetchOptions, debug: false }, - sheetId - } = props + const { document, options: receivedOptions, sheetId } = props + + const options = receivedOptions || { ...defaultFetchOptions, debug: false } const sheet = sheetId && sheetId !== '' && document.sheetsById[sheetId] diff --git a/src/utils/parseEvents/byDefinedColumns/index.ts b/src/utils/parseEvents/byDefinedColumns/index.ts index 98beef2..dca6fdd 100644 --- a/src/utils/parseEvents/byDefinedColumns/index.ts +++ b/src/utils/parseEvents/byDefinedColumns/index.ts @@ -32,7 +32,7 @@ export default ( const parsedEvents = dates.map((dateString) => { const date = parse( - dateValue.toString().replace(/.*[0-9]/gm, `${dateString}`), + dateValue.toString(), options.dateFormat, new Date(), { @@ -43,7 +43,7 @@ export default ( date.setHours(0, 0, 0, 0) return { date, - title: `${title.value} - ${format(date, 'dMY')}` + title: `${title.value}` } }) diff --git a/src/utils/parseEvents/byRead/index.ts b/src/utils/parseEvents/byRead/index.ts index e2742a3..7133164 100644 --- a/src/utils/parseEvents/byRead/index.ts +++ b/src/utils/parseEvents/byRead/index.ts @@ -29,19 +29,20 @@ export default async ( if (!dates || !nearCell.value) return const parsedEvents = dates.map((dateString) => { + console.log(cell.value.toString()) const date = parse( - cell.value.toString().replace(/.*[0-9]/gm, `${dateString}`), + cell.value.toString(), options.dateFormat, new Date(), { locale: Locales[options.locale] } ) - date.setHours(0, 0, 0, 0) + return { date, - title: `${nearCell.value} - ${format(date, 'dMY')}` + title: `${nearCell.value}` } }) From c0f954b79a8e1a36012e5a5aae8c9b3a8df41483 Mon Sep 17 00:00:00 2001 From: Santiago Souza Date: Mon, 20 Feb 2023 20:45:20 -0300 Subject: [PATCH 2/2] Bump to 0.0.6 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1a7cc8e..a25fd87 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gsheet-to-calendar", - "version": "0.0.5-5", + "version": "0.0.6", "license": "MIT", "repository": "git@github.com:makis-san/gsheet-to-calendar.git", "description": "CLI app and package that uses google spreadsheet api to export calendar data directly into google agenda or other supported methods :)",