Skip to content

Commit 2738e80

Browse files
correctly delete nested directories and modules
1 parent e2a328e commit 2738e80

File tree

5 files changed

+79
-18
lines changed

5 files changed

+79
-18
lines changed

packages/app/src/app/overmind/effects/vscode/sandboxFsSync/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ class SandboxFsSync {
123123
m.path = path;
124124
sandboxFs[path] = {
125125
...m,
126-
path,
127126
type: 'file',
128127
};
129128
}
@@ -132,10 +131,10 @@ class SandboxFsSync {
132131
sandbox.directories.forEach(d => {
133132
const path = getDirectoryPath(sandbox.modules, sandbox.directories, d.id);
134133

134+
d.path = path;
135135
// If this is a single directory with no children
136136
if (!Object.keys(sandboxFs).some(p => dirname(p) === path)) {
137-
d.path = path;
138-
sandboxFs[path] = { ...d, path, type: 'directory' };
137+
sandboxFs[path] = { ...d, type: 'directory' };
139138
}
140139
});
141140

packages/app/src/app/overmind/namespaces/editor/actions.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ export const sandboxChanged: AsyncAction<{ id: string }> = withLoadApp<{
101101
await effects.vscode.closeAllTabs();
102102

103103
actions.internal.setCurrentSandbox(sandbox);
104-
105-
state.editor.modulesByPath = effects.vscode.fs.create(sandbox);
106104
} catch (error) {
107105
state.editor.notFound = true;
108106
state.editor.error = error.message;
@@ -112,6 +110,7 @@ export const sandboxChanged: AsyncAction<{ id: string }> = withLoadApp<{
112110

113111
const sandbox = state.editor.currentSandbox;
114112

113+
state.editor.modulesByPath = effects.vscode.fs.create(sandbox);
115114
actions.internal.ensurePackageJSON();
116115

117116
await actions.editor.internal.initializeLiveSandbox(sandbox);

packages/app/src/app/overmind/namespaces/files/actions.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {
22
getDirectoryPath,
33
getModulePath,
4+
getModulesAndDirectoriesInDirectory,
45
} from '@codesandbox/common/lib/sandbox/modules';
56
import getDefinition from '@codesandbox/common/lib/templates';
6-
import { Module } from '@codesandbox/common/lib/types';
7+
import { Directory, Module } from '@codesandbox/common/lib/types';
78
import { AsyncAction } from 'app/overmind';
89
import { withOwnedSandbox } from 'app/overmind/factories';
910
import { createOptimisticModule } from 'app/overmind/utils/common';
@@ -75,14 +76,15 @@ export const directoryCreated: AsyncAction<{
7576
insertedAt: new Date().toString(),
7677
updatedAt: new Date().toString(),
7778
type: 'directory' as 'directory',
78-
path: getDirectoryPath(
79-
sandbox.modules,
80-
sandbox.directories,
81-
optimisticId
82-
),
79+
path: null,
8380
};
8481

85-
sandbox.directories.push(optimisticDirectory);
82+
sandbox.directories.push(optimisticDirectory as Directory);
83+
optimisticDirectory.path = getDirectoryPath(
84+
sandbox.modules,
85+
sandbox.directories,
86+
optimisticId
87+
);
8688
effects.vscode.fs.mkdir(state.editor.modulesByPath, optimisticDirectory);
8789

8890
try {
@@ -216,10 +218,26 @@ export const directoryDeleted: AsyncAction<{
216218
sandbox.directories.indexOf(directory),
217219
1
218220
)[0];
221+
const {
222+
removedModules,
223+
removedDirectories,
224+
} = getModulesAndDirectoriesInDirectory(
225+
removedDirectory,
226+
sandbox.modules,
227+
sandbox.directories
228+
);
219229

220-
// We need to recreate everything, as you might have deleted any number
221-
// of nested directories or files
222-
state.editor.modulesByPath = effects.vscode.fs.create(sandbox);
230+
removedModules.forEach(removedModule => {
231+
effects.vscode.fs.unlink(state.editor.modulesByPath, removedModule);
232+
sandbox.modules.splice(sandbox.modules.indexOf(removedModule), 1);
233+
});
234+
235+
removedDirectories.forEach(removedDirectoryItem => {
236+
sandbox.directories.splice(
237+
sandbox.directories.indexOf(removedDirectoryItem),
238+
1
239+
);
240+
});
223241

224242
// We open the main module as we do not really know if you had opened
225243
// any nested file of this directory. It would require complex logic
@@ -231,6 +249,12 @@ export const directoryDeleted: AsyncAction<{
231249
effects.live.sendDirectoryDeleted(directoryShortid);
232250
} catch (error) {
233251
sandbox.directories.push(removedDirectory);
252+
removedModules.forEach(removedModule => {
253+
sandbox.modules.push(removedModule);
254+
});
255+
removedDirectories.forEach(removedDirectoryItem => {
256+
sandbox.directories.push(removedDirectoryItem);
257+
});
234258
state.editor.modulesByPath = effects.vscode.fs.create(sandbox);
235259
effects.notificationToast.error('Could not delete directory');
236260
}

packages/app/src/app/overmind/namespaces/live/liveMessageOperators.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getModulesAndDirectoriesInDirectory } from '@codesandbox/common/lib/sandbox/modules';
12
import {
23
Directory,
34
LiveDisconnectReason,
@@ -274,9 +275,30 @@ export const onDirectoryDeleted: Operator<
274275
return;
275276
}
276277

277-
// We need to recreate everything, as you might have deleted any number
278-
// of nested directories or files
279-
state.editor.modulesByPath = effects.vscode.fs.create(sandbox);
278+
const removedDirectory = sandbox.directories.splice(
279+
sandbox.directories.indexOf(directory),
280+
1
281+
)[0];
282+
const {
283+
removedModules,
284+
removedDirectories,
285+
} = getModulesAndDirectoriesInDirectory(
286+
removedDirectory,
287+
sandbox.modules,
288+
sandbox.directories
289+
);
290+
291+
removedModules.forEach(removedModule => {
292+
effects.vscode.fs.unlink(state.editor.modulesByPath, removedModule);
293+
sandbox.modules.splice(sandbox.modules.indexOf(removedModule), 1);
294+
});
295+
296+
removedDirectories.forEach(removedDirectoryItem => {
297+
sandbox.directories.splice(
298+
sandbox.directories.indexOf(removedDirectoryItem),
299+
1
300+
);
301+
});
280302

281303
// We open the main module as we do not really know if you had opened
282304
// any nested file of this directory. It would require complex logic

packages/common/src/sandbox/modules.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ export function resolveDirectory(
7676
return directories.find(d => d.shortid === foundDirectoryShortid);
7777
}
7878

79+
export function getModulesAndDirectoriesInDirectory(
80+
directory: Directory,
81+
modules: Array<Module>,
82+
directories: Array<Directory>
83+
) {
84+
const { path } = directory;
85+
return {
86+
removedModules: modules.filter(moduleItem =>
87+
moduleItem.path.startsWith(path)
88+
),
89+
removedDirectories: directories.filter(
90+
directoryItem =>
91+
directoryItem.path.startsWith(path) && directoryItem !== directory
92+
),
93+
};
94+
}
95+
7996
export function getModulesInDirectory(
8097
_path: string | undefined,
8198
modules: Array<Module>,

0 commit comments

Comments
 (0)