|
1 | 1 | // triggered via `$your-command inspire`
|
2 |
| - |
3 |
| -import { Command, intro, log } from '@stacksjs/cli' |
4 |
| -import { type CliOptions } from '@stacksjs/types' |
5 |
| - |
6 |
| -export default new Command({ |
7 |
| - name: 'inspire', |
8 |
| - description: 'Inspire yourself with a random quote', |
9 |
| - active: true, // default is true |
10 |
| - |
11 |
| - options: [ // alternatively, `options: ['--two, -t', 'Show two quotes', { default: false }]` |
12 |
| - { |
13 |
| - name: '--two, -t', |
14 |
| - description: 'Show two quotes', |
15 |
| - default: false, |
16 |
| - }, |
17 |
| - ], |
18 |
| - |
19 |
| - run: async (options?: CliOptions) => { |
20 |
| - log.info('Passed options are:', options) |
21 |
| - |
22 |
| - await intro('buddy inspire') |
23 |
| - |
24 |
| - return await runAction(Action.Inspire) |
25 |
| - }, |
26 |
| - |
27 |
| - // optional |
28 |
| - onFail: (error: Error) => { |
29 |
| - // outro('While running the inspire command, there was an issue', { startTime }, result.error) |
30 |
| - log.error(error) |
31 |
| - }, |
32 |
| - |
33 |
| - // optional |
34 |
| - onSuccess: () => { |
35 |
| - // const quote = result.value |
36 |
| - // outro(`Your custom quote is ${quote}`, { startTime }) |
37 |
| - log.success('Success!') |
38 |
| - }, |
39 |
| -}) |
40 |
| - |
41 |
| -// alternatively, you may use defineCommand() to create a command |
42 |
| -// export default defineCommand({ |
43 |
| -// name: 'inspire', |
44 |
| -// description: 'Inspire yourself with a random quote', |
45 |
| -// options: [] |
46 |
| -// run: async (options: CliOptions) => {} |
47 |
| -// onFail: (error: Error) => {} |
48 |
| -// onSuccess: () => {} |
49 |
| -// }) |
| 2 | +import process from 'node:process' |
| 3 | +import { cli, log } from '@stacksjs/cli' |
| 4 | +import { collect } from '@stacksjs/collections' |
| 5 | + |
| 6 | +// for enhanced type safety |
| 7 | +interface InspireOptions { |
| 8 | + two: boolean |
| 9 | +} |
| 10 | + |
| 11 | +const quotes = collect([ |
| 12 | + 'The best way to get started is to quit talking and begin doing.', |
| 13 | + 'The pessimist sees difficulty in every opportunity. The optimist sees opportunity in every difficulty.', |
| 14 | + 'Don’t let yesterday take up too much of today.', |
| 15 | + 'You learn more from failure than from success. Don’t let it stop you. Failure builds character.', |
| 16 | + 'It’s not whether you get knocked down, it’s whether you get up.', |
| 17 | + 'If you are working on something that you really care about, you don’t have to be pushed. The vision pulls you.', |
| 18 | + 'People who are crazy enough to think they can change the world, are the ones who do.', |
| 19 | + 'Failure will never overtake me if my determination to succeed is strong enough.', |
| 20 | + 'Entrepreneurs are great at dealing with uncertainty and also very good at minimizing risk. That’s the classic entrepreneur.', |
| 21 | + 'We may encounter many defeats but we must not be defeated.', |
| 22 | + 'Knowing is not enough; we must apply. Wishing is not enough; we must do.', |
| 23 | + 'Imagine your life is perfect in every respect; what would it look like?', |
| 24 | + 'We generate fears while we sit. We overcome them by action.', |
| 25 | + 'Whether you think you can or think you can’t, you’re right.', |
| 26 | + 'Security is mostly a superstition. Life is either a daring adventure or nothing.', |
| 27 | +]) |
| 28 | + |
| 29 | +cli() |
| 30 | + .command('inspire', 'Inspire yourself with a random quote') |
| 31 | + .option('--two, -t', 'Show two quotes', { default: false }) |
| 32 | + .alias('insp') |
| 33 | + .action((options: InspireOptions) => { |
| 34 | + if (options.two) |
| 35 | + quotes.random(2).forEach(quote => log.info(quote)) |
| 36 | + else |
| 37 | + log.info(quotes.random()) |
| 38 | + |
| 39 | + log.info('') |
| 40 | + log.success('Have a great day!') |
| 41 | + process.exit(ExitCode.Success) |
| 42 | + }) |
0 commit comments