Skip to content

Commit

Permalink
reformat all with prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
baruchiro committed Sep 17, 2024
1 parent b73f982 commit 225cd3f
Show file tree
Hide file tree
Showing 65 changed files with 1,013 additions and 437 deletions.
9 changes: 2 additions & 7 deletions .github/renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@
{
"groupName": "Vite packages",
"matchUpdateTypes": "major",
"matchSourceUrlPrefixes": [
"https://github.com/vitejs/"
]
"matchSourceUrlPrefixes": ["https://github.com/vitejs/"]
}
],
"gitNoVerify": [
"commit",
"push"
]
"gitNoVerify": ["commit", "push"]
}
16 changes: 11 additions & 5 deletions .nano-staged.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@ export default {
* @param {string[]} filenames
* @return {string[]}
*/
'{package-lock.json,packages/**/{*.ts,*.tsx,tsconfig.json}}': ({ filenames }) => {
'{package-lock.json,packages/**/{*.ts,*.tsx,tsconfig.json}}': ({
filenames,
}) => {
// if dependencies was changed run type checking for all packages
if (filenames.some(f => f.endsWith('package-lock.json'))) {
if (filenames.some((f) => f.endsWith('package-lock.json'))) {
return ['yarn typecheck'];
}

// else run type checking for staged packages
const fileNameToPackageName = filename =>
filename.replace(resolve(process.cwd(), 'packages') + sep, '').split(sep)[0];
return [...new Set(filenames.map(fileNameToPackageName))].map(p => `yarn typecheck:${p}`);
const fileNameToPackageName = (filename) =>
filename
.replace(resolve(process.cwd(), 'packages') + sep, '')
.split(sep)[0];
return [...new Set(filenames.map(fileNameToPackageName))].map(
(p) => `yarn typecheck:${p}`,
);
},
};
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"singleQuote": true
}
}
10 changes: 8 additions & 2 deletions packages/main/src/backend/commonTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { type CompanyTypes, type ScraperCredentials } from 'israeli-bank-scrapers-core';
import {
type CompanyTypes,
type ScraperCredentials,
} from 'israeli-bank-scrapers-core';
import { type Transaction } from 'israeli-bank-scrapers-core/lib/transactions';
import { type Account, type BudgetSummary } from 'ynab';
import { type EventPublisher } from './eventEmitters/EventEmitter';
Expand Down Expand Up @@ -35,7 +38,10 @@ export enum OutputVendorName {
CSV = 'csv',
}

export type OutputVendorConfigs = Exclude<Config['outputVendors'][OutputVendorName], undefined>;
export type OutputVendorConfigs = Exclude<
Config['outputVendors'][OutputVendorName],
undefined
>;
export type OutputVendorConfig<T extends OutputVendorName> = Exclude<
Config['outputVendors'][T],
undefined
Expand Down
9 changes: 7 additions & 2 deletions packages/main/src/backend/configManager/configManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { decrypt, encrypt } from '@/backend/configManager/encryption/crypto';
import { existsSync, promises as fs } from 'fs';
import configExample from './defaultConfig';

export async function getConfig(configPath: string = configFilePath): Promise<Config> {
export async function getConfig(
configPath: string = configFilePath,
): Promise<Config> {
const configFromFile = await getConfigFromFile(configPath);
if (configFromFile) {
const decrypted = (await decrypt(configFromFile)) as string;
Expand All @@ -23,7 +25,10 @@ export async function getConfig(configPath: string = configFilePath): Promise<Co
return configExample;
}

export async function updateConfig(configPath: string, configToUpdate: Config): Promise<void> {
export async function updateConfig(
configPath: string,
configToUpdate: Config,
): Promise<void> {
const stringifiedConfig = JSON.stringify(configToUpdate, null, 2);
const encryptedConfigStr = await encrypt(stringifiedConfig);
await fs.writeFile(configPath, encryptedConfigStr);
Expand Down
3 changes: 2 additions & 1 deletion packages/main/src/backend/configManager/encryption/salt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export default async function SALT(defaultValue?: string): Promise<string> {
const existedSALT = await loadSALT();
if (existedSALT) return existedSALT;

if (!defaultValue) throw Error('SALT not existed and no default value provided');
if (!defaultValue)
throw Error('SALT not existed and no default value provided');

await saveSALT(defaultValue);
return defaultValue;
Expand Down
19 changes: 16 additions & 3 deletions packages/main/src/backend/eventEmitters/EventEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// eslint-disable-next-line max-classes-per-file
import { type EnrichedTransaction, type OutputVendorName } from '@/backend/commonTypes';
import {
type EnrichedTransaction,
type OutputVendorName,
} from '@/backend/commonTypes';
import Emittery from 'emittery';
import { type CompanyTypes } from 'israeli-bank-scrapers-core';

Expand Down Expand Up @@ -90,7 +93,13 @@ export interface ExporterEventParams {
export class ExporterEvent extends BudgetTrackingEvent {
allTransactions: EnrichedTransaction[];

constructor({ message, allTransactions, status, error, exporterName }: ExporterEventParams) {
constructor({
message,
allTransactions,
status,
error,
exporterName,
}: ExporterEventParams) {
super({
message,
accountType: AccountType.EXPORTER,
Expand All @@ -105,7 +114,11 @@ export class ExporterEvent extends BudgetTrackingEvent {
export class ExporterEndEvent extends ExporterEvent {
exportedTransactionsNum: number;

constructor(exporterEventParams: ExporterEventParams & { exportedTransactionsNum: number }) {
constructor(
exporterEventParams: ExporterEventParams & {
exportedTransactionsNum: number;
},
) {
super(exporterEventParams);
this.exportedTransactionsNum = exporterEventParams.exportedTransactionsNum;
}
Expand Down
15 changes: 10 additions & 5 deletions packages/main/src/backend/export/exportTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import {
import outputVendors from '@/backend/export/outputVendors';
import _ from 'lodash';

type ExecutionResult = Partial<Record<OutputVendorName, ExportTransactionsResult>>;
type ExecutionResult = Partial<
Record<OutputVendorName, ExportTransactionsResult>
>;

export async function createTransactionsInExternalVendors(
outputVendorsConfig: Config['outputVendors'],
Expand All @@ -27,8 +29,8 @@ export async function createTransactionsInExternalVendors(
const allTransactions = _.flatten(Object.values(companyIdToTransactions));

const exportPromises = outputVendors
.filter(outputVendor => outputVendorsConfig[outputVendor.name]?.active)
.map(async outputVendor => {
.filter((outputVendor) => outputVendorsConfig[outputVendor.name]?.active)
.map(async (outputVendor) => {
const baseEvent = {
exporterName: outputVendor.name,
allTransactions,
Expand All @@ -54,7 +56,8 @@ export async function createTransactionsInExternalVendors(
message: 'Finished',
...baseEvent,
status: AccountStatus.DONE,
exportedTransactionsNum: exportTransactionsResult.exportedTransactionsNum,
exportedTransactionsNum:
exportTransactionsResult.exportedTransactionsNum,
}),
);
executionResult[outputVendor.name] = exportTransactionsResult;
Expand All @@ -73,7 +76,9 @@ export async function createTransactionsInExternalVendors(

await Promise.all(exportPromises);
if (!Object.keys(executionResult).length) {
const error = new Error('You need to set at least one output vendor to be active');
const error = new Error(
'You need to set at least one output vendor to be active',
);
throw error;
}

Expand Down
13 changes: 10 additions & 3 deletions packages/main/src/backend/export/outputVendors/csv/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {
type ExportTransactionsFunction,
type OutputVendor,
} from '@/backend/commonTypes';
import { mergeTransactions, sortByDate } from '@/backend/transactions/transactions';
import {
mergeTransactions,
sortByDate,
} from '@/backend/transactions/transactions';
import { parse } from 'csv-parse/sync';
import { stringify } from 'csv-stringify/sync';
import { promises as fs } from 'fs';
Expand Down Expand Up @@ -88,11 +91,15 @@ const exportTransactions: ExportTransactionsFunction = async ({
}) => {
const { filePath } = outputVendorsConfig.csv!.options;
const savedTransactions = await parseTransactionsFile(filePath);
const mergedTransactions = mergeTransactions(savedTransactions, transactionsToCreate);
const mergedTransactions = mergeTransactions(
savedTransactions,
transactionsToCreate,
);
const sorted = sortByDate(mergedTransactions);
await writeCsvFile(filePath, serializeTransactions(sorted));
return {
exportedTransactionsNum: mergedTransactions.length - savedTransactions.length,
exportedTransactionsNum:
mergedTransactions.length - savedTransactions.length,
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import ElectronGoogleOAuth2 from 'electron-google-oauth2';
import { clientId, clientSecret, redirectUri, scopes } from './googleAuth';

export default () => {
if (!clientId || !clientSecret) throw Error("No 'clientId' or 'clientSecret' for google login");
if (!clientId || !clientSecret)
throw Error("No 'clientId' or 'clientSecret' for google login");

// @ts-expect-error - The package 'electron-google-oauth2' is my own package, I don't know why it's not recognized
const electronGoogleOAuth2 = new ElectronGoogleOAuth2.default(clientId, clientSecret, scopes, {
successRedirectURL: redirectUri,
});
const electronGoogleOAuth2 = new ElectronGoogleOAuth2.default(
clientId,
clientSecret,
scopes,
{
successRedirectURL: redirectUri,
},
);

return electronGoogleOAuth2.openAuthWindowAndGetTokens();
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ export const redirectUri = homepage;
export const scopes = ['https://www.googleapis.com/auth/drive.file'];

export const createClient = (credentials: Auth.Credentials) => {
const authClient = new Auth.OAuth2Client({ clientId, clientSecret, redirectUri });
const authClient = new Auth.OAuth2Client({
clientId,
clientSecret,
redirectUri,
});
authClient.setCredentials(credentials);
return authClient;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,28 @@ const createTransactionsInGoogleSheets: ExportTransactionsFunction = async (
{ transactionsToCreate: transactions, outputVendorsConfig },
eventPublisher,
) => {
const { spreadsheetId, credentials } = outputVendorsConfig.googleSheets!.options;
const { spreadsheetId, credentials } =
outputVendorsConfig.googleSheets!.options;
if (!credentials) throw new Error("You must set the 'credentials'");
const oAuthClient = createClient(credentials);

const sheet = await googleSheets.getSheet(spreadsheetId, DEFAULT_SHEET_NAME, oAuthClient);
if (!sheet) {
throw new Error(`There is no sheet called ${DEFAULT_SHEET_NAME} in the spreadsheet`);
}

const hashesAlreadyExistingInGoogleSheets = await googleSheets.getExistingHashes(
const sheet = await googleSheets.getSheet(
spreadsheetId,
DEFAULT_SHEET_NAME,
oAuthClient,
);
if (!sheet) {
throw new Error(
`There is no sheet called ${DEFAULT_SHEET_NAME} in the spreadsheet`,
);
}

const hashesAlreadyExistingInGoogleSheets =
await googleSheets.getExistingHashes(
spreadsheetId,
DEFAULT_SHEET_NAME,
oAuthClient,
);
const transactionsToCreate = filterExistedHashes(
transactions,
hashesAlreadyExistingInGoogleSheets,
Expand All @@ -73,7 +81,7 @@ const createTransactionsInGoogleSheets: ExportTransactionsFunction = async (
`Creating ${transactionsToCreate.length} transactions in google sheets`,
);

const transactionsInSheetsFormat = transactionsToCreate.map(transaction => [
const transactionsInSheetsFormat = transactionsToCreate.map((transaction) => [
moment(transaction.date).format(GOOGLE_SHEETS_DATE_FORMAT),
transaction.chargedAmount,
transaction.description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ export const getAllSpreadsheets = async (auth: OAuth2Client) => {
return response.data.files as Spreadsheet[];
};

export async function getSheet(spreadsheetId: string, sheetName: string, auth: OAuth2Client) {
export async function getSheet(
spreadsheetId: string,
sheetName: string,
auth: OAuth2Client,
) {
const spreadsheetResponse = await sheets.spreadsheets.get({
auth,
spreadsheetId,
Expand Down
13 changes: 10 additions & 3 deletions packages/main/src/backend/export/outputVendors/json/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {
type ExportTransactionsFunction,
type OutputVendor,
} from '@/backend/commonTypes';
import { mergeTransactions, sortByDate } from '@/backend/transactions/transactions';
import {
mergeTransactions,
sortByDate,
} from '@/backend/transactions/transactions';
import { promises as fs } from 'fs';

const parseTransactionsFile = async (filename: string) => {
Expand All @@ -25,11 +28,15 @@ const exportTransactions: ExportTransactionsFunction = async ({
}) => {
const { filePath } = outputVendorsConfig.json!.options;
const savedTransactions = await parseTransactionsFile(filePath);
const mergedTransactions = mergeTransactions(savedTransactions, transactionsToCreate);
const mergedTransactions = mergeTransactions(
savedTransactions,
transactionsToCreate,
);
const sorted = sortByDate(mergedTransactions);
await fs.writeFile(filePath, JSON.stringify(sorted, null, 4));
return {
exportedTransactionsNum: mergedTransactions.length - savedTransactions.length,
exportedTransactionsNum:
mergedTransactions.length - savedTransactions.length,
};
};

Expand Down
Loading

0 comments on commit 225cd3f

Please sign in to comment.