added user info
parent
6bb0f6ad05
commit
a180505a8c
19
README.md
19
README.md
|
@ -1,3 +1,20 @@
|
||||||
# API-CLI
|
# API-CLI
|
||||||
|
|
||||||
A commandline interface for generating and linting apibuilder-based api-specs.
|
A commandline interface for generating and linting apibuilder-based api-specs.
|
||||||
|
|
||||||
|
# Installation
|
||||||
|
|
||||||
|
<code>npm install -g @nclazz/api-cli</code>
|
||||||
|
|
||||||
|
# Generate a new service
|
||||||
|
|
||||||
|
Generating is a new service spec is easily done with the <code>generate</code> command.
|
||||||
|
|
||||||
|
i.e.:
|
||||||
|
|
||||||
|
<code>api-cli generate --name=myservice --description="My Service description" --silent</code>
|
||||||
|
|
||||||
|
As a template a default template will be used. The template can be overridden either by specifying it as a parameter
|
||||||
|
<code>--template=<path-to-template></code> or by creating a template json file in <code>~/.api/templates/default.json</code>
|
||||||
|
|
||||||
|
If the parameter <code>--silent</code> is not given an interactive prompt will lead through the generation process.
|
||||||
|
|
|
@ -2,24 +2,37 @@ const path = require('path');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const linter = require('./linter');
|
const linter = require('./linter');
|
||||||
const prompt = require('./prompt');
|
const prompt = require('./prompt');
|
||||||
const { toNamespacedPath } = require('path');
|
const os = require('os')
|
||||||
|
|
||||||
|
const it_root_questions = [
|
||||||
|
{ 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) },
|
||||||
|
{ question: 'Namespace', name: 'namespace' },
|
||||||
|
{ question: 'Base URL', name: 'base_url'}
|
||||||
|
];
|
||||||
|
|
||||||
|
function userTemplatePath(name) {
|
||||||
|
let templatePath = path.join(os.homedir(), '.api', 'templates', name)
|
||||||
|
if(fs.existsSync(templatePath)) {
|
||||||
|
return templatePath;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
function defaultTemplatePath() {
|
function defaultTemplatePath() {
|
||||||
return path.join(__dirname, '..', 'templates', 'default.json');
|
return userTemplatePath('default.json') || path.join(__dirname, '..', 'templates', 'default.json')
|
||||||
}
|
}
|
||||||
|
|
||||||
function defaultOutputPath(name) {
|
function defaultOutputPath(name) {
|
||||||
return path.join(process.cwd(), `${name}.json`)
|
return path.join(process.cwd(), `${name}.json`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function doInteractiveGeneration(args) {
|
function doInteractiveGeneration(args) {
|
||||||
prompt.start([
|
prompt.start([
|
||||||
{ question: 'Service name', name: 'name', validator: linter.regex.NAME_REGEX, error: "Name must only contain lowercase letters, numbers or '-_'!" },
|
...it_root_questions
|
||||||
{ 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) },
|
|
||||||
{ question: 'Namespace', name: 'namespace' },
|
|
||||||
{ question: 'Base URL', name: 'base_url'}
|
|
||||||
], results => {
|
], results => {
|
||||||
generateTemplate(results);
|
generateTemplate(results);
|
||||||
});
|
});
|
||||||
|
@ -34,6 +47,7 @@ function doSilentGeneration(args) {
|
||||||
config.output = args.output || defaultOutputPath(args.name)
|
config.output = args.output || defaultOutputPath(args.name)
|
||||||
config.namespace = args.namespace || undefined
|
config.namespace = args.namespace || undefined
|
||||||
config.base_url = args.base_url || undefined
|
config.base_url = args.base_url || undefined
|
||||||
|
config.info = userTemplatePath('info.json') || undefined
|
||||||
|
|
||||||
generateTemplate(config);
|
generateTemplate(config);
|
||||||
}
|
}
|
||||||
|
@ -47,6 +61,10 @@ function generateTemplate(config) {
|
||||||
template.namespace = config.namespace
|
template.namespace = config.namespace
|
||||||
template.base_url = config.base_url
|
template.base_url = config.base_url
|
||||||
|
|
||||||
|
if(config.info) {
|
||||||
|
template.info = require(config.info)
|
||||||
|
}
|
||||||
|
|
||||||
let messages = linter.lint(template);
|
let messages = linter.lint(template);
|
||||||
if(messages.length) {
|
if(messages.length) {
|
||||||
console.error("Failed to generate service spec:");
|
console.error("Failed to generate service spec:");
|
||||||
|
|
|
@ -1,13 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "",
|
"name": "",
|
||||||
"description": "Common resources to be implemented by all other services. This spec can be used as a template for other services.",
|
"description": "Common resources to be implemented by all other services. This spec can be used as a template for other services.",
|
||||||
"info": {
|
"info": {},
|
||||||
"contact": {
|
|
||||||
"name": "Niclas Thobaben",
|
|
||||||
"email": "info@nclazz.de",
|
|
||||||
"url": "nclazz.de"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"imports": [],
|
"imports": [],
|
||||||
"headers": [],
|
"headers": [],
|
||||||
"enums": {},
|
"enums": {},
|
||||||
|
|
|
@ -1,11 +1,15 @@
|
||||||
{
|
{
|
||||||
"name": "test",
|
"name": "test",
|
||||||
"description": "Hadasd.",
|
"description": "Test.",
|
||||||
"info": {
|
"info": {
|
||||||
"contact": {
|
"contact": {
|
||||||
"name": "Niclas Thobaben",
|
"name": "Niclas Thobaben",
|
||||||
"email": "info@nclazz.de",
|
"email": "info@nclazz.de",
|
||||||
"url": "nclazz.de"
|
"url": "nclazz.de"
|
||||||
|
},
|
||||||
|
"license": {
|
||||||
|
"name": "MIT",
|
||||||
|
"url": "https://opensource.org/licenses/MIT"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"imports": [],
|
"imports": [],
|
||||||
|
|
Loading…
Reference in New Issue