11const fs = require ( 'fs' ) ;
22const path = require ( 'path' ) ;
3+ const repo = '/Users/vitaliisemianchuk/Projects/javascript-questions-pro' ;
34
45// Get the input file path from command-line arguments
56const inputFilePath = process . argv [ 2 ] ;
@@ -20,20 +21,74 @@ try {
2021 process . exit ( 1 ) ;
2122}
2223
23- // Convert the array of objects to Markdown format
24- const markdownContent = data . map ( obj => {
24+ // Function to delete a folder and its contents
25+ function clearFolder ( folderPath ) {
26+ if ( fs . existsSync ( folderPath ) ) {
27+ fs . rmSync ( folderPath , { recursive : true , force : true } ) ;
28+ }
29+ fs . mkdirSync ( folderPath , { recursive : true } ) ;
30+ }
31+
32+ // Function to create folders and write files
33+ function writeToFile ( folderPath , fileName , content ) {
34+ if ( ! fs . existsSync ( folderPath ) ) {
35+ fs . mkdirSync ( folderPath , { recursive : true } ) ;
36+ }
37+ const filePath = path . join ( folderPath , fileName ) ;
38+ fs . writeFileSync ( filePath , content , 'utf8' ) ;
39+ }
40+
41+ // Generate Markdown content for a question
42+ function generateMarkdown ( obj ) {
2543 const titleWithLink = obj . link ? `[${ obj . title } ](${ obj . link } )` : obj . title ;
2644 const tags = obj . level && obj . theme ? `**Tags**: ${ obj . level } , ${ obj . theme } ` : '' ;
2745 const url = obj . url ? `**URL**: [${ obj . url } ](${ obj . url } )` : '' ;
2846 return `## ${ titleWithLink } \n\n${ obj . text } \n\n${ tags } \n\n${ url } \n` ;
29- } ) . join ( '\n---\n\n' ) ;
47+ }
3048
31- // Save the Markdown content to a .md file
32- const outputFilePath = path . resolve ( __dirname , 'output.md' ) ;
49+ // Clear and recreate the level folder
50+ const levelsFolder = path . resolve ( __dirname , `${ repo } /level` ) ;
51+ clearFolder ( levelsFolder ) ;
52+ data . forEach ( obj => {
53+ if ( obj . level ) {
54+ const levelFolderPath = path . join ( levelsFolder , obj . level ) ;
55+ const content = generateMarkdown ( obj ) ;
56+ writeToFile ( levelFolderPath , `${ obj . title . replace ( / [ ^ a - z 0 - 9 ] + / gi, '_' ) } .md` , content ) ;
57+ }
58+ } ) ;
59+
60+ // Clear and recreate the theme folder
61+ const themesFolder = path . resolve ( __dirname , `${ repo } /theme` ) ;
62+ clearFolder ( themesFolder ) ;
63+ data . forEach ( obj => {
64+ if ( obj . theme ) {
65+ const themes = obj . theme . split ( ',' ) . map ( theme => theme . trim ( ) ) ;
66+ themes . forEach ( theme => {
67+ const themeFolderPath = path . join ( themesFolder , theme ) ;
68+ const content = generateMarkdown ( obj ) ;
69+ writeToFile ( themeFolderPath , `${ obj . title . replace ( / [ ^ a - z 0 - 9 ] + / gi, '_' ) } .md` , content ) ;
70+ } ) ;
71+ }
72+ } ) ;
73+
74+ // Clear and recreate the video folder
75+ const videoFolder = path . resolve ( __dirname , `${ repo } /video` ) ;
76+ clearFolder ( videoFolder ) ;
77+ data . forEach ( obj => {
78+ if ( obj . url && obj . url . includes ( 'tiktok' ) ) {
79+ const content = generateMarkdown ( obj ) ;
80+ writeToFile ( videoFolder , `${ obj . title . replace ( / [ ^ a - z 0 - 9 ] + / gi, '_' ) } .md` , content ) ;
81+ }
82+ } ) ;
83+
84+ // Save the Markdown content to a README.md file
85+ const outputFilePath = path . resolve ( __dirname , './repo/README.md' ) ;
3386try {
34- fs . writeFileSync ( outputFilePath , markdownContent , 'utf8' ) ;
87+ fs . writeFileSync ( outputFilePath , generateMarkdown , 'utf8' ) ;
3588 console . log ( `Markdown file has been saved to ${ outputFilePath } ` ) ;
3689} catch ( err ) {
3790 console . error ( `Failed to write to file at ${ outputFilePath } :` , err . message ) ;
3891 process . exit ( 1 ) ;
3992}
93+
94+ console . log ( 'Folders and files have been generated successfully.' ) ;
0 commit comments