From a180505a8c6ec7cf1d63219a491825cb2728ce04 Mon Sep 17 00:00:00 2001 From: Niclas Thobaben Date: Wed, 7 Jul 2021 14:16:19 +0200 Subject: [PATCH] added user info --- README.md | 19 ++++++++++++++++++- bin/cmd.generate.js | 34 ++++++++++++++++++++++++++-------- templates/default.json | 8 +------- test.json | 6 +++++- 4 files changed, 50 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index da001a1..e8ac65c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,20 @@ # API-CLI -A commandline interface for generating and linting apibuilder-based api-specs. \ No newline at end of file +A commandline interface for generating and linting apibuilder-based api-specs. + +# Installation + +npm install -g @nclazz/api-cli + +# Generate a new service + +Generating is a new service spec is easily done with the generate command. + +i.e.: + +api-cli generate --name=myservice --description="My Service description" --silent + +As a template a default template will be used. The template can be overridden either by specifying it as a parameter +--template= or by creating a template json file in ~/.api/templates/default.json + +If the parameter --silent is not given an interactive prompt will lead through the generation process. diff --git a/bin/cmd.generate.js b/bin/cmd.generate.js index 25e1cba..af6e96c 100644 --- a/bin/cmd.generate.js +++ b/bin/cmd.generate.js @@ -2,24 +2,37 @@ const path = require('path'); const fs = require('fs'); const linter = require('./linter'); 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() { - return path.join(__dirname, '..', 'templates', 'default.json'); + return userTemplatePath('default.json') || path.join(__dirname, '..', 'templates', 'default.json') } function defaultOutputPath(name) { return path.join(process.cwd(), `${name}.json`) } + 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) }, - { question: 'Namespace', name: 'namespace' }, - { question: 'Base URL', name: 'base_url'} + ...it_root_questions ], results => { generateTemplate(results); }); @@ -34,6 +47,7 @@ function doSilentGeneration(args) { config.output = args.output || defaultOutputPath(args.name) config.namespace = args.namespace || undefined config.base_url = args.base_url || undefined + config.info = userTemplatePath('info.json') || undefined generateTemplate(config); } @@ -47,6 +61,10 @@ function generateTemplate(config) { template.namespace = config.namespace template.base_url = config.base_url + if(config.info) { + template.info = require(config.info) + } + let messages = linter.lint(template); if(messages.length) { console.error("Failed to generate service spec:"); diff --git a/templates/default.json b/templates/default.json index 8bfb1ea..06028ec 100644 --- a/templates/default.json +++ b/templates/default.json @@ -1,13 +1,7 @@ { "name": "", "description": "Common resources to be implemented by all other services. This spec can be used as a template for other services.", - "info": { - "contact": { - "name": "Niclas Thobaben", - "email": "info@nclazz.de", - "url": "nclazz.de" - } - }, + "info": {}, "imports": [], "headers": [], "enums": {}, diff --git a/test.json b/test.json index 626ab3c..4c4cd93 100644 --- a/test.json +++ b/test.json @@ -1,11 +1,15 @@ { "name": "test", - "description": "Hadasd.", + "description": "Test.", "info": { "contact": { "name": "Niclas Thobaben", "email": "info@nclazz.de", "url": "nclazz.de" + }, + "license": { + "name": "MIT", + "url": "https://opensource.org/licenses/MIT" } }, "imports": [],