added interactive generation
parent
4026d6549d
commit
1c16cd8617
|
@ -1,17 +1,34 @@
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const linter = require('./linter');
|
const linter = require('./linter');
|
||||||
|
const prompt = require('./prompt');
|
||||||
|
|
||||||
|
function defaultTemplatePath() {
|
||||||
|
return path.join(__dirname, '..', 'templates', 'default.json');
|
||||||
|
}
|
||||||
|
|
||||||
|
function defaultOutputPath(name) {
|
||||||
|
return path.join(process.cwd(), `${name}.json`)
|
||||||
|
}
|
||||||
|
|
||||||
function doInteractiveGeneration(args) {
|
function doInteractiveGeneration(args) {
|
||||||
|
prompt.start([
|
||||||
|
{ question: 'Service name', name: 'name', validator: linter.regex.NAME_REGEX, error: "Name must only contain lowercase letters, numbers or '-_'!" },
|
||||||
|
{ question: 'Service description', name: 'description', validator: linter.regex.DESCRIPTION_REGEX, error: "Description must be a descriptive sentence starting with a uppercase letter and ending with a '.'!" },
|
||||||
|
{ question: 'Template', name: 'template', default: defaultTemplatePath},
|
||||||
|
{ question: 'Output', name: 'output', default: results => defaultOutputPath(results.name) }
|
||||||
|
], results => {
|
||||||
|
generateTemplate(results);
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function doSilentGeneration(args) {
|
function doSilentGeneration(args) {
|
||||||
let config = {};
|
let config = {};
|
||||||
config.template = args.template || path.join(__dirname, '..', 'templates', 'default.json')
|
config.template = args.template || defaultTemplatePath();
|
||||||
config.name = args.name;
|
config.name = args.name;
|
||||||
config.description = args.description;
|
config.description = args.description;
|
||||||
config.output = args.output || path.join(process.cwd(), `${args.name}.json`)
|
config.output = args.output || defaultOutputPath(args.name)
|
||||||
|
|
||||||
generateTemplate(config);
|
generateTemplate(config);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
const objectPath = require('object-path');
|
const objectPath = require('object-path');
|
||||||
|
|
||||||
|
const NAME_REGEX = /^[a-z0-9_-]+$/;
|
||||||
|
const DESCRIPTION_REGEX = /^[A-Z]{1}.+\.$/
|
||||||
|
|
||||||
function required() {
|
function required() {
|
||||||
return (path, input, msgs) => {
|
return (path, input, msgs) => {
|
||||||
|
@ -30,8 +32,8 @@ function regex(regex) {
|
||||||
|
|
||||||
|
|
||||||
const linterMappings = [
|
const linterMappings = [
|
||||||
{ path: 'name', linters: [ required(), length(4), regex(/^[a-z0-9_-]+$/) ] },
|
{ path: 'name', linters: [ required(), length(4), regex(NAME_REGEX) ] },
|
||||||
{ path: 'description', linters: [ required(), length(4), regex(/^[A-Z]{1}[a-z0-9_-]+\.$/) ] }
|
{ path: 'description', linters: [ required(), length(4), regex(DESCRIPTION_REGEX) ] }
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -45,3 +47,8 @@ module.exports.lint = function(spec) {
|
||||||
});
|
});
|
||||||
return messages;
|
return messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports.regex = {
|
||||||
|
NAME_REGEX,
|
||||||
|
DESCRIPTION_REGEX
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
const readline = require('readline');
|
||||||
|
|
||||||
|
function createQuestion(item, result) {
|
||||||
|
let question = item.question || item.name;
|
||||||
|
if(item.default) {
|
||||||
|
question += ` (${item.default(result)})`
|
||||||
|
}
|
||||||
|
question += ': '
|
||||||
|
return question
|
||||||
|
}
|
||||||
|
|
||||||
|
function promptQuestion(question, results) {
|
||||||
|
process.stdout.write(createQuestion(question, results))
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.start = function(schema, cb) {
|
||||||
|
|
||||||
|
let rl = readline.createInterface({
|
||||||
|
input: process.stdin,
|
||||||
|
output: process.stdout,
|
||||||
|
terminal: true
|
||||||
|
});
|
||||||
|
|
||||||
|
let results = {};
|
||||||
|
let idx = 0;
|
||||||
|
let currentQuestion = schema[idx];
|
||||||
|
|
||||||
|
promptQuestion(currentQuestion, results);
|
||||||
|
|
||||||
|
rl.on('line', function(line){
|
||||||
|
if(currentQuestion.validator && !line.match(currentQuestion.validator)) {
|
||||||
|
console.error('Error:', currentQuestion.error || 'Invalid input!')
|
||||||
|
} else {
|
||||||
|
if(!line && currentQuestion.default) {
|
||||||
|
line = currentQuestion.default(results)
|
||||||
|
}
|
||||||
|
results[currentQuestion.name] = line;
|
||||||
|
idx++;
|
||||||
|
if(idx >= schema.length) {
|
||||||
|
rl.close();
|
||||||
|
cb(results);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
currentQuestion = schema[idx];
|
||||||
|
}
|
||||||
|
promptQuestion(currentQuestion, results);
|
||||||
|
})
|
||||||
|
}
|
|
@ -13,6 +13,11 @@
|
||||||
"version": "0.11.5",
|
"version": "0.11.5",
|
||||||
"resolved": "https://nexus.nclazz.de/repository/npm_public/object-path/-/object-path-0.11.5.tgz",
|
"resolved": "https://nexus.nclazz.de/repository/npm_public/object-path/-/object-path-0.11.5.tgz",
|
||||||
"integrity": "sha512-jgSbThcoR/s+XumvGMTMf81QVBmah+/Q7K7YduKeKVWL7N111unR2d6pZZarSk6kY/caeNxUDyxOvMWyzoU2eg=="
|
"integrity": "sha512-jgSbThcoR/s+XumvGMTMf81QVBmah+/Q7K7YduKeKVWL7N111unR2d6pZZarSk6kY/caeNxUDyxOvMWyzoU2eg=="
|
||||||
|
},
|
||||||
|
"readline": {
|
||||||
|
"version": "1.3.0",
|
||||||
|
"resolved": "https://nexus.nclazz.de/repository/npm_public/readline/-/readline-1.3.0.tgz",
|
||||||
|
"integrity": "sha1-xYDXfvLPyHUrEySYBg3JeTp6wBw="
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"args-parser": "^1.2.0",
|
"args-parser": "^1.2.0",
|
||||||
"object-path": "^0.11.5"
|
"object-path": "^0.11.5",
|
||||||
|
"readline": "^1.3.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue