11import { EOL } from "os"
2- import { AppRuntime } from "@/ effect/app-runtime "
2+ import { Effect } from "effect"
33import { File } from "../../../file"
44import { Ripgrep } from "@/file/ripgrep"
5- import { bootstrap } from "../../bootstrap "
5+ import { effectCmd } from "../../effect-cmd "
66import { cmd } from "../cmd"
7+ import { InstanceRef } from "@/effect/instance-ref"
8+ import { InstanceStore } from "@/project/instance-store"
79
8- const FileSearchCommand = cmd ( {
10+ const FileSearchCommand = effectCmd ( {
911 command : "search <query>" ,
1012 describe : "search files by query" ,
1113 builder : ( yargs ) =>
@@ -14,15 +16,18 @@ const FileSearchCommand = cmd({
1416 demandOption : true ,
1517 description : "Search query" ,
1618 } ) ,
17- async handler ( args ) {
18- await bootstrap ( process . cwd ( ) , async ( ) => {
19- const results = await AppRuntime . runPromise ( File . Service . use ( ( svc ) => svc . search ( { query : args . query } ) ) )
19+ handler : Effect . fn ( "Cli.debug.file.search" ) ( function * ( args ) {
20+ const ctx = yield * InstanceRef
21+ if ( ! ctx ) return
22+ const store = yield * InstanceStore . Service
23+ return yield * Effect . gen ( function * ( ) {
24+ const results = yield * File . Service . use ( ( svc ) => svc . search ( { query : args . query } ) )
2025 process . stdout . write ( results . join ( EOL ) + EOL )
21- } )
22- } ,
26+ } ) . pipe ( Effect . ensuring ( store . dispose ( ctx ) ) )
27+ } ) ,
2328} )
2429
25- const FileReadCommand = cmd ( {
30+ const FileReadCommand = effectCmd ( {
2631 command : "read <path>" ,
2732 describe : "read file contents as JSON" ,
2833 builder : ( yargs ) =>
@@ -31,27 +36,33 @@ const FileReadCommand = cmd({
3136 demandOption : true ,
3237 description : "File path to read" ,
3338 } ) ,
34- async handler ( args ) {
35- await bootstrap ( process . cwd ( ) , async ( ) => {
36- const content = await AppRuntime . runPromise ( File . Service . use ( ( svc ) => svc . read ( args . path ) ) )
39+ handler : Effect . fn ( "Cli.debug.file.read" ) ( function * ( args ) {
40+ const ctx = yield * InstanceRef
41+ if ( ! ctx ) return
42+ const store = yield * InstanceStore . Service
43+ return yield * Effect . gen ( function * ( ) {
44+ const content = yield * File . Service . use ( ( svc ) => svc . read ( args . path ) )
3745 process . stdout . write ( JSON . stringify ( content , null , 2 ) + EOL )
38- } )
39- } ,
46+ } ) . pipe ( Effect . ensuring ( store . dispose ( ctx ) ) )
47+ } ) ,
4048} )
4149
42- const FileStatusCommand = cmd ( {
50+ const FileStatusCommand = effectCmd ( {
4351 command : "status" ,
4452 describe : "show file status information" ,
4553 builder : ( yargs ) => yargs ,
46- async handler ( ) {
47- await bootstrap ( process . cwd ( ) , async ( ) => {
48- const status = await AppRuntime . runPromise ( File . Service . use ( ( svc ) => svc . status ( ) ) )
54+ handler : Effect . fn ( "Cli.debug.file.status" ) ( function * ( ) {
55+ const ctx = yield * InstanceRef
56+ if ( ! ctx ) return
57+ const store = yield * InstanceStore . Service
58+ return yield * Effect . gen ( function * ( ) {
59+ const status = yield * File . Service . use ( ( svc ) => svc . status ( ) )
4960 process . stdout . write ( JSON . stringify ( status , null , 2 ) + EOL )
50- } )
51- } ,
61+ } ) . pipe ( Effect . ensuring ( store . dispose ( ctx ) ) )
62+ } ) ,
5263} )
5364
54- const FileListCommand = cmd ( {
65+ const FileListCommand = effectCmd ( {
5566 command : "list <path>" ,
5667 describe : "list files in a directory" ,
5768 builder : ( yargs ) =>
@@ -60,15 +71,18 @@ const FileListCommand = cmd({
6071 demandOption : true ,
6172 description : "File path to list" ,
6273 } ) ,
63- async handler ( args ) {
64- await bootstrap ( process . cwd ( ) , async ( ) => {
65- const files = await AppRuntime . runPromise ( File . Service . use ( ( svc ) => svc . list ( args . path ) ) )
74+ handler : Effect . fn ( "Cli.debug.file.list" ) ( function * ( args ) {
75+ const ctx = yield * InstanceRef
76+ if ( ! ctx ) return
77+ const store = yield * InstanceStore . Service
78+ return yield * Effect . gen ( function * ( ) {
79+ const files = yield * File . Service . use ( ( svc ) => svc . list ( args . path ) )
6680 process . stdout . write ( JSON . stringify ( files , null , 2 ) + EOL )
67- } )
68- } ,
81+ } ) . pipe ( Effect . ensuring ( store . dispose ( ctx ) ) )
82+ } ) ,
6983} )
7084
71- const FileTreeCommand = cmd ( {
85+ const FileTreeCommand = effectCmd ( {
7286 command : "tree [dir]" ,
7387 describe : "show directory tree" ,
7488 builder : ( yargs ) =>
@@ -77,12 +91,15 @@ const FileTreeCommand = cmd({
7791 description : "Directory to tree" ,
7892 default : process . cwd ( ) ,
7993 } ) ,
80- async handler ( args ) {
81- await bootstrap ( process . cwd ( ) , async ( ) => {
82- const tree = await AppRuntime . runPromise ( Ripgrep . Service . use ( ( svc ) => svc . tree ( { cwd : args . dir , limit : 200 } ) ) )
94+ handler : Effect . fn ( "Cli.debug.file.tree" ) ( function * ( args ) {
95+ const ctx = yield * InstanceRef
96+ if ( ! ctx ) return
97+ const store = yield * InstanceStore . Service
98+ return yield * Effect . gen ( function * ( ) {
99+ const tree = yield * Effect . orDie ( Ripgrep . Service . use ( ( svc ) => svc . tree ( { cwd : args . dir , limit : 200 } ) ) )
83100 console . log ( JSON . stringify ( tree , null , 2 ) )
84- } )
85- } ,
101+ } ) . pipe ( Effect . ensuring ( store . dispose ( ctx ) ) )
102+ } ) ,
86103} )
87104
88105export const FileCommand = cmd ( {
0 commit comments