This repository has been archived by the owner on Jun 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
installer.js
118 lines (102 loc) · 3.37 KB
/
installer.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
116
117
118
const fs = require('fs');
const exec = require('child_process').exec;
const path = require('path');
const vm = require('vm');
if (require.main === module) {
if (process.argv.length < 4) {
console.log('Usage: "node installer <tenantId> <adminKey> [hostId]"');
return 0;
}
main(process.argv[2], process.argv[3], process.argv[4]);
}
module.exports = main;
function logStep(header) {
console.log('*********************************************************************************');
header += header.length%2 ? '' : ' ';
const l = Math.floor((80 - header.length) / 2);
console.log('*'.repeat(l) + ' ' + header + ' ' + '*'.repeat(l));
}
function promisify(cb) {
return new Promise((resolve, reject) => {
cb((err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
}
function asyncFor(arr, body) {
let i = 0;
return Promise.resolve().then(() => iterate());
function iterate() {
if (i < arr.length) {
let ci = i++;
return Promise.resolve(body(arr[ci], ci, arr)).then(() => iterate());
}
}
}
function instantiate(context){
const mkdirp = require('mkdirp'); // It is important to require in here, as may be done as part of this script
const glob = require('glob');
logStep('Instantiating template');
return promisify(
cb => glob(path.resolve('.', '**/*.*'),
{ignore: [path.resolve('./node_modules/', '**/*.*'),
path.resolve('./app/', '**/*.*')]}, cb)
).then(files => {
return asyncFor(files, globFileName => {
const fileName = path.resolve(globFileName);
const relFile = path.relative('.', fileName);
console.log('Processing file ' + relFile);
const fileExt = path.extname(fileName);
if (fileExt !== '.include') {
return promisify(
cb => fs.readFile(fileName, 'utf8', cb)
).then(content => {
const processed = content.replace(/{{[^}]*?}}/g, function(x){
const s = x.substring(2, x.length - 2);
return vm.runInContext(s, context);
});
return putFile(path.resolve('./app', relFile), processed);
});
}
});
});
function putFile(fname, contents) {
return promisify(cb => mkdirp(path.dirname(fname), cb)).then(
() => promisify(cb => fs.writeFile(fname, contents, 'utf8', cb))
);
}
}
function main(tenantId, adminKey, hostId) {
logStep('Starting install');
if (!hostId) {
hostId = tenantId;
} else {
hostId = 'host-' + hostId;
}
const context = vm.createContext({
ZkitSdk: {
'AdminKey': adminKey,
'AdminUserId': `admin@${tenantId}.tresorit.io`,
'ApiBase': `https://${hostId}.api.tresorit.io`,
'TenantRoot': hostId !== tenantId ? '/tenant-' + tenantId : '',
'FrameOrigin': `https://${hostId}.api.tresorit.io` // Can be different from ApiBase when testing
}
});
logStep('Installing templating tools and example dependencies');
exec('npm install --only=production', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(stdout);
console.error(stderr);
return instantiate(context).then(() => {
logStep('Done :)');
console.log('You can start the app with: "cd app && npm start"');
}, console.error);
});
}