Skip to content

Commit

Permalink
Create backup file in web worker when opening project (for testing pu…
Browse files Browse the repository at this point in the history
…rposes) [build]
  • Loading branch information
tkleinke committed Feb 13, 2025
1 parent 14aa435 commit b35acd7
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
4 changes: 3 additions & 1 deletion desktop/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@
"extractLicenses": false,
"buildOptimizer": false,
"optimization": false,
"namedChunks": true
"namedChunks": true,
"webWorkerTsConfig": "tsconfig.worker.json",
"preserveSymlinks": true
},
"configurations": {
"en": {
Expand Down
1 change: 1 addition & 0 deletions desktop/electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ const createWindow = () => {
minHeight: 600,
webPreferences: {
nodeIntegration: true,
nodeIntegrationInWorker: true,
enableRemoteModule: true,
contextIsolation: false,
preload: require('path').join(electron.app.getAppPath(), 'electron/preload.js'),
Expand Down
6 changes: 6 additions & 0 deletions desktop/src/app/components/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ export class AppComponent {
if (!Settings.hasUsername(settingsProvider.getSettings())) {
this.menuModalLauncher.openUpdateUsernameModal(true);
}

const projectName: string = settingsProvider.getSettings().selectedProject;
const backupDirectoryPath: string = remote.getGlobal('appDataPath') + '/backup';
const worker = new Worker(new URL('./app.worker', import.meta.url));
worker.onmessage = ({ data }) => console.log(data);
worker.postMessage({ projectName, backupDirectoryPath });
}


Expand Down
37 changes: 37 additions & 0 deletions desktop/src/app/components/app.worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// <reference lib="webworker" />

const replicationStream = require('../../../node_modules/pouchdb-replication-stream/dist/pouchdb.replication-stream');
const stream = require('stream');
const fs = require('fs');
const PouchDB = require('pouchdb-browser').default;


addEventListener('message', async ({ data }) => {

const projectName: string = data.projectName;
const backupDirectoryPath: string = data.backupDirectoryPath;
const targetFilePath: string = backupDirectoryPath + '/' + projectName + '.jsonl';

if (!fs.existsSync(backupDirectoryPath)) fs.mkdirSync(backupDirectoryPath);

await dump(targetFilePath, projectName);

const response = 'Backup of project ' + projectName + ' successful!';
postMessage(response);
});


export async function dump(filePath: string, project: string) {

PouchDB.plugin(replicationStream.plugin);
(PouchDB as any).adapter('writableStream', replicationStream.adapters.writableStream);

let dumpedString = '';
const memoryStream = new stream.Writable();
memoryStream._write = (chunk: any, _: any, done: any) => {
dumpedString += chunk.toString().replace(/"data"[\s\S]+?,/g,'\"data\":\"\",');
done();
};
await new PouchDB(project).dump(memoryStream, { attachments: false });
fs.writeFileSync(filePath, dumpedString);
}
15 changes: 15 additions & 0 deletions desktop/tsconfig.worker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/worker",
"lib": [
"es2018",
"webworker"
],
"types": []
},
"include": [
"src/**/*.worker.ts",
"node_modules/idai-field-core/**/*.d.ts"
]
}

0 comments on commit b35acd7

Please sign in to comment.