1
1
import { BFSRequire , configure } from 'browserfs' ;
2
2
import { ApiError } from 'browserfs/dist/node/core/api_error' ;
3
+ import { FSModule } from 'browserfs/dist/node/core/FS' ;
3
4
import { Store } from 'redux' ;
4
5
6
+ import { OverallState } from '../../commons/application/ApplicationTypes' ;
5
7
import { setInBrowserFileSystem } from '../../commons/fileSystem/FileSystemActions' ;
6
- import { BASE_PLAYGROUND_FILE_PATH } from '../playground/Playground ' ;
8
+ import { EditorTabState , WorkspaceManagerState } from '../../commons/workspace/WorkspaceTypes ' ;
7
9
8
- export const createInBrowserFileSystem = ( store : Store ) => {
10
+ export const WORKSPACE_BASE_PATHS : Record < keyof WorkspaceManagerState , string > = {
11
+ assessment : '/assessment' ,
12
+ githubAssessment : '/githubAssessment' ,
13
+ grading : '/grading' ,
14
+ playground : '/playground' ,
15
+ sicp : '/sicp' ,
16
+ sourcecast : '/sourcecast' ,
17
+ sourcereel : '/sourcereel'
18
+ } ;
19
+
20
+ export const createInBrowserFileSystem = ( store : Store < OverallState > ) => {
9
21
configure (
10
22
{
11
23
fs : 'MountableFileSystem' ,
12
24
options : {
13
- [ BASE_PLAYGROUND_FILE_PATH ] : {
25
+ [ WORKSPACE_BASE_PATHS . playground ] : {
14
26
fs : 'IndexedDB' ,
15
27
options : {
16
28
storeName : 'playground'
@@ -22,7 +34,42 @@ export const createInBrowserFileSystem = (store: Store) => {
22
34
if ( err ) {
23
35
console . error ( err ) ;
24
36
}
25
- store . dispatch ( setInBrowserFileSystem ( BFSRequire ( 'fs' ) ) ) ;
37
+ // Set the browser file system reference in the Redux store for retrieval
38
+ // in other parts of the codebase.
39
+ const fileSystem = BFSRequire ( 'fs' ) ;
40
+ store . dispatch ( setInBrowserFileSystem ( fileSystem ) ) ;
41
+ // Create files for editor tabs if they do not exist. This can happen when
42
+ // editor tabs are initialised from the Redux store defaults, as opposed to
43
+ // being created by the user.
44
+ const workspaceStates = store . getState ( ) . workspaces ;
45
+ Object . entries ( workspaceStates ) . forEach ( ( [ _workspaceLocation , workspaceState ] ) => {
46
+ const editorTabs = workspaceState . editorTabs ;
47
+ createFilesForEditorTabs ( fileSystem , editorTabs ) ;
48
+ } ) ;
26
49
}
27
50
) ;
28
51
} ;
52
+
53
+ // An EditorTabWithFile is an editor tab with an associated file path.
54
+ type EditorTabWithFile = EditorTabState & {
55
+ filePath : string ;
56
+ } ;
57
+
58
+ const createFilesForEditorTabs = ( fileSystem : FSModule , editorTabs : EditorTabState [ ] ) => {
59
+ const editorTabsWithFile = editorTabs . filter (
60
+ ( editorTab ) : editorTab is EditorTabWithFile => editorTab . filePath !== undefined
61
+ ) ;
62
+ editorTabsWithFile . forEach ( editorTab => {
63
+ fileSystem . exists ( editorTab . filePath , filePathExists => {
64
+ if ( filePathExists ) {
65
+ return ;
66
+ }
67
+
68
+ fileSystem . writeFile ( editorTab . filePath , editorTab . value , err => {
69
+ if ( err ) {
70
+ console . error ( err ) ;
71
+ }
72
+ } ) ;
73
+ } ) ;
74
+ } ) ;
75
+ } ;
0 commit comments