diff --git a/src/cmd/cmd.gen.js b/src/cmd/cmd.gen.js index 6693a26..47e5a10 100644 --- a/src/cmd/cmd.gen.js +++ b/src/cmd/cmd.gen.js @@ -81,6 +81,13 @@ function generateTemplate(config) { return template } +function writeToFile(config, template) { + console.trace('cmd.gen.writeToFile') + console.debug(`Write to ${config.output}`) + + fs.writeFileSync(config.output, JSON.stringify(template, null, '\t')) +} + function prepare(config) { console.trace('cmd.gen.prepare') prepareUserDir(config) @@ -106,12 +113,18 @@ module.exports = { '--template ': 'Path to a template spec to use for generation. (optional)', '--info ': "Path to an info template. (optional)", '--annotations ': "Path to an annotations template. (optional)", - '--out ': "Path to output the generated template. (optional)" + '--out ': "Path to output the generated template. (optional)", + '--quiet, -q': "Do not create the spec file, just output it to stdout. (optional)" } }, run(args) { let config = prepare({ args }) let spec = generateTemplate(config) + + if(!args.q && !args.quiet) { + writeToFile(config, spec) + } + return spec } diff --git a/tests/cmd.test.js b/tests/cmd.test.js index fb57e7e..ab0af84 100644 --- a/tests/cmd.test.js +++ b/tests/cmd.test.js @@ -2,51 +2,70 @@ const assert = require('assert').strict const loader = require('../src/loader') const logging = require('../src/logging') const expect = require('chai').expect +const fs = require('fs') +const path = require('path') logging.init(-1) const commands = loader.load() describe("gen command", () => { + + const test_spec = { + name: 'test-service', + description: 'Description for service.', + baseurl: 'https://nclazz.de', + namespace: 'de.nclazz' , + quiet: true + } + it('throws an error on missing name', () => { - expect(() => commands.gen.run({ })).to.throw('Name must not be empty!') + expect(() => commands.gen.run({ quiet: true })).to.throw('Name must not be empty!') }) it('throws an error on missing description', () => { - expect(() => commands.gen.run({ name: 'testename' })).to.throw('Description must not be empty!') + expect(() => commands.gen.run({ name: 'testename', quiet: true })).to.throw('Description must not be empty!') }) it('generates a spec', () => { - let spec = commands.gen.run({ - name: 'test-service', - description: 'Description for service.', - baseurl: 'https://nclazz.de', - namspace: 'de.nclazz' - }); + let spec = commands.gen.run(test_spec); expect(spec).to.not.equal(null) expect(spec).to.not.equal(undefined) expect(spec).to.not.equal({}) }) it('includes [name, description, base_url, namespace] as top level properties', () => { - let spec = commands.gen.run({ - name: 'test-service', - description: 'Description for service.', - baseurl: 'https://nclazz.de', - namespace: 'de.nclazz' - }); + let spec = commands.gen.run(test_spec); expect(spec).to.have.ownProperty('name') expect(spec).to.have.ownProperty('description') expect(spec).to.have.ownProperty('base_url') expect(spec).to.have.ownProperty('namespace') }) it('correctly propagates [name, description, base_url, namespace] to spec', () => { - let spec = commands.gen.run({ - name: 'test-service', - description: 'Description for service.', - baseurl: 'https://nclazz.de', - namespace: 'de.nclazz' - }); + let spec = commands.gen.run(test_spec); expect(spec.name).to.equal('test-service') expect(spec.description).to.equal('Description for service.') expect(spec.base_url).to.equal('https://nclazz.de') expect(spec.namespace).to.equal('de.nclazz') }) + it('does not creates a file in quiet mode', () => { + let spec = commands.gen.run({ + ...test_spec, + out: './test-spec.json' + }); + expect(fs.existsSync('./test-spec.json')).to.equal(false) + }) + it('creates a file with valid json spec', () => { + let spec_path = path.join(__dirname, 'test-spec.json') + commands.gen.run({ + ...test_spec, + out: spec_path, + quiet: false + }); + expect(fs.existsSync(spec_path)).to.equal(true) + let spec_json = require(spec_path) + expect(spec_json).to.have.ownProperty('name') + expect(spec_json).to.have.ownProperty('description') + expect(spec_json).to.have.ownProperty('base_url') + expect(spec_json).to.have.ownProperty('namespace') + fs.unlinkSync(spec_path) + }) + }) \ No newline at end of file