Skip to content

Commit

Permalink
Merge pull request #6 from jdeniau/lint-and-tests
Browse files Browse the repository at this point in the history
Lint and tests
  • Loading branch information
jdeniau authored Feb 20, 2024
2 parents e8dead4 + e43c894 commit 2403416
Show file tree
Hide file tree
Showing 21 changed files with 100 additions and 28 deletions.
15 changes: 14 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"es6": true,
"node": true
},
"ignorePatterns": [
"forge.config.ts",
"vite.*.config.*",
"stories/" // exclude stories from linting for now
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
Expand All @@ -12,5 +17,13 @@
"plugin:import/electron",
"plugin:import/typescript"
],
"parser": "@typescript-eslint/parser"
"parser": "@typescript-eslint/parser",
"overrides": [
{
"files": ["*.js", "*.jsx", "*.ts", "*.tsx"],
"parserOptions": {
"project": "./tsconfig.json"
}
}
]
}
30 changes: 30 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Lint & Tests
on:
push:
branches: [ "main" ]
pull_request: ~

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'

- run: yarn install --frozen-lockfile
- run: yarn lint
- run: yarn test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,5 @@ typings/

# Electron-Forge
out/

.vscode/
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"package": "electron-forge package",
"make": "electron-forge make",
"publish": "electron-forge publish",
"lint:eslint": "eslint --ext .ts,.tsx .",
"lint": "yarn lint:eslint && yarn lint:types",
"lint:eslint": "eslint --quiet .",
"lint:types": "tsc --noEmit",
"test": "vitest"
},
Expand Down
5 changes: 4 additions & 1 deletion src/Contexts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ interface ConnexionContextProps {
export const ConnectionContext = createContext<ConnexionContextProps>({
currentConnectionName: null,
connectionNameList: [],
// eslint-disable-next-line @typescript-eslint/no-empty-function
connectTo: () => {},
// eslint-disable-next-line @typescript-eslint/no-empty-function
setCurrentConnectionName: () => {},
});

Expand All @@ -28,6 +30,7 @@ interface DatabaseContextProps {

export const DatabaseContext = createContext<DatabaseContextProps>({
database: null,
// eslint-disable-next-line @typescript-eslint/no-empty-function
setDatabase: () => {},
});

Expand All @@ -40,6 +43,7 @@ interface ThemeContextProps {
}
const ThemeContext = createContext<ThemeContextProps>({
themeName: DEFAULT_THEME.name,
// eslint-disable-next-line @typescript-eslint/no-empty-function
changeTheme: () => {},
});

Expand All @@ -55,7 +59,6 @@ export function ThemeContextProvider({
const [themeName, setThemeName] = useState(DEFAULT_THEME.name);

const changeTheme = (newTheme: string) => {
window.config.changeTheme(newTheme);
setThemeName(newTheme);
};

Expand Down
5 changes: 2 additions & 3 deletions src/component/Connection/ConnectionForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ConnectionContext, ConnectToFunc } from '../../Contexts';
import connections from './SavedConnections';
import { ConnectionObject } from '.';
import { ConnectionObject } from './types';
import { ChangeEvent, FormEvent, PureComponent, useContext } from 'react';

interface ConnectionFormProps {
Expand Down Expand Up @@ -42,8 +42,7 @@ class ConnectionForm extends PureComponent<
);
}

// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore: name is in state due to previous line
// @ts-expect-error: name is in state due to previous line
this.setState({
[name]: value,
});
Expand Down
2 changes: 1 addition & 1 deletion src/component/Connection/ConnectionPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Link, Navigate } from 'react-router-dom';
import connections from './SavedConnections';
import { ConnectionObject } from '.';
import { ConnectionObject } from './types';
import { ConnectionContext } from '../../Contexts';
import { useContext, useEffect, useState } from 'react';

Expand Down
2 changes: 1 addition & 1 deletion src/component/Connection/ConnectionStack.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NavigateFunction, useNavigate } from 'react-router';
import { ConnectionContext, DatabaseContext } from '../../Contexts';
import { PureComponent, ReactNode } from 'react';
import { ConnectionObject } from '.';
import { ConnectionObject } from './types';

interface PropsWithoutHistory {
children: ReactNode;
Expand Down
2 changes: 1 addition & 1 deletion src/component/Connection/SavedConnections.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// import Store from 'electron-store';
import { ConnectionObject } from '.';
import { ConnectionObject } from './types';
// import fs from 'node:fs';
// import path from 'node:path';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type ConnectionObject = {
name: string;
host: string;
port: number;
user: string;
password: string;
};
export type ConnectionObject = {
name: string;
host: string;
port: number;
user: string;
password: string;
};
4 changes: 1 addition & 3 deletions src/component/DatabaseSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ interface DatabaseRow {
export default function DatabaseSelector() {
const { currentConnectionName } = useContext(ConnectionContext);

const [databaseList, setDatabaseList]: [DatabaseRow[], Function] = useState(
[]
);
const [databaseList, setDatabaseList] = useState<DatabaseRow[]>([]);

const { database, setDatabase } = useContext(DatabaseContext);

Expand Down
1 change: 1 addition & 0 deletions src/component/Query/WhereFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function WhereFilter({ defaultValue, onSubmit }: Props): ReactElement {
onSubmit(where);
}}
>
{/* @ts-expect-error not present infuture TS verwsion */}
<WhereArea value={where} onChange={(e) => setWhere(e.target.value)} />

<input type="submit" />
Expand Down
3 changes: 1 addition & 2 deletions src/component/TableGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,17 @@ function TableGrid({ fields, result }: TableGridProps): ReactElement {
)}
{result && fields && (
<tbody>
{/* eslint-disable react/jsx-key */}
{/* TODO : use the table primary key to make a real key */}
{result.map((row: object) => (
<tr>
{fields.map((field) => (
<Td key={field.name}>
{/* @ts-expect-error field name is in object. Need a better type ? */}
<Cell type={field.type} value={row[field.name]} />
</Td>
))}
</tr>
))}
{/* eslint-enable react/jsx-key */}
</tbody>
)}
</Table>
Expand Down
2 changes: 1 addition & 1 deletion src/component/TableList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function ConnectedTableList({ database }: TableListProps): ReactElement | null {
<div key={rowDataPacket.Name}>
<StyledNavLink
to={`/tables/${rowDataPacket.Name}`}
style={({ isActive }) => ({
style={({ isActive }: { isActive: boolean }) => ({
fontWeight: isActive ? 'bold' : undefined,
})}
>
Expand Down
2 changes: 1 addition & 1 deletion src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { dialog, safeStorage } from 'electron';
import { resolve } from 'node:path';
import { existsSync, mkdirSync, readFileSync, writeFile } from 'node:fs';
import envPaths from 'env-paths';
import { ConnectionObject } from './component/Connection';
import { ConnectionObject } from './component/Connection/types';

export type Configuration = {
version: 1;
Expand Down
1 change: 1 addition & 0 deletions src/error-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default function ErrorPage() {
<h1>Oops!</h1>
<p>Sorry, an unexpected error has occurred.</p>
<p>
{/* @ts-expect-error error might have one of tjose */}
<i>{error.statusText || error.message}</i>
</p>
</div>
Expand Down
4 changes: 3 additions & 1 deletion src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts

import { Connection } from 'mysql2/promise';
import { ConnectionObject } from './component/Connection';
import { ConnectionObject } from './component/Connection/types';
import { Configuration } from './configuration';
import { contextBridge, ipcRenderer } from 'electron';

Expand All @@ -28,12 +28,14 @@ contextBridge.exposeInMainWorld('config', config);
interface Sql {
openConnection(params: ConnectionObject): Promise<Connection>;
query(query: string): Promise<unknown>;
closeAllConnections(): Promise<void>;
}

// TODO : clone the binder object in sql/index.ts ?
const sql: Sql = {
openConnection: (params) => ipcRenderer.invoke('sql:connect', params),
query: (query) => ipcRenderer.invoke('sql:query', query),
closeAllConnections: () => ipcRenderer.invoke('sql:closeAll'),
};

contextBridge.exposeInMainWorld('sql', sql);
Expand Down
13 changes: 12 additions & 1 deletion src/sql/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Connection, createConnection } from 'mysql2/promise';
import { ConnectionObject } from 'src/component/Connection';
import { ConnectionObject } from '../component/Connection/types';

class ConnectionStack {
private connections: Map<number, Connection> = new Map();
Expand All @@ -8,6 +8,7 @@ class ConnectionStack {
#ipcEventBinding = {
'sql:connect': this.connect,
'sql:query': this.query,
'sql:closeAll': this.closeAllConnections,
};

bindIpcMain(ipcMain: Electron.IpcMain): void {
Expand All @@ -34,6 +35,16 @@ class ConnectionStack {
return await connection.query(query);
}

async closeAllConnections(): Promise<void> {
await Promise.all(
Array.from(this.connections.values()).map((connection) =>
connection.end()
)
);

this.connections.clear();
}

#getConnection(senderId: number): Connection {
const connection = this.connections.get(senderId);

Expand Down
9 changes: 9 additions & 0 deletions src/styled.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// import original module declarations
import 'styled-components';
import { TmTheme } from './theme';

// and extend them!
declare module 'styled-components' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface DefaultTheme extends TmTheme {}
}
4 changes: 2 additions & 2 deletions src/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as active4d from '../theme/active4d.json';

const THEME_LIST_AS_ARRAY = [dracula, visualStudio, active4d] as const;

type Theme = typeof THEME_LIST_AS_ARRAY[number];
type Theme = (typeof THEME_LIST_AS_ARRAY)[number];

export const THEME_LIST: Record<string, Theme> = {};
THEME_LIST_AS_ARRAY.forEach((t) => {
Expand All @@ -13,7 +13,7 @@ THEME_LIST_AS_ARRAY.forEach((t) => {

export const DEFAULT_THEME = THEME_LIST_AS_ARRAY[0];

interface TmTheme {
export interface TmTheme {
readonly name: string;
readonly author?: string;
readonly settings: TmThemeSetting[];
Expand Down
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
"resolveJsonModule": true,
"jsx": "react-jsx"
},
"include": ["src", "stories"]
"include": [
"src"
// "stories" /ignore stories for now
]
}

0 comments on commit 2403416

Please sign in to comment.