Skip to content

Commit 2d1ddc3

Browse files
committed
fix: fixes for fnm
1 parent b1bfecf commit 2d1ddc3

2 files changed

Lines changed: 40 additions & 34 deletions

File tree

src/resources/javascript/fast-node-manager/fast-node-manager.ts

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import { FnmDefaultVersionParameter } from './default-version-parameter.js';
77
import { FnmNodeVersionsParameter } from './node-versions-parameter.js';
88

99
const 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

1312
const 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
}

test/node/fast-node-manager/fast-node-manager.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { SpawnStatus } from '@codifycli/plugin-core';
66
describe('fast-node-manager tests', () => {
77
const pluginPath = path.resolve('./src/index.ts');
88

9-
it('Can install fnm and node', { timeout: 500000, skip: true }, async () => {
9+
it('Can install fnm and node', { timeout: 500000 }, async () => {
1010
await PluginTester.fullTest(pluginPath, [
1111
{
1212
type: 'fast-node-manager',
@@ -15,10 +15,10 @@ describe('fast-node-manager tests', () => {
1515
},
1616
], {
1717
validateApply: async () => {
18-
expect(testSpawn('fnm --version', { interactive: true })).resolves.toMatchObject({ status: SpawnStatus.SUCCESS });
19-
expect(testSpawn('node --version', { interactive: true })).resolves.toMatchObject({ data: expect.stringContaining('20') });
18+
expect(testSpawn('fnm --version')).resolves.toMatchObject({ status: SpawnStatus.SUCCESS });
19+
expect(testSpawn('fnm exec --using=20 node --version')).resolves.toMatchObject({ data: expect.stringContaining('20') });
2020

21-
const { data: installedVersions } = await testSpawn('fnm list', { interactive: true });
21+
const { data: installedVersions } = await testSpawn('fnm list');
2222
expect(installedVersions).toContain('20');
2323
expect(installedVersions).toContain('18');
2424
},
@@ -29,11 +29,11 @@ describe('fast-node-manager tests', () => {
2929
nodeVersions: ['22'],
3030
}],
3131
validateModify: async () => {
32-
expect(testSpawn('node --version', { interactive: true })).resolves.toMatchObject({ data: expect.stringContaining('22') });
32+
expect(testSpawn('fnm exec --using=22 node --version')).resolves.toMatchObject({ data: expect.stringContaining('22') });
3333
},
3434
},
3535
validateDestroy: async () => {
36-
expect(testSpawn('fnm --version', { interactive: true })).resolves.toMatchObject({ status: SpawnStatus.ERROR });
36+
expect(testSpawn('fnm --version')).resolves.toMatchObject({ status: SpawnStatus.ERROR });
3737
},
3838
});
3939
});

0 commit comments

Comments
 (0)