/
cli.js
155 lines (128 loc) · 4.17 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var program = require("commander")
var app = require("./lib/app");
var async = require("async")
var _ = require("underscore")
program.arguments("<configPath> <revelioUrl> [configName] [options]")
.option('-p, --publicKey <publicKey>', 'public API key')
.option('-s, --secretKey <secretKey>', 'secret API key')
.parse(process.argv);
program.configPath = program.args[0];
program.revelioUrl = program.args[1];
program.configName = program.args[2];
var publisher = require("revelio-publisher");
var revelioConfig;
async.series([
function (cb) {
getConfig(program.configPath, program.configName, cb)
},
setRevelioUrl,
setApiKey,
], function (err, data) {
if (err) {
logError(err)
process.exit(0)
}
else {
app.publish(data[0])
.then(function (msg) {
console.log(msg)
process.exit(1)
}, function (err) {
logError(err)
process.exit(0)
})
}
})
function logError(err) {
if (err.constructor === Error) {
console.error(err.message)
}
else {
console.error(err)
}
}
function getConfig(configPath, configName, getConfigCb) {
var path = require("path")
var fs = require("fs")
var fullPath = path.resolve(configPath);
async.waterfall([
readConfig,
parseConfig,
validateConfig
], function (err, config) {
getConfigCb(err, config)
})
function readConfig(cb) {
fs.readFile(fullPath, "utf8", function (err, fileText) {
if (err) {
cb("File '" + fullPath + "' does not exist or is inaccessible")
}
else cb(null, fileText)
});
}
function parseConfig(fileText, cb) {
var config;
try {
config = JSON.parse(fileText);
}
catch (ex) {
cb("Configuration file is not a valid JSON format")
return;
}
var configDir = path.dirname(configPath)
var revelioConfig = {
targets: normalizePaths(config.targets),
path: config.path,
url: config.url,
parsers: config.parsers
};
if (configName) {
if (!config.configurations ||
!config.configurations[configName]) {
getConfigCb("Configuration " + configName + " does not exist")
return;
}
var childConfig = config.configurations[configName];
if (childConfig.targets) revelioConfig.targets = normalizePaths(childConfig.targets)
if (childConfig.path) revelioConfig.path = childConfig.path
if (childConfig.url) revelioConfig.url = childConfig.url
if (childConfig.parsers) revelioConfig.parsers = childConfig.parsers
}
if (revelioConfig.parsers) {
revelioConfig.parsers = _.mapObject(revelioConfig.parsers, function (file) {
return normalizePath(file);
})
}
function normalizePaths(targets) {
if (!targets) return null
return targets.map(normalizePath)
}
function normalizePath(target) {
return path.resolve(configDir, target)
}
cb(null, revelioConfig)
}
function validateConfig(config, cb) {
if (!config.targets || config.targets.length == 0) {
cb("No targets specified")
return;
}
if (!config.path || config.path.length == 0) {
cb("No site path specified")
return;
}
cb(null, config);
}
}
function setRevelioUrl(cb) {
publisher.setRevelioUrl(program.revelioUrl)
cb();
}
function setApiKey(cb) {
if (program.publicKey) {
if (!program.secretKey) cb("You must provide both public and secret API keys")
publisher.setApiKey(program.publicKey, program.secretKey);
}
else if (program.secretKey) cb("You must provide both public and secret API keys")
cb();
}