-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
60 lines (54 loc) · 2.06 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { Sync } from '@retorquere/zotero-sync/index'
import type { Zotero } from '@retorquere/zotero-sync/typings/zotero'
import { Store, StoreOptions } from './src'
import * as process from 'process';
import * as dotenv from 'dotenv';
import { connect } from 'couchbase';
const Gauge = require('gauge');
(async () => {
// config
dotenv.config();
const {
ZOTERO_API_KEY,
COUCHBASE_URL,
COUCHBASE_USER,
COUCHBASE_PASSWORD,
COUCHBASE_BUCKET
} = process.env as {[key : string]: string};
const storeOptions : StoreOptions = {
bucketName : COUCHBASE_BUCKET as string,
throwSyncErrors: false
}
// initialize the sync engine
const syncEngine = new Sync;
await syncEngine.login(ZOTERO_API_KEY);
// configure visual feedback
const gauge = new Gauge;
syncEngine.on(Sync.event.library, (library: {type:string, name: string }, index: number, total: number) => {
let name = library.type === "group" ? library.name : "User library";
gauge.show(`Synchronizing library "${name}" (${index}/${total})`, index/total);
});
syncEngine.on(Sync.event.remove, (type: string, objects: string[]) => {
gauge.show(`Removing ${objects.length} ${type}`);
});
syncEngine.on(Sync.event.collection, (collection: Zotero.Collection, index: number, total: number) => {
gauge.show(`Saving collection ${index}/${total}`, index/total);
});
syncEngine.on(Sync.event.item, (item: Zotero.Item.Any, index, total) => {
gauge.show(`Saving item ${index}/${total}`, index/total);
});
// error handling
syncEngine.on(Sync.event.error, e => {
gauge.hide();
console.error(`Error during task "${gauge._status.section.replace(/"/g,"'")}": ${e.message}`);
throw e;
});
// synchronize with the couchbase store
const store = new Store(COUCHBASE_URL, COUCHBASE_USER, COUCHBASE_PASSWORD, storeOptions);
await syncEngine.sync(store);
gauge.hide();
process.exit(0);
})().catch(err => {
console.log(err)
process.exit(1)
})