Skip to content

Commit bcaf271

Browse files
Christian AlfoniChristian Alfoni
authored andcommitted
Use appendFile and rename for VSCode to work correctly
1 parent 0607d44 commit bcaf271

File tree

7 files changed

+28
-14
lines changed

7 files changed

+28
-14
lines changed

packages/app/src/app/overmind/effects/vscode/ModelsHandler.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,8 @@ export class ModelsHandler {
503503
model,
504504
});
505505
} catch (err) {
506-
if (process.env.NODE_ENV === 'development') {
507-
console.error('caught', err);
508-
}
506+
// This can throw when a file is deleted and you add new code to it. When
507+
// saving it a new file is created
509508
}
510509
});
511510
}

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import browserFs from 'fs';
12
import { dirname, join } from 'path';
23

3-
import browserFs from 'fs';
44
import {
55
getDirectoryPath,
66
getModulePath,
@@ -17,7 +17,7 @@ import { protocolAndHost } from '@codesandbox/common/lib/utils/url-generator';
1717
import { json } from 'overmind';
1818

1919
import { WAIT_INITIAL_TYPINGS_MS } from '../constants';
20-
import { mkdir, rename, rmdir, unlink, writeFile } from './utils';
20+
import { appendFile, mkdir, rename, rmdir, unlink, writeFile } from './utils';
2121

2222
const global = getGlobal() as Window & { BrowserFS: any };
2323

@@ -86,7 +86,7 @@ class SandboxFsSync {
8686
sandboxFs[path] = { ...d, type: 'directory' };
8787
}
8888

89-
browserFs.mkdirSync(join('/sandbox', path));
89+
browserFs.mkdir(join('/sandbox', path), () => {});
9090
});
9191

9292
sandbox.modules.forEach(m => {
@@ -98,23 +98,27 @@ class SandboxFsSync {
9898
type: 'file',
9999
};
100100

101-
browserFs.writeFileSync(join('/sandbox', path), m.code);
101+
browserFs.writeFile(join('/sandbox', path), m.code, () => {});
102102
}
103103
});
104104

105105
return sandboxFs;
106106
}
107107

108+
public appendFile(fs: SandboxFs, module: Module) {
109+
const copy = json(module);
110+
111+
appendFile(fs, copy);
112+
this.send('append-file', copy);
113+
browserFs.appendFile(join('/sandbox', module.path), module.code, () => {});
114+
}
115+
108116
public writeFile(fs: SandboxFs, module: Module) {
109117
const copy = json(module);
110118

111119
writeFile(fs, copy);
112120
this.send('write-file', copy);
113-
browserFs.writeFileSync(
114-
join('/sandbox', module.path),
115-
module.code,
116-
() => {}
117-
);
121+
browserFs.writeFile(join('/sandbox', module.path), module.code, () => {});
118122

119123
if (module.title === 'package.json') {
120124
this.syncDependencyTypings();

packages/app/src/app/overmind/effects/vscode/SandboxFsSync/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { Directory, Module, SandboxFs } from '@codesandbox/common/lib/types';
22
import { json } from 'overmind';
33

4+
export const appendFile = (fs: SandboxFs, module: Module) => {
5+
fs[module.path] = module;
6+
};
7+
48
export const writeFile = (fs: SandboxFs, module: Module) => {
59
fs[module.path] = module;
610
};

packages/app/src/app/overmind/effects/vscode/extensionHostWorker/common/fs.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { FileSystemConfiguration } from '../../../../../../../../../standalone-p
88
import { IModule } from '../../../../../../../../../standalone-packages/codesandbox-browserfs/dist/node/backend/CodeSandboxFS';
99
import { EXTENSIONS_LOCATION } from '../../constants';
1010
import {
11+
appendFile,
1112
mkdir,
1213
rename,
1314
rmdir,
@@ -173,6 +174,11 @@ export async function initializeBrowserFS({
173174
}
174175
break;
175176
}
177+
case 'append-file': {
178+
const module = evt.data.$data;
179+
appendFile(currentSandboxFs, module);
180+
break;
181+
}
176182
case 'write-file': {
177183
const module = evt.data.$data;
178184
writeFile(currentSandboxFs, module);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ export const moduleCreated: AsyncAction<{
520520
}
521521
}
522522

523-
effects.vscode.sandboxFsSync.writeFile(state.editor.modulesByPath, module);
523+
effects.vscode.sandboxFsSync.appendFile(state.editor.modulesByPath, module);
524524
actions.editor.internal.setCurrentModule(module);
525525

526526
try {

standalone-packages/codesandbox-browserfs/src/core/FS.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ export default class FS {
634634
}
635635
setImmediate(() => {
636636
this.stat(filename, (err, stat) => {
637-
this.fileWatcher.triggerWatch(filename, 'change', stat);
637+
this.fileWatcher.triggerWatch(filename, 'rename', stat);
638638
});
639639
});
640640
assertRoot(this.root).appendFile(normalizePath(filename), data, options.encoding, flag, options.mode, newCb);

standalone-packages/codesandbox-browserfs/src/core/file_watcher.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Typing info only.
22
import * as _fs from 'fs';
3+
34
import Stats from './node_fs_stats';
45

56
const EventEmitter = require('events');

0 commit comments

Comments
 (0)