Skip to content

Commit

Permalink
Complete copy worker
Browse files Browse the repository at this point in the history
  • Loading branch information
BradyMitch committed Oct 28, 2024
1 parent 8a2761b commit 3bcf7dd
Show file tree
Hide file tree
Showing 20 changed files with 255 additions and 101 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules
package-lock.json
.env
coverage
desktop/resources/transfers
44 changes: 39 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,57 @@ The DATS project will be transferring inactive Full Retention (FR) government di

## Quick Start

1. Set up the `.env` based on the `.env.template` file.
1. **Set Up Environment**: Set up the `.env` based on the `.env.template` file.

2. Run `npm run up` to start the API, MongoDB, and RabbitMQ services.
2. **Start Services**: Run `npm run up` to start the API, MongoDB, and RabbitMQ services.

3. Change directory to `desktop` and run `npm run dev` to launch the desktop app.
3. **Desktop Directory**: Navigate to `desktop`.

4. **Install Dependencies**: Run `npm install`.

5. **Build App**: Run `npm run build` to enable worker functionality.

6. **Launch App**: Run `npm run dev`.

### Next Steps

- [Change API URL](#change-api-url)
- [Test Desktop App Executable](#test-desktop-app-executable)

<br />

## Change API URL

If you need to change the API URL to a different Environment, look to the toolbar options in the top left of your application window on Windows or the top left of your screen on Mac and select `Edit` > `Select API URL` > Select either `Local`, `Dev`, `Test`, or `Prod`.
To switch API environments:

1. In the app, go to `Edit > Select API URL`.

2. Choose `Local`, `Dev`, `Test`, or `Prod`.
- Default: `Local` during development using `npm run dev`, `Prod` in executable builds.

<br />

## Test Desktop App Executable

1. **Clear Build Folders**: Remove `desktop/out/` and `desktop/dist/`.

2. **Build Executable**:

- Run from `desktop/` as an Administrator:

```
npm run build:win
npm run build:mac
npm run build:linux
```

3. **Uninstall Previous Version**: Remove existing installation (e.g., in `C:\Users\<username>\AppData\Local\Programs\desktop` on Windows).

4. **Locate Build**: In VSCode, right-click `desktop/dist/` and select `Reveal in File Explorer`. Run the setup executable.

5. **Test App Launch**: Ensure the desktop app opens correctly, then close it.

By default when running in development using `npm run dev`, the default selection is `Local` and when running in the executable the default selection is `Prod`.
6. **Run in Console**: Open Command Prompt and run `<file-location>/desktop.exe` (e.g., `C:\Users\<username>\AppData\Local\Programs\desktop\desktop.exe`). Execution may differ on Mac, use Terminal to run the executable.

<br />

Expand Down
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"files": {
"ignoreUnknown": false,
"ignore": ["node_modules/", "build/", "coverage/"]
"ignore": ["node_modules/", "build/", "coverage/", "out/", "dist/"]
},
"formatter": {
"enabled": true,
Expand Down
34 changes: 0 additions & 34 deletions desktop/README.md

This file was deleted.

41 changes: 41 additions & 0 deletions desktop/cjs.workers.vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { defineConfig } from "vite";
import { resolve } from "node:path";
import fs from "node:fs";

// Define an array of worker entry points
const workerEntries = fs
.readdirSync(resolve(__dirname, "src/main/fileProcessing/workers"))
.filter((file) => file.endsWith(".ts"))
.reduce(
(entries, file) => {
const name = file.replace(".ts", "");
entries[name] = resolve(__dirname, `src/main/fileProcessing/workers/${file}`);
return entries;
},
{} as Record<string, string>,
);

/**
* Config used to build commonjs worker files in the out/cjs-workers directory.
* These files are added to the executable's program files when configured in the package.json build.
*/

export default defineConfig({
build: {
lib: {
entry: workerEntries, // Multiple entry points
formats: ["cjs"], // Build as CommonJS
fileName: (format, entryName) => `${entryName}.js`, // Use entry name for output
},
outDir: "out/cjs-workers", // Output directory
rollupOptions: {
external: ["worker_threads", "fs", "path", "p-limit"], // Externalize Node.js modules
},
target: "node22",
ssr: true,
minify: false, // Disable minification to help with debugging
},
optimizeDeps: {
exclude: ["fs", "path"], // Exclude Node.js modules from optimization
},
});
41 changes: 41 additions & 0 deletions desktop/es.workers.vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { defineConfig } from "vite";
import { resolve } from "node:path";
import fs from "node:fs";

// Define an array of worker entry points
const workerEntries = fs
.readdirSync(resolve(__dirname, "src/main/fileProcessing/workers"))
.filter((file) => file.endsWith(".ts"))
.reduce(
(entries, file) => {
const name = file.replace(".ts", "");
entries[name] = resolve(__dirname, `src/main/fileProcessing/workers/${file}`);
return entries;
},
{} as Record<string, string>,
);

/**
* Config used to build esmodule worker files in the out/es-workers directory.
* These files are used when in development mode using 'npm run dev'.
*/

export default defineConfig({
build: {
lib: {
entry: workerEntries, // Multiple entry points
formats: ["es"], // Build as ESModule
fileName: (format, entryName) => `${entryName}.js`, // Use entry name for output
},
outDir: "out/es-workers", // Output directory
rollupOptions: {
external: ["worker_threads", "fs", "path", "p-limit"], // Externalize Node.js modules
},
target: "node22",
ssr: true,
minify: false, // Disable minification to help with debugging
},
optimizeDeps: {
exclude: ["fs", "path"], // Exclude Node.js modules from optimization
},
});
11 changes: 10 additions & 1 deletion desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@
"version": "1.0.0",
"main": "./out/main/index.js",
"type": "module",
"build": {
"extraFiles": [
{
"from": "out/cjs-workers/copyWorker.cjs",
"to": "resources/copyWorker.cjs"
}
]
},
"scripts": {
"typecheck:node": "tsc --noEmit -p tsconfig.node.json --composite false",
"typecheck:web": "tsc --noEmit -p tsconfig.web.json --composite false",
"typecheck": "npm run typecheck:node && npm run typecheck:web",
"dev": "electron-vite dev",
"build": "npm run typecheck && electron-vite build",
"build": "npm run typecheck && electron-vite build && npm run build:workers",
"build:workers": "vite build --config es.workers.vite.config.ts && vite build --config cjs.workers.vite.config.ts",
"postinstall": "electron-builder install-app-deps",
"build:unpack": "npm run build && electron-builder --dir",
"build:win": "npm run build && electron-builder --win",
Expand Down
21 changes: 21 additions & 0 deletions desktop/src/main/fileProcessing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# IMPORTANT

Workers must be compiled to JavaScript before they can be used.

Follow these steps every time changes are made to an existing worker, OR a new worker is added.

## Adding a New Worker

1. Add the worker to the `build > extraFiles` in `desktop\package.json`. This will tell the build process to include it in the executable's program files.

2. Rebuild the app using `npm run build`.

3. Restart the development build using `npm run dev`.

<br />

## Updating an Existing Worker

1. Rebuild the app using `npm run build`.

2. Restart the development build using `npm run dev`.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Worker } from "node:worker_threads";

type WorkerData<T = unknown> = {
payload: T;
[key: string]: T;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./WorkerPool";
export * from "./copyWorker";
export * from "./createWorkerPool";
export * from "./processFolder";
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
import path from "node:path";
import type { WorkerPool } from "./WorkerPool";
import { app } from "electron";

/**
* Processes a folder by running the workers using the WorkerPool.
*
* @param workerPool - The WorkerPool instance to manage worker threads.
* @param filePath - The source folder path to be processed.
* @param destination - The destination folder path for the copied files.
* @param isDev - Is running in the development build (npm run dev).
* @returns A Promise that resolves when the worker processes complete.
*/
export const processFolder = async (
workerPool: WorkerPool,
pool: WorkerPool,
filePath: string,
destination: string,
isDev = false,
): Promise<void> => {
// Worker script path
const workerScript = path.resolve(__dirname, "copyWorker.js");
const workerScript = isDev
? path.resolve(__dirname, "../es-workers/copyWorker.js")
: path.join(app.getAppPath(), "../../resources/copyWorker.cjs");

const destinationPath = isDev
? path.resolve(__dirname, "../../resources/transfers/TR_0000_0000")
: path.join(app.getAppPath(), "../../resources/transfers/TR_0000_0000");

const workerData = {
payload: {
source: filePath,
destination,
},
source: filePath,
destination: destinationPath,
};

try {
// Run the copyWorker task using the WorkerPool
await workerPool.runTask(workerScript, workerData);
console.log(`Successfully copied folder from ${filePath} to ${destination}`);
await pool.runTask(workerScript, workerData);
console.log(`Successfully copied folder from ${filePath} to ${destinationPath}`);
} catch (error) {
console.error(`Failed to copy folder from ${filePath} to ${destination}:`, error);
console.error(`Failed to copy folder from ${filePath} to ${destinationPath}:`, error);
}
};
Loading

0 comments on commit 3bcf7dd

Please sign in to comment.