fetch-projects.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. var fs = require('fs')
  2. const YAML = require('json-to-pretty-yaml');
  3. const TurndownService = require('turndown');
  4. var turndownService = new TurndownService()
  5. const getDirectories = source =>
  6. fs.readdirSync(source, { withFileTypes: true })
  7. .filter(item => item.isDirectory())
  8. .map(item => item.name);
  9. var source = "/Users/james/codex/projects"
  10. var projects = getDirectories(source);
  11. var project_data = []
  12. projects.forEach(project => {
  13. try {
  14. project_data.push(JSON.parse(fs.readFileSync(`${source}/${project}/project.json`)));
  15. } catch(err){}
  16. });
  17. var createProject = function(project){
  18. // Don't copy private projects
  19. if(project.visibility == "private") return;
  20. // Copy thumbnail
  21. var cover_path = `${source}/${project.permalink}/${project.cover_image}`
  22. var cover_ext = project.cover_image.split(".")[1];
  23. var thumbnail_destination = `source/images/thumbnails/projects/${project.permalink}.${cover_ext}`
  24. try {
  25. fs.copyFileSync(cover_path, thumbnail_destination);
  26. } catch (err){
  27. if(err.code == 'ENOENT'){
  28. console.log(`> ERROR: File not found! (\'${cover_path}\')`)
  29. } else {
  30. console.log(err);
  31. }
  32. return;
  33. }
  34. // Create markdown file
  35. var file = "";
  36. file += "---\n";
  37. file += `title: ${project.title}\n`;
  38. file += `date: ${project.start_date}\n`;
  39. file += `layout: project\n`
  40. file += `cover: /images/thumbnails/projects/${project.permalink}.${cover_ext}\n`;
  41. try {
  42. var data = JSON.parse(JSON.stringify(project));
  43. delete data.title;
  44. delete data.index;
  45. delete data.status;
  46. delete data.size
  47. delete data.cover_image;
  48. delete data.local_copies;
  49. delete data.folder;
  50. delete data.aws_backups;
  51. delete data.permalink;
  52. delete data.backup_folder;
  53. if(data.repositories == []) delete data.repositories;
  54. if(data.files == []) delete data.files;
  55. file += YAML.stringify(data);
  56. } catch (err){
  57. console.log(err);
  58. }
  59. file += "---\n";
  60. if(project.text == undefined){
  61. file += project.description;
  62. } else {
  63. file += turndownService.turndown(project.text);;
  64. }
  65. // Save markdown file
  66. var project_file_path = `source/_posts/projects/${project.permalink}.md`
  67. try {
  68. fs.writeFileSync(project_file_path, file);
  69. console.log(`> Created file \'${project.permalink}.md\'`);
  70. counter += 1;
  71. } catch (err){
  72. console.log(err);
  73. }
  74. }
  75. var counter = 0;
  76. project_data.forEach(project => {
  77. createProject(project);
  78. });
  79. console.log(`Copied ${counter}/${project_data.length} projects`);