Skip to content

Commit

Permalink
Merge pull request #89 from EXXETA/feature/87-code-formatting-setup
Browse files Browse the repository at this point in the history
#87 - added Prettier check to CI pipeline.
  • Loading branch information
AmirHassanConsulting authored Oct 23, 2024
2 parents 170564a + 3fd0788 commit 1eb16e6
Show file tree
Hide file tree
Showing 85 changed files with 1,277 additions and 1,094 deletions.
3 changes: 2 additions & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ If relevant, mention your manual testing here. If possible, include screenshots.

- [ ] Issue has been linked to this PR
- [ ] Code has been reviewed by person creating the PR
- [ ] Commit messages, branch names, code formatting adheres to our guidelines
- [ ] Commit messages, branch names, code formatting adheres to our [Contributing Guidelines
](https://github.com/EXXETA/rufus/blob/main/CONTRIBUTING.md)
- [ ] Automated tests have been written, if possible
- [ ] Manual testing has been performed
- [ ] Documentation has been updated, if necessary
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ jobs:
- name: Install dependencies
run: npm ci

- name: Prettier check
run: npx prettier '**/*.{ts,tsx}' --check

- name: Run tests
run: npm test

- name: Build package
run: npm run package
run: npm run package
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ out/
# IntelliJ
.idea

# Visual Studio Code
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets

# Local History for Visual Studio Code
.history/

# Built Visual Studio Code Extensions
*.vsix

# SCSS
*.css.d.ts
*.sass.d.ts
Expand Down
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true
"singleQuote": true,
"printWidth": 100
}
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
}
38 changes: 36 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,40 @@ For this repository we have agreed on the following git workflow:
- Preferably rebase your branch onto `main` before completing the PR
- Once the `main` branch reaches a stable state, we can create a release by...
- creating an annotated tag `vX.Y.Z` for the target commit on the `main` branch
- tag message should be: `Release vX.Y.Z`
- tag message should be: `Release vX.Y.Z`
- creating a release on github that references the tag
- attaching the release binaries to the github release
- attaching the release binaries to the github release

## Code Formatting

We use [Prettier](https://prettier.io/) for TypeScript code formatting.
To ensure that the code formatting follows our configuration
in [.prettierrc](https://github.com/EXXETA/rufus/blob/main/.prettierrc), the Prettier plugin should
be installed in your IDE.
The setup for Prettier is described in the following sections for IntelliJ and Visual Studio Code:

### IntelliJ

1. Navigate to **Settings** > **Plugins**.
2. Install [Prettier](https://plugins.jetbrains.com/plugin/10456-prettier) from marketplace.
3. Install [Save Actions X](https://plugins.jetbrains.com/plugin/22113-save-actions-x) from
marketplace.
4. Activate options "Activate save actions on save (before saving each file, performs the configured
actions below)" and "Reformat file":
![Code Formatting IntelliJ](images/contributing/code-formatting-intellij.png)

### Visual Studio Code

Install [Prettier - Code formatter](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode)
from Extensions. When installation is completed, Prettier will be used directly as the default
formatter when saving a file, as it is already configured in `.vscode/settings.json`.

---
After following these instructions, the code should always be formatted automatically when saving a
file.

However, you can check all Typescript files for code style issues with this command:
`npx prettier '**/*.{ts,tsx}' --check`

If you want Prettier to fix code style issues, you can use the following command:
`npx prettier '**/*.{ts,tsx}' --write`
22 changes: 11 additions & 11 deletions forge.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { rendererConfig } from './webpack.renderer.config';
const config: ForgeConfig = {
packagerConfig: {
asar: true,
icon: './images/icon'
icon: './images/icon',
},
rebuildConfig: {},
makers: [new MakerSquirrel({}), new MakerZIP({}, ['darwin']), new MakerRpm({}), new MakerDeb({})],
Expand All @@ -30,16 +30,16 @@ const config: ForgeConfig = {
js: './src/renderer/index.tsx',
name: 'main_window',
preload: {
js: './src/main/preload.ts'
}
}
]
js: './src/main/preload.ts',
},
},
],
},
devServer: {
client: {
overlay: { runtimeErrors: error => !error.message.startsWith('ResizeObserver loop') }
}
}
overlay: { runtimeErrors: (error) => !error.message.startsWith('ResizeObserver loop') },
},
},
}),
// Fuses are used to enable/disable various Electron functionality
// at package time, before code signing the application
Expand All @@ -50,9 +50,9 @@ const config: ForgeConfig = {
[FuseV1Options.EnableNodeOptionsEnvironmentVariable]: false,
[FuseV1Options.EnableNodeCliInspectArguments]: false,
[FuseV1Options.EnableEmbeddedAsarIntegrityValidation]: true,
[FuseV1Options.OnlyLoadAppFromAsar]: true
})
]
[FuseV1Options.OnlyLoadAppFromAsar]: true,
}),
],
};

export default config;
Binary file added images/contributing/code-formatting-intellij.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"monaco-editor-webpack-plugin": "^7.1.0",
"node-loader": "^2.0.0",
"postcss": "^8.4.38",
"prettier": "^3.3.3",
"style-loader": "^4.0.0",
"tailwindcss": "^3.4.4",
"ts-jest": "^29.2.5",
Expand Down
6 changes: 2 additions & 4 deletions scripts/setup-jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ jest.mock('electron', () => ({
getName: () => 'DiagClient',
},
safeStorage: {
encryptString: (plainText: string) =>
Buffer.from(plainText).toString('base64'),
decryptString: (encrypted: Buffer) =>
Buffer.from(encrypted.toString(), 'base64').toString(),
encryptString: (plainText: string) => Buffer.from(plainText).toString('base64'),
decryptString: (encrypted: Buffer) => Buffer.from(encrypted.toString(), 'base64').toString(),
},
}));
jest.mock('node:fs', () => fs);
Expand Down
16 changes: 4 additions & 12 deletions src/main/environment/service/environment-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ const persistenceService = PersistenceService.instance;
* request body, headers, etc.).
*/
export class EnvironmentService implements Initializable {
public static readonly instance: EnvironmentService =
new EnvironmentService();
public static readonly instance: EnvironmentService = new EnvironmentService();

public currentCollection: Collection;

Expand All @@ -34,9 +33,7 @@ export class EnvironmentService implements Initializable {
* @returns The stream with the variables replaced.
*/
public setVariablesInStream(stream: Readable) {
return stream.pipe(
new TemplateReplaceStream(this.getVariableValue.bind(this)),
);
return stream.pipe(new TemplateReplaceStream(this.getVariableValue.bind(this)));
}

/**
Expand Down Expand Up @@ -80,9 +77,7 @@ export class EnvironmentService implements Initializable {
* @param path The path of the collection to load and set as the current collection.
*/
public async changeCollection(path: string) {
return (this.currentCollection = await persistenceService.loadCollection(
path,
));
return (this.currentCollection = await persistenceService.loadCollection(path));
}

/**
Expand All @@ -94,10 +89,7 @@ export class EnvironmentService implements Initializable {
* @returns The value of the variable if it exists and is enabled, otherwise undefined.
*/
private getVariableValue(key: string) {
return (
this.currentCollection.variables[key]?.value ??
this.getSystemVariableValue(key)
);
return this.currentCollection.variables[key]?.value ?? this.getSystemVariableValue(key);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/main/error/internal-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ export enum InternalErrorType {
}

export class InternalError extends Error {
constructor(public readonly type: InternalErrorType, message: string, public readonly cause?: Error) {
constructor(
public readonly type: InternalErrorType,
message: string,
public readonly cause?: Error
) {
super(message);
this.name = 'InternalError';
}
Expand Down
23 changes: 8 additions & 15 deletions src/main/event/main-event-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ import path from 'node:path';
import { tmpdir } from 'node:os';
import { fs } from 'memfs';

jest.mock(
'electron',
() => ({
ipcMain: {
handle: jest.fn(),
},
app: {
getPath: jest.fn().mockReturnValue(''),
},
}),
);
jest.mock('electron', () => ({
ipcMain: {
handle: jest.fn(),
},
app: {
getPath: jest.fn().mockReturnValue(''),
},
}));

const eventService = MainEventService.instance;

Expand All @@ -30,7 +27,6 @@ describe('MainEventService', () => {
});

it('should read the file correctly providing no parameters', async () => {

// Act
const buffer = await eventService.readFile(TEST_FILE_PATH);

Expand All @@ -39,7 +35,6 @@ describe('MainEventService', () => {
});

it('should read the file correctly with offset', async () => {

// Act
const buffer = await eventService.readFile(TEST_FILE_PATH, 1);

Expand All @@ -48,12 +43,10 @@ describe('MainEventService', () => {
});

it('should read the file correctly with offset and length', async () => {

// Act
const buffer = await eventService.readFile(TEST_FILE_PATH, 1, 2);

// Assert
expect(Buffer.from(buffer).toString()).toBe(TEST_STRING.substring(1, 3));
});

});
14 changes: 5 additions & 9 deletions src/main/event/main-event-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ declare type AsyncFunction<R> = (...args: unknown[]) => Promise<R>;
* @param fn The function to wrap.
*/
function wrapWithErrorHandler<F extends AsyncFunction<R>, R>(fn: F) {
return async function(...args: Parameters<F>) {
return async function (...args: Parameters<F>) {
try {
return (await fn(...args)) as R;
} catch (error) {
Expand All @@ -36,14 +36,13 @@ function wrapWithErrorHandler<F extends AsyncFunction<R>, R>(fn: F) {
* @param functionName The name of the function to register.
*/
function registerEvent<T>(instance: T, functionName: keyof T) {
if (typeof functionName !== 'string' || functionName === 'constructor')
return;
if (typeof functionName !== 'string' || functionName === 'constructor') return;

const method = instance[functionName];
if (typeof method === 'function') {
console.debug(`Registering event function "${functionName}()" on backend`);
ipcMain.handle(functionName as string, (_event, ...args) =>
wrapWithErrorHandler(method as unknown as AsyncFunction<unknown>)(...args),
wrapWithErrorHandler(method as unknown as AsyncFunction<unknown>)(...args)
);
}
}
Expand Down Expand Up @@ -84,7 +83,7 @@ export class MainEventService implements IEventService {
offset,
'and length limited to',
length ?? 'unlimited',
'bytes',
'bytes'
);

let file: FileHandle | null = null;
Expand All @@ -105,10 +104,7 @@ export class MainEventService implements IEventService {
}
}

async saveRequest(
request: RufusRequest,
textBody?: string,
) {
async saveRequest(request: RufusRequest, textBody?: string) {
await persistenceService.saveRequest(request, textBody);
}

Expand Down
6 changes: 1 addition & 5 deletions src/main/filesystem/filesystem-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ import fs from 'node:fs';
* Singleton service for file system operations
*/
export class FileSystemService {

private static readonly _instance: FileSystemService = new FileSystemService();
private static readonly _tempDir = app?.getPath('temp') ?? '';

constructor() {

}
constructor() {}

public static get instance() {
return this._instance;
Expand All @@ -36,5 +33,4 @@ export class FileSystemService {
// how does this even work...
return fs.createReadStream(filePath);
}

}
Loading

0 comments on commit 1eb16e6

Please sign in to comment.