@@ -9,7 +9,7 @@ import * as Stream from "effect/Stream";
99import * as ChildProcess from "effect/process/ChildProcess" ;
1010import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner" ;
1111
12- import { pathSetupCommand } from "@t3tools/shared/shell" ;
12+ import { pathSetup } from "@t3tools/shared/shell" ;
1313
1414import * as DesktopCliShim from "./DesktopCliShim.ts" ;
1515import * as DesktopEnvironment from "./DesktopEnvironment.ts" ;
@@ -63,6 +63,8 @@ export class DesktopCliCommand extends Context.Service<
6363 readonly state : Effect . Effect < DesktopCliCommandState > ;
6464 readonly install : Effect . Effect < DesktopCliCommandState , DesktopCliCommandError > ;
6565 readonly uninstall : Effect . Effect < DesktopCliCommandState , DesktopCliCommandError > ;
66+ /** Adds the installed command's folder to PATH in the user's shell startup file. */
67+ readonly addToPath : Effect . Effect < DesktopCliCommandState , DesktopCliCommandError > ;
6668 /** Installs the command on an app's first launch; removing it in Settings sticks. */
6769 readonly installOnFirstLaunch : Effect . Effect < void > ;
6870 }
@@ -159,6 +161,21 @@ export const make = Effect.gen(function* () {
159161 return Option . none < string > ( ) ;
160162 } ) ;
161163
164+ const startupLine = ( installedPath : string ) => {
165+ const setup = pathSetup ( {
166+ directory : path . dirname ( installedPath ) ,
167+ home : environment . homeDirectory ,
168+ shell : process . env . SHELL ,
169+ platform : environment . platform ,
170+ } ) ;
171+ return setup && { ...setup , path : path . join ( environment . homeDirectory , setup . file . slice ( 2 ) ) } ;
172+ } ;
173+ const hasLine = ( setup : { readonly path : string ; readonly line : string } ) =>
174+ fs . readFileString ( setup . path ) . pipe (
175+ Effect . map ( ( content ) => content . split ( "\n" ) . some ( ( line ) => line . trim ( ) === setup . line ) ) ,
176+ Effect . orElseSucceed ( ( ) => false ) ,
177+ ) ;
178+
162179 const state : DesktopCliCommand [ "Service" ] [ "state" ] = Effect . gen ( function * ( ) {
163180 if ( ! environment . isPackaged ) {
164181 return { supported : false , installedPath : null , onPath : false , launcherPath : null } as const ;
@@ -172,21 +189,16 @@ export const make = Effect.gen(function* () {
172189 }
173190 const first = yield * firstOnPath ;
174191 const onPath = windows || ( Option . isSome ( first ) && ( yield * isOurLink ( first . value ) ) ) ;
175- const pathCommand = onPath
176- ? undefined
177- : pathSetupCommand ( {
178- directory : path . dirname ( installed . value ) ,
179- home : environment . homeDirectory ,
180- shell : process . env . SHELL ,
181- platform : environment . platform ,
182- } ) ;
192+ const setup = onPath ? undefined : startupLine ( installed . value ) ;
183193 return {
184194 supported : true ,
185195 installedPath : installed . value ,
186196 onPath,
187197 launcherPath,
188198 ...shadow ,
189- ...( pathCommand === undefined ? { } : { pathCommand } ) ,
199+ ...( setup === undefined
200+ ? { }
201+ : { pathSetup : { file : setup . file , added : yield * hasLine ( setup ) } } ) ,
190202 } ;
191203 } ) . pipe (
192204 Effect . orElseSucceed ( ( ) => ( {
@@ -286,6 +298,22 @@ export const make = Effect.gen(function* () {
286298 return yield * state ;
287299 } ) . pipe ( Effect . withSpan ( "desktop.cliCommand.uninstall" ) ) ;
288300
301+ const addToPath : DesktopCliCommand [ "Service" ] [ "addToPath" ] = Effect . gen ( function * ( ) {
302+ const installed = yield * installedAt ;
303+ if ( Option . isNone ( installed ) ) return yield * fail ( "Install the t3 command first." ) ;
304+ const setup = startupLine ( installed . value ) ;
305+ if ( setup === undefined ) return yield * fail ( "T3 Code cannot add t3 to this shell's PATH." ) ;
306+ if ( ! ( yield * hasLine ( setup ) ) ) {
307+ yield * fs . makeDirectory ( path . dirname ( setup . path ) , { recursive : true } ) . pipe (
308+ Effect . andThen (
309+ fs . writeFileString ( setup . path , `\n# Added by T3 Code\n${ setup . line } \n` , { flag : "a" } ) ,
310+ ) ,
311+ Effect . mapError ( ( ) => fail ( `Could not add t3 to ${ setup . file } .` ) ) ,
312+ ) ;
313+ }
314+ return yield * state ;
315+ } ) . pipe ( Effect . withSpan ( "desktop.cliCommand.addToPath" ) ) ;
316+
289317 const installOnFirstLaunch : DesktopCliCommand [ "Service" ] [ "installOnFirstLaunch" ] = Effect . gen (
290318 function * ( ) {
291319 if ( ! environment . isPackaged || ( yield * exists ( firstLaunchMarker ) ) ) return ;
@@ -304,7 +332,7 @@ export const make = Effect.gen(function* () {
304332 Effect . withSpan ( "desktop.cliCommand.installOnFirstLaunch" ) ,
305333 ) ;
306334
307- return DesktopCliCommand . of ( { state, install, uninstall, installOnFirstLaunch } ) ;
335+ return DesktopCliCommand . of ( { state, install, uninstall, addToPath , installOnFirstLaunch } ) ;
308336} ) ;
309337
310338export const layer = Layer . effect ( DesktopCliCommand , make ) ;
0 commit comments