1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | var dbg = require('debug');
|
17 | var debug = dbg('pub:generator');
|
18 | var asyncbuilder = require('asyncbuilder');
|
19 | var u = require('pub-util');
|
20 |
|
21 | require('pub-src-github');
|
22 |
|
23 | module.exports = function initOpts(cb) {
|
24 | cb = u.onceMaybe(cb);
|
25 |
|
26 | var staticRoot = location.pathname.slice(0, location.pathname.indexOf(pubRef.href));
|
27 | debug('initOpts staticRoot: "' + staticRoot + '"');
|
28 |
|
29 | $.getJSON(staticRoot + '/pub/_opts.json')
|
30 | .fail(function(jqXHR) { cb(new Error(jqXHR.responseText)); })
|
31 | .done(function(respData) {
|
32 |
|
33 |
|
34 |
|
35 | var opts = respData;
|
36 |
|
37 |
|
38 | opts.staticRoot = staticRoot;
|
39 |
|
40 |
|
41 | dbg.enable(opts.dbg);
|
42 |
|
43 | var ab = asyncbuilder(function(err) { cb(err, opts); });
|
44 |
|
45 |
|
46 |
|
47 | opts.source$ = {};
|
48 |
|
49 | opts.sources.forEach(function(source) {
|
50 | opts.source$[source.name] = source;
|
51 |
|
52 |
|
53 | if (opts.staticHost && source.staticSrc) {
|
54 | if (source.gatekeeper) {
|
55 | var append = ab.asyncAppend();
|
56 | debug('authenticating ' + source.gatekeeper);
|
57 | $.getJSON(source.gatekeeper + '/status' + location.search)
|
58 | .fail(function(jqXHR) { append(new Error(jqXHR.responseText)); })
|
59 | .done(function(status) {
|
60 |
|
61 |
|
62 | if (!status || !status.access_token) {
|
63 | location.assign(source.gatekeeper +
|
64 | '?ref=' + encodeURIComponent(location.href));
|
65 | }
|
66 |
|
67 | else {
|
68 | source.auth = status;
|
69 | source.src = require(source.staticSrc)(source);
|
70 | }
|
71 | debug(status);
|
72 | append(status);
|
73 | });
|
74 | }
|
75 | }
|
76 | });
|
77 | ab.complete();
|
78 |
|
79 | });
|
80 | };
|