123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- var fs = require('fs')
- const sharp = require('sharp');
- var utilities = require('./utilities');
- const chalk = require('chalk');
- const logSymbols = require('log-symbols');
- const YAML = require('json-to-pretty-yaml');
- const TurndownService = require('turndown');
- var turndownService = new TurndownService()
- var source = "/Users/james/codex/projects"
- var project_directories = utilities.getDirectories(source);
- var projects = []
- // Load Files into JSON object
- project_directories.forEach(project => {
- try {
- projects.push(JSON.parse(fs.readFileSync(`${source}/${project}/project.json`)));
- } catch(err){}
- });
- var hasRequirements = function(project){
- var requirements = {
- is_public : project.visibility == "private" || project.visibility == undefined,
- has_thumbnail : utilities.fileExists( `${source}/${project.permalink}/${project.cover_image}`),
- has_hero_img : utilities.fileExists(`${source}/${project.permalink}/${project.hero_image}`),
- has_description : project.description != "" && project.description != undefined,
- has_text : project.text != "" && project.text != undefined,
- has_videos : countVideos(project) > 0,
- has_screenshots : countScreenshots(project) > 0
- }
- var pass = ''
- if(!requirements.is_public) pass = 'no'
- else if(!requirements.has_thumbnail) pass = 'no'
- else if(!requirements.has_description) pass = 'no'
- else if(!requirements.has_hero_img && !requirements.has_videos) pass = 'no'
- else if(!requirements.has_text) pass = 'some'
- else if(!requirements.has_videos && !requirements.has_screenshots) pass = 'some'
- else pass = 'all';
- requirements.pass = pass;
- return requirements;
- }
- var requirementMessages = function(project, requirements){
- var warning_thumbnail = chalk.red(` - Missing the thumbnail image`);
- var warning_hero_img = chalk.red(` - Missing hero image or video`);
- var warning_description = chalk.red(` - Missing description`);
- var warning_text = chalk.yellow(` - Missing long text description`);
- var warning_screenshots = chalk.yellow(` - Missing screenshots`);
- if(global.isVerbose){
- if(!requirements.has_thumbnail) console.log(warning_thumbnail);
- if(!requirements.has_description) console.log(warning_description);
- if(!requirements.has_hero_img && !requirements.has_videos) console.log(warning_hero_img);
- if(!requirements.has_text) console.log(warning_text);
- if(!requirements.has_videos && !requirements.has_screenshots) console.log(warning_screenshots);
- }
- }
- var countVideos = function(project){
- if(project.videos != undefined){
- return project.videos.length;
- } else {
- return 0;
- }
- }
- var countScreenshots = function(project){
- if(project.screenshots != undefined){
- return project.screenshots.length;
- } else {
- return 0;
- }
- }
- var createProject = function(project){
- var requirements = hasRequirements(project);
- //console.log(`${project.title} ${hasRequirements}`)
- if(requirements.pass == 'no'){
- console.log(logSymbols.error, `Error creating project \'${chalk.bold(project.permalink)}\'`);
- requirementMessages(project, requirements);
- return;
- }
- // Create folder
- utilities.createDirectory(`source/_posts/projects/${project.permalink}`)
- // Generate cover
- var cover_path = `${source}/${project.permalink}/${project.cover_image}`
- var cover_ext = project.cover_image.split(".")[1];
- var cover_destination = `source/_posts/projects/${project.permalink}/cover.jpg`
- sharp(cover_path).resize(1000, 625).toFile(cover_destination);
- project.cover_image = `/projects/${project.permalink}/cover.jpg`
- // Generate thumbnail
- sharp(cover_path).resize(550, 344).toFile(`source/_posts/projects/${project.permalink}/thumbnail.jpg`);
- project.thumbnail_image = `/projects/${project.permalink}/thumbnail.jpg`
- // Copy Hero
- var hero_path, hero_ext, hero_destination;
- if(project.hero_image != undefined){
- hero_path = `${source}/${project.permalink}/${project.hero_image}`
- hero_ext = project.hero_image.split(".")[1];
- hero_destination = `source/_posts/projects/${project.permalink}/hero.${hero_ext}`
- utilities.copyFile(hero_path, hero_destination);
- project.hero_image = `/projects/${project.permalink}/hero.${hero_ext}`
- }
- // Copy screenshots
- var screenshot_data = []
- if(project.screenshots != undefined){
- if(project.screenshots.length > 0) {
- for (let i = 0; i < project.screenshots.length; i++) {
- var screenshot = project.screenshots[i];
- screenshot_path = `${source}/${project.permalink}/${screenshot.url}`
- screenshot_ext = project.hero_image.split(".")[1];
- screenshot_destination = `source/_posts/projects/${project.permalink}/screenshot-${i}.${screenshot_ext}`
- utilities.copyFile(screenshot_path, screenshot_destination);
- screenshot.url = `/projects/${project.permalink}/screenshot-${i}.${screenshot_ext}`;
- screenshot_data.push(screenshot)
- }
- }
- }
- // Create markdown file
- var file = "";
- file += "---\n";
- file += `title: ${project.title}\n`;
- file += `folder: ${project.permalink}\n`;
- file += `date: ${project.start_date}\n`;
- file += `layout: project\n`
- try {
- var data = JSON.parse(JSON.stringify(project));
- delete data.title;
- delete data.index;
- delete data.status;
- delete data.size
- 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;
- if(screenshot_data.length > 0) data.screenshots = screenshot_data;
- file += YAML.stringify(data);
- } catch (err){
- if(global.isVerbose) console.log(logSymbols.error, chalk.red('An error occured: '));
- if(global.isVerbose) 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);
- if(requirements.pass == 'all') {
- console.log(logSymbols.success, `Created Project \'${chalk.bold(project.permalink)}\'`);
- } else if(requirements.pass == 'some') {
- console.log(logSymbols.warning, `Created Project \'${chalk.bold(project.permalink)}\' with warnings`);
- requirementMessages(project, requirements);
- }
- counter += 1;
- } catch (err){
- if(global.isVerbose) console.log(logSymbols.error, chalk.red('An error occured: '));
- if(global.isVerbose) console.log(err);
- }
- }
- var counter = 0;
- module.exports.all = function(){
- projects.forEach(project => {
- createProject(project);
- });
- console.log(`Copied ${counter}/${projects.length} projects`);
- }
- module.exports.showRequirements = function(query){
- if(query != undefined) query = query.toLowerCase();
- var result = [ 0, 0, 0]
- console.log();
- projects.forEach(project => {
- if(project.title.toLowerCase() == query || project.permalink == query || query == ""){
- var requirements = hasRequirements(project);
- if(requirements.pass == 'all'){
- result[0] += 1;
- console.log(logSymbols.success,
- `Project \'${chalk.bold(project.permalink)}\' has all requirements`);
- } else if (requirements.pass == 'some') {
- result[1] += 1;
- console.log(logSymbols.warning,
- `Project \'${chalk.bold(project.permalink)}\' has some requirements`);
- } else if(requirements.pass == 'no') {
- result[2] += 1;
- console.log(logSymbols.error,
- `Project \'${chalk.bold(project.permalink)}\' doesn\'t have necessary requirements`);
- }
- requirementMessages(project, requirements);
- }
- });
- console.log();
- if(query == "")
- console.log(chalk.green(`${result[0]} passed`) + ", " + chalk.yellow(`${result[1]} passed with warnings`) + ", " + chalk.red(`${result[2]} missing requirements`));
- }
|