@@ -7,8 +7,7 @@ import { FnmDefaultVersionParameter } from './default-version-parameter.js';
77import { FnmNodeVersionsParameter } from './node-versions-parameter.js' ;
88
99const FNM_DIR = path . join ( os . homedir ( ) , '.fnm' ) ;
10- const FNM_PATH_EXPORT = 'export PATH="$HOME/.fnm:$PATH"' ;
11- const FNM_EVAL = 'eval "$(fnm env --use-on-cd)"' ;
10+ const FNM_MULTISHELL_EXPORT = 'export FNM_MULTISHELL_PATH="${TMPDIR:-/tmp}/fnm_multishells"' ;
1211
1312const schema = z . object ( {
1413 nodeVersions : z
@@ -73,41 +72,48 @@ export class FnmResource extends Resource<FnmConfig> {
7372 }
7473
7574 override async create ( ) : Promise < void > {
76- if ( Utils . isMacOS ( ) ) {
77- await installOnMacOS ( ) ;
78- } else {
79- await installOnLinux ( ) ;
80- }
75+ await install ( ) ;
8176 }
8277
8378 override async destroy ( ) : Promise < void > {
84- if ( Utils . isMacOS ( ) ) {
85- await uninstallOnMacOS ( ) ;
86- } else {
87- await uninstallOnLinux ( ) ;
88- }
79+ await uninstall ( ) ;
8980 }
9081}
9182
92- async function installOnMacOS ( ) : Promise < void > {
93- await Utils . installViaPkgMgr ( 'fnm' ) ;
94- await FileUtils . addToShellRc ( FNM_EVAL ) ;
95- }
83+ async function install ( ) : Promise < void > {
84+ if ( Utils . isLinux ( ) ) {
85+ await Utils . installViaPkgMgr ( 'curl unzip' ) ;
86+ }
9687
97- async function installOnLinux ( ) : Promise < void > {
9888 const $ = getPty ( ) ;
99- await $ . spawn ( 'curl -fsSL https://fnm.vercel.app/install | bash -s -- --skip-shell ' , { interactive : true } ) ;
100- await FileUtils . addAllToShellRc ( [ FNM_PATH_EXPORT , FNM_EVAL ] ) ;
89+ await $ . spawn ( 'curl -fsSL https://fnm.vercel.app/install | bash' , { interactive : true } ) ;
90+ await FileUtils . addToShellRc ( FNM_MULTISHELL_EXPORT ) ;
10191}
10292
103- async function uninstallOnMacOS ( ) : Promise < void > {
104- await Utils . uninstallViaPkgMgr ( 'fnm' ) ;
105- await FileUtils . removeLineFromShellRc ( FNM_EVAL ) ;
106- }
107-
108- async function uninstallOnLinux ( ) : Promise < void > {
93+ async function uninstall ( ) : Promise < void > {
10994 const $ = getPty ( ) ;
110- await $ . spawnSafe ( `rm -rf ${ FNM_DIR } ` ) ;
111- await FileUtils . removeLineFromShellRc ( FNM_PATH_EXPORT ) ;
112- await FileUtils . removeLineFromShellRc ( FNM_EVAL ) ;
95+
96+ const { status : brewStatus } = await $ . spawnSafe ( 'brew list fnm' , {
97+ env : { HOMEBREW_NO_AUTO_UPDATE : '1' } ,
98+ } ) ;
99+
100+ if ( brewStatus === SpawnStatus . SUCCESS ) {
101+ await Utils . uninstallViaPkgMgr ( 'fnm' ) ;
102+ } else {
103+ await $ . spawnSafe ( `rm -rf ${ FNM_DIR } ` ) ;
104+ }
105+
106+ // Remove the block the installer appends to the shell rc file
107+ for ( const line of [
108+ '# fnm' ,
109+ `FNM_PATH="${ FNM_DIR } "` ,
110+ 'if [ -d "$FNM_PATH" ]; then' ,
111+ ' export PATH="$FNM_PATH:$PATH"' ,
112+ ' eval "$(fnm env --shell zsh)"' ,
113+ ' eval "$(fnm env --shell bash)"' ,
114+ 'fi' ,
115+ FNM_MULTISHELL_EXPORT ,
116+ ] ) {
117+ await FileUtils . removeLineFromShellRc ( line ) ;
118+ }
113119}
0 commit comments