11import { cmd } from '.' ;
2- import { Lesson , LessonType } from '../models/Lesson ' ;
2+ import { Node , NodeType } from '../models/Node ' ;
33import * as vscode from 'vscode' ;
4+ import { FILES_FOLDER , SOLUTION_FOLDER } from '../models/tree/constants' ;
5+ import { updateNodeMetadataInVFS } from '../models/tree/update' ;
46
57let kebabCase : ( string : string ) => string ;
68let capitalize : ( string : string ) => string ;
@@ -11,34 +13,34 @@ let capitalize: (string: string) => string;
1113 capitalize = module . capitalCase ;
1214} ) ( ) ;
1315
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' ) ;
1618
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 ) ) ;
2321
2422 await cmd . refresh ( ) ;
2523
26- return navigateToUnit ( lessonFolderPath , 'lesson' ) ;
24+ return cmd . goto ( metaFilePath ) ;
2725}
2826
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' ) ;
3129
32- const chapterName = await getUnitName ( 'chapter' , chapterNumber ) ;
30+ await cmd . refresh ( ) ;
3331
34- const chapterFolderPath = await createUnitFolder ( parent . path , chapterNumber , chapterName , 'chapter' ) ;
32+ return cmd . goto ( metaFilePath ) ;
33+ }
3534
36- await navigateToUnit ( chapterFolderPath , 'chapter' ) ;
35+ export async function addPart ( parent : Node ) {
36+ const { metaFilePath } = await createNodeFolder ( parent , 'part' ) ;
3737
3838 await cmd . refresh ( ) ;
39+
40+ return cmd . goto ( metaFilePath ) ;
3941}
4042
41- async function getUnitName ( unitType : LessonType , unitNumber : number ) {
43+ async function getNodeName ( unitType : NodeType , unitNumber : number ) {
4244 const unitName = await vscode . window . showInputBox ( {
4345 prompt : `Enter the name of the new ${ unitType } ` ,
4446 value : `${ capitalize ( unitType ) } ${ unitNumber } ` ,
@@ -52,20 +54,26 @@ async function getUnitName(unitType: LessonType, unitNumber: number) {
5254 return unitName ;
5355}
5456
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 ) } ` ;
5861
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 ) ;
6364
64- return unitFolderPath ;
65- }
65+ if ( parent . order ) {
66+ parent . pushChild ( unitFolderPath ) ;
67+ await updateNodeMetadataInVFS ( parent ) ;
68+ }
6669
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+ ) ;
6974
70- return cmd . goto ( `${ path } /${ metaFile } ` ) ;
75+ return {
76+ folderPath : vscode . Uri . joinPath ( parent . path , unitFolderPath ) ,
77+ metaFilePath,
78+ } ;
7179}
0 commit comments