var fs = require('fs') const YAML = require('json-to-pretty-yaml'); const TurndownService = require('turndown'); var turndownService = new TurndownService() const getDirectories = source => fs.readdirSync(source, { withFileTypes: true }) .filter(item => item.isDirectory()) .map(item => item.name); var source = "/Users/james/codex/projects" var projects = getDirectories(source); var project_data = [] projects.forEach(project => { try { project_data.push(JSON.parse(fs.readFileSync(`${source}/${project}/project.json`))); } catch(err){} }); var createProject = function(project){ // Don't copy private projects if(project.visibility == "private") return; // Copy thumbnail var cover_path = `${source}/${project.permalink}/${project.cover_image}` var cover_ext = project.cover_image.split(".")[1]; var thumbnail_destination = `source/images/thumbnails/projects/${project.permalink}.${cover_ext}` try { fs.copyFileSync(cover_path, thumbnail_destination); } catch (err){ if(err.code == 'ENOENT'){ console.log(`> ERROR: File not found! (\'${cover_path}\')`) } else { console.log(err); } return; } // Create markdown file var file = ""; file += "---\n"; file += `title: ${project.title}\n`; file += `date: ${project.start_date}\n`; file += `layout: project\n` file += `cover: /images/thumbnails/projects/${project.permalink}.${cover_ext}\n`; try { var data = JSON.parse(JSON.stringify(project)); delete data.title; delete data.index; delete data.status; delete data.size delete data.cover_image; delete data.local_copies; delete data.folder; delete data.aws_backups; delete data.permalink; delete data.backup_folder; if(data.repositories == []) delete data.repositories; if(data.files == []) delete data.files; file += YAML.stringify(data); } catch (err){ console.log(err); } file += "---\n"; if(project.text == undefined){ file += project.description; } else { file += turndownService.turndown(project.text);; } // Save markdown file var project_file_path = `source/_posts/projects/${project.permalink}.md` try { fs.writeFileSync(project_file_path, file); console.log(`> Created file \'${project.permalink}.md\'`); counter += 1; } catch (err){ console.log(err); } } var counter = 0; project_data.forEach(project => { createProject(project); }); console.log(`Copied ${counter}/${project_data.length} projects`);