11import * as vscode from "vscode"
22import * as path from "path"
33import * as fs from "fs/promises"
4+ import * as yaml from "yaml"
45import { GitFetcher } from "./GitFetcher"
56import {
67 MarketplaceItem ,
@@ -684,17 +685,16 @@ export class MarketplaceManager {
684685 mcps : [ ] ,
685686 files : [ ] ,
686687 }
687- customHookable . hook ( "onFileOutput" , ( { filePath, data } ) => {
688+ customHookable . hook ( "onFileOutput" , ( { filePath, parsedData } ) => {
689+ const pD = parsedData as Record < string , any >
688690 if ( filePath . endsWith ( "/.roomodes" ) ) {
689- const parsedData = JSON . parse ( data )
690- if ( parsedData ?. customModes ?. length ) {
691- parsedData . customModes . forEach ( ( mode : any ) => {
691+ if ( pD ?. customModes ?. length ) {
692+ pD . customModes . forEach ( ( mode : any ) => {
692693 itemInstalledMetadata . modes ?. push ( mode . slug )
693694 } )
694695 }
695696 } else if ( filePath . endsWith ( "/.roo/mcp.json" ) ) {
696- const parsedData = JSON . parse ( data )
697- const mcpSlugs = Object . keys ( parsedData ?. mcpServers ?? { } )
697+ const mcpSlugs = Object . keys ( pD ?. mcpServers ?? { } )
698698 if ( mcpSlugs . length ) {
699699 mcpSlugs . forEach ( ( mcpSlug : any ) => {
700700 itemInstalledMetadata . mcps ?. push ( mcpSlug )
@@ -737,11 +737,13 @@ export class MarketplaceManager {
737737 if ( await fs . access ( modesFilePath ) . catch ( ( ) => true ) )
738738 vscode . window . showWarningMessage ( `"${ item . name } ": modes file not found` )
739739 else {
740- const parsedModesFile = JSON . parse ( await fs . readFile ( modesFilePath , "utf-8" ) )
740+ const parsedModesFile = yaml . parse ( await fs . readFile ( modesFilePath , "utf-8" ) )
741741 parsedModesFile . customModes = parsedModesFile . customModes . filter (
742742 ( m : any ) => ! itemInstalledMetadata . modes ! . includes ( m . slug ) ,
743743 )
744- await fs . writeFile ( modesFilePath , JSON . stringify ( parsedModesFile , null , 2 ) , "utf-8" )
744+ if ( parsedModesFile . customModes . length )
745+ await fs . writeFile ( modesFilePath , yaml . stringify ( parsedModesFile , null , 2 ) , "utf-8" ) // Remove file if no more modes left
746+ else await fs . rm ( modesFilePath )
745747 }
746748 }
747749 if ( itemInstalledMetadata . mcps ) {
@@ -752,7 +754,9 @@ export class MarketplaceManager {
752754 itemInstalledMetadata . mcps . forEach ( ( mcp ) => {
753755 delete parsedMcpsFile . mcpServers [ mcp ]
754756 } )
755- await fs . writeFile ( mcpsFilePath , JSON . stringify ( parsedMcpsFile , null , 2 ) , "utf-8" )
757+ if ( Object . keys ( parsedMcpsFile . mcpServers ) . length )
758+ await fs . writeFile ( mcpsFilePath , JSON . stringify ( parsedMcpsFile , null , 2 ) , "utf-8" ) // Remove file if no more modes left
759+ else await fs . rm ( mcpsFilePath )
756760 }
757761 }
758762 if ( itemInstalledMetadata . files ) {
0 commit comments