|
@@ -1,51 +1,98 @@
|
|
|
var fs = require('fs')
|
|
|
+
|
|
|
+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()
|
|
|
|
|
|
-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 => {
|
|
|
+var project_directories = utilities.getDirectories(source);
|
|
|
+var projects = []
|
|
|
+
|
|
|
+// Load Files into JSON object
|
|
|
+project_directories.forEach(project => {
|
|
|
try {
|
|
|
- project_data.push(JSON.parse(fs.readFileSync(`${source}/${project}/project.json`)));
|
|
|
+ projects.push(JSON.parse(fs.readFileSync(`${source}/${project}/project.json`)));
|
|
|
} catch(err){}
|
|
|
});
|
|
|
|
|
|
-var copyFile = function(original_path, destination_path){
|
|
|
- try {
|
|
|
- fs.copyFileSync(original_path, destination_path);
|
|
|
- return true;
|
|
|
- } catch (err){
|
|
|
- if(err.code == 'ENOENT'){
|
|
|
- console.log(`> ERROR: File not found! (\'${original_path}\')`)
|
|
|
- } else {
|
|
|
- console.log(err);
|
|
|
- }
|
|
|
- return false;
|
|
|
+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){
|
|
|
- // Don't copy private projects
|
|
|
- if(project.visibility == "private") return;
|
|
|
+ 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;
|
|
|
+ }
|
|
|
// 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}`
|
|
|
- if(!copyFile(cover_path, thumbnail_destination)) return;
|
|
|
+ if(!utilities.copyFile(cover_path, thumbnail_destination)) return;
|
|
|
// 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/images/hero/projects/${project.permalink}.${hero_ext}`
|
|
|
- copyFile(hero_path, hero_destination);
|
|
|
+ utilities.copyFile(hero_path, hero_destination);
|
|
|
}
|
|
|
// Copy screenshots
|
|
|
var screenshot_data = []
|
|
@@ -56,7 +103,7 @@ var createProject = function(project){
|
|
|
screenshot_path = `${source}/${project.permalink}/${screenshot.url}`
|
|
|
screenshot_ext = project.hero_image.split(".")[1];
|
|
|
screenshot_destination = `/images/${project.permalink}-screenshot-${i}.${screenshot_ext}`
|
|
|
- copyFile(screenshot_path, 'source' + screenshot_destination);
|
|
|
+ utilities.copyFile(screenshot_path, 'source' + screenshot_destination);
|
|
|
screenshot.url = screenshot_destination;
|
|
|
screenshot_data.push(screenshot)
|
|
|
}
|
|
@@ -90,7 +137,8 @@ var createProject = function(project){
|
|
|
if(screenshot_data.length > 0) data.screenshots = screenshot_data;
|
|
|
file += YAML.stringify(data);
|
|
|
} catch (err){
|
|
|
- console.log(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){
|
|
@@ -102,17 +150,52 @@ var createProject = function(project){
|
|
|
var project_file_path = `source/_posts/projects/${project.permalink}.md`
|
|
|
try {
|
|
|
fs.writeFileSync(project_file_path, file);
|
|
|
- console.log(`> Created file \'${project.permalink}.md\'`);
|
|
|
+ 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){
|
|
|
- console.log(err);
|
|
|
+ if(global.isVerbose) console.log(logSymbols.error, chalk.red('An error occured: '));
|
|
|
+ if(global.isVerbose) console.log(err);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
var counter = 0;
|
|
|
|
|
|
-module.exports = function(){
|
|
|
- project_data.forEach(project => {
|
|
|
+module.exports.all = function(){
|
|
|
+ projects.forEach(project => {
|
|
|
createProject(project);
|
|
|
});
|
|
|
- console.log(`Copied ${counter}/${project_data.length} projects`);
|
|
|
+ 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`));
|
|
|
}
|