From adf468785dd4d452a48ba6c3d7657b5282005b6d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jakub=20Sko=C5=99epa?= <jakub@skorepa.info>
Date: Thu, 7 Apr 2016 21:38:13 +0200
Subject: [PATCH] Option --themedir

---
 index.js              | 18 +++++++++---------
 sitegin/config.js     | 15 ++++++++++-----
 sitegin/gitInfo.js    |  4 +++-
 sitegin/markdown.js   |  1 +
 sitegin/nunjucks.js   | 17 ++++++++++-------
 sitegin/parseHugo.js  |  1 +
 sitegin/readFiles.js  |  1 +
 sitegin/sitemap.js    |  1 +
 sitegin/tags.js       |  1 +
 sitegin/theme.js      |  1 +
 sitegin/urls.js       |  1 +
 sitegin/writeFiles.js |  1 +
 12 files changed, 40 insertions(+), 22 deletions(-)

diff --git a/index.js b/index.js
index 3231a53a..dbc6dd87 100755
--- a/index.js
+++ b/index.js
@@ -51,19 +51,19 @@ var sitegin = function(config) {
       .catch(function(e) {
         isRunning = false;
         console.log('Generator crashed in',moment().diff(startTime,'seconds'),'seconds')
-        if(config.options.noserver) process.exit(13);
         if(runAgain) run();
         if(e.stack)
           console.log(e.stack)
         else
           console.log(e)
+        if(config.options.noserver) process.exit(13);
       });
     }
 
     console.log('Sitegin successfully loaded');
     run();
-    copyStaticFiles(config.builddir, config.staticDir);
-    rendersass(config.builddir);
+    copyStaticFiles(config.builddir, config.staticDir, config.themeDir);
+    rendersass(config.builddir, config.themeDir);
 
     if(!options.noserver) {
       var sync = require('browser-sync').create();
@@ -82,7 +82,7 @@ var sitegin = function(config) {
 
       var chokidar = require('chokidar');
       // article or theme reload
-      chokidar.watch([config.sourceDir,'theme/'], {ignoreInitial: true})
+      chokidar.watch([config.sourceDir,config.themeDir], {ignoreInitial: true})
       .on('all', function(event, path) {
         console.log('Content or theme changed. Rebuilding...');
         run();
@@ -101,7 +101,7 @@ var sitegin = function(config) {
   });
 }
 
-var copyStaticFiles = function(builddir, staticdir) {
+var copyStaticFiles = function(builddir, staticdir,themedir) {
   // STATIC FILES
   var fsextra = require('node-fs-extra');
   fsextra.copy(
@@ -116,19 +116,19 @@ var copyStaticFiles = function(builddir, staticdir) {
       function(){console.log('copy static/articles done');}
     );
   fsextra.copy(
-      'theme/static',
+      themedir+'/static',
       builddir,
       function(file){ return !file.match('\\.git') },
-      function(){console.log('copy theme/static done');}
+      function(){console.log('copy '+themedir+'/static done');}
     );
 }
 
-var rendersass = function(builddir) {
+var rendersass = function(builddir, themedir) {
   // SASS
   var sass = require('node-sass');
   var fs = require('fs');
 
-  sass.render ({file: 'theme/sass/style.scss'},function(err, result) {
+  sass.render ({file: themedir+'/sass/style.scss'},function(err, result) {
     if(err === null) {
       console.log('compiled sass');
       fs.writeFile(builddir+'/theme/style.css',result.css);
diff --git a/sitegin/config.js b/sitegin/config.js
index 23ddaa8e..e43f4ec1 100644
--- a/sitegin/config.js
+++ b/sitegin/config.js
@@ -13,15 +13,18 @@ var options = cli.parse({
  uiport: [null, 'BrowserSync UI port', 'number', 'port+1'],
  httpsonly: [null, 'Converts all http://ok1kvk.cz links to https'],
  httponly: [null, 'Converts all https://ok1kvk.cz links to http'],
- contentdir: [null, 'Allows to specify arbitrary content directory. '+
-  'Can also be http(s) url to zip file.', 'string', 'content'],
- staticdir: [null, 'Allows to specify arbitrary directory for static files. '+
-  'Can also be http(s) url to zip file.', 'string', 'static']
+ contentdir: [null, 'Allows to specify arbitrary content directory.', 'string', 'static'],
+ themedir: [null, 'Allows to specify arbitrary content directory.', 'string', 'theme'],
+ builddir: [null, 'Allows to specify arbitrary content directory.', 'string', null],
+ staticdir: [null, 'Allows to specify arbitrary directory for static files.', 'string', 'static']
 });
 
 module.exports = function() {
+  console.log('Build step: Config');
   var builddir = 'build';
-  if(!options.noserver) builddir = 'build-debug';
+  if(options.builddir)
+    builddir = options.builddir;
+  else if(!options.noserver) builddir = 'build-debug';
 
   if(options.uiport == 'port+1') {
     options.uiport = options.port+1;
@@ -34,6 +37,7 @@ module.exports = function() {
         builddir: builddir,
         sourceDir: options.contentdir,
         staticDir: options.staticdir,
+        themeDir: options.themedir,
         articlesLocation: 'articles',
         linksPerPage: 6
       }});
@@ -42,3 +46,4 @@ module.exports = function() {
   })
 }
 module.exports.watch = !options.noserver;
+module.exports.themeDir = options.themedir;
diff --git a/sitegin/gitInfo.js b/sitegin/gitInfo.js
index 867cac52..e27c4721 100644
--- a/sitegin/gitInfo.js
+++ b/sitegin/gitInfo.js
@@ -7,7 +7,7 @@ var nodegit = require('nodegit');
 var path = require('path');
 var moment = require('moment');
 
-var pathToRepo = path.resolve('content');
+var pathToRepo;
 
 // Returns promise for patch of single commit
 var gitShow = function(repo, commitData) {
@@ -116,6 +116,8 @@ var getFilesHistory = function() {
 }
 
 module.exports = function(obj) {
+  console.log('Build step: GitInfo');
+  pathToRepo = path.resolve(obj.config.sourceDir);
   return getFilesHistory()
   .then((filesHistory) => {
     obj.pages.forEach(function(article) {
diff --git a/sitegin/markdown.js b/sitegin/markdown.js
index 99daeef2..df8dd718 100644
--- a/sitegin/markdown.js
+++ b/sitegin/markdown.js
@@ -113,6 +113,7 @@ marked.setOptions({
 });
 
 module.exports = function(obj) {
+  console.log('Build step: Markdown');
   var options = obj.config.options;
   renderer.link = function(href, title, text) {
     if(options.httponly) {
diff --git a/sitegin/nunjucks.js b/sitegin/nunjucks.js
index 0a364b95..32fc645c 100644
--- a/sitegin/nunjucks.js
+++ b/sitegin/nunjucks.js
@@ -8,10 +8,10 @@ var fs = require('fs');
 var dateFilter = require('nunjucks-date-filter');
 var util = require('util');
 
-var watch = require('./config').watch;
-console.log('watch: '+watch);
+var config = require('./config');
+console.log('watch: '+config.watch);
 var env = new nunjucks.Environment(
-  new nunjucks.FileSystemLoader('theme/templates',{watch: watch})
+  new nunjucks.FileSystemLoader(config.themeDir+'/templates',{watch: config.watch})
 );
 
 env.addFilter('date', dateFilter);
@@ -56,8 +56,11 @@ module.exports = function(data, type) {
   return new Promise(function(resolve, reject) {
     if(formats[type]) resolve(formats[type]);
     else {
-      fs.readdir('theme/templates',function(err, files) {
-        if(err) return reject(new Error('Error listing files in theme/templates'));
+      fs.readdir(config.themeDir+'/templates',function(err, files) {
+        if(err) {
+          console.log(config);
+          return reject(new Error('Error listing files in '+config.themeDir+'/templates'));
+        }
 
         files = files.filter(function(file) {
           return file.match(type+'\..*\.nunj');
@@ -66,8 +69,8 @@ module.exports = function(data, type) {
         var filteredFiles = [];
         var todo = 0;
         files.forEach(function(file) {
-          fs.stat('theme/templates/'+file,function(err, stat) {
-            if(err) reject(new Error('Error stating theme/templates/'+file));
+          fs.stat(config.themeDir+'/templates/'+file,function(err, stat) {
+            if(err) reject(new Error('Error stating '+config.themeDir+'/templates'+file));
             if(stat.isFile())
               filteredFiles.push(file);
             todo--;
diff --git a/sitegin/parseHugo.js b/sitegin/parseHugo.js
index c76b3537..e685065f 100644
--- a/sitegin/parseHugo.js
+++ b/sitegin/parseHugo.js
@@ -27,6 +27,7 @@ var readPageWorker = function(content, obj) {
 }
 
 module.exports = function(obj) {
+  console.log('Build step: ParseHugo');
   return new Promise(function(resolve, reject) {
     var pages = [];
     obj.pages.forEach(function(page) {
diff --git a/sitegin/readFiles.js b/sitegin/readFiles.js
index 83fa9a5a..e467e782 100644
--- a/sitegin/readFiles.js
+++ b/sitegin/readFiles.js
@@ -6,6 +6,7 @@ var fs = require('fs');
 var path = require('path');
 
 module.exports = function(obj) {
+  console.log('Build step: ReadFiles');
   return new Promise(function(resolve, reject) {
     var pages = [];
     walk.walk(path.join(obj.config.sourceDir,obj.config.articlesLocation))
diff --git a/sitegin/sitemap.js b/sitegin/sitemap.js
index af09456d..73b0129f 100644
--- a/sitegin/sitemap.js
+++ b/sitegin/sitemap.js
@@ -1,5 +1,6 @@
 var fs = require('fs');
 module.exports = function(obj) {
+  console.log('Build step: Sitemap');
   return new Promise(function(resolve, reject) {
     var file = '<?xml version="1.0" encoding="UTF-8"?>\n'+
       '<urlset\n'+
diff --git a/sitegin/tags.js b/sitegin/tags.js
index 42b14afb..be2386ed 100644
--- a/sitegin/tags.js
+++ b/sitegin/tags.js
@@ -8,6 +8,7 @@ var toURL = function(url) {
 }
 
 module.exports = function(obj) {
+  console.log('Build step: Tags');
   return new Promise(function(resolve, reject) {
     var tags = [];
     var tagPages = {};
diff --git a/sitegin/theme.js b/sitegin/theme.js
index bb743ea7..dde9b8a7 100644
--- a/sitegin/theme.js
+++ b/sitegin/theme.js
@@ -1,6 +1,7 @@
 var jobs = require('./jobs');
 
 module.exports = function(obj) {
+  console.log('Build step: Theme');
   return new Promise(function(resolve,reject){
     var todo = 0;
     var done = function(){
diff --git a/sitegin/urls.js b/sitegin/urls.js
index 11693250..e4263f08 100644
--- a/sitegin/urls.js
+++ b/sitegin/urls.js
@@ -3,6 +3,7 @@
  */
 
 module.exports = function(obj) {
+  console.log('Build step: URLs');
   return new Promise(function(resolve, reject) {
     obj.pages.forEach(function(page) {
       var file = page.file;
diff --git a/sitegin/writeFiles.js b/sitegin/writeFiles.js
index 7fbfa8b3..afc4ff2a 100644
--- a/sitegin/writeFiles.js
+++ b/sitegin/writeFiles.js
@@ -4,6 +4,7 @@ var path = require('path');
 var mkdirp = require('mkdirp');
 
 module.exports = function(obj) {
+  console.log('Build step: WriteFiles');
   return new Promise(function(resolve,reject) {
     var builddir = obj.config.builddir;
     var todo = 0;
-- 
GitLab