-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
generate.ts
38 lines (32 loc) · 1.26 KB
/
generate.ts
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
import { FileType, GenerateConfig } from './types';
import { join } from 'path';
import { Position, Uri, window, workspace, WorkspaceEdit } from 'vscode';
export async function generateResources(config: GenerateConfig) {
workspace.fs.createDirectory(Uri.file(config.path));
const edit = new WorkspaceEdit();
for (const resource of config.resources) {
const ext = resource.type === FileType.PAGE ? config.pageExtension : config.scriptExtension;
const filepath = join(config.path, `${resource.filename}.${ext}`);
const uri = Uri.file(filepath);
edit.createFile(uri, {
overwrite: false,
ignoreIfExists: true
});
const data = await resource.generate(config);
edit.insert(uri, new Position(0, 0), data);
}
await workspace.applyEdit(edit);
// save documents and open the first
await Promise.all(
edit.entries().map(async ([uri], i) => {
const doc = workspace.textDocuments.find((t) => t.uri.path === uri.path);
if (doc) {
await doc?.save();
if (i === 0) {
await workspace.openTextDocument(uri);
await window.showTextDocument(doc);
}
}
})
);
}