forked from CoEDL/ocfl-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.js
105 lines (80 loc) · 4.14 KB
/
demo.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Simple Demo script to show usage
const path = require('path');
const fs = require('fs-extra');
const OCFLRepository = require('./lib/repository');
// This is an asynchronous library so you need to call using await, or use promises
async function demo() {
const demoRepoPath = path.join("demo", "repo");
const demoContentPath = path.join("demo", "content");
const demoExportPath = path.join("demo", "export");
const dd = await fs.remove(demoExportPath);
const demoObjectID1 = "https://example.com/object1";
const demoObjectID2 = "https://example.com/object2";
// new repository needs to be in an empty directory, so make one
const rmdir = await fs.remove(demoRepoPath);
if (!await fs.exists("demo")) {
const demodir = await fs.mkdir("demo");
}
const dir = await fs.mkdir(demoRepoPath);
// Make a new repository and initialise it
var repo = new OCFLRepository();
var init = await repo.create(demoRepoPath);
// Make up some content
const c = await fs.remove(demoContentPath);
const rmc = await fs.mkdir(demoContentPath);
// Test file
const f1 = await fs.writeFile(path.join(demoContentPath, "file1.txt"), "some content");
// Empty directory - won't get added to the repo
const subDir = path.join(demoContentPath, "folder");
const emp = await fs.mkdir(subDir);
// Add demoContentPath to the repository
const new_object1 = await repo.importNewObjectDir(demoObjectID1, demoContentPath);
var inv1 = await new_object1.getInventory();
console.log("Head version of object 1", inv1.head);
console.log("Object 1 has this many files", Object.keys(inv1.versions[inv1.head].state).length)
const new_object2 = await repo.importNewObjectDir(demoObjectID2, demoContentPath);
var inv1 = await new_object1.getInventory();
console.log("Head version of object 1", inv1.head);
// Same content as before so this will not be saved in the repository when we add the content
const f21 = await fs.writeFile(path.join(demoContentPath, "file2.txt"), "some content");
const f31 = await fs.writeFile(path.join(demoContentPath, "file3.txt"), "some other content");
// Add something to our empty dir
const f3 = await fs.writeFile(path.join(subDir, "file4.txt"), "yet more content");
var objects = await repo.objects();
console.log("Repo has this many objects:", objects.length);
console.log("Re-add the demo directory to object1");
const new_object1v1 = await repo.importNewObjectDir(demoObjectID1, demoContentPath);
inv1 = await new_object1.getInventory();
console.log("Head version of object 1:", inv1.head);
console.log("Object 1 has this many files:", Object.keys(inv1.versions[inv1.head].state).length)
console.log("Removing file3.txt from the content");
const rmf3 = await fs.remove(path.join(demoContentPath, "file3.txt"));
const new_object1v3 = await repo.importNewObjectDir(demoObjectID1, demoContentPath);
inv1 = await new_object1.getInventory();
console.log("Head version of object 1:", inv1.head);
console.log("Object 1 has this many files:", Object.keys(inv1.versions[inv1.head].state).length)
console.log("Exporting all the repository objects ")
var objects = await repo.objects();
async function exportVersions(o) {
const inv = await (o.getInventory());
const versionList = Object.keys(inv.versions);
const id = inv.id;
const exportPromises = versionList.map(async (v) => {
const exportVDir = path.join(demoExportPath, o.path.replace(/\//g, ""), v)
const e = await fs.mkdirp(exportVDir);
return repo.export(id, exportVDir, { "version": v });
});
return await Promise.all(exportPromises);
}
const promises = objects.map((o) => exportVersions(o));
await Promise.all(promises);
// Now make a new repository object pointing to the same path we created above
var repo2 = new OCFLRepository();
// This will throw an error as there's already a repo there
// var init = await repo2.create(demoRepoPath);
// This will throw an error as there's already a repo there
var init = await repo2.load(demoRepoPath);
var objects2 = await repo2.objects();
console.log("Repo 2 has this many objects:", Object.keys(objects).length);
}
demo().then(() => { console.log("done") })