@@ -3,50 +3,34 @@ import type * as FileSystem from "@effect/platform/FileSystem"
33import type * as Path from "@effect/platform/Path"
44import { Effect } from "effect"
55
6+ import { hasNonEmptyFile } from "./menu-auth-file-helpers.js"
7+
68type HasCredentials = (
79 fs : FileSystem . FileSystem ,
810 accountPath : string
911) => Effect . Effect < boolean , PlatformError >
1012
13+ type CredentialDirectoryCounterInput = {
14+ readonly fs : FileSystem . FileSystem
15+ readonly hasCredentials : HasCredentials
16+ readonly path : Path . Path
17+ readonly root : string
18+ }
19+
1120const ignoredAuthAccountEntries : ReadonlySet < string > = new Set ( [ ".image" ] )
1221
13- const hasFileAtPath = (
22+ export const hasCodexAccountCredentials = (
1423 fs : FileSystem . FileSystem ,
15- filePath : string
16- ) : Effect . Effect < boolean , PlatformError > =>
17- Effect . gen ( function * ( _ ) {
18- const exists = yield * _ ( fs . exists ( filePath ) )
19- if ( ! exists ) {
20- return false
21- }
22- const info = yield * _ ( fs . stat ( filePath ) )
23- return info . type === "File"
24- } )
24+ accountPath : string
25+ ) : Effect . Effect < boolean , PlatformError > => hasNonEmptyFile ( fs , `${ accountPath } /auth.json` )
2526
26- const hasNonEmptyFile = (
27- fs : FileSystem . FileSystem ,
28- filePath : string
29- ) : Effect . Effect < boolean , PlatformError > =>
27+ const countCredentialDirectories = ( {
28+ fs,
29+ hasCredentials,
30+ path,
31+ root
32+ } : CredentialDirectoryCounterInput ) : Effect . Effect < number , PlatformError > =>
3033 Effect . gen ( function * ( _ ) {
31- const hasFile = yield * _ ( hasFileAtPath ( fs , filePath ) )
32- if ( ! hasFile ) {
33- return false
34- }
35- const content = yield * _ ( fs . readFileString ( filePath ) , Effect . orElseSucceed ( ( ) => "" ) )
36- return content . trim ( ) . length > 0
37- } )
38-
39- export const countAuthCredentialAccounts = (
40- fs : FileSystem . FileSystem ,
41- path : Path . Path ,
42- root : string ,
43- hasCredentials : HasCredentials
44- ) : Effect . Effect < number , PlatformError > =>
45- Effect . gen ( function * ( _ ) {
46- const exists = yield * _ ( fs . exists ( root ) )
47- if ( ! exists ) {
48- return 0
49- }
5034 const entries = yield * _ ( fs . readDirectory ( root ) )
5135 let count = 0
5236 for ( const entry of entries ) {
@@ -66,38 +50,44 @@ export const countAuthCredentialAccounts = (
6650 return count
6751 } )
6852
69- export const hasCodexAccountCredentials = (
53+ const countExistingCredentialDirectories = (
54+ input : CredentialDirectoryCounterInput
55+ ) : Effect . Effect < number , PlatformError > =>
56+ input . fs . exists ( input . root ) . pipe (
57+ Effect . flatMap ( ( exists ) =>
58+ exists
59+ ? countCredentialDirectories ( input )
60+ : Effect . succeed ( 0 )
61+ )
62+ )
63+
64+ export const countAuthCredentialAccounts = (
7065 fs : FileSystem . FileSystem ,
71- accountPath : string
72- ) : Effect . Effect < boolean , PlatformError > =>
73- hasNonEmptyFile ( fs , `${ accountPath } /auth.json` )
66+ path : Path . Path ,
67+ root : string ,
68+ hasCredentials : HasCredentials
69+ ) : Effect . Effect < number , PlatformError > => countExistingCredentialDirectories ( { fs, hasCredentials, path, root } )
7470
7571export const countCodexCredentialAccounts = (
7672 fs : FileSystem . FileSystem ,
7773 path : Path . Path ,
7874 root : string
7975) : Effect . Effect < number , PlatformError > =>
80- Effect . gen ( function * ( _ ) {
81- const exists = yield * _ ( fs . exists ( root ) )
82- if ( ! exists ) {
83- return 0
84- }
85-
86- let count = yield * _ ( hasCodexAccountCredentials ( fs , root ) , Effect . map ( ( connected ) => connected ? 1 : 0 ) )
87- const entries = yield * _ ( fs . readDirectory ( root ) )
88- for ( const entry of entries ) {
89- if ( ignoredAuthAccountEntries . has ( entry ) ) {
90- continue
76+ fs . exists ( root ) . pipe (
77+ Effect . flatMap ( ( exists ) => {
78+ if ( ! exists ) {
79+ return Effect . succeed ( 0 )
9180 }
92- const accountPath = path . join ( root , entry )
93- const info = yield * _ ( fs . stat ( accountPath ) )
94- if ( info . type !== "Directory" ) {
95- continue
96- }
97- const connected = yield * _ ( hasCodexAccountCredentials ( fs , accountPath ) , Effect . orElseSucceed ( ( ) => false ) )
98- if ( connected ) {
99- count += 1
100- }
101- }
102- return count
103- } )
81+ return Effect . all ( {
82+ directoryCount : countCredentialDirectories ( {
83+ fs,
84+ hasCredentials : hasCodexAccountCredentials ,
85+ path,
86+ root
87+ } ) ,
88+ rootConnected : hasCodexAccountCredentials ( fs , root ) . pipe ( Effect . orElseSucceed ( ( ) => false ) )
89+ } ) . pipe (
90+ Effect . map ( ( { directoryCount, rootConnected } ) => directoryCount + ( rootConnected ? 1 : 0 ) )
91+ )
92+ } )
93+ )
0 commit comments