-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
medic-conf.js
executable file
·88 lines (76 loc) · 2.68 KB
/
medic-conf.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
#!/usr/bin/env node
const error = require('../src/lib/log').error;
const fs = require('../src/lib/sync-fs');
const info = require('../src/lib/log').info;
const readline = require('readline-sync');
const redactBasicAuth = require('redact-basic-auth');
const supportedActions = require('../src/cli/supported-actions');
const usage = require('../src/cli/usage');
const args = process.argv.slice(2);
switch(args[0]) {
case '--help': return usage(0);
case '--shell-completion':
return require('../src/cli/shell-completion-setup')(args[1]);
case '--supported-actions':
console.log('Supported actions:\n ', supportedActions.join('\n '));
return process.exit(0);
case '--version':
console.log('beta-' + require('../package.json').version);
return process.exit(0);
}
if(args.length < 1) return usage(1);
const projectName = fs.path.basename(fs.path.resolve('.'));
const instanceUrl = args[0];
const couchUrl = `${instanceUrl}/medic`;
const productionUrlMatch = /^http(?:s)?:\/\/(?:[^@]*@)?(.*)\.app\.medicmobile\.org(?:$|\/)/.exec(instanceUrl);
if(productionUrlMatch && productionUrlMatch[1] !== projectName) {
if(!readline.keyInYN('\x1b[33mWARN ' +
`Attempting to upload configuration for \x1b[31m${projectName}\x1b[33m ` +
`to production instance: \x1b[31m${redactBasicAuth(instanceUrl)}\x1b[33m\n` +
'Continue?\x1b[0m')) {
error('User failed to confirm action.');
process.exit(1);
}
}
let actions = args.slice(1);
let extraArgs;
const argDivider = actions.indexOf('--');
if(argDivider !== -1) {
extraArgs = actions.slice(argDivider + 1);
actions = actions.slice(0, argDivider);
}
if(!actions.length) {
actions = [
'compile-app-settings',
'backup-app-settings',
'upload-app-settings',
'convert-app-forms',
'convert-contact-forms',
'backup-all-forms',
'delete-all-forms',
'upload-app-forms',
'upload-collect-forms',
'upload-contact-forms',
'upload-resources',
'upload-custom-translations',
];
}
const unsupported = actions.filter(a => !supportedActions.includes(a));
if(unsupported.length) {
error(`Unsupported action(s): ${unsupported.join(' ')}`);
process.exit(1);
}
info(`Processing config in ${projectName} for ${instanceUrl}.`);
info('Actions:\n -', actions.join('\n - '));
info('Extra args:', extraArgs);
return actions.reduce((promiseChain, action) =>
promiseChain
.then(() => info(`Starting action: ${action}…`))
.then(() => require(`../src/fn/${action}`)('.', couchUrl, extraArgs))
.then(() => info(`${action} complete.`)),
Promise.resolve())
.then(() => { if(actions.length > 1) info('All actions completed.'); })
.catch(e => {
error(e);
process.exit(1);
});