-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.js
116 lines (100 loc) · 3.72 KB
/
bootstrap.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
106
107
108
109
110
111
112
113
114
115
'use strict';
/*\
title: $:/plugins/cjxgm/tearly/bootstrap.js
type: application/javascript
module-type: library
\*/
(function () {
var WebDAV = require('$:/plugins/cjxgm/tearly/webdav.js').WebDAV;
var Underscode = require('$:/plugins/cjxgm/tearly/underscode.js').Underscode;
var Utils = require('$:/plugins/cjxgm/tearly/utils.js').Utils;
function Bootstrap(wiki)
{
this.wiki = wiki;
this.dav = new WebDAV();
this.uc = new Underscode();
this.utils = new Utils();
this.used = false;
this.prefix = this.tiddlerText("$:/config/tearly/BootstrapPrefix");
this.tiddlersTitles = this.utils.tiddlersTitles(this.wiki);
this.tiddlersList = this.tiddlersTitles.map(x => this.titlePath(x, true)).join("\n");
}
// What needs to be done:
//
// - MKCOL {{prefix}}tiddlers/
// - PUT {{prefix}}tiddlers.list
// - PUT {{prefix}}bootloader.{html,css,js}
// - PUT tiddlers that match $:/config/SyncFilter
// with path {{prefix}}tiddlers/{{encodedTitle}}.tid
Bootstrap.prototype.bootstrap = function () {
if (this.used) throw Error("Bootstrap object can only be used once.");
this.used = true;
Promise.resolve()
.then(() => this.uploadStructure())
.then(() => this.uploadBootloader())
.then(() => this.uploadTiddlers())
.then(() => this.done())
.catch(err => {
console.error(err);
this.setStatus("@@color:red;''Failed.''@@");
alert(err);
})
;
};
Bootstrap.prototype.done = function () {
this.setStatus("''Success.''");
};
Bootstrap.prototype.uploadStructure = function () {
return Promise.resolve()
.then(() => this.dav.mkcol(this.fullPath("tiddlers/")))
.then(() => this.dav.put(this.fullPath("tiddlers.list"), this.tiddlersList))
;
};
Bootstrap.prototype.uploadBootloader = function () {
var uploads = [
// [ extension, rename ]
[ 'html', 'index' ],
[ 'css' ],
[ 'js' ],
];
var pathPairs = uploads.map(upload => {
var ext = upload[0];
var name = upload[1] || "bootloader";
var dst = this.fullPath(name + "." + ext);
var src = "$:/plugins/cjxgm/tearly/bootloader." + ext;
return [ dst, src ];
});
var pending = pathPairs.map(pair => {
var path = pair[0];
var text = this.tiddlerText(pair[1]);
return this.dav.put(path, text);
});
return Promise.all(pending);
};
Bootstrap.prototype.uploadTiddlers = function () {
var pending = this.tiddlersTitles.map(title => {
var path = this.titlePath(title);
var tiddler = this.wiki.getTiddler(title);
tiddler = this.utils.encodeTiddler(tiddler);
return this.dav.put(path, tiddler);
});
return Promise.all(pending);
};
Bootstrap.prototype.setStatus = function (status) {
this.setTiddlerText("$:/state/tearly/Bootstrapping", status);
};
Bootstrap.prototype.setTiddlerText = function (title, text) {
this.wiki.setText(title, null, null, text);
};
Bootstrap.prototype.tiddlerText = function (title) {
return this.wiki.getTiddlerText(title);
}
Bootstrap.prototype.fullPath = function (path) {
return this.prefix + path;
}
Bootstrap.prototype.titlePath = function (title, forLoading) {
var titlePath = "tiddlers/" + this.uc.encode(title) + ".tid";
return forLoading ? "./" + titlePath : this.fullPath(titlePath);
}
this.Bootstrap = Bootstrap;
}).call(exports);