Skip to content

Commit 5224f0c

Browse files
committed
multiple publish/unpublish on environment available
1 parent b2ca05a commit 5224f0c

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

lib/publish.js

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ function Publish(_event, options) {
186186
Publish.prototype.init = function() {
187187
try {
188188
var self = this;
189-
inputs.environment_ids = [];
190189
async.waterfall([
191190
function(cb) {
192191
//proceed after confirmation
@@ -212,21 +211,46 @@ Publish.prototype.init = function() {
212211
},
213212
function(authtoken, cb) {
214213
headers.authtoken = authtoken;
214+
var body = {
215+
_method: 'GET'
216+
};
217+
if (typeof inputs.environment === 'object' && Array.isArray(inputs.environment)) {
218+
body.query = {
219+
$or: []
220+
};
221+
inputs.environment.forEach(function (envName) {
222+
body['query']['$or'].push({
223+
name: envName
224+
});
225+
});
226+
} else if (typeof inputs.environment === 'string') {
227+
inputs.environment = [inputs.environment];
228+
}
215229
// get environment names from Contentstack
216230
return request({
217-
uri: `${api.host}/${api.version}${api.urls.environments}${inputs.environment}`,
231+
uri: `${api.host}/${api.version}${api.urls.environments}`,
218232
headers: headers,
219-
method: 'GET',
220-
json: true
221-
}, function(err, body) {
233+
method: 'POST',
234+
json: body
235+
}, function(err, response) {
222236
if (err) {
223237
return cb(err);
224-
} else if (body.environments && body.environments.length) {
225-
log(info(JSON.stringify(body)));
226-
inputs.environment_ids = _.map(body.environments, 'uid');
238+
} else if (response && response.environments && response.environments.length) {
239+
// set environment details
240+
var envNames = _.map(response.environments, 'name');
241+
var envNotPresent = [];
242+
inputs.environment.forEach(function (envName) {
243+
if (envNames.indexOf(envName) === -1) {
244+
log(error(`${envName} was not found!`));
245+
envNotPresent.push(envName);
246+
}
247+
});
248+
if (envNotPresent.length > 0) {
249+
return cb(`The following environments were not found in the stack!\n${envNotPresent}`);
250+
}
227251
return cb(null);
228252
}
229-
return cb(body);
253+
return cb(`No environments were found in the stack! Query made on environment was:\n${JSON.stringify(body)}`);
230254
});
231255
},
232256
function(cb) {
@@ -251,10 +275,10 @@ Publish.prototype.init = function() {
251275
}
252276
], function(err) {
253277
if (err) {
254-
log(error(`Errorred in bulk ${inputs.event}ing on ${inputs.environment} environment due to\n${err.message || JSON.stringify(err)}`));
278+
log(error(`Errorred in bulk ${inputs.event}ing on ${JSON.stringify(inputs.environment)} environment due to\n${err.message || JSON.stringify(err)}`));
255279
process.exit(1);
256280
}
257-
log(success(`Bulk ${inputs.event}ing finished on ${inputs.environment} environment.`));
281+
log(success(`Bulk ${inputs.event}ing finished on ${JSON.stringify(inputs.environment)} environment.`));
258282
process.exit(0);
259283
return;
260284
});
@@ -425,7 +449,7 @@ Publish.prototype.publishAssets = function(assets, finalCallback) {
425449
json: {
426450
asset: {
427451
locales: (Array.isArray(inputs.languages)) ? inputs.languages : [inputs.languages],
428-
environments: (Array.isArray(inputs.environment)) ? inputs.environment : [inputs.environment]
452+
environments: inputs.environment
429453
}
430454
}
431455
}, function(error) {

0 commit comments

Comments
 (0)