1
1
import { cmd } from '.' ;
2
- import { Lesson , LessonType } from '../models/Lesson ' ;
2
+ import { Node , NodeType } from '../models/Node ' ;
3
3
import * as vscode from 'vscode' ;
4
+ import { FILES_FOLDER , SOLUTION_FOLDER } from '../models/tree/constants' ;
5
+ import { updateNodeMetadataInVFS } from '../models/tree/update' ;
4
6
5
7
let kebabCase : ( string : string ) => string ;
6
8
let capitalize : ( string : string ) => string ;
@@ -11,34 +13,34 @@ let capitalize: (string: string) => string;
11
13
capitalize = module . capitalCase ;
12
14
} ) ( ) ;
13
15
14
- export async function addLesson ( parent : Lesson ) {
15
- const lessonNumber = parent . children . length + 1 ;
16
+ export async function addLesson ( parent : Node ) {
17
+ const { folderPath , metaFilePath } = await createNodeFolder ( parent , 'lesson' ) ;
16
18
17
- const lessonName = await getUnitName ( 'lesson' , lessonNumber ) ;
18
-
19
- const lessonFolderPath = await createUnitFolder ( parent . path , lessonNumber , lessonName , 'lesson' ) ;
20
-
21
- await vscode . workspace . fs . createDirectory ( vscode . Uri . file ( `${ lessonFolderPath } /_files` ) ) ;
22
- await vscode . workspace . fs . createDirectory ( vscode . Uri . file ( `${ lessonFolderPath } /_solution` ) ) ;
19
+ await vscode . workspace . fs . createDirectory ( vscode . Uri . joinPath ( folderPath , FILES_FOLDER ) ) ;
20
+ await vscode . workspace . fs . createDirectory ( vscode . Uri . joinPath ( folderPath , SOLUTION_FOLDER ) ) ;
23
21
24
22
await cmd . refresh ( ) ;
25
23
26
- return navigateToUnit ( lessonFolderPath , 'lesson' ) ;
24
+ return cmd . goto ( metaFilePath ) ;
27
25
}
28
26
29
- export async function addChapter ( parent : Lesson ) {
30
- const chapterNumber = parent . children . length + 1 ;
27
+ export async function addChapter ( parent : Node ) {
28
+ const { metaFilePath } = await createNodeFolder ( parent , 'chapter' ) ;
31
29
32
- const chapterName = await getUnitName ( 'chapter' , chapterNumber ) ;
30
+ await cmd . refresh ( ) ;
33
31
34
- const chapterFolderPath = await createUnitFolder ( parent . path , chapterNumber , chapterName , 'chapter' ) ;
32
+ return cmd . goto ( metaFilePath ) ;
33
+ }
35
34
36
- await navigateToUnit ( chapterFolderPath , 'chapter' ) ;
35
+ export async function addPart ( parent : Node ) {
36
+ const { metaFilePath } = await createNodeFolder ( parent , 'part' ) ;
37
37
38
38
await cmd . refresh ( ) ;
39
+
40
+ return cmd . goto ( metaFilePath ) ;
39
41
}
40
42
41
- async function getUnitName ( unitType : LessonType , unitNumber : number ) {
43
+ async function getNodeName ( unitType : NodeType , unitNumber : number ) {
42
44
const unitName = await vscode . window . showInputBox ( {
43
45
prompt : `Enter the name of the new ${ unitType } ` ,
44
46
value : `${ capitalize ( unitType ) } ${ unitNumber } ` ,
@@ -52,20 +54,26 @@ async function getUnitName(unitType: LessonType, unitNumber: number) {
52
54
return unitName ;
53
55
}
54
56
55
- async function createUnitFolder ( parentPath : string , unitNumber : number , unitName : string , unitType : LessonType ) {
56
- const unitFolderPath = `${ parentPath } /${ unitNumber } -${ kebabCase ( unitName ) } ` ;
57
- const metaFile = unitType === 'lesson' ? 'content.mdx' : 'meta.md' ;
57
+ async function createNodeFolder ( parent : Node , nodeType : NodeType ) {
58
+ const unitNumber = parent . children . length + 1 ;
59
+ const unitName = await getNodeName ( nodeType , unitNumber ) ;
60
+ const unitFolderPath = parent . order ? kebabCase ( unitName ) : `${ unitNumber } -${ kebabCase ( unitName ) } ` ;
58
61
59
- await vscode . workspace . fs . writeFile (
60
- vscode . Uri . file ( `${ unitFolderPath } /${ metaFile } ` ) ,
61
- new TextEncoder ( ) . encode ( `---\ntype: ${ unitType } \ntitle: ${ unitName } \n---\n` ) ,
62
- ) ;
62
+ const metaFile = nodeType === 'lesson' ? 'content.mdx' : 'meta.md' ;
63
+ const metaFilePath = vscode . Uri . joinPath ( parent . path , unitFolderPath , metaFile ) ;
63
64
64
- return unitFolderPath ;
65
- }
65
+ if ( parent . order ) {
66
+ parent . pushChild ( unitFolderPath ) ;
67
+ await updateNodeMetadataInVFS ( parent ) ;
68
+ }
66
69
67
- async function navigateToUnit ( path : string , unitType : LessonType ) {
68
- const metaFile = unitType === 'lesson' ? 'content.mdx' : 'meta.md' ;
70
+ await vscode . workspace . fs . writeFile (
71
+ metaFilePath ,
72
+ new TextEncoder ( ) . encode ( `---\ntype: ${ nodeType } \ntitle: ${ unitName } \n---\n` ) ,
73
+ ) ;
69
74
70
- return cmd . goto ( `${ path } /${ metaFile } ` ) ;
75
+ return {
76
+ folderPath : vscode . Uri . joinPath ( parent . path , unitFolderPath ) ,
77
+ metaFilePath,
78
+ } ;
71
79
}
0 commit comments