Skip to content
This repository was archived by the owner on Jul 2, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ coverage/**
node_modules/**
scripts/**
submodules/**
generators/app/templates/**
generators/web/templates/**
temp
generators/**/templates/**
temp
73 changes: 73 additions & 0 deletions generators/static-api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';

const generators = require('yeoman-generator');

const module_dependencies = [
'@springworks/static-api-server',
'fixture-loader',
'forever',
];

module.exports = generators.Base.extend({

constructor: function() {
generators.Base.apply(this, arguments);
},

initializing: function() {
const welcomeMessage = [
'\nGenerate a static API\n',
];
this.log(welcomeMessage.join(''));
},

prompting: function() {
const done = this.async();
const prompts = [
{
type: 'input',
name: 'apiName',
message: 'Name of API',
},
];

this.prompt(prompts, function(answers) {
this.appname = answers.apiName.toLowerCase();
done();
}.bind(this));
},

configuring: function() {
this.config.set({
apiName: this.appname,
});
},

writing: {

serverScripts: function() {
this.copy('index.js', 'index.js');
this.copy('run-server.js', 'run-server.js');
this.copy('bin/start-server.js', 'bin/start-server.js');
this.copy('bin/stop-server.js', 'bin/stop-server.js');
},

packageFile: function() {
this.template('_package.json', 'package.json');
},

docs: function() {
this.template('_README.md', 'README.md');
},
},

install: {

installDependencies: function() {
this.npmInstall(module_dependencies, { save: true });
},

},


});
28 changes: 28 additions & 0 deletions generators/static-api/templates/_README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Static API
Expects Swagger spec (`api.json`) to be copied into this package's directory, e.g. with `npm run static-api`.

## API

### `createApiServer({ host = 'localhost', port = 3001 })`
Creates a [static server](https://www.npmjs.com/package/@springworks/static-api-server) based on the copied Swagger API spec. Maps fixtures to paths in API spec.


### `provideFixtures()`
Returns [fixture loader](https://www.npmjs.com/package/fixture-loader) configured to load fixtures for this API.

## CLI
There are two CLI functions available to start and stop the static api server from the command line.

**start-server \<host> \<port>**

Example:
```bash
start-<%=appname%>-static localhost 3001
```

**stop-server**

Example:
```bash
stop-<%=appname%>-static
```
15 changes: 15 additions & 0 deletions generators/static-api/templates/_package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@springworks/<%=appname%>-static",
"version": "0.0.1",
"description": "Static implementation of the API",
"main": "index.js",
"bin": {
"start-<%=appname%>-static": "bin/start-server.js",
"stop-<%=appname%>-static": "bin/stop-server.js"
},
"engines": {
"node": ">=4",
"npm": ">=2.7"
},
"dependencies": {}
}
19 changes: 19 additions & 0 deletions generators/static-api/templates/bin/start-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#! /usr/bin/env node

const forever = require('forever');
const path = require('path');

if (process.argv.length !== 4) {
console.log('ERROR: Please provide \'host\' and \'port\' arguments to \'start-server\'');
console.log('');
console.log('USAGE: start-server <host> <port>');
process.exit(1);
}

const script_path = path.join(__dirname, '..', 'run-server.js');
forever.startDaemon(script_path, {
args: [
process.argv[2],
process.argv[3],
],
});
8 changes: 8 additions & 0 deletions generators/static-api/templates/bin/stop-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#! /usr/bin/env node

const forever = require('forever');
const path = require('path');

const script_path = path.join(__dirname, '..', 'run-server.js');

forever.stop(script_path);
24 changes: 24 additions & 0 deletions generators/static-api/templates/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const path = require('path');
const static_server = require('@springworks/static-api-server');
const fixture_loader = require('fixture-loader');

const swagger_spec = require('./api.json');
const fixtures_path = path.join(__dirname, 'fixtures');
const created_fixture_loader = fixture_loader.create(fixtures_path);

module.exports = {

createApiServer: function(params) {
return static_server.default.createServer({
swagger_spec: swagger_spec,
fixture_loader: created_fixture_loader,
host: params.host || 'localhost',
port: params.port || 3001,
});
},

provideFixtures: function() {
return created_fixture_loader;
},

};
11 changes: 11 additions & 0 deletions generators/static-api/templates/run-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const index = require('./index');
const args = process.argv;

index.createApiServer({ host: args[2], port: args[3] })
.then(created_server => {
created_server.start(err => {
if (err) {
process.exit(1);
}
});
});
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@
"Springworks"
],
"author": "Springworks",
"contributors": [
{
"name": "Patrik Skoog",
"email": "patrik.skoog@springworks.se"
}
],
"license": "MIT",
"dependencies": {
"chalk": "1.1.1",
"yeoman-generator": "0.22.5"
},
"devDependencies": {
"@springworks/test-harness": "1.2.4",
"chai": "3.5.0",
"coveralls": "2.11.2",
"eslint": "1.10.3",
"eslint-config-springworks": "6.0.0",
"eslint-plugin-import": "0.12.1",
"eslint-plugin-mocha": "1.1.0",
"eslint-plugin-should-promised": "1.0.7",
"eslint-plugin-springworks": "1.1.1",
"istanbul": "0.4.2",
"mocha": "2.2.1",
"mocha-lcov-reporter": "1.0.0"
"mocha-lcov-reporter": "1.0.0",
"yeoman-assert": "2.1.1",
"yeoman-test": "1.0.0"
},
"peerDependencies": {
"yo": ">=1.4.2"
Expand Down
6 changes: 6 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
--ui bdd
--check-leaks
--recursive
--slow 200
--reporter spec

48 changes: 48 additions & 0 deletions test/static-api-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

const path = require('path');
const assert = require('yeoman-assert');
const helpers = require('yeoman-test');

describe('test/static-api-test.js', () => {
const api_name = 'foo-app';

before(done => {
helpers.run(path.join(__dirname, '..', 'generators', 'static-api'))
.withPrompts({
apiName: api_name,
})
.on('end', done);
});

it('should copy index file', () => {
assert.file('index.js');
});

it('should copy README.md and set correct script names', () => {
assert.file('README.md');
assert.fileContent('README.md', /start-foo-app-static/);
assert.fileContent('README.md', /stop-foo-app-static/);
});

it('should copy scripts for starting server', () => {
assert.file('bin/start-server.js');
});

it('should copy script for stopping server', () => {
assert.file('bin/stop-server.js');
});

it('should copy script for running server', () => {
assert.file('run-server.js');
});

it('should copy package.json', () => {
assert.file('package.json');
});

it('should update package.json with api name from input', () => {
assert.jsonFileContent('package.json', { name: `@springworks\/${api_name}-static` });
});

});