Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(typings): add typings to support TypeScript #646

Merged
merged 5 commits into from
Jul 23, 2017
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
17 changes: 11 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@
"type": "git",
"url": "https://github.com/tj/commander.js.git"
},
"devDependencies": {
"should": "^11.2.1",
"sinon": "^2.3.5"
},
"scripts": {
"test": "make test"
"test": "make test && npm run test-typings",
"test-typings": "node_modules/typescript/bin/tsc -p tsconfig.json"
},
"main": "index",
"files": [
"index.js"
],
"dependencies": {}
"dependencies": {
"@types/node": "^7.0.39"
},
"devDependencies": {
"should": "^11.2.1",
"sinon": "^2.3.5",
"typescript": "^2.4.1"
},
"typings": "typings/index.d.ts"
}
20 changes: 20 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"types": [
"node"
],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"typings/index.d.ts",
"typings/commander-tests.ts"
]
}
96 changes: 96 additions & 0 deletions typings/commander-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import * as program from './index';

interface ExtendedOptions extends program.CommandOptions {
isNew: any;
}

const commandInstance = new program.Command('-f');
const optionsInstance = new program.Option('-f');

program
.version('0.0.1')
.option('-p, --peppers', 'Add peppers')
.option('-P, --pineapple', 'Add pineapple')
.option('-b, --bbq', 'Add bbq sauce')
.option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
.parse(process.argv);

console.log('you ordered a pizza with:');
if (program['peppers']) console.log(' - peppers');
if (program['pineapple']) console.log(' - pineapple');
if (program['bbq']) console.log(' - bbq');
console.log(' - %s cheese', program['cheese']);

function range(val: string) {
return val.split('..').map(Number);
}

function list(val: string) {
return val.split(',');
}

function collect(val: string, memo: string[]) {
memo.push(val);
return memo;
}

function increaseVerbosity(v: any, total: number) {
return total + 1;
}

program
.version('0.0.1')
.usage('[options] <file ...>')
.option('-i, --integer <n>', 'An integer argument', parseInt)
.option('-f, --float <n>', 'A float argument', parseFloat)
.option('-r, --range <a>..<b>', 'A range', range)
.option('-l, --list <items>', 'A list', list)
.option('-o, --optional [value]', 'An optional value')
.option('-c, --collect [value]', 'A repeatable value', collect, [])
.option('-v, --verbose', 'A value that can be increased', increaseVerbosity, 0)
.parse(process.argv);

console.log(' int: %j', program['integer']);
console.log(' float: %j', program['float']);
console.log(' optional: %j', program['optional']);
program['range'] = program['range'] || [];
console.log(' range: %j..%j', program['range'][0], program['range'][1]);
console.log(' list: %j', program['list']);
console.log(' collect: %j', program['collect']);
console.log(' verbosity: %j', program['verbose']);
console.log(' args: %j', program['args']);

program
.version('0.0.1')
.option('-f, --foo', 'enable some foo')
.option('-b, --bar', 'enable some bar')
.option('-B, --baz', 'enable some baz');

// must be before .parse() since
// node's emit() is immediate

program.on('--help', () => {
console.log(' Examples:');
console.log('');
console.log(' $ custom-help --help');
console.log(' $ custom-help -h');
console.log('');
});

program
.command('allow-unknown-option')
.allowUnknownOption()
.action(() => {
console.log('unknown option is allowed');
});

program
.version('0.0.1')
.arguments('<cmd> [env]')
.action((cmd, env) => {
console.log(cmd, env);
});

program.parse(process.argv);

console.log('stuff');
Loading