From 3417f507abff7a1761d5f93ebadf0fc3df5c7f0c Mon Sep 17 00:00:00 2001 From: Santiago Souza Date: Mon, 13 Feb 2023 23:28:23 -0300 Subject: [PATCH 1/5] Bundle script --- package.json | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index f82bf1d..e778431 100644 --- a/package.json +++ b/package.json @@ -14,15 +14,21 @@ "scripts": { "start": "tsx ./src/cli.ts", "start:help": "tsx ./src/cli.ts --help", - "package": "pkg . --targets node16-macos-x64 --output ./bin/gsToCalendar", - "build": "rollup -c rollup.config.js", "prod": "npm run build && npm run package", "dev": "tsx watch ./src/cli.ts", + "build": "rollup -c rollup.config.js", + "package": "pkg . --output ./bin/gsToCalendar", + "bundle": "cd bin && mv gsToCalendar-macos gsToCalendar && tar -czf gsToCalendar.tar.gz gsToCalendar", "lint": "eslint ./src/**/*.{js, ts, tsx} --no-error-on-unmatched-pattern", "pretty-quick": "pretty-quick" }, "pkg": { - "scripts": "./dist/**/*.js" + "scripts": "./dist/**/*.js", + "targets": [ + "node16-linux-x64", + "node16-macos-x64", + "node16-win-x64" + ] }, "keywords": [], "author": "", From c85aae9b8fd7e0fcc81e0f4c9f17cb4ae8ac5cc3 Mon Sep 17 00:00:00 2001 From: Santiago Souza Date: Tue, 14 Feb 2023 00:04:56 -0300 Subject: [PATCH 2/5] Basic implementation and build --- package.json | 5 +- rollup.config.js | 75 +++++++++----- src/export/google/generateCalendar.ts | 4 +- src/index.ts | 143 ++++++++++++++++++++++++++ 4 files changed, 196 insertions(+), 31 deletions(-) create mode 100644 src/index.ts diff --git a/package.json b/package.json index e778431..96f1bf5 100644 --- a/package.json +++ b/package.json @@ -8,15 +8,12 @@ "engines": { "node": ">=16.0.0" }, - "files": [ - "./dist" - ], "scripts": { "start": "tsx ./src/cli.ts", "start:help": "tsx ./src/cli.ts --help", "prod": "npm run build && npm run package", "dev": "tsx watch ./src/cli.ts", - "build": "rollup -c rollup.config.js", + "build": "rollup -c rollup.config.js && cp ./package.json ./dist/module", "package": "pkg . --output ./bin/gsToCalendar", "bundle": "cd bin && mv gsToCalendar-macos gsToCalendar && tar -czf gsToCalendar.tar.gz gsToCalendar", "lint": "eslint ./src/**/*.{js, ts, tsx} --no-error-on-unmatched-pattern", diff --git a/rollup.config.js b/rollup.config.js index 700b158..c2e87de 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -9,38 +9,56 @@ dotenv.config(); const bundle = (config) => ({ ...config, - input: './src/cli.ts', external: (id) => { return !id.startsWith('.') && !path.isAbsolute(id); } }); +const esbuildBaseConfig = { + minify: false, + tsconfig: './tsconfig.json', + sourceMap: false, + define: { + 'process.env.CLIENT_ID': `"${process.env.CLIENT_ID}"`, + 'process.env.CLIENT_SECRET': `"${process.env.CLIENT_SECRET}"`, + 'process.env.DOC_API_KEY': `"${process.env.DOC_API_KEY}"`, + 'process.env.TIME_ZONE': `"${process.env.TIME_ZONE}"`, + 'process.env.DISABLE_OAUTH': `"${process.env.DISABLE_OAUTH}"` + } +}; + +const baseOutput = (config) => ({ + ...config, + format: 'commonjs', + sourcemap: true, + preserveModules: true +}); + export default [ bundle({ + input: './src/cli.ts', plugins: [ del({ targets: 'dist/*' }), peerDepsExternal({ packageJsonPath: './package.json' }), - esbuild({ - minify: false, - tsconfig: './tsconfig.json', - sourceMap: false, - define: { - 'process.env.CLIENT_ID': `"${process.env.CLIENT_ID}"`, - 'process.env.CLIENT_SECRET': `"${process.env.CLIENT_SECRET}"`, - 'process.env.DOC_API_KEY': `"${process.env.DOC_API_KEY}"`, - 'process.env.TIME_ZONE': `"${process.env.TIME_ZONE}"`, - 'process.env.DISABLE_OAUTH': `"${process.env.DISABLE_OAUTH}"` - } - }) + esbuild(esbuildBaseConfig) + ], + output: baseOutput({ + dir: `./dist/cli` + }) + }), + bundle({ + input: './src/index.ts', + plugins: [ + peerDepsExternal({ + packageJsonPath: './package.json' + }), + esbuild(esbuildBaseConfig) ], - output: { - dir: `./dist`, - format: 'commonjs', - sourcemap: true, - preserveModules: true - } + output: baseOutput({ + dir: `./dist/module` + }) }), { input: './src/cli.ts', @@ -49,10 +67,19 @@ export default [ tsconfig: './tsconfig.json' }) ], - output: { - dir: `./dist`, - format: 'commonjs', - preserveModules: true - } + output: baseOutput({ + dir: `./dist/cli` + }) + }, + { + input: './src/index.ts', + plugins: [ + dts({ + tsconfig: './tsconfig.json' + }) + ], + output: baseOutput({ + dir: `./dist/module` + }) } ]; diff --git a/src/export/google/generateCalendar.ts b/src/export/google/generateCalendar.ts index 72e306b..ee778d3 100644 --- a/src/export/google/generateCalendar.ts +++ b/src/export/google/generateCalendar.ts @@ -7,9 +7,7 @@ export default async (calendarTitle: string, oAuthClient: OAuth2Client) => { version: 'v3' }); - const calendars = await api.calendarList - .list(undefined) - .then((res) => res.data.items); + const calendars = await api.calendarList.list().then((res) => res.data.items); const calendarSearch = calendars?.find( (where) => where.summary === calendarTitle diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..0a9d93f --- /dev/null +++ b/src/index.ts @@ -0,0 +1,143 @@ +import { GoogleSpreadsheet } from 'google-spreadsheet'; +import { defaultFetchOptions } from './actions/fetchEvents/fetchEvents'; +import { FetchEventsFNOptions } from './actions/fetchEvents/fetchEvents.types'; +import { getColumnRange } from './actions/fetchEvents/utils/columnRange'; +import parseEvents from './actions/fetchEvents/utils/parseEvents/parseEvents'; +import googleSheet from './actions/googleSheet'; +import generateCalendar from './export/google/generateCalendar'; +import { getOAuthClient } from './export/google/getOAuthClient'; +import saveEvents from './export/google/saveEvents'; +import { log } from './utils'; + +export interface SaveToGoogleProps { + calendarTitle: string; + accessToken: string; + events: EventTypes[]; +} + +type BindedSaveToGoogleFN = ( + props: Omit +) => Promise; + +export const saveToGoogle = async (props: { + calendarTitle: string; + accessToken: string; + events: EventTypes[]; +}) => { + const { accessToken, calendarTitle, events } = props; + const OAuthClient = getOAuthClient(accessToken); + + const calendarId = await generateCalendar(calendarTitle, OAuthClient); + + if (!calendarId) { + log.error(`Unable to create calendar id`); + return; + } + + saveEvents(calendarId, events, OAuthClient); +}; + +interface FetchEventsProps { + document: GoogleSpreadsheet; + sheetId?: string; + options?: FetchEventsFNOptions & { debug?: boolean }; +} + +export type FetchEventsReturnTypes = Promise< + | { + events: EventTypes[]; + calendarTitle: string; + saveToGoogle: BindedSaveToGoogleFN; + } + | undefined +>; + +export const fetchEvents = async (props: { + document: GoogleSpreadsheet; + sheetId?: string; + options?: FetchEventsFNOptions & { debug?: boolean }; +}) => { + const { + document, + options = { ...defaultFetchOptions, debug: false }, + sheetId + } = props; + + const sheet = + sheetId && sheetId !== '' && document.sheetsById[sheetId] + ? document.sheetsById[sheetId] + : document.sheetsByIndex.at(-1); + + if (((sheetId && !document.sheetsById[sheetId]) || sheetId === '') && sheet) { + return undefined; + } + + if (!sheet) { + log.error(`Unable to locate sheet`); + return; + } + + const rowRange = sheet.rowCount || 34; + + await sheet.loadCells(`${options?.startColumn || 'A'}1:${rowRange}`); + + const columnsToFetchData = [ + options?.dateStringColumn, + options?.titleStringColumn + ]; + + const columnRange = getColumnRange([ + options?.startColumn || 'A', + sheet.lastColumnLetter + ]); + + let events: EventTypes[] = []; + + if (!columnsToFetchData[0] && !columnsToFetchData[1]) { + events = await parseEvents.byRead(sheet, rowRange, columnRange, options); + } else { + events = parseEvents.byDefinedColumns(sheet, rowRange, options); + } + + if (events.length <= 0) { + log.error('Unable to find any events in this document.'); + process.exit(1); + } + + if (options.debug) { + log.info((chalk) => `Loaded ${chalk.bold(events.length)} events!`); + } + + return { + events, + saveToGoogle: saveToGoogle.bind({ + calendarTitle: document.title, + events + }) as ( + props: Omit + ) => Promise, + calendarTitle: sheet.title + }; +}; + +export const loadSheetDocument = async (props: { url: string }) => { + const { url } = props; + + const docId = url + .replace('https://docs.google.com/spreadsheets/', '') + .split('/')[1]; + + const document = await googleSheet.loadDocument(docId, (msg) => + log.error(msg) + ); + + return { + document, + sheetList: googleSheet.getSheets(document), + fetchEvents: fetchEvents.bind({ document }) as ( + props: Omit + ) => FetchEventsReturnTypes + }; +}; + +export { googleSheet }; From 6b2f43009c911e4de3419900561fa6eb8e41cae4 Mon Sep 17 00:00:00 2001 From: Santiago Souza Date: Tue, 14 Feb 2023 18:27:24 -0300 Subject: [PATCH 3/5] Organize imports --- package.json | 4 +- rollup.config.js | 38 +++-- src/actions/fetchEvents/fetchEvents.ts | 13 +- src/export/google/generateCalendar.ts | 5 +- src/export/google/handleLogin.ts | 2 +- src/export/google/index.ts | 11 +- src/export/google/saveEvents.ts | 2 +- src/index.ts | 143 ------------------ src/module/actions/fetchEvents/fetchEvents.ts | 72 +++++++++ .../actions/fetchEvents/fetchEvents.types.ts | 10 ++ src/module/actions/index.ts | 3 + .../loadSheetDocument/loadSheetDocument.ts | 21 +++ .../actions/saveToGoogle/saveToGoogle.ts | 25 +++ src/module/index.ts | 4 + .../fetchEvents => }/utils/columnRange.ts | 0 src/utils/defaultFetchOptions.ts | 7 + .../parseEvents/byDefinedColumns/index.ts | 2 +- .../utils/parseEvents/byRead/index.ts | 2 +- .../utils/parseEvents/parseEvents.ts | 0 19 files changed, 186 insertions(+), 178 deletions(-) delete mode 100644 src/index.ts create mode 100644 src/module/actions/fetchEvents/fetchEvents.ts create mode 100644 src/module/actions/fetchEvents/fetchEvents.types.ts create mode 100644 src/module/actions/index.ts create mode 100644 src/module/actions/loadSheetDocument/loadSheetDocument.ts create mode 100644 src/module/actions/saveToGoogle/saveToGoogle.ts create mode 100644 src/module/index.ts rename src/{actions/fetchEvents => }/utils/columnRange.ts (100%) create mode 100644 src/utils/defaultFetchOptions.ts rename src/{actions/fetchEvents => }/utils/parseEvents/byDefinedColumns/index.ts (94%) rename src/{actions/fetchEvents => }/utils/parseEvents/byRead/index.ts (94%) rename src/{actions/fetchEvents => }/utils/parseEvents/parseEvents.ts (100%) diff --git a/package.json b/package.json index 96f1bf5..d7cbe59 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.0.2", "description": "", "bin": { - "gsToCalendar": "./dist/cli.js" + "gsToCalendar": "./dist/cli/cli.js" }, "engines": { "node": ">=16.0.0" @@ -20,7 +20,7 @@ "pretty-quick": "pretty-quick" }, "pkg": { - "scripts": "./dist/**/*.js", + "scripts": "./dist/cli/**/*.js", "targets": [ "node16-linux-x64", "node16-macos-x64", diff --git a/rollup.config.js b/rollup.config.js index c2e87de..3cb4cc7 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -28,13 +28,14 @@ const esbuildBaseConfig = { }; const baseOutput = (config) => ({ - ...config, format: 'commonjs', sourcemap: true, - preserveModules: true + preserveModules: true, + ...config }); export default [ + /// CLI BUILD bundle({ input: './src/cli.ts', plugins: [ @@ -48,18 +49,6 @@ export default [ dir: `./dist/cli` }) }), - bundle({ - input: './src/index.ts', - plugins: [ - peerDepsExternal({ - packageJsonPath: './package.json' - }), - esbuild(esbuildBaseConfig) - ], - output: baseOutput({ - dir: `./dist/module` - }) - }), { input: './src/cli.ts', plugins: [ @@ -71,15 +60,32 @@ export default [ dir: `./dist/cli` }) }, + + /// MODULE BUILD + bundle({ + input: './src/module/index.ts', + plugins: [ + peerDepsExternal({ + packageJsonPath: './package.json' + }), + esbuild({ ...esbuildBaseConfig, define: undefined }) + ], + output: baseOutput({ + dir: `./dist/module`, + preserveModules: false + }) + }), + { - input: './src/index.ts', + input: './src/module/index.ts', plugins: [ dts({ tsconfig: './tsconfig.json' }) ], output: baseOutput({ - dir: `./dist/module` + dir: `./dist/module`, + preserveModules: false }) } ]; diff --git a/src/actions/fetchEvents/fetchEvents.ts b/src/actions/fetchEvents/fetchEvents.ts index bb0f0a5..b2f7606 100644 --- a/src/actions/fetchEvents/fetchEvents.ts +++ b/src/actions/fetchEvents/fetchEvents.ts @@ -1,14 +1,9 @@ import _export from '../../export'; import chalk from 'chalk'; -import { getColumnRange } from './utils/columnRange'; -import { FetchEventsFN, FetchEventsFNOptions } from './fetchEvents.types'; -import parseEvents from './utils/parseEvents/parseEvents'; - -export const defaultFetchOptions: FetchEventsFNOptions = { - dateFormat: `d 'de' MMMM`, - startColumn: 'A', - locale: 'ptBR' -}; +import { getColumnRange } from '../../utils/columnRange'; +import { FetchEventsFN } from './fetchEvents.types'; +import parseEvents from '../../utils/parseEvents/parseEvents'; +import { defaultFetchOptions } from '../../utils/defaultFetchOptions'; export const fetchEvents: FetchEventsFN = async (props) => { const { callback, document, options: fnOptions, sheetId } = props; diff --git a/src/export/google/generateCalendar.ts b/src/export/google/generateCalendar.ts index ee778d3..1cdff2f 100644 --- a/src/export/google/generateCalendar.ts +++ b/src/export/google/generateCalendar.ts @@ -1,7 +1,10 @@ import { calendar } from '@googleapis/calendar'; import { OAuth2Client } from 'googleapis-common'; -export default async (calendarTitle: string, oAuthClient: OAuth2Client) => { +export const generateCalendar = async ( + calendarTitle: string, + oAuthClient: OAuth2Client +) => { const api = calendar({ auth: oAuthClient, version: 'v3' diff --git a/src/export/google/handleLogin.ts b/src/export/google/handleLogin.ts index ea5d98c..7a4e46b 100644 --- a/src/export/google/handleLogin.ts +++ b/src/export/google/handleLogin.ts @@ -2,7 +2,7 @@ import express from 'express'; import open from 'open'; import { getOAuthClient, port, redirect } from './getOAuthClient'; -export default async () => { +export const handleLogin = async () => { let resolve: (value: string | PromiseLike) => void; const app = express(); const p = new Promise((_resolve) => { diff --git a/src/export/google/index.ts b/src/export/google/index.ts index 3961b10..1dc72b5 100644 --- a/src/export/google/index.ts +++ b/src/export/google/index.ts @@ -4,10 +4,10 @@ import cliSpinners from 'cli-spinners'; import Enquirer from 'enquirer'; import ora from 'ora'; import { log } from '../../utils'; -import generateCalendar from './generateCalendar'; +import { generateCalendar } from './generateCalendar'; import { getOAuthClient } from './getOAuthClient'; -import handleLogin from './handleLogin'; -import saveEvents from './saveEvents'; +import { handleLogin } from './handleLogin'; +import { saveEvents } from './saveEvents'; const canUseOauth = () => process.env.CLIENT_ID !== undefined && @@ -95,3 +95,8 @@ export default async ( if (!calendarId) return; await saveEvents(calendarId, events, oAuthClient); }; + +export * from './generateCalendar'; +export * from './getOAuthClient'; +export * from './handleLogin'; +export * from './saveEvents'; diff --git a/src/export/google/saveEvents.ts b/src/export/google/saveEvents.ts index 6e42a88..8ffb863 100644 --- a/src/export/google/saveEvents.ts +++ b/src/export/google/saveEvents.ts @@ -2,7 +2,7 @@ import { calendar } from '@googleapis/calendar'; import { OAuth2Client } from 'googleapis-common'; import { log } from '../../utils'; -export default async ( +export const saveEvents = async ( calendarId: string, events: EventTypes[], oAuthClient: OAuth2Client diff --git a/src/index.ts b/src/index.ts deleted file mode 100644 index 0a9d93f..0000000 --- a/src/index.ts +++ /dev/null @@ -1,143 +0,0 @@ -import { GoogleSpreadsheet } from 'google-spreadsheet'; -import { defaultFetchOptions } from './actions/fetchEvents/fetchEvents'; -import { FetchEventsFNOptions } from './actions/fetchEvents/fetchEvents.types'; -import { getColumnRange } from './actions/fetchEvents/utils/columnRange'; -import parseEvents from './actions/fetchEvents/utils/parseEvents/parseEvents'; -import googleSheet from './actions/googleSheet'; -import generateCalendar from './export/google/generateCalendar'; -import { getOAuthClient } from './export/google/getOAuthClient'; -import saveEvents from './export/google/saveEvents'; -import { log } from './utils'; - -export interface SaveToGoogleProps { - calendarTitle: string; - accessToken: string; - events: EventTypes[]; -} - -type BindedSaveToGoogleFN = ( - props: Omit -) => Promise; - -export const saveToGoogle = async (props: { - calendarTitle: string; - accessToken: string; - events: EventTypes[]; -}) => { - const { accessToken, calendarTitle, events } = props; - const OAuthClient = getOAuthClient(accessToken); - - const calendarId = await generateCalendar(calendarTitle, OAuthClient); - - if (!calendarId) { - log.error(`Unable to create calendar id`); - return; - } - - saveEvents(calendarId, events, OAuthClient); -}; - -interface FetchEventsProps { - document: GoogleSpreadsheet; - sheetId?: string; - options?: FetchEventsFNOptions & { debug?: boolean }; -} - -export type FetchEventsReturnTypes = Promise< - | { - events: EventTypes[]; - calendarTitle: string; - saveToGoogle: BindedSaveToGoogleFN; - } - | undefined ->; - -export const fetchEvents = async (props: { - document: GoogleSpreadsheet; - sheetId?: string; - options?: FetchEventsFNOptions & { debug?: boolean }; -}) => { - const { - document, - options = { ...defaultFetchOptions, debug: false }, - sheetId - } = props; - - const sheet = - sheetId && sheetId !== '' && document.sheetsById[sheetId] - ? document.sheetsById[sheetId] - : document.sheetsByIndex.at(-1); - - if (((sheetId && !document.sheetsById[sheetId]) || sheetId === '') && sheet) { - return undefined; - } - - if (!sheet) { - log.error(`Unable to locate sheet`); - return; - } - - const rowRange = sheet.rowCount || 34; - - await sheet.loadCells(`${options?.startColumn || 'A'}1:${rowRange}`); - - const columnsToFetchData = [ - options?.dateStringColumn, - options?.titleStringColumn - ]; - - const columnRange = getColumnRange([ - options?.startColumn || 'A', - sheet.lastColumnLetter - ]); - - let events: EventTypes[] = []; - - if (!columnsToFetchData[0] && !columnsToFetchData[1]) { - events = await parseEvents.byRead(sheet, rowRange, columnRange, options); - } else { - events = parseEvents.byDefinedColumns(sheet, rowRange, options); - } - - if (events.length <= 0) { - log.error('Unable to find any events in this document.'); - process.exit(1); - } - - if (options.debug) { - log.info((chalk) => `Loaded ${chalk.bold(events.length)} events!`); - } - - return { - events, - saveToGoogle: saveToGoogle.bind({ - calendarTitle: document.title, - events - }) as ( - props: Omit - ) => Promise, - calendarTitle: sheet.title - }; -}; - -export const loadSheetDocument = async (props: { url: string }) => { - const { url } = props; - - const docId = url - .replace('https://docs.google.com/spreadsheets/', '') - .split('/')[1]; - - const document = await googleSheet.loadDocument(docId, (msg) => - log.error(msg) - ); - - return { - document, - sheetList: googleSheet.getSheets(document), - fetchEvents: fetchEvents.bind({ document }) as ( - props: Omit - ) => FetchEventsReturnTypes - }; -}; - -export { googleSheet }; diff --git a/src/module/actions/fetchEvents/fetchEvents.ts b/src/module/actions/fetchEvents/fetchEvents.ts new file mode 100644 index 0000000..d409fa9 --- /dev/null +++ b/src/module/actions/fetchEvents/fetchEvents.ts @@ -0,0 +1,72 @@ +import { GoogleSpreadsheet } from 'google-spreadsheet'; +import { FetchEventsFNOptions } from '../../../actions/fetchEvents/fetchEvents.types'; +import { log } from '../../../utils'; +import { getColumnRange } from '../../../utils/columnRange'; +import { defaultFetchOptions } from '../../../utils/defaultFetchOptions'; +import parseEvents from '../../../utils/parseEvents/parseEvents'; +import { saveToGoogle } from '../saveToGoogle/saveToGoogle'; + +export const fetchEvents = async ( + document?: GoogleSpreadsheet, + props: { + sheetId?: string; + options?: FetchEventsFNOptions & { debug?: boolean }; + } = {} +) => { + if (!document) return; + + const { options = { ...defaultFetchOptions, debug: false }, sheetId } = props; + + const sheet = + sheetId && sheetId !== '' && document.sheetsById[sheetId] + ? document.sheetsById[sheetId] + : document.sheetsByIndex.at(-1); + + if (((sheetId && !document.sheetsById[sheetId]) || sheetId === '') && sheet) { + return undefined; + } + + if (!sheet) { + log.error(`Unable to locate sheet`); + return; + } + + const rowRange = sheet.rowCount || 34; + + await sheet.loadCells(`${options?.startColumn || 'A'}1:${rowRange}`); + + const columnsToFetchData = [ + options?.dateStringColumn, + options?.titleStringColumn + ]; + + const columnRange = getColumnRange([ + options?.startColumn || 'A', + sheet.lastColumnLetter + ]); + + let events: EventTypes[] = []; + + if (!columnsToFetchData[0] && !columnsToFetchData[1]) { + events = await parseEvents.byRead(sheet, rowRange, columnRange, options); + } else { + events = parseEvents.byDefinedColumns(sheet, rowRange, options); + } + + if (events.length <= 0) { + log.error('Unable to find any events in this document.'); + process.exit(1); + } + + if (options.debug) { + log.info((chalk) => `Loaded ${chalk.bold(events.length)} events!`); + } + + return { + events, + saveToGoogle: saveToGoogle.bind(undefined, document.title, events), + calendarTitle: sheet.title + }; +}; + +export * from './fetchEvents.types'; diff --git a/src/module/actions/fetchEvents/fetchEvents.types.ts b/src/module/actions/fetchEvents/fetchEvents.types.ts new file mode 100644 index 0000000..ace9d12 --- /dev/null +++ b/src/module/actions/fetchEvents/fetchEvents.types.ts @@ -0,0 +1,10 @@ +import { GoogleSpreadsheet } from 'google-spreadsheet'; +import { FetchEventsFNOptions } from '../../../actions/fetchEvents/fetchEvents.types'; + +export interface FetchEventsProps { + document: GoogleSpreadsheet; + props: { + sheetId?: string; + options?: FetchEventsFNOptions & { debug?: boolean }; + }; +} diff --git a/src/module/actions/index.ts b/src/module/actions/index.ts new file mode 100644 index 0000000..d7f8506 --- /dev/null +++ b/src/module/actions/index.ts @@ -0,0 +1,3 @@ +export * from './fetchEvents/fetchEvents'; +export * from './loadSheetDocument/loadSheetDocument'; +export * from './saveToGoogle/saveToGoogle'; diff --git a/src/module/actions/loadSheetDocument/loadSheetDocument.ts b/src/module/actions/loadSheetDocument/loadSheetDocument.ts new file mode 100644 index 0000000..6ce5ca9 --- /dev/null +++ b/src/module/actions/loadSheetDocument/loadSheetDocument.ts @@ -0,0 +1,21 @@ +import googleSheet from '../../../actions/googleSheet'; +import { log } from '../../../utils'; +import { fetchEvents } from '../fetchEvents/fetchEvents'; + +export const loadSheetDocument = async (props: { url: string }) => { + const { url } = props; + + const docId = url + .replace('https://docs.google.com/spreadsheets/', '') + .split('/')[1]; + + const document = await googleSheet.loadDocument(docId, (msg) => + log.error(msg) + ); + + return { + document, + sheetList: googleSheet.getSheets(document), + fetchEvents: fetchEvents.bind(undefined, document, undefined) + }; +}; diff --git a/src/module/actions/saveToGoogle/saveToGoogle.ts b/src/module/actions/saveToGoogle/saveToGoogle.ts new file mode 100644 index 0000000..2bc4ec5 --- /dev/null +++ b/src/module/actions/saveToGoogle/saveToGoogle.ts @@ -0,0 +1,25 @@ +import { + getOAuthClient, + generateCalendar, + saveEvents +} from '../../../export/google'; +import { log } from '../../../utils'; + +export const saveToGoogle = async ( + calendarTitle: string, + events: EventTypes[], + accessToken: string +) => { + if (!accessToken) return; + + const OAuthClient = getOAuthClient(accessToken); + + const calendarId = await generateCalendar(calendarTitle, OAuthClient); + + if (!calendarId) { + log.error(`Unable to create calendar id`); + return; + } + + return saveEvents(calendarId, events, OAuthClient); +}; diff --git a/src/module/index.ts b/src/module/index.ts new file mode 100644 index 0000000..70ff77b --- /dev/null +++ b/src/module/index.ts @@ -0,0 +1,4 @@ +import googleSheet from '../actions/googleSheet'; +export * from './actions'; + +export { googleSheet }; diff --git a/src/actions/fetchEvents/utils/columnRange.ts b/src/utils/columnRange.ts similarity index 100% rename from src/actions/fetchEvents/utils/columnRange.ts rename to src/utils/columnRange.ts diff --git a/src/utils/defaultFetchOptions.ts b/src/utils/defaultFetchOptions.ts new file mode 100644 index 0000000..6e58fbc --- /dev/null +++ b/src/utils/defaultFetchOptions.ts @@ -0,0 +1,7 @@ +import { FetchEventsFNOptions } from '../actions/fetchEvents/fetchEvents.types'; + +export const defaultFetchOptions: FetchEventsFNOptions = { + dateFormat: `d 'de' MMMM`, + startColumn: 'A', + locale: 'ptBR' +}; diff --git a/src/actions/fetchEvents/utils/parseEvents/byDefinedColumns/index.ts b/src/utils/parseEvents/byDefinedColumns/index.ts similarity index 94% rename from src/actions/fetchEvents/utils/parseEvents/byDefinedColumns/index.ts rename to src/utils/parseEvents/byDefinedColumns/index.ts index a276a8a..f4c66f4 100644 --- a/src/actions/fetchEvents/utils/parseEvents/byDefinedColumns/index.ts +++ b/src/utils/parseEvents/byDefinedColumns/index.ts @@ -1,7 +1,7 @@ import { format, parse } from 'date-fns'; import * as Locales from 'date-fns/locale'; import { GoogleSpreadsheetWorksheet } from 'google-spreadsheet'; -import { FetchEventsFNOptions } from '../../../fetchEvents.types'; +import { FetchEventsFNOptions } from '../../../actions/fetchEvents/fetchEvents.types'; export default ( sheet: GoogleSpreadsheetWorksheet, diff --git a/src/actions/fetchEvents/utils/parseEvents/byRead/index.ts b/src/utils/parseEvents/byRead/index.ts similarity index 94% rename from src/actions/fetchEvents/utils/parseEvents/byRead/index.ts rename to src/utils/parseEvents/byRead/index.ts index 46df568..7331770 100644 --- a/src/actions/fetchEvents/utils/parseEvents/byRead/index.ts +++ b/src/utils/parseEvents/byRead/index.ts @@ -1,7 +1,7 @@ import { format, parse } from 'date-fns'; import * as Locales from 'date-fns/locale'; import { GoogleSpreadsheetWorksheet } from 'google-spreadsheet'; -import { FetchEventsFNOptions } from '../../../fetchEvents.types'; +import { FetchEventsFNOptions } from '../../../actions/fetchEvents/fetchEvents.types'; export default async ( sheet: GoogleSpreadsheetWorksheet, diff --git a/src/actions/fetchEvents/utils/parseEvents/parseEvents.ts b/src/utils/parseEvents/parseEvents.ts similarity index 100% rename from src/actions/fetchEvents/utils/parseEvents/parseEvents.ts rename to src/utils/parseEvents/parseEvents.ts From ce1ed00759979ba8f004354f92924315b10909cf Mon Sep 17 00:00:00 2001 From: Santiago Souza Date: Tue, 14 Feb 2023 18:37:31 -0300 Subject: [PATCH 4/5] Publish script --- package-lock.json | 228 +++++++++++++++++++++++++++++++++++----------- package.json | 7 +- rollup.config.js | 11 ++- 3 files changed, 188 insertions(+), 58 deletions(-) diff --git a/package-lock.json b/package-lock.json index a1190d4..5f9d87d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "gsheet-to-calendar", - "version": "0.0.1", + "version": "0.0.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "gsheet-to-calendar", - "version": "0.0.1", + "version": "0.0.2", "license": "MIT", "dependencies": { "@googleapis/calendar": "^3.0.0", @@ -25,7 +25,7 @@ "yargs": "^17.6.2" }, "bin": { - "gsToCalendar": "build/src/cli.js" + "gsToCalendar": "dist/cli/cli.js" }, "devDependencies": { "@types/cli-table": "^0.3.1", @@ -47,10 +47,10 @@ "prettier": "2.8.4", "pretty-quick": "^3.1.3", "rollup": "2.78", + "rollup-plugin-copy": "^3.4.0", "rollup-plugin-delete": "^2.0.0", "rollup-plugin-dts": "^5.1.1", "rollup-plugin-esbuild": "^5.0.0", - "rollup-plugin-inject-process-env": "^1.3.1", "rollup-plugin-peer-deps-external": "^2.2.4", "rollup-plugin-url": "^3.0.1", "ts-node": "^10.9.1", @@ -891,6 +891,15 @@ "@types/range-parser": "*" } }, + "node_modules/@types/fs-extra": { + "version": "8.1.2", + "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-8.1.2.tgz", + "integrity": "sha512-SvSrYXfWSc7R4eqnOzbQF4TZmfpNSM9FrSWLU3EUnWBuyZqNBOrv1B1JA3byUDPUl9z4Ab3jeZG2eDdySlgNMg==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/glob": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.2.0.tgz", @@ -1783,6 +1792,12 @@ "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" }, + "node_modules/colorette": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/colorette/-/colorette-1.4.0.tgz", + "integrity": "sha512-Y2oEozpomLn7Q3HFP7dpww7AtMJplbM9lGZP6RDfHqmbeRjiwRg4n6VM6j4KLmRke85uWEI7JqF17f3pqdRA0g==", + "dev": true + }, "node_modules/colors": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/colors/-/colors-1.0.3.tgz", @@ -3974,6 +3989,15 @@ "node": ">=8" } }, + "node_modules/is-plain-object": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/is-plain-object/-/is-plain-object-3.0.1.tgz", + "integrity": "sha512-Xnpx182SBMrr/aBik8y+GuR4U1L9FqMSojwDQwPMmxyC6bvEqly9UBCxhauBF5vNh2gwWJNX6oDV7O+OM4z34g==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/is-regex": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.1.4.tgz", @@ -5421,6 +5445,73 @@ "fsevents": "~2.3.2" } }, + "node_modules/rollup-plugin-copy": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/rollup-plugin-copy/-/rollup-plugin-copy-3.4.0.tgz", + "integrity": "sha512-rGUmYYsYsceRJRqLVlE9FivJMxJ7X6jDlP79fmFkL8sJs7VVMSVyA2yfyL+PGyO/vJs4A87hwhgVfz61njI+uQ==", + "dev": true, + "dependencies": { + "@types/fs-extra": "^8.0.1", + "colorette": "^1.1.0", + "fs-extra": "^8.1.0", + "globby": "10.0.1", + "is-plain-object": "^3.0.0" + }, + "engines": { + "node": ">=8.3" + } + }, + "node_modules/rollup-plugin-copy/node_modules/fs-extra": { + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz", + "integrity": "sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==", + "dev": true, + "dependencies": { + "graceful-fs": "^4.2.0", + "jsonfile": "^4.0.0", + "universalify": "^0.1.0" + }, + "engines": { + "node": ">=6 <7 || >=8" + } + }, + "node_modules/rollup-plugin-copy/node_modules/globby": { + "version": "10.0.1", + "resolved": "https://registry.npmjs.org/globby/-/globby-10.0.1.tgz", + "integrity": "sha512-sSs4inE1FB2YQiymcmTv6NWENryABjUNPeWhOvmn4SjtKybglsyPZxFB3U1/+L1bYi0rNZDqCLlHyLYDl1Pq5A==", + "dev": true, + "dependencies": { + "@types/glob": "^7.1.1", + "array-union": "^2.1.0", + "dir-glob": "^3.0.1", + "fast-glob": "^3.0.3", + "glob": "^7.1.3", + "ignore": "^5.1.1", + "merge2": "^1.2.3", + "slash": "^3.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/rollup-plugin-copy/node_modules/jsonfile": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", + "integrity": "sha512-m6F1R3z8jjlf2imQHS2Qez5sjKWQzbuuhuJ/FKYFRZvPE3PuHcSMVZzfsLhGVOkfd20obL5SWEBew5ShlquNxg==", + "dev": true, + "optionalDependencies": { + "graceful-fs": "^4.1.6" + } + }, + "node_modules/rollup-plugin-copy/node_modules/universalify": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz", + "integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==", + "dev": true, + "engines": { + "node": ">= 4.0.0" + } + }, "node_modules/rollup-plugin-delete": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/rollup-plugin-delete/-/rollup-plugin-delete-2.0.0.tgz", @@ -5476,24 +5567,6 @@ "rollup": "^1.20.0 || ^2.0.0 || ^3.0.0" } }, - "node_modules/rollup-plugin-inject-process-env": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/rollup-plugin-inject-process-env/-/rollup-plugin-inject-process-env-1.3.1.tgz", - "integrity": "sha512-kKDoL30IZr0wxbNVJjq+OS92RJSKRbKV6B5eNW4q3mZTFqoWDh6lHy+mPDYuuGuERFNKXkG+AKxvYqC9+DRpKQ==", - "dev": true, - "dependencies": { - "magic-string": "^0.25.7" - } - }, - "node_modules/rollup-plugin-inject-process-env/node_modules/magic-string": { - "version": "0.25.9", - "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.9.tgz", - "integrity": "sha512-RmF0AsMzgt25qzqqLc1+MbHmhdx0ojF2Fvs4XnOqz2ZOBXzzkEwc/dJQZCYHAn7v1jbVOjAZfK8msRn4BxO4VQ==", - "dev": true, - "dependencies": { - "sourcemap-codec": "^1.4.8" - } - }, "node_modules/rollup-plugin-peer-deps-external": { "version": "2.2.4", "resolved": "https://registry.npmjs.org/rollup-plugin-peer-deps-external/-/rollup-plugin-peer-deps-external-2.2.4.tgz", @@ -5787,13 +5860,6 @@ "source-map": "^0.6.0" } }, - "node_modules/sourcemap-codec": { - "version": "1.4.8", - "resolved": "https://registry.npmjs.org/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz", - "integrity": "sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==", - "deprecated": "Please use @jridgewell/sourcemap-codec instead", - "dev": true - }, "node_modules/statuses": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz", @@ -7007,6 +7073,15 @@ "@types/range-parser": "*" } }, + "@types/fs-extra": { + "version": "8.1.2", + "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-8.1.2.tgz", + "integrity": "sha512-SvSrYXfWSc7R4eqnOzbQF4TZmfpNSM9FrSWLU3EUnWBuyZqNBOrv1B1JA3byUDPUl9z4Ab3jeZG2eDdySlgNMg==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/glob": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.2.0.tgz", @@ -7654,6 +7729,12 @@ "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" }, + "colorette": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/colorette/-/colorette-1.4.0.tgz", + "integrity": "sha512-Y2oEozpomLn7Q3HFP7dpww7AtMJplbM9lGZP6RDfHqmbeRjiwRg4n6VM6j4KLmRke85uWEI7JqF17f3pqdRA0g==", + "dev": true + }, "colors": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/colors/-/colors-1.0.3.tgz", @@ -9274,6 +9355,12 @@ "integrity": "sha512-Fd4gABb+ycGAmKou8eMftCupSir5lRxqf4aD/vd0cD2qc4HL07OjCeuHMr8Ro4CoMaeCKDB0/ECBOVWjTwUvPQ==", "dev": true }, + "is-plain-object": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/is-plain-object/-/is-plain-object-3.0.1.tgz", + "integrity": "sha512-Xnpx182SBMrr/aBik8y+GuR4U1L9FqMSojwDQwPMmxyC6bvEqly9UBCxhauBF5vNh2gwWJNX6oDV7O+OM4z34g==", + "dev": true + }, "is-regex": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.1.4.tgz", @@ -10305,6 +10392,63 @@ "fsevents": "~2.3.2" } }, + "rollup-plugin-copy": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/rollup-plugin-copy/-/rollup-plugin-copy-3.4.0.tgz", + "integrity": "sha512-rGUmYYsYsceRJRqLVlE9FivJMxJ7X6jDlP79fmFkL8sJs7VVMSVyA2yfyL+PGyO/vJs4A87hwhgVfz61njI+uQ==", + "dev": true, + "requires": { + "@types/fs-extra": "^8.0.1", + "colorette": "^1.1.0", + "fs-extra": "^8.1.0", + "globby": "10.0.1", + "is-plain-object": "^3.0.0" + }, + "dependencies": { + "fs-extra": { + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz", + "integrity": "sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==", + "dev": true, + "requires": { + "graceful-fs": "^4.2.0", + "jsonfile": "^4.0.0", + "universalify": "^0.1.0" + } + }, + "globby": { + "version": "10.0.1", + "resolved": "https://registry.npmjs.org/globby/-/globby-10.0.1.tgz", + "integrity": "sha512-sSs4inE1FB2YQiymcmTv6NWENryABjUNPeWhOvmn4SjtKybglsyPZxFB3U1/+L1bYi0rNZDqCLlHyLYDl1Pq5A==", + "dev": true, + "requires": { + "@types/glob": "^7.1.1", + "array-union": "^2.1.0", + "dir-glob": "^3.0.1", + "fast-glob": "^3.0.3", + "glob": "^7.1.3", + "ignore": "^5.1.1", + "merge2": "^1.2.3", + "slash": "^3.0.0" + } + }, + "jsonfile": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", + "integrity": "sha512-m6F1R3z8jjlf2imQHS2Qez5sjKWQzbuuhuJ/FKYFRZvPE3PuHcSMVZzfsLhGVOkfd20obL5SWEBew5ShlquNxg==", + "dev": true, + "requires": { + "graceful-fs": "^4.1.6" + } + }, + "universalify": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz", + "integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==", + "dev": true + } + } + }, "rollup-plugin-delete": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/rollup-plugin-delete/-/rollup-plugin-delete-2.0.0.tgz", @@ -10337,26 +10481,6 @@ "jsonc-parser": "^3.2.0" } }, - "rollup-plugin-inject-process-env": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/rollup-plugin-inject-process-env/-/rollup-plugin-inject-process-env-1.3.1.tgz", - "integrity": "sha512-kKDoL30IZr0wxbNVJjq+OS92RJSKRbKV6B5eNW4q3mZTFqoWDh6lHy+mPDYuuGuERFNKXkG+AKxvYqC9+DRpKQ==", - "dev": true, - "requires": { - "magic-string": "^0.25.7" - }, - "dependencies": { - "magic-string": { - "version": "0.25.9", - "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.9.tgz", - "integrity": "sha512-RmF0AsMzgt25qzqqLc1+MbHmhdx0ojF2Fvs4XnOqz2ZOBXzzkEwc/dJQZCYHAn7v1jbVOjAZfK8msRn4BxO4VQ==", - "dev": true, - "requires": { - "sourcemap-codec": "^1.4.8" - } - } - } - }, "rollup-plugin-peer-deps-external": { "version": "2.2.4", "resolved": "https://registry.npmjs.org/rollup-plugin-peer-deps-external/-/rollup-plugin-peer-deps-external-2.2.4.tgz", @@ -10571,12 +10695,6 @@ "source-map": "^0.6.0" } }, - "sourcemap-codec": { - "version": "1.4.8", - "resolved": "https://registry.npmjs.org/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz", - "integrity": "sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==", - "dev": true - }, "statuses": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz", diff --git a/package.json b/package.json index d7cbe59..e54c18b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,8 @@ { "name": "gsheet-to-calendar", - "version": "0.0.2", + "version": "0.0.3", "description": "", + "main": "index.js", "bin": { "gsToCalendar": "./dist/cli/cli.js" }, @@ -13,7 +14,8 @@ "start:help": "tsx ./src/cli.ts --help", "prod": "npm run build && npm run package", "dev": "tsx watch ./src/cli.ts", - "build": "rollup -c rollup.config.js && cp ./package.json ./dist/module", + "build": "rollup -c rollup.config.js", + "publish": "cd ./dist/module && npm publish", "package": "pkg . --output ./bin/gsToCalendar", "bundle": "cd bin && mv gsToCalendar-macos gsToCalendar && tar -czf gsToCalendar.tar.gz gsToCalendar", "lint": "eslint ./src/**/*.{js, ts, tsx} --no-error-on-unmatched-pattern", @@ -50,6 +52,7 @@ "prettier": "2.8.4", "pretty-quick": "^3.1.3", "rollup": "2.78", + "rollup-plugin-copy": "^3.4.0", "rollup-plugin-delete": "^2.0.0", "rollup-plugin-dts": "^5.1.1", "rollup-plugin-esbuild": "^5.0.0", diff --git a/rollup.config.js b/rollup.config.js index 3cb4cc7..e93515b 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -2,6 +2,7 @@ import dts from 'rollup-plugin-dts'; import esbuild from 'rollup-plugin-esbuild'; import peerDepsExternal from 'rollup-plugin-peer-deps-external'; import del from 'rollup-plugin-delete'; +import copy from 'rollup-plugin-copy'; import path from 'path'; import * as dotenv from 'dotenv'; @@ -68,7 +69,15 @@ export default [ peerDepsExternal({ packageJsonPath: './package.json' }), - esbuild({ ...esbuildBaseConfig, define: undefined }) + esbuild({ ...esbuildBaseConfig, define: undefined }), + copy({ + targets: [ + { + src: ['./package.json', './LICENSE.md', './README.md'], + dest: './dist/module' + } + ] + }) ], output: baseOutput({ dir: `./dist/module`, From b4616bc49f08cd1e2a8967c0896880cbc83e55c1 Mon Sep 17 00:00:00 2001 From: Santiago Souza Date: Tue, 14 Feb 2023 19:18:44 -0300 Subject: [PATCH 5/5] Update readme --- README.md | 129 +++++++++++++++++- .../loadSheetDocument/loadSheetDocument.ts | 2 +- 2 files changed, 129 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 76b83a4..48115f8 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,12 @@ CLI api that uses google spreadsheet api to export calendar data. + + + + + +

@@ -48,4 +54,125 @@ $ gsToCalendar ## Usage as a package -(Coming soon...) +Install the package using your favorite package manager. + +```shell +$ npm i gsheet-to-calendar +``` + +Add the following enviroment variable: + +```ts +// .env +// https://developers.google.com/sheets + +DOC_API_KEY= Your google spreadsheet api key +``` + +### Example snippet + +Here's a basic usage of the package. + +```ts +// yourApiRoute.ts + +import { loadSheetDocument } from 'gsheet-to-calendar'; + +export const handle = async (documentUrl: string) => { + const data = await loadSheetDocument({ + url: documentUrl + }); + + if (!document) return; + + const events = await data.fetchEvents(); + + if (!events) return; + + return events.saveToGoogle(body.token as string); +}; +``` + +### loadSheetDocument() + +| Props | Description | Required | +| ----- | ------------------------------- | -------- | +| url | Google spreadsheet document url | true | + +```ts +// Return of loadSheetDocument() + +document: GoogleSpreadsheet; // Read more at https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets#spreadsheetproperties +sheetList: [{ + value: string; + name: string; +}, ...] +fetchEvents: () => Promise<{...}>; +``` + +### fetchEvents() + +Fetch events from the spreadsheet and return. + +| Props | Description | Required | Default | +| ------------------------- | ------------------------------------------------------------------------------------------------------------- | -------- | ------------- | +| sheetId | Google sheet id from current document | false | null | +| options | Options object | false | null | +| options.debug | Enable error logging | false | false | +| options.docId | Defines the google document id | false | null | +| options.sheetId | Defines the google sheet id | false | null | +| options.dateFormat | Defines the date string format.Follow [date-fns parse documentation](https://date-fns.org/v2.29.3/docs/parse) | false | "d 'de' MMMM" | +| options.dateStringColumn | Defines wich column contains the date string | false | null | +| options.titleStringColumn | Defines wich column contains the event title string | false | null | +| options.locale | Defines the locale for the date-fns. (ISO 639-1) | false | ptBR | +| options.startColumn | Define the initial column | false | A | + +```ts +// Return of fetch events() + +events: EventTypes[]; +saveToGoogle: (accessToken: string) => Promise; +calendarTitle: string; +``` + +### EventTypes + +```ts +interface EventTypes { + date: Date; + title: string; +} +``` + +### saveToGoogle() + +Save events to google calendar. + +| Props | Description | Required | +| ----------- | ------------------------- | -------- | +| accessToken | Google OAuth access_token | true | + +## Standalone functions + +You can also import each function and execute it directly anytime. + +```ts +import { fetchEvents, googleSheet, saveToGoogle } from 'gsheet-to-calendar'; + +fetchEvents(document, { + sheetId, + options: { + dateFormat, + locale, + startColumn, + dateStringColumn, + debug, + titleStringColumn + } +}); + +googleSheet.loadDocument(url, (error) => console.log(error)); +googleSheet.getSheets(document); + +saveToGoogle(calendarTitle, events, accessToken); +``` diff --git a/src/module/actions/loadSheetDocument/loadSheetDocument.ts b/src/module/actions/loadSheetDocument/loadSheetDocument.ts index 6ce5ca9..a1571a9 100644 --- a/src/module/actions/loadSheetDocument/loadSheetDocument.ts +++ b/src/module/actions/loadSheetDocument/loadSheetDocument.ts @@ -16,6 +16,6 @@ export const loadSheetDocument = async (props: { url: string }) => { return { document, sheetList: googleSheet.getSheets(document), - fetchEvents: fetchEvents.bind(undefined, document, undefined) + fetchEvents: fetchEvents.bind(undefined, document) }; };