From 7043da5250d7b8fb06298691e709ec6df7176958 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jakub=20Sko=C5=99epa?= <jakub@skorepa.info>
Date: Mon, 7 Mar 2016 19:02:21 +0100
Subject: [PATCH] Add config, readFiles, and print jobs

Config - returns configuration
ReadFiles - reads files from config.articlesLocation
Print - prints everything it gets
---
 index.js              | 180 ++++--------------------------------------
 package.json          |   3 +-
 sitegin/config.js     |  22 ++++++
 sitegin/print.js      |   7 ++
 sitegin/read-files.js |  36 +++++++++
 sitegin/sitegin.js    |  25 +++---
 6 files changed, 95 insertions(+), 178 deletions(-)
 create mode 100644 sitegin/config.js
 create mode 100644 sitegin/print.js
 create mode 100644 sitegin/read-files.js

diff --git a/index.js b/index.js
index 8b12354e..8e2615e9 100755
--- a/index.js
+++ b/index.js
@@ -1,175 +1,25 @@
 #!/usr/bin/env node
+
 require('./sitegin/sitegin')({
-  watch: false
-}, function onLoad(jobs) {
+  watch: true
+})
+.then(function onLoad(jobs) {
   var sass = require('node-sass');
   var fs = require('fs');
 
+  function run() {
+    jobs.runSequence('config','readFiles','print')
+    .catch(e => console.log(e.stack));
+  }
+
   console.log('Sitegin successfully loaded');
   jobs.onReload(function() {
     console.log("Sitegin reloaded");
+    run();
   })
-
-/*
-  var builddir = "build";
-  if(process.argv[2] !== "compileonly") builddir = "build-debug";
-
-  var oldEmit = sequencer.emit;
-  sequencer.emit = function() {
-    console.log("Event:",arguments[0]);
-    oldEmit.apply(sequencer, arguments);
-  }
-
-  sequencer.on("", function(ev) {
-    console.log("Event:",ev);
-  })
-
-  var tasks = [];
-  var onDone = function(what) {
-    console.log(what, 'done');
-    var removed = false;
-    for(var i = 0; i < tasks.length; i++) {
-      if(tasks[i] == what) {
-        removed = true;
-        tasks.splice(i,1);
-        break;
-      }
-    }
-    if(!removed && process.argv[2] == "compileonly") throw new Error("Task "+what+" ended but was never registered");
-    if(tasks.length == 0 && process.argv[2] == "compileonly") {
-      console.log("exitting");
-      process.exit();
-    }
-  }
-
-
-  // PIPELINE
-  sequencer
-  .load(input)
-  .registerNext(transformerGit)
-  .registerNext(transformerOther)
-  .registerNext(transformerImages)
-  .registerNext(transformerMD)
-  .registerNext(transformerUrlizeTags)
-  .registerNext(transformerTags)
-  .registerNext(transformerNunjucks)
-  .registerNext(output)
-  .finish(function(list) {
-    onDone("sequencer");
-  })
-  tasks.push('sequencer');
-
-  try {fs.mkdirSync(builddir);}catch(e){}
-  try {fs.mkdirSync(builddir+"/theme");}catch(e){}
-
-  // SASS
-  var rendersass = function() {
-    sass.render ({file: "theme/sass/style.scss"},function(err, result) {
-      if(err == null) {
-        console.log("compiled sass");
-        fs.writeFile(builddir+"/theme/style.css",result.css);
-      } else console.log("error ", err);
-      onDone("sass");
-    });
-  };
-  tasks.push('sass');
-
-  rendersass();
-
-  // STATIC FILES
-  var fsextra = require('node-fs-extra');
-  tasks.push('copy static');
-  fsextra.copy(
-      'static',
-      builddir,
-      function(file){ return !(file.match(".git") || file.match("static/articles")); },
-      function(){onDone("copy static");}
-    );
-  tasks.push('copy static/articles');
-  fsextra.copy(
-      'static/articles',
-      builddir+"/clanek",
-      function(){onDone("copy static/articles");}
-    );
-  tasks.push('copy theme static');
-  fsextra.copy(
-      'theme/static',
-      builddir,
-      function(file){ return !file.match("\\.git") },
-      function(){onDone("copy theme static");}
-    );
-
-  // DEVEL INCOMING
-  if(process.argv[2] !== "compileonly") {
-    var sync = require('browser-sync').create();
-    sync.init({
-      server: {
-        baseDir: builddir
-      },
-      ui: {
-        port: 1338
-      },
-      port: 1337
-    });
-
-    // CHANGE WATCHER
-    var chokidar = require('chokidar');
-
-    chokidar.watch('theme/static', {ignoreInitial: true, usePolling: false})
-    .on('all', (event, path) => {
-      console.log(path);
-      console.log("theme static copy watcher");
-      fsextra.copy(
-        'theme/static',
-        builddir,
-        function(file){ return !file.match(".git"); },
-        function(){}
-        );
-      sync.reload(path);
-    });
-
-    chokidar.watch('static/', {ignoreInitial: true, usePolling: false})
-    .on('all', (event, path) => {
-      if(path.match(/^static\/articles\/.*$/)) return;
-      console.log("static copy watcher");
-      fsextra.copy(
-        'static',
-        builddir,
-        function(file){ return !(file.match(".git") || file.match("static/articles")); },
-        function(){}
-      );
-      sync.reload(path);
-    })
-    .unwatch('static/articles');
-
-    chokidar.watch('static/articles', {ignoreInitial: true, usePolling: false})
-    .on('all', (event, path) => {
-      console.log("static articles copy watcher");
-      fsextra.copy('static/articles',builddir+"/clanek",function(){});
-      sync.reload(path);
-    })
-    .unwatch('static/articles');
-
-    chokidar.watch(['theme/','content/'], {ignoreInitial: true, usePolling: false})
-    .on('all', (event, path) => {
-      if(path.match(/^theme\/static\/.*$/)) return;
-      if(path.match(/^theme\/sass\/.*$/)) return;
-      console.log("rebuild watcher",path);
-      sequencer.load(input);
-    })
-    .unwatch("theme/static")
-    .unwatch("theme/sass");
-
-    sequencer.finish(function() {
-      sync.reload("*.html");
-    });
-
-    chokidar.watch('theme/sass', {ignoreInitial: true, usePolling: false})
-    .on('all', function(event, path) {
-      console.log("sass: file change detected - rerendering "+event+" "+path);
-      rendersass();
-      sync.reload(path);
-    });
-  }
-  */
+  run();
+})
+.catch(function(e) {
+  console.log(e.stack);
+  process.exit();
 });
diff --git a/package.json b/package.json
index d37bc7b6..34b487f1 100644
--- a/package.json
+++ b/package.json
@@ -23,7 +23,8 @@
     "nunjucks-date-filter": "^0.1.1",
     "syntax-error": "^1.1.5",
     "toml": "~2.3.0",
-    "toml-js": "0.0.8"
+    "toml-js": "0.0.8",
+    "walk": "^2.3.9"
   },
   "devDependencies": {
     "istanbul": "^0.4.2",
diff --git a/sitegin/config.js b/sitegin/config.js
new file mode 100644
index 00000000..3030b938
--- /dev/null
+++ b/sitegin/config.js
@@ -0,0 +1,22 @@
+/*
+ * This file specifies whole configuration of sitegin.
+ * In future it might actually read configuration files but for now - if you
+ * want to configure sitegin you have to modify this file.
+ * (or write config reader which would replace this file - I'd appreciate
+ * merge request for this feature ;) )
+ */
+
+module.exports = function() {
+  var builddir = "build";
+  if(process.argv[2] !== "compileonly") builddir = "build-debug";
+  var watch = true;
+  if(process.argv[2] == "compileonly") watch = false;
+
+  return Promise.resolve({
+    config: {
+      builddir: builddir,
+      watch: watch,
+      articlesLocation: 'content/articles'
+    }
+  })
+}
diff --git a/sitegin/print.js b/sitegin/print.js
new file mode 100644
index 00000000..332c1087
--- /dev/null
+++ b/sitegin/print.js
@@ -0,0 +1,7 @@
+module.exports = function(a) {
+  var pages = a.pages
+  a.pages = ['...']
+  console.log(a)
+  a.pages = pages
+  return Promise.resolve(a)
+}
diff --git a/sitegin/read-files.js b/sitegin/read-files.js
new file mode 100644
index 00000000..551d7f30
--- /dev/null
+++ b/sitegin/read-files.js
@@ -0,0 +1,36 @@
+/*
+ * This job reads files from config.articlesLocation and loads this list to `pages`
+ */
+var walk = require('walk');
+var fs = require('fs');
+var path = require('path');
+
+module.exports = function(obj) {
+  return new Promise(function(resolve, reject) {
+    var pages = [];
+    walk.walk(obj.config.articlesLocation)
+    .on('file',function(root, fileStats, next) {
+      var file = path.join(root,fileStats.name);
+      fs.readFile(file, 'utf8', function(err,data) {
+        if(err) {
+          console.log('Error reading file '+file)
+          console.log(err);
+        } else {
+          pages.push({
+            filename: file,
+            content: data
+          })
+        }
+        next()
+      })
+    })
+    .on('errors',function(root, nodeStatsArray, next) {
+      console.log('Walker error', root, nodeStatsArray);
+      next();
+    })
+    .on('end', function() {
+      obj.pages = pages;
+      resolve(obj);
+    })
+  })
+}
diff --git a/sitegin/sitegin.js b/sitegin/sitegin.js
index 3c413c70..9587ec1c 100644
--- a/sitegin/sitegin.js
+++ b/sitegin/sitegin.js
@@ -1,25 +1,26 @@
 var jobs = require('./jobs');
 
-module.exports = function(config, onDone) {
-  jobs.registerMultiple(
+module.exports = function(config) {
+  return jobs.registerMultiple(
     config,
-    /*['input-hugo', './input-hugo'],
+    ['config', './config'],
+    ['readFiles', './read-files'],
+    ['parseHugo', './input-hugo'],
+    ['print', './print'],
+
+    ['transformer-git', './transformer-git'],
     ['output-console', './output-console'],
     ['output-filesystem', './output-filesystem'],
     ['transformer-basichtml', './transformer-basichtml'],
-    ['transformer-git', './transformer-git'],
+
     ['transformer-markdown', './transformer-markdown'],
     ['transformer-nunjucks', './transformer-nunjucks'],
     ['transformer-tags', './transformer-tags'],
-    ['transformer-urlizetags', './transformer-urlizetags'],*/
-    ['transformer-other', './transformer-other']
-    //['transformer-images', './transformer-images']
+    ['transformer-urlizetags', './transformer-urlizetags'],
+    ['transformer-other', './transformer-other'],
+    ['transformer-images', './transformer-images']
   )
   .then(function() {
-    if(onDone) onDone(jobs);
+    return jobs;
   })
-  .catch(function(e) {
-    console.log(e.stack)
-    jobs.close();
-  });
 }
-- 
GitLab