forked from mauricedb/presentation-buddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
presentation-buddy.ts
70 lines (58 loc) · 1.9 KB
/
presentation-buddy.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
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
import { window, workspace } from 'vscode';
import { join } from 'path';
import { jsonc } from 'jsonc';
import { Instruction, InstructionHandler } from './instructions';
import * as instructionHandlers from './instruction-handlers';
import { existsAsync, mkdirIfNotExists } from './utils';
export const init = async () => {
if (!workspace.workspaceFolders) {
return;
}
const workspaceFolder = workspace.workspaceFolders[0].uri.fsPath;
const json = await jsonc.read(
join(__dirname, '..', 'examples', 'init', 'instructions.json')
);
const dir = join(workspaceFolder, '.presentation-buddy');
const fileName = join(dir, 'instructions.json');
if (await existsAsync(fileName)) {
window.showWarningMessage(
`File ${fileName} exists: overwrite it?`, "Yes", "No"
).then(async answer => {
if (answer === "Yes") {
await jsonc.write(fileName, json, { space: 2 });
};
});
} else {
await mkdirIfNotExists(dir);
await jsonc.write(fileName, json, { space: 2 });
}
};
export const start = async () => {
if (!workspace.workspaceFolders) {
return;
}
const workspaceFolder = workspace.workspaceFolders[0].uri.fsPath;
const instructions = await loadInstructions(workspaceFolder);
let instruction = instructions.shift();
while (instruction) {
const handler = instructionHandlers[instruction.type] as InstructionHandler;
if (handler) {
await handler(instruction);
} else {
window.showErrorMessage(`Unkown instruction type '${instruction.type}'`);
}
instruction = instructions.shift();
}
console.log(instructions);
};
async function loadInstructions(
workspaceFolder: string
): Promise<Instruction[]> {
const path = join(
workspaceFolder,
'.presentation-buddy',
'instructions.json'
);
const instructions: Instruction[] = await jsonc.read(path);
return instructions.filter((instruction) => !instruction.skip);
}