Skip to content

Commit dc50ed8

Browse files
authored
feat(cli): Cleanup REST application tooling (#774)
* feat(cli): init commit * feat(cli): add tests * feat(cli): add sequence.ts * feat(cli): add and delete licensing * feat(cli): apply feedback * feat(cli): fix application name validation * feat(cli): implement SequenceHandler
1 parent bb77e1b commit dc50ed8

32 files changed

+459
-146
lines changed

packages/cli/.eslintrc.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"env": {
3+
"es6": true,
4+
"node": true
5+
},
6+
"extends": "eslint:recommended",
7+
"parserOptions": {
8+
"sourceType": "module"
9+
},
10+
"rules": {
11+
"indent": ["error", 2],
12+
"linebreak-style": ["error", "unix"],
13+
"quotes": ["error", "single"],
14+
"semi": ["error", "always"],
15+
"no-console": "off"
16+
}
17+
}

packages/cli/bin/cli.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const yeoman = require('yeoman-environment');
1515

1616
const opts = minimist(process.argv.slice(2), {
1717
alias: {
18-
help: 'h',
1918
version: 'v',
2019
commands: 'l',
2120
},
@@ -30,8 +29,14 @@ if (opts.version) {
3029
var env = yeoman.createEnv();
3130

3231
env.register(path.join(__dirname, '../generators/app'), 'loopback4:app');
33-
env.register(path.join(__dirname, '../generators/extension'), 'loopback4:extension');
34-
env.register(path.join(__dirname, '../generators/controller'), 'loopback4:controller');
32+
env.register(
33+
path.join(__dirname, '../generators/extension'),
34+
'loopback4:extension'
35+
);
36+
env.register(
37+
path.join(__dirname, '../generators/controller'),
38+
'loopback4:controller'
39+
);
3540

3641
// list generators
3742
if (opts.commands) {

packages/cli/generators/app/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ module.exports = class extends ProjectGenerator {
4141
type: 'input',
4242
name: 'applicationName',
4343
message: 'Application class name:',
44-
default: utils.toClassName(this.projectInfo.name) + 'Application',
44+
default: utils.pascalCase(this.projectInfo.name) + 'Application',
45+
validate: utils.validateClassName,
4546
},
4647
];
4748

4849
return this.prompt(prompts).then(props => {
50+
props.applicationName = utils.toClassName(props.applicationName);
4951
Object.assign(this.projectInfo, props);
5052
});
5153
}

packages/cli/generators/app/templates/index.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// Copyright IBM Corp. 2017. All Rights Reserved.
2-
// Node module: <%= project.name %>
3-
// This file is licensed under the MIT License.
4-
// License text available at https://opensource.org/licenses/MIT
5-
61
const nodeMajorVersion = +process.versions.node.split('.')[0];
72
const dist = nodeMajorVersion >= 7 ? './dist' : './dist6';
83

packages/cli/generators/app/templates/src/application.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
// Copyright IBM Corp. 2017. All Rights Reserved.
2-
// Node module: <%= project.name %>
3-
// This file is licensed under the MIT License.
4-
// License text available at https://opensource.org/licenses/MIT
5-
61
import {Application, ApplicationConfig} from '@loopback/core';
7-
import {RestComponent} from '@loopback/rest';
8-
import {PingController} from './controllers/ping-controller';
2+
import {RestComponent, RestServer} from '@loopback/rest';
3+
import {PingController} from './controllers/ping.controller';
4+
import {MySequence} from './sequence';
95

106
export class <%= project.applicationName %> extends Application {
117
constructor(options?: ApplicationConfig) {
@@ -18,9 +14,19 @@ export class <%= project.applicationName %> extends Application {
1814
options,
1915
);
2016
super(options);
17+
this.server(RestServer);
2118
this.setupControllers();
2219
}
2320

21+
async start() {
22+
const server = await this.getServer(RestServer);
23+
server.sequence(MySequence);
24+
const port = await server.get('rest.port');
25+
console.log(`Server is running at http://127.0.0.1:${port}`);
26+
console.log(`Try http://127.0.0.1:${port}/ping`);
27+
return await super.start();
28+
}
29+
2430
setupControllers() {
2531
this.controller(PingController);
2632
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
# Controllers
22

33
This directory contains source files for the controllers exported by this app.
4+
5+
To add a new empty controller, type in `lb4 controller [<name>]` from the
6+
command-line of your application's root directory.
7+
8+
For more information, please visit
9+
[Controller generator](http://loopback.io/doc/en/lb4/Controller-generator.html).

packages/cli/generators/app/templates/src/controllers/ping-controller.ts renamed to packages/cli/generators/app/templates/src/controllers/ping.controller.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// Copyright IBM Corp. 2017. All Rights Reserved.
2-
// Node module: <%= project.name %>
3-
// This file is licensed under the MIT License.
4-
// License text available at https://opensource.org/licenses/MIT
5-
61
import {get, ServerRequest} from '@loopback/rest';
72
import {inject} from '@loopback/context';
83

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Datasources
2+
3+
This directory contains config for datasources used by this app.

packages/cli/generators/app/templates/src/index.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
// Copyright IBM Corp. 2017. All Rights Reserved.
2-
// Node module: <%= project.name %>
3-
// This file is licensed under the MIT License.
4-
// License text available at https://opensource.org/licenses/MIT
5-
61
import {<%= project.applicationName %>} from './application';
7-
import {RestServer} from '@loopback/rest';
82
import {ApplicationConfig} from '@loopback/core';
93

104
export {<%= project.applicationName %>};
@@ -14,10 +8,6 @@ export async function main(options?: ApplicationConfig) {
148

159
try {
1610
await app.start();
17-
const server = await app.getServer(RestServer);
18-
const port = await server.get('rest.port');
19-
console.log(`Server is running at http://127.0.0.1:${port}`);
20-
console.log(`Try http://127.0.0.1:${port}/ping`);
2111
} catch (err) {
2212
console.error(`Unable to start application: ${err}`);
2313
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Models
2+
3+
This directory contains code for models provided by this app.

0 commit comments

Comments
 (0)