Skip to content

Commit

Permalink
feat(example): add example code
Browse files Browse the repository at this point in the history
  • Loading branch information
codeandcats committed Sep 12, 2018
1 parent 6948854 commit ac51bc9
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
14 changes: 14 additions & 0 deletions example/commands/greet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// tslint:disable:no-console
import { Command, command, value } from '../../src';

export class GreetParams {
@value()
name: string = '';
}

@command('greet', GreetParams)
export class Greet implements Command<GreetParams> {
async execute(params: GreetParams) {
console.log(`Hello ${params.name}`);
}
}
30 changes: 30 additions & 0 deletions example/commands/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// tslint:disable:no-console
import { Command, command, option, value } from '../../src';

export class LoginParams {
@value()
username: string = '';

@value({ optional: true })
password: string = '';

@option({ valueName: 'days', description: 'Number of days to keep user logged in for' })
rememberMeFor: number = 1;
}

@command('login', LoginParams)
export class LoginCommand implements Command<LoginParams> {
async execute(params: LoginParams) {
if (params.username.toLowerCase() !== 'guest') {
if (params.password !== 'Password123') {
return console.error('Password incorrect');
}
}

console.log(`Authenticated. Welcome ${params.username}`);

if (params.rememberMeFor) {
console.log(`Session will be kept alive for ${params.rememberMeFor} days`);
}
}
}
6 changes: 6 additions & 0 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as cli from '../src';

import './commands/greet';
import './commands/login';

cli.execute();
10 changes: 10 additions & 0 deletions example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},
"extends": "../tsconfig.json",
"files": [
"./index.ts"
]
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"build": "tsc -p ./",
"clean": "rimraf ./dist",
"commit": "commit",
"preexample": "npm run package",
"example": "ts-node ./example",
"lint": "tslint -p ./tsconfig.json",
"lint:fix": "npm run lint -- --fix",
Expand Down

0 comments on commit ac51bc9

Please sign in to comment.