fetch-projects.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. var fs = require('fs')
  2. const sharp = require('sharp');
  3. var utilities = require('./utilities');
  4. const chalk = require('chalk');
  5. const logSymbols = require('log-symbols');
  6. const YAML = require('json-to-pretty-yaml');
  7. const TurndownService = require('turndown');
  8. var turndownService = new TurndownService()
  9. var source = "/Users/james/codex/projects"
  10. var project_directories = utilities.getDirectories(source);
  11. var projects = []
  12. // Load Files into JSON object
  13. project_directories.forEach(project => {
  14. try {
  15. projects.push(JSON.parse(fs.readFileSync(`${source}/${project}/project.json`)));
  16. } catch(err){}
  17. });
  18. var hasRequirements = function(project){
  19. var requirements = {
  20. is_public : project.visibility == "private" || project.visibility == undefined,
  21. has_thumbnail : utilities.fileExists( `${source}/${project.permalink}/${project.cover_image}`),
  22. has_hero_img : utilities.fileExists(`${source}/${project.permalink}/${project.hero_image}`),
  23. has_description : project.description != "" && project.description != undefined,
  24. has_text : project.text != "" && project.text != undefined,
  25. has_videos : countVideos(project) > 0,
  26. has_screenshots : countScreenshots(project) > 0
  27. }
  28. var pass = ''
  29. if(!requirements.is_public) pass = 'no'
  30. else if(!requirements.has_thumbnail) pass = 'no'
  31. else if(!requirements.has_description) pass = 'no'
  32. else if(!requirements.has_hero_img && !requirements.has_videos) pass = 'no'
  33. else if(!requirements.has_text) pass = 'some'
  34. else if(!requirements.has_videos && !requirements.has_screenshots) pass = 'some'
  35. else pass = 'all';
  36. requirements.pass = pass;
  37. return requirements;
  38. }
  39. var requirementMessages = function(project, requirements){
  40. var warning_thumbnail = chalk.red(` - Missing the thumbnail image`);
  41. var warning_hero_img = chalk.red(` - Missing hero image or video`);
  42. var warning_description = chalk.red(` - Missing description`);
  43. var warning_text = chalk.yellow(` - Missing long text description`);
  44. var warning_screenshots = chalk.yellow(` - Missing screenshots`);
  45. if(global.isVerbose){
  46. if(!requirements.has_thumbnail) console.log(warning_thumbnail);
  47. if(!requirements.has_description) console.log(warning_description);
  48. if(!requirements.has_hero_img && !requirements.has_videos) console.log(warning_hero_img);
  49. if(!requirements.has_text) console.log(warning_text);
  50. if(!requirements.has_videos && !requirements.has_screenshots) console.log(warning_screenshots);
  51. }
  52. }
  53. var countVideos = function(project){
  54. if(project.videos != undefined){
  55. return project.videos.length;
  56. } else {
  57. return 0;
  58. }
  59. }
  60. var countScreenshots = function(project){
  61. if(project.screenshots != undefined){
  62. return project.screenshots.length;
  63. } else {
  64. return 0;
  65. }
  66. }
  67. var createProject = function(project){
  68. var requirements = hasRequirements(project);
  69. //console.log(`${project.title} ${hasRequirements}`)
  70. if(requirements.pass == 'no'){
  71. console.log(logSymbols.error, `Error creating project \'${chalk.bold(project.permalink)}\'`);
  72. requirementMessages(project, requirements);
  73. return;
  74. }
  75. // Create folder
  76. utilities.createDirectory(`source/_posts/projects/${project.permalink}`)
  77. // Generate cover
  78. var cover_path = `${source}/${project.permalink}/${project.cover_image}`
  79. var cover_ext = project.cover_image.split(".")[1];
  80. var cover_destination = `source/_posts/projects/${project.permalink}/cover.jpg`
  81. sharp(cover_path).resize(1000, 625).toFile(cover_destination);
  82. project.cover_image = `/projects/${project.permalink}/cover.jpg`
  83. // Generate thumbnail
  84. sharp(cover_path).resize(550, 344).toFile(`source/_posts/projects/${project.permalink}/thumbnail.jpg`);
  85. project.thumbnail_image = `/projects/${project.permalink}/thumbnail.jpg`
  86. // Copy Hero
  87. var hero_path, hero_ext, hero_destination;
  88. if(project.hero_image != undefined){
  89. hero_path = `${source}/${project.permalink}/${project.hero_image}`
  90. hero_ext = project.hero_image.split(".")[1];
  91. hero_destination = `source/_posts/projects/${project.permalink}/hero.${hero_ext}`
  92. utilities.copyFile(hero_path, hero_destination);
  93. project.hero_image = `/projects/${project.permalink}/hero.${hero_ext}`
  94. }
  95. // Copy screenshots
  96. var screenshot_data = []
  97. if(project.screenshots != undefined){
  98. if(project.screenshots.length > 0) {
  99. for (let i = 0; i < project.screenshots.length; i++) {
  100. var screenshot = project.screenshots[i];
  101. screenshot_path = `${source}/${project.permalink}/${screenshot.url}`
  102. screenshot_ext = project.hero_image.split(".")[1];
  103. screenshot_destination = `source/_posts/projects/${project.permalink}/screenshot-${i}.${screenshot_ext}`
  104. utilities.copyFile(screenshot_path, screenshot_destination);
  105. screenshot.url = `/projects/${project.permalink}/screenshot-${i}.${screenshot_ext}`;
  106. screenshot_data.push(screenshot)
  107. }
  108. }
  109. }
  110. // Create markdown file
  111. var file = "";
  112. file += "---\n";
  113. file += `title: ${project.title}\n`;
  114. file += `folder: ${project.permalink}\n`;
  115. file += `date: ${project.start_date}\n`;
  116. file += `layout: project\n`
  117. try {
  118. var data = JSON.parse(JSON.stringify(project));
  119. delete data.title;
  120. delete data.index;
  121. delete data.status;
  122. delete data.size
  123. delete data.local_copies;
  124. delete data.folder;
  125. delete data.aws_backups;
  126. delete data.permalink;
  127. delete data.backup_folder;
  128. if(data.repositories == []) delete data.repositories;
  129. if(data.files == []) delete data.files;
  130. if(screenshot_data.length > 0) data.screenshots = screenshot_data;
  131. file += YAML.stringify(data);
  132. } catch (err){
  133. if(global.isVerbose) console.log(logSymbols.error, chalk.red('An error occured: '));
  134. if(global.isVerbose) console.log(err);
  135. }
  136. file += "---\n";
  137. if(project.text == undefined){
  138. file += project.description;
  139. } else {
  140. file += turndownService.turndown(project.text);;
  141. }
  142. // Save markdown file
  143. var project_file_path = `source/_posts/projects/${project.permalink}.md`
  144. try {
  145. fs.writeFileSync(project_file_path, file);
  146. if(requirements.pass == 'all') {
  147. console.log(logSymbols.success, `Created Project \'${chalk.bold(project.permalink)}\'`);
  148. } else if(requirements.pass == 'some') {
  149. console.log(logSymbols.warning, `Created Project \'${chalk.bold(project.permalink)}\' with warnings`);
  150. requirementMessages(project, requirements);
  151. }
  152. counter += 1;
  153. } catch (err){
  154. if(global.isVerbose) console.log(logSymbols.error, chalk.red('An error occured: '));
  155. if(global.isVerbose) console.log(err);
  156. }
  157. }
  158. var counter = 0;
  159. module.exports.all = function(){
  160. projects.forEach(project => {
  161. createProject(project);
  162. });
  163. console.log(`Copied ${counter}/${projects.length} projects`);
  164. }
  165. module.exports.showRequirements = function(query){
  166. if(query != undefined) query = query.toLowerCase();
  167. var result = [ 0, 0, 0]
  168. console.log();
  169. projects.forEach(project => {
  170. if(project.title.toLowerCase() == query || project.permalink == query || query == ""){
  171. var requirements = hasRequirements(project);
  172. if(requirements.pass == 'all'){
  173. result[0] += 1;
  174. console.log(logSymbols.success,
  175. `Project \'${chalk.bold(project.permalink)}\' has all requirements`);
  176. } else if (requirements.pass == 'some') {
  177. result[1] += 1;
  178. console.log(logSymbols.warning,
  179. `Project \'${chalk.bold(project.permalink)}\' has some requirements`);
  180. } else if(requirements.pass == 'no') {
  181. result[2] += 1;
  182. console.log(logSymbols.error,
  183. `Project \'${chalk.bold(project.permalink)}\' doesn\'t have necessary requirements`);
  184. }
  185. requirementMessages(project, requirements);
  186. }
  187. });
  188. console.log();
  189. if(query == "")
  190. console.log(chalk.green(`${result[0]} passed`) + ", " + chalk.yellow(`${result[1]} passed with warnings`) + ", " + chalk.red(`${result[2]} missing requirements`));
  191. }