-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfilesystem.tsx
205 lines (190 loc) · 6.15 KB
/
filesystem.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import React, { useState, useMemo } from 'react';
import ReactDOM from 'react-dom/client';
import { AppRenderer, BrowserFSFileType as FileType, IAppRendererProps, Uri , requireModule} from '@codeblitzjs/ide-core';
import '@codeblitzjs/ide-core/bundle/codeblitz.css';
import '@codeblitzjs/ide-core/languages';
import Select from 'antd/lib/select';
import 'antd/lib/select/style/index.css';
import { Button } from 'antd';
const path = requireModule('path');
const fse = requireModule('fs-extra');
const dirMap: Record<string, [string, FileType][]> = {
'/': [
['lib', FileType.DIRECTORY],
['Readme.md', FileType.FILE],
['LICENSE', FileType.FILE],
['package.json', FileType.FILE],
],
'/lib': [
['application.js', FileType.FILE],
['context.js', FileType.FILE],
['request.js', FileType.FILE],
['response.js', FileType.FILE],
],
};
const FileService = requireModule('@opensumi/ide-file-service');
const { IFileServiceClient } = FileService;
let zipData: Buffer;
const zipDataPromise = (async () => {
const res = await fetch(
'http://alipay-rmsdeploy-image.cn-hangzhou.alipay.aliyun-inc.com/green-trail-test/dc85f34d-2467-436b-a0fe-092133ead0d6/demo.zip'
);
const buf = await res.arrayBuffer();
zipData = Buffer.from(new Uint8Array(buf));
})();
const workspaceDir = 'filesystem';
const App = () => {
const [fsType, setFsType] = useState<string>('FileIndexSystem');
const filesystem = useMemo<
NonNullable<IAppRendererProps['runtimeConfig']['workspace']>['filesystem'] | undefined
>(() => {
switch (fsType) {
case 'IndexedDB':
return {
fs: 'IndexedDB',
options: {
storeName: 'my_db',
},
};
case 'InMemory':
return {
fs: 'InMemory',
};
case 'FileIndexSystem':
return {
fs: 'FileIndexSystem',
options: {
// 初始全量文件索引
requestFileIndex() {
return Promise.resolve({
'main.html': '<div id="root"></div>',
'main.css': 'body {}',
'main.js': 'console.log("main")',
'package.json': '{\n "name": "Codeblitz"\n}',
'readme.md': '# Codeblitz',
});
},
},
};
case 'DynamicRequest':
return {
fs: 'DynamicRequest',
options: {
readDirectory(p: string) {
return dirMap[p];
},
async readFile(p) {
const res = await fetch(
`http://alipay-rmsdeploy-image.cn-hangzhou.alipay.aliyun-inc.com/green-trail-test/a87fb80d-3028-4b19-93a9-2da6f871f369/koa${p}`
);
return Buffer.from(await res.arrayBuffer());
},
},
};
case 'ZipFS':
return {
fs: 'ZipFS',
options: {
zipData,
},
};
case 'FolderAdapter':
return {
fs: 'FolderAdapter',
options: {
folder: '/demo',
wrapped: {
fs: 'ZipFS',
options: {
zipData,
},
},
},
};
case 'OverlayFS':
return {
fs: 'OverlayFS',
options: {
writable: { fs: 'InMemory' },
readable: {
fs: 'ZipFS',
options: {
zipData,
},
},
},
};
}
}, [fsType]);
const workspace = filesystem ? { filesystem } : undefined;
return (
<div style={{ height: '100%' }}>
<div style={{ height: 48, display: 'flex', alignItems: 'center' }}>
<Select<string> onChange={(e) => setFsType(e)} style={{ width: 200 }} value={fsType}>
<Select.Option value="IndexedDB">IndexedDB</Select.Option>
<Select.Option value="InMemory">InMemory</Select.Option>
<Select.Option value="FileIndexSystem">FileIndexSystem</Select.Option>
<Select.Option value="DynamicRequest">DynamicRequest</Select.Option>
<Select.Option value="ZipFS">ZipFS</Select.Option>
<Select.Option value="FolderAdapter">FolderAdapter</Select.Option>
<Select.Option value="OverlayFS">OverlayFS</Select.Option>
</Select>
{
fsType === 'FileIndexSystem' && (
<Button onClick={async () => {
// All files in codeblitz are mounted to /workspace/${workspaceDir}
const targetFile = path.join('/workspace', workspaceDir, 'readme.md');
const content = await fse.readFile(targetFile);
await fse.writeFile(targetFile, content.toString() +'\n\nThis is a demo file.', { encoding: 'utf8' });
}}>
修改 readme.md
</Button>
)
}
</div>
<div style={{ height: 'calc(100% - 48px)' }}>
<AppRenderer
key={fsType}
onLoad={app=> {
window.app = app;
const fileService = app.injector.get(IFileServiceClient)
fileService.onFilesChanged(async e => {
// 获取新建的文件
const createFiles = e.filter(f => {
// FileChangeType
// UPDATED = 0,
// ADDED = 1,
// DELETED = 2
if(f.type ===1){
return f;
}
})
const promiseAll: Promise<any>[] = [];
createFiles.map( file => {
const uri = file.uri;
promiseAll.push(fileService.readFile(uri));
})
await Promise.all(promiseAll).then(res => {
res.map(r => {
console.log(r.content.toString())
})
})
});
}}
appConfig={{
workspaceDir,
defaultPreferences: {
'general.theme': 'opensumi-design-light-theme',
},
}}
runtimeConfig={{
workspace,
}}
/>
</div>
</div>
);
};
zipDataPromise.then(() => {
ReactDOM.createRoot(document.getElementById('main')!).render(<App />);
});