diff --git a/.gitignore b/.gitignore index 7bc5cb1e..f3982254 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ /lib /node_modules /tmp +.idea diff --git a/README.md b/README.md index 9c552910..2f048d5d 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,36 @@ LS.run() .catch(require('@oclif/errors/handle')) ``` +**TypeScript and Decorators** +```js +#!/usr/bin/env ts-node + +import * as fs from 'fs' +import {Command, flags, Flags} from '@oclif/command' + +@Flags({ + version: flags.version(), + help: flags.help(), + // run with --dir= or -d= + dir: flags.string({ + char: 'd', + default: process.cwd(), + }), +}) +class LS extends Command { + async run() { + const {flags} = this.parse(LS) + let files = fs.readdirSync(flags.dir) + for (let f of files) { + this.log(f) + } + } +} + +LS.run() +.catch(require('@oclif/errors/handle')) +``` + **JavaScript** ```js #!/usr/bin/env node @@ -80,6 +110,37 @@ LS.run() .catch(require('@oclif/errors/handle')) ``` +**JavaScript and Decorators** +```js +#!/usr/bin/env node + +const fs = require('fs') +const {Command, flags, Flags} = require('@oclif/command') + +class LS extends Command { + async run() { + const {flags} = this.parse(LS) + let files = fs.readdirSync(flags.dir) + for (let f of files) { + this.log(f) + } + } +} + +Flags({ + version: flags.version(), + help: flags.help(), + // run with --dir= or -d= + dir: flags.string({ + char: 'd', + default: process.cwd(), + }), +})(LS) + +LS.run() +.catch(require('@oclif/errors/handle')) +``` + Then run either of these with: ```sh-session diff --git a/src/decorators.ts b/src/decorators.ts new file mode 100644 index 00000000..dd556cd4 --- /dev/null +++ b/src/decorators.ts @@ -0,0 +1,21 @@ +import Command from './command' + +export type CommandConstructor = typeof Command + +export const makeDecorator = (field: Field) => + (value: CommandConstructor[Field]) => + (target: CommandConstructor) => { + target[field] = value + } + +export const Description = makeDecorator('description') +export const Hidden = makeDecorator('hidden') +export const Usage = makeDecorator('usage') +export const Help = makeDecorator('help') +export const Aliases = makeDecorator('aliases') +export const Strict = makeDecorator('strict') +export const Parse = makeDecorator('parse') +export const Flags = makeDecorator('flags') +export const Args = makeDecorator('args') +export const Examples = makeDecorator('plugin') +export const ParserOptions = makeDecorator('parserOptions') diff --git a/src/index.ts b/src/index.ts index 5bce2377..216ff7d3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,3 +29,5 @@ export { Command, flags, } + +export * from './decorators' diff --git a/tsconfig.json b/tsconfig.json index c6454f7a..ad712711 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,7 +10,8 @@ "./src" ], "strict": true, - "target": "es2017" + "target": "es2017", + "experimentalDecorators": true }, "include": [ "./src/**/*"