diff --git a/src/config.js b/src/config.js index 3703012..899692a 100644 --- a/src/config.js +++ b/src/config.js @@ -1,3 +1,6 @@ +const fs = require("fs"); +const path = require('path'); + const config = { dev: { postsdir: "./content", @@ -8,4 +11,43 @@ const config = { } }; -module.exports = config; + +//chelc if it's just null or whitespace +function isNullOrWhitespace( input ) { + return !input || !input.trim(); +} + +//if config JSON file exist, save the content to config module +const setConfig = (argvConfig) => { + if (!fs.existsSync(argvConfig) || path.extname(argvConfig)!=".JSON") { + console.log("It's not an existing JSON file."); + return 0; + } + + //read the parse the config file + const configString = fs.readFileSync(argvConfig).toString(); + try { + var temptConfig = JSON.parse(configString); + }catch(err) { + console.log("Please make sure the JSON file is valid. Encountered an issue: "+err); + return 0; + } + + //checking if each option exist from the config file before assiging to confgi module + if(!isNullOrWhitespace(temptConfig.postsdir)) config.dev.postsdir = temptConfig.postsdir; + if(!isNullOrWhitespace(temptConfig.inputPath)) config.dev.inputPath = temptConfig.inputPath; + if(!isNullOrWhitespace(temptConfig.outdir)) config.dev.outdir = temptConfig.outdir; + if(!isNullOrWhitespace(temptConfig.lang)) config.dev.lang= temptConfig.lang; + if(!isNullOrWhitespace(temptConfig.stylesheet)) config.dev.stylesheet= temptConfig.stylesheet; + + return true; +} + + + + + +module.exports = { + config: config, + setConfig: setConfig +}; diff --git a/src/homepage.js b/src/homepage.js index a2fccf4..df3c260 100644 --- a/src/homepage.js +++ b/src/homepage.js @@ -1,4 +1,5 @@ -const config = require("./config"); +const configModule = require("./config"); +const config = configModule.config; const fs = require("fs"); //const postMethods = require("./posts"); diff --git a/src/index.js b/src/index.js index 72e4fa7..ee56623 100644 --- a/src/index.js +++ b/src/index.js @@ -1,49 +1,23 @@ -#!/usr/bin/env node +#!/usr/bin/env nodedev const args = process.argv; const fs = require("fs"); const path = require('path'); const postMethods = require("./posts"); -const config = require("./config"); +const configModule = require("./config"); +const config = configModule.config; const addHomePage = require("./homepage"); const minArgv = require('minimist')(process.argv.slice(2)); -//chelc if it's just null or whitespace -function isNullOrWhitespace( input ) { - return !input || !input.trim(); -} - -//if config JSON file exist, save the content to config module -if(minArgv.config || minArgv.c){ - let argvConfig=minArgv.config || minArgv.c; - if (!fs.existsSync(argvConfig) || path.extname(argvConfig)!=".JSON") { - console.log("It's not an existing JSON file."); - return 0; - } - - //read the parse the config file - const configString = fs.readFileSync(argvConfig).toString(); - try { - var temptConfig = JSON.parse(configString); - }catch(err) { - console.log("Please make sure the JSON file is valid. Encountered an issue: "+err); - return 0; - } - - //checking if each option exist from the config file before assiging to confgi module - if(!isNullOrWhitespace(temptConfig.postsdir)) config.dev.postsdir = temptConfig.postsdir; - if(!isNullOrWhitespace(temptConfig.inputPath)) config.dev.inputPath = temptConfig.inputPath; - if(!isNullOrWhitespace(temptConfig.outdir)) config.dev.outdir = temptConfig.outdir; - if(!isNullOrWhitespace(temptConfig.lang)) config.dev.lang= temptConfig.lang; - if(!isNullOrWhitespace(temptConfig.stylesheet)) config.dev.stylesheet= temptConfig.stylesheet; - - var configFile=true;//flag for using config file - -} +var configFile=false; +if(minArgv.config || minArgv.c) { + //flag for using config file + configFile=configModule.setConfig(minArgv.config || minArgv.c); +} const posts = fs .readdirSync(config.dev.postsdir) @@ -51,10 +25,9 @@ const posts = fs .map(post => postMethods.createPost(post)) - -//if config file is used, and the file path is valid, parse the input files + //if config file is used, and the file path is valid, parse the input files if(configFile && fs.existsSync(config.dev.postsdir)) { - + //if it's a single .txt or .md file if (path.extname(config.dev.inputPath) == ".txt") { let p=path.join(config.dev.postsdir,config.dev.inputPath); diff --git a/src/posts.js b/src/posts.js index 9ba29ac..94fa2e2 100644 --- a/src/posts.js +++ b/src/posts.js @@ -1,4 +1,5 @@ -const config = require("./config"); +const configModule = require("./config"); +const config = configModule.config; const fm = require("front-matter"); const fs = require("fs"); const marked = require("./marked"); @@ -143,6 +144,4 @@ module.exports = { createPost: createPost, createPosts: createPosts, createSingle: createSingle, - setStyle:setStyle, - setLang:setLang };