-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin.js
136 lines (105 loc) · 4.12 KB
/
bin.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env node
const fs = require('fs');
const tmp = require('tmp');
const parseArgs = require('minimist');
const update = require('./lib/update');
const database = require('./lib/database');
const merge = require('./lib/merge');
const save = require('./lib/save');
const autoupdate = require('./lib/autoupdate');
const backup = require('./lib/backup');
const verify = require('./lib/verify');
const zip = require('./lib/zip');
const shell = require('shelljs');
const commandArgs = parseArgs(process.argv, { '--': true });
const isVerbose = () => (commandArgs.v || commandArgs.verbose);
const out = (...a) => { if (isVerbose()) console.info(...a); };
let localPath = './';
const getLocalPath = (args) => {
return new Promise((resolve, reject) => {
const url = args._[3];
if (!url || ! /^https?:/.test(url)) { resolve(localPath); return; }
out('Remote URL= ', url);
if (!/\.tar\.gz$/.test(url)) { reject('ERROR: only TAR.GZ is expected in remote URL'); return; }
const tmpobj = tmp.dirSync();
localPath = tmpobj.name;
out('Temporary Directory: ', tmpobj.name);
shell.exec('cd ' + localPath + ' && curl -s ' + url + ' | tar xz');
resolve(localPath);
});
};
const cleanup = () => {
if (localPath && localPath !== './') {
out('Cleaning Path=', localPath);
shell.exec( 'rm -rf ' + localPath );
}
};
const err = (e) => {
console.log(e);
cleanup();
process.exit(1);
};
const done = (data) => {
if (typeof data === 'string') console.log(data);
cleanup();
process.exit(0);
};
if (process.mainModule && process.mainModule.filename === __filename) {
const args = parseArgs(process.argv, { '--': true });
const command = args._[2];
if (command === 'merge' || command === 'm') {
merge('./')
.then(data => {
fs.writeFileSync('./_bulk_docs.json', JSON.stringify(data));
done();
})
.catch(err);
} else if (command === 'zip' || command === 'z') {
verify('./')
.then((files)=> { zip('./').then(done).catch(err); })
.catch(err);
} else if (command === 'verify' || command === 'e') {
getLocalPath(args).then(path => {
verify(path).then(done).catch(e);
}).catch(err);
} else if (command === 'create' || command === 'c') {
getLocalPath(args).then(path => {
const couchCredentials = require('./lib/credentials')(path, args);
merge(path).then(data => {
const db = database(couchCredentials);
db.create().then(() => (db.upload(data))).then(done).catch(err);
}).catch(err);
}).catch(err);
} else if (command === 'recreate' || command === 'r') {
getLocalPath(args).then(path => {
const couchCredentials = require('./lib/credentials')(path, args);
merge(path).then(data => {
const db = database(couchCredentials);
db.removeIfExists()
.then(() => (db.create().then(() => (db.upload(data).then(done).catch(err))).then(done).catch(err)))
.catch(err);
}).catch(err);
}).catch(err);
} else if (command === 'autoupdate' || command === 'a') {
getLocalPath(args).then(path => {
const couchCredentials = require('./lib/credentials')(path, args);
autoupdate(couchCredentials, path, args).then(done).catch(err);
}).catch(err);
} else if (command === 'backup' || command === 'b') {
// backing up all databases using couchbackup
const couchCredentials = require('./lib/credentials')('./', args);
backup(couchCredentials, args).then(done).catch(err);
} else if (command === 'save' || command === 's') {
// save existing database into current folder
const couchCredentials = require('./lib/credentials')('./', args);
save(couchCredentials, args).then(done).catch(err);
} else if (command === 'update' || command === 'u') {
// update document from a current folder
const couchCredentials = require('./lib/credentials')('./', args);
update(couchCredentials, args).then(done).catch(err);
} else {
err( [ "USAGE: couchdocs create|recreate|save|update|autoupdate|verify|merge" ].join('\n'));
}
} else {
err( [ "ERROR: couchdocs -- not in main module" ].join('\n'));
}