Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

feature/decorators #57

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/lib
/node_modules
/tmp
.idea
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions src/decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Command from './command'

export type CommandConstructor = typeof Command

export const makeDecorator = <Field extends keyof CommandConstructor>(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')
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ export {
Command,
flags,
}

export * from './decorators'
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"./src"
],
"strict": true,
"target": "es2017"
"target": "es2017",
"experimentalDecorators": true
},
"include": [
"./src/**/*"
Expand Down