44 lines
1.2 KiB
JavaScript
44 lines
1.2 KiB
JavaScript
|
const path = require('path');
|
||
|
const fs = require('fs');
|
||
|
const linter = require('./linter');
|
||
|
|
||
|
function doInteractiveGeneration(args) {
|
||
|
|
||
|
}
|
||
|
|
||
|
function doSilentGeneration(args) {
|
||
|
let config = {};
|
||
|
config.template = args.template || path.join(__dirname, '..', 'templates', 'default.json')
|
||
|
config.name = args.name;
|
||
|
config.description = args.description;
|
||
|
config.output = args.output || path.join(process.cwd(), `${args.name}.json`)
|
||
|
|
||
|
generateTemplate(config);
|
||
|
}
|
||
|
|
||
|
function generateTemplate(config) {
|
||
|
console.log(`Generate Service Spec for service '${config.name}' to ${config.output}`)
|
||
|
let template = require(config.template);
|
||
|
|
||
|
template.name = config.name
|
||
|
template.description = config.description
|
||
|
|
||
|
let messages = linter.lint(template);
|
||
|
if(messages.length) {
|
||
|
console.error("Failed to generate service spec:");
|
||
|
messages.forEach(message => console.log(`\t${message}`));
|
||
|
process.exit(-1)
|
||
|
}
|
||
|
|
||
|
let output = JSON.stringify(template, null, '\t');
|
||
|
fs.writeFileSync(config.output, output);
|
||
|
}
|
||
|
|
||
|
|
||
|
module.exports = function(args) {
|
||
|
if(args.silent) {
|
||
|
return doSilentGeneration(args);
|
||
|
}
|
||
|
return doInteractiveGeneration(args);
|
||
|
|
||
|
}
|