-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.js
106 lines (93 loc) · 2.96 KB
/
deploy.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
'use strict';
let fs = require('fs');
let request = require('request').defaults({ jar: true });
module.exports = function (environment) {
let appDBName = 'newtable_test';
let createdb = function (callback) {
request({
uri: url + appDBName,
method: 'PUT',
}, function (err, response, body) {
if (err) throw err;
if (!JSON.parse(body).ok) throw 'Error!!' + body.toString();
callback();
}
);
};
let moveFiles = function () {
// get rev if doc exists
request(url + appDBName + '/_design/newtable', function (err, response, body) {
let b = JSON.parse(body);
if (b._rev) {
let rev = b._rev.split('-')[0];
} else {
let rev = 0;
}
let newtablejsFilename = 'newtable-' + rev + '.js';
let newtablecssFilename = 'newtable-' + rev + '.css';
let htmlfile = new Buffer(fs.readFileSync('./dist/index.html', 'utf8')
.replace(/js\/main.js/g, newtablejsFilename)
.replace(/css\/main.css/g, newtablecssFilename))
.toString('base64');
let newtablecss = fs.readFileSync('./dist/css/main.css').toString('base64');
// .replace(/..\/fonts\//g, '')).toString('base64');
let newtablejs = fs.readFileSync('./dist/js/main.js').toString('base64');
let data = {
_attachments: {
'index.html': { content_type: 'text\/html', data: htmlfile },
},
};
data._attachments[newtablejsFilename] = { content_type: 'application\/javascript', data: newtablejs };
data._attachments[newtablecssFilename] = { content_type: 'text\/css', data: newtablecss };
if (rev) data._rev = b._rev || null;
data = JSON.stringify(data);
request({
uri: url + appDBName + '/_design/newtable',
method: 'PUT',
form: data,
}, function (err, response, body) {
if (err) throw err;
console.log(body);
});
});
};
try {
let credentials = require('./couchdb_credentials');
let url = credentials.local.url;
let username = credentials.local.admin.username;
let password = credentials.local.admin.password;
} catch (e) {
throw 'credentials file missing, see readme';
}
if (environment === 'prod') {
url = credentials.prod.url;
username = credentials.prod.admin.username;
password = credentials.prod.admin.password;
}
// login
return request({
uri: url + '_session',
method: 'POST',
form: {
name: username,
password: password,
},
}, (err, response, body) => {
if (err) throw err;
if (!JSON.parse(body).ok) {
throw 'Error!!' + body.toString();
}
// get design doc rev
request(url + appDBName + '/_design/newtable', function (err, response, body) {
if (err) throw err;
let b = JSON.parse(body);
if (b && b.reason && b.reason === 'no_db_file') {
createdb(function (resp) {
moveFiles();
});
} else {
moveFiles();
}
});
});
};