apiconnect-cli-loopback
Version:
Plugin for IBM API Connect Developer Toolkit
67 lines (59 loc) • 2.21 kB
JavaScript
/********************************************************* {COPYRIGHT-TOP} ***
* Licensed Materials - Property of IBM
* 5725-Z22, 5725-Z63, 5725-U33, 5725-Z63
*
* (C) Copyright IBM Corporation 2016, 2017
*
* All Rights Reserved.
* US Government Users Restricted Rights - Use, duplication or disclosure
* restricted by GSA ADP Schedule Contract with IBM Corp.
********************************************************** {COPYRIGHT-END} **/
// Node module: apiconnect-cli-loopback
var _ = require('lodash');
var apiGenerator = require('loopback-api-definition');
var fs = require('fs');
var g = require('strong-globalize')();
var jsYaml = require('js-yaml');
var path = require('path');
var argv = process.argv;
// 0 = node, 1 = this file
var swaggerFilePath = path.resolve(argv[2]);
var lbRoot = argv[3];
if (!(swaggerFilePath && lbRoot)) {
console.error(g.f('Invalid arguments'));
process.exit(1);
}
// Set LB_LAZYCONNECT_DATASOURCES as true
// to skip connection phase when booting app
process.env.LB_LAZYCONNECT_DATASOURCES = true;
var options = { format: 'json' };
try {
var app = require(lbRoot);
var apiDef = jsYaml.safeLoad(apiGenerator.getApiDef(app, options));
// Remove unsupported ops
_.each(apiDef.paths, function(ops, pathName) {
apiDef.paths[pathName] = _.omitBy(ops, function(op) {
var isSupported = true;
_.each(op.responses, function(v) {
isSupported = isSupported && !(v.schema && v.schema.type && v.schema.type === 'file');
// if (!isSupported) {
// console.error(op.operationId + ' is unsupported');
// }
});
return !isSupported;
});
});
// Remove paths with no ops
apiDef.paths = _.omitBy(apiDef.paths, function(val) {
return _.keys(val).length === 0;
});
fs.writeFileSync(swaggerFilePath, jsYaml.safeDump(apiDef), 'utf8');
// https://github.ibm.com/apimesh/scrum-platform-foundation/issues/253
// We need to exit the process as active connections will hang
process.exit(0);
} catch (err) {
var message = g.f('Cannot load the LoopBack application: %s' +
'\nPlease fix the problem and run `apic loopback:refresh`', err.message);
console.error(message);
process.exit(1);
}