-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathtutorialkit.add.ts
More file actions
79 lines (59 loc) · 2.3 KB
/
tutorialkit.add.ts
File metadata and controls
79 lines (59 loc) · 2.3 KB
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
import { cmd } from '.';
import { Node, NodeType } from '../models/Node';
import * as vscode from 'vscode';
import { FILES_FOLDER, SOLUTION_FOLDER } from '../models/tree/constants';
import { updateNodeMetadataInVFS } from '../models/tree/update';
let kebabCase: (string: string) => string;
let capitalize: (string: string) => string;
(async () => {
const module = await import('case-anything');
kebabCase = module.kebabCase;
capitalize = module.capitalCase;
})();
export async function addLesson(parent: Node) {
const { folderPath, metaFilePath } = await createNodeFolder(parent, 'lesson');
await vscode.workspace.fs.createDirectory(vscode.Uri.joinPath(folderPath, FILES_FOLDER));
await vscode.workspace.fs.createDirectory(vscode.Uri.joinPath(folderPath, SOLUTION_FOLDER));
await cmd.refresh();
return cmd.goto(metaFilePath);
}
export async function addChapter(parent: Node) {
const { metaFilePath } = await createNodeFolder(parent, 'chapter');
await cmd.refresh();
return cmd.goto(metaFilePath);
}
export async function addPart(parent: Node) {
const { metaFilePath } = await createNodeFolder(parent, 'part');
await cmd.refresh();
return cmd.goto(metaFilePath);
}
async function getNodeName(unitType: NodeType, unitNumber: number) {
const unitName = await vscode.window.showInputBox({
prompt: `Enter the name of the new ${unitType}`,
value: `${capitalize(unitType)} ${unitNumber}`,
});
// break if no name provided
if (!unitName) {
throw new Error(`No ${unitType} name provided`);
}
return unitName;
}
async function createNodeFolder(parent: Node, nodeType: NodeType) {
const unitNumber = parent.children.length + 1;
const unitName = await getNodeName(nodeType, unitNumber);
const unitFolderPath = parent.order ? kebabCase(unitName) : `${unitNumber}-${kebabCase(unitName)}`;
const metaFile = nodeType === 'lesson' ? 'content.mdx' : 'meta.md';
const metaFilePath = vscode.Uri.joinPath(parent.path, unitFolderPath, metaFile);
if (parent.order) {
parent.pushChild(unitFolderPath);
await updateNodeMetadataInVFS(parent);
}
await vscode.workspace.fs.writeFile(
metaFilePath,
new TextEncoder().encode(`---\ntype: ${nodeType}\ntitle: ${unitName}\n---\n`),
);
return {
folderPath: vscode.Uri.joinPath(parent.path, unitFolderPath),
metaFilePath,
};
}