This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcli.js
111 lines (95 loc) · 3.19 KB
/
cli.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
#!/usr/bin/env node
'use strict';
var yarr = require('yargs')
.usage('Usage: nsexport <command> [options]')
.command('<recType> <id>', 'Record Type and ID, "nsexport customrecord_japo 122"')
.command('<file>', 'JSON Array of Record Types and IDs, "nsexport ./records.json"')
.demand(1)
//.demand(2)
.describe('account','Account id.').alias('account','a')
.describe('bundle', 'Bundle ID').alias('bundle', 'b')
.describe('depth','Depth of join with others Records. Default: 1.').alias('depth','d')
.describe('email','Account email.').alias('email','e')
.describe('password','Account password.').alias('password','p')
.describe('show-inactive', 'Set "Show Inactive" Configuration. Default: false').alias('show-inactive', 'si')
.argv;
var child = require('child_process'),
//nsconfig = require('nsconfig'),
fs = require('fs'),
spawn = child.spawn;
var bin = __dirname + '/node_modules/.bin/phantomjs',
args = [__dirname + '/index.js', __dirname],
//config = nsconfig({}, true),
//configObj = JSON.stringify(config),
dir = '.ns-exports';
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
var nsconfig = require('nsconfig'),
custom_params = [
{name: 'hostname'},
{name : 'quiz' , required : true}
],
config = nsconfig({}, custom_params);
// #####################################
// read command line options
// #####################################
var record = yarr._[0],
id = yarr._[1];
for (var it in yarr) {
if (yarr[it]) {
config[it] = yarr[it];
}
}
if (!config.downloads || (record && id)) {
config.downloads = [];
if (record === '*') {
config.downloads = [];
} else if (record && id) {
('' + id).split(',').forEach(function(id) {
config.downloads.push({
record: record,
id: id
});
});
} else {
var hasDot = record.indexOf('./') === 0,
hasSpt = record.indexOf('/') === 0,
file = hasDot ? record : hasSpt ? '.' + record : './' + record;
if (hasDot || hasSpt || fs.existsSync(file)) {
config.downloads = [];
var content = fs.readFileSync(file, 'utf8');
JSON.parse(content).forEach(function(data) {
if (Array.isArray(data.id)) {
data.id.forEach(function(id) {
config.downloads.push({
record: data.record,
id: id
});
});
} else {
config.downloads.push(data);
}
});
} else {
config.downloads = [{
record: record
}];
}
}
}
var configObj = JSON.stringify(config);
fs.writeFileSync(dir + '/nsconfig.json', configObj, 'utf8');
var cspr = spawn(bin, args);
cspr.stdout.on('data', function (data) {
var buff = new Buffer(data);
console.log(buff.toString('utf8').trim());
});
cspr.stderr.on('data', function (data) {
data += '';
console.log(data.replace('\n', '\nstderr: '));
});
cspr.on('exit', function (code) {
process.exit(code);
console.log('\n');
});