|
1 | | -import { join } from 'path' |
2 | | - |
3 | 1 | import { Command, Options } from '@effect/cli' |
4 | 2 | import { NodeContext, NodeRuntime } from '@effect/platform-node' |
5 | | -import { Console, Effect, Option, pipe } from 'effect' |
| 3 | +import { Console, Effect } from 'effect' |
6 | 4 |
|
7 | | -const file = Options.text('file').pipe( |
8 | | - Options.withAlias('f'), |
9 | | - Options.withDefault('./config.json'), |
10 | | - Options.withDescription('Path to the configuration file') |
| 5 | +const skip = Options.boolean('skip').pipe( |
| 6 | + Options.withAlias('s'), |
| 7 | + Options.withDescription('Skip prompts and use defaults') |
11 | 8 | ) |
12 | 9 |
|
13 | | -const content = Options.text('content').pipe( |
14 | | - Options.optional, // this makes it an Option |
| 10 | +const config = Options.text('config').pipe( |
15 | 11 | Options.withAlias('c'), |
16 | | - Options.withDefault(Option.some('src/content')), |
17 | | - Options.withDescription('Path to the content directory') |
| 12 | + Options.withDefault('default.json'), |
| 13 | + Options.withDescription('Path to the JSON configuration file') |
18 | 14 | ) |
19 | 15 |
|
20 | | -const blogx = Command.make('blogx', { file, content }, (o) => { |
21 | | - // these will be printed |
22 | | - console.log('Config', `"${o.file}"`) |
23 | | - |
24 | | - // this will not be printed |
25 | | - Console.log('Add .pipe(NodeRuntime.runMain) to print this.') |
26 | | - |
27 | | - // example using pipe |
28 | | - pipe( |
29 | | - Effect.succeed(o.content), |
30 | | - Effect.map((content) => { |
31 | | - if (Option.isNone(content)) { |
32 | | - return console.warn('No content directory, skipping.') |
33 | | - } |
34 | | - return console.log(`${join(process.cwd(), content.toString())}`) |
35 | | - }), |
36 | | - Effect.catchAll((e) => { |
37 | | - console.error('Error:', e) |
38 | | - return Effect.succeed(0) |
39 | | - }) |
40 | | - ) |
41 | | - |
42 | | - // example using option match |
43 | | - Option.match(o.content, { |
44 | | - onNone: () => |
45 | | - Effect.succeed(console.warn('No content directory, skipping.')), |
46 | | - onSome: (dir) => Effect.succeed(console.log(`${join(process.cwd(), dir)}`)), |
47 | | - }).pipe(NodeRuntime.runMain) |
| 16 | +let count = 1 |
48 | 17 |
|
49 | | - // example using generator |
| 18 | +const init = Command.make('init', { skip, config }, ({ skip, config }) => |
50 | 19 | Effect.gen(function* () { |
51 | | - yield* Effect.succeed( |
52 | | - Option.isNone(o.content) |
53 | | - ? console.log('Content OK ✅') |
54 | | - : console.warn('Content FAIL ❌') |
55 | | - ) |
56 | | - |
57 | | - // throws 💥 |
58 | | - // yield* Effect.fail(new Error('Effect.fail works like a throw here')) |
59 | | - |
60 | | - // this would not be executed |
61 | | - yield* Console.log('I would never run if the previous effect had failed') |
62 | | - }).pipe(NodeRuntime.runMain) |
63 | | - |
64 | | - // this would never execute |
65 | | - console.error("The previous failure would bubble up, I'd never be printed") |
| 20 | + yield* Console.log('run:', count++) |
| 21 | + yield* Console.log('-c', config) |
| 22 | + yield* Console.log('--skip', skip, '\n') |
| 23 | + }) |
| 24 | +) |
66 | 25 |
|
67 | | - // we could exit with a failure |
68 | | - return Effect.exit(Effect.succeed(0)) |
| 26 | +const cli = Command.run(init, { |
| 27 | + name: 'my-cli', |
| 28 | + version: '1.0.0', |
69 | 29 | }) |
70 | 30 |
|
71 | | -// cli program |
72 | | -const cli = Command.run(blogx, { |
73 | | - name: 'blogx cli', |
74 | | - version: 'v0.0.1', |
75 | | -}) |
| 31 | +// tsx cli.ts --skip |
| 32 | +cli(['', '', '--skip']).pipe( |
| 33 | + Effect.provide(NodeContext.layer), |
| 34 | + NodeRuntime.runMain |
| 35 | +) |
| 36 | + |
| 37 | +// tsx cli.ts -c my-config.json |
| 38 | +cli(['', '', '-c', 'my-config.json']).pipe( |
| 39 | + Effect.provide(NodeContext.layer), |
| 40 | + NodeRuntime.runMain |
| 41 | +) |
76 | 42 |
|
77 | | -// tsx effect-cli.ts -f src/info.json -c src/content |
78 | | -export default cli(process.argv).pipe( |
| 43 | +// tsx cli.ts --help |
| 44 | +cli(['', '', '--help']).pipe( |
79 | 45 | Effect.provide(NodeContext.layer), |
80 | 46 | NodeRuntime.runMain |
81 | 47 | ) |
0 commit comments