Skip to content

Commit

Permalink
Merge pull request #21 from makis-san/hotfix/module-issues-with-docum…
Browse files Browse the repository at this point in the history
…ent-loading

Fix event parsing locales and error handling
  • Loading branch information
makis-san authored Feb 20, 2023
2 parents 7f04bfc + c0f954b commit 191bdb7
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gsheet-to-calendar",
"version": "0.0.5-1",
"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 :)",
Expand Down
20 changes: 14 additions & 6 deletions src/actions/googleSheet/loadDocument.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
20 changes: 15 additions & 5 deletions src/actions/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
8 changes: 3 additions & 5 deletions src/module/actions/fetchEvents/fetchEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions src/utils/parseEvents/byDefinedColumns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
{
Expand All @@ -43,7 +43,7 @@ export default (
date.setHours(0, 0, 0, 0)
return {
date,
title: `${title.value} - ${format(date, 'dMY')}`
title: `${title.value}`
}
})

Expand Down
7 changes: 4 additions & 3 deletions src/utils/parseEvents/byRead/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
}
})

Expand Down

0 comments on commit 191bdb7

Please sign in to comment.