-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmicroplan-publish.js
More file actions
256 lines (217 loc) · 6.88 KB
/
Copy pathmicroplan-publish.js
File metadata and controls
256 lines (217 loc) · 6.88 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
var program = require('commander')
var async = require('async')
var path = require('path')
var parsers = require('./parsers/parsers.js')
var publishers = require('./publishers/publishers.js')
var _ = require('underscore')
var homeDir = require('home-dir')
var credentialsLocation = '.microplan'
var fs = require('fs')
var ora = require('ora')
var util = require('util')
async.waterfall(
[
parseProgramArgs,
checkForCredentials,
parseFileToBePublished,
validateConfiguration,
validatePlans,
generatePublishItems,
injectCredentials,
determinePublishMethod,
publishPlans,
writeStateFile
],
function (err, result) {
if (err) {
console.error(err)
process.exit(1)
}
process.exit(0)
}
)
function parseProgramArgs (callback) {
program
.option('-s, --serial', 'Publish plans serially')
.option('-p, --parallel', 'Publish plans parallely')
.parse(process.argv)
var args = program.args
if (!args.length) {
return callback(new Error('Filename required'))
}
var fileNameToBePublished = args[0]
return callback(null, fileNameToBePublished)
}
function checkForCredentials (fileNameToBePublished, callback) {
var credFileFullPath = path.join(homeDir(), credentialsLocation)
var publisherCredentials
try {
var credFile = fs.readFileSync(credFileFullPath)
publisherCredentials = JSON.parse(credFile).publisherCredentials
} catch (er) {
return callback(new Error(
'Please use `microplan login` command to login and start publishing'))
}
publisherCredentials =
_.isArray(publisherCredentials) ? publisherCredentials : []
return callback(null, publisherCredentials, fileNameToBePublished)
}
function parseFileToBePublished (publisherCredentials, fileNameToBePublished,
callback) {
if (_.isEmpty(fileNameToBePublished)) {
return callback(new Error('FileName is empty'))
}
var parsedPublishFileOutputs = parsers.parseFile(
path.join(process.cwd(), fileNameToBePublished)
)
var parsedPublishFile = _.first(parsedPublishFileOutputs)
if (_.isEmpty(parsedPublishFile)) {
return callback(new Error('Unable to parse input file.'))
}
return callback(null, publisherCredentials, fileNameToBePublished,
parsedPublishFile)
}
function validateConfiguration (publisherCredentials, fileNameToBePublished,
parsedPublishFile, callback) {
if (!_.isObject(parsedPublishFile.configuration) ||
_.isArray(parsedPublishFile.configuration)) {
return callback(new Error('Configuration should be an object with ' +
'unique configuration identifiers'))
}
var errorsInConfig = []
_.each(parsedPublishFile.configuration,
function (config) {
// check type
if (_.isEmpty(config.type)) {
errorsInConfig.push('Missing type for configuration:' +
config)
return
}
// check if valid publisher exists for given yml type
config.publisher = publishers[config.type]
if (_.isEmpty(config.publisher)) {
errorsInConfig.push('Publisher not found for configuration:' +
config)
}
}
)
if (!_.isEmpty(errorsInConfig)) {
return callback(new Error('Invalid configuration ' +
errorsInConfig.join()))
}
return callback(null, publisherCredentials, fileNameToBePublished,
parsedPublishFile)
}
function validatePlans (publisherCredentials, fileNameToBePublished,
parsedPublishFile, callback) {
if (!_.isArray(parsedPublishFile.plans)) {
return callback(new Error('plans should be an array with ' +
'title, description and in elements'))
}
// Do more validation if needed.
return callback(null, publisherCredentials, fileNameToBePublished,
parsedPublishFile)
}
function generatePublishItems (publisherCredentials, fileNameToBePublished,
parsedPublishFile, callback) {
// publishItems are building blocks of a publish operation
// goal of it to produce individual, self contained object, which contains
// all the information (payload, credentials etc.) needed for publishing an item
var publishItems = []
_.each(parsedPublishFile.plans,
function (plan) {
var inArr = []
if (_.isString(plan.in)) {
inArr = [plan.in]
} else if (_.isArray(plan.in)) {
inArr = plan.in
}
publishItems = publishItems.concat(
_.map(inArr,
function (inConfigName) {
return {
in: inConfigName,
plan: plan,
config: parsedPublishFile.configuration[inConfigName]
}
}
)
)
}
)
return callback(null, publisherCredentials, fileNameToBePublished,
parsedPublishFile, publishItems)
}
function injectCredentials (publisherCredentials, fileNameToBePublished,
parsedPublishFile, publishItems, callback) {
publishItems = _.map(publishItems,
function (item) {
var availableCreds = _.filter(publisherCredentials,
function (cred) {
return cred.type === item.config.type
}
)
// considering first object to be found with same type as
// default credentials for a publisher to publisher
item.creds = _.first(availableCreds)
return item
}
)
return callback(null, fileNameToBePublished, publishItems)
}
function determinePublishMethod (fileNameToBePublished, publishItems,
callback) {
// default publish method is serial
var publishMethod = async.eachSeries.bind(null, publishItems)
if (program.parallel === true) {
publishMethod = async.eachLimit.bind(null, publishItems, 10)
}
return callback(null, fileNameToBePublished, publishItems, publishMethod)
}
function publishPlans (fileNameToBePublished, publishItems, publishMethod,
callback) {
var publishState = {
succeededItems: [],
failedItems: []
}
publishMethod(
function (item, nextPublish) {
var spinner = ora(
util.format('%s :: %s', item.in, item.plan.title)
).start()
var publish = item.config.publisher.publish
publish(item, function (err, result) {
// credentials should not be stored in the state file
delete item.creds
if (err) {
publishState.failedItems.push({
error: err,
publishItem: item
})
spinner.fail()
return nextPublish()
}
publishState.succeededItems.push({
result: result,
publishItem: item
})
spinner.succeed()
return nextPublish()
})
},
function () {
return callback(null, fileNameToBePublished, publishState)
}
)
}
function writeStateFile (fileNameToBePublished, publishState, callback) {
try {
var stateFilePath = path.join(process.cwd(),
fileNameToBePublished + '.state.json')
fs.writeFileSync(stateFilePath, JSON.stringify(publishState))
return callback(null)
} catch (err) {
return callback(new Error('Unable to save state file for ' +
fileNameToBePublished + ' - ' + err))
}
}