-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create backup file in web worker when opening project (for testing pu…
…rposes) [build]
- Loading branch information
Showing
5 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |