From f3b9f8c7994bda048eb5152792f4d3464121535d Mon Sep 17 00:00:00 2001
From: David Baker <dave@matrix.org>
Date: Fri, 11 Sep 2015 15:42:11 +0100
Subject: [PATCH] WIP reworking of skinning and app integration process

---
 package.json      |  5 +++
 reskindex.js      | 85 +++++++++++++++++++++++++++++++++++++++++++++++
 src/dispatcher.js | 24 ++++++-------
 src/index.js      |  6 +++-
 4 files changed, 105 insertions(+), 15 deletions(-)
 create mode 100755 reskindex.js

diff --git a/package.json b/package.json
index 05da63869d..152d1599c0 100644
--- a/package.json
+++ b/package.json
@@ -9,6 +9,9 @@
   },
   "license": "Apache-2.0",
   "main": "lib/index.js",
+  "bin": {
+    "reskindex": "./reskindex.js"
+  },
   "scripts": {
     "build": "babel src -d lib --source-maps",
     "start": "babel src -w -d lib --source-maps",
@@ -16,6 +19,8 @@
     "prepublish": "npm run build"
   },
   "dependencies": {
+    "glob": "^5.0.14",
+    "optimist": "^0.6.1",
     "classnames": "^2.1.2",
     "filesize": "^3.1.2",
     "flux": "^2.0.3",
diff --git a/reskindex.js b/reskindex.js
new file mode 100755
index 0000000000..3ad1f36888
--- /dev/null
+++ b/reskindex.js
@@ -0,0 +1,85 @@
+#!/usr/bin/env node
+
+var fs = require('fs');
+var path = require('path');
+var glob = require('glob');
+
+var args = require('optimist').argv;
+
+var header = args.h || args.header;
+
+if (args._.length == 0) {
+    console.log("No skin given");
+    process.exit(1);
+}
+
+var skin = args._[0];
+
+try {
+    fs.accessSync(path.join('src', 'skins', skin), fs.F_OK);
+} catch (e) {
+    console.log("Skin "+skin+" not found");
+    process.exit(1);
+}
+
+try {
+    fs.accessSync(path.join('src', 'skins', skin, 'views'), fs.F_OK);
+} catch (e) {
+    console.log("Skin "+skin+" has no views directory");
+    process.exit(1);
+}
+
+var skindex = path.join('src', 'skins', skin, 'skindex.js');
+var viewsDir = path.join('src', 'skins', skin, 'views');
+
+var strm = fs.createWriteStream(skindex);
+
+if (header) {
+   strm.write(fs.readFileSync(header));
+   strm.write('\n');
+}
+
+strm.write("/*\n");
+strm.write(" * THIS FILE IS AUTO-GENERATED\n");
+strm.write(" * You can edit it you like, but your changes will be overwritten,\n");
+strm.write(" * so you'd just be trying to swim upstream like a salmon.\n");
+strm.write(" * You are not a salmon.\n");
+strm.write(" */\n\n");
+
+strm.write("var sdk = require('matrix-react-sdk');\n\n");
+
+var tree = {
+    atoms: {},
+    molecules: {},
+    organisms: {},
+    templates: {},
+    pages: {}
+};
+
+var files = glob.sync('**/*.js', {cwd: viewsDir});
+for (var i = 0; i < files.length; ++i) {
+    var file = files[i].replace('.js', '');
+    var module = (file.replace(/\//g, '.'));
+
+    // create objects for submodules
+    // NB. that we do not support creating additional
+    // top level modules. Perhaps we should?
+    var subtree = tree;
+    var restOfPath = module.split('.').slice(0, -1);
+    var currentPath = restOfPath[0];
+    restOfPath = restOfPath.slice(1);
+    while (restOfPath.length) {
+        currentPath += '.'+restOfPath[0];
+        if (subtree[restOfPath[0]] == undefined) {
+            strm.write('sdk.'+currentPath+' = {};\n');
+            strm.uncork();
+        }
+        subtree[restOfPath[0]] = {};
+        restOfPath = restOfPath.slice(1);
+    }
+
+    strm.write('sdk.'+module+" = require('./views/"+file+"');\n");
+    strm.uncork();
+}
+strm.end();
+
diff --git a/src/dispatcher.js b/src/dispatcher.js
index 3edb9c6947..cd954b573f 100644
--- a/src/dispatcher.js
+++ b/src/dispatcher.js
@@ -17,21 +17,17 @@ limitations under the License.
 'use strict';
 
 var flux = require("flux");
-var extend = require("./extend");
 
-var MatrixDispatcher = function() {
-    flux.Dispatcher.call(this);
+class MatrixDispatcher extends flux.Dispatcher {
+    dispatch(payload) {
+        if (this.dispatching) {
+            setTimeout(super.dispatch.bind(this, payload), 0);
+        } else {
+            this.dispatching = true;
+            super.dispatch.call(this, payload);
+            this.dispatching = false;
+        }
+    }
 };
 
-extend(MatrixDispatcher.prototype, flux.Dispatcher.prototype);
-MatrixDispatcher.prototype.dispatch = function(payload) {
-    if (this.dispatching) {
-        setTimeout(flux.Dispatcher.prototype.dispatch.bind(this, payload), 0);
-    } else {
-        this.dispatching = true;
-        flux.Dispatcher.prototype.dispatch.call(this, payload);
-        this.dispatching = false;
-    }
-}
-
 module.exports = new MatrixDispatcher();
diff --git a/src/index.js b/src/index.js
index a7b6ae4cc7..adb0e04dc7 100644
--- a/src/index.js
+++ b/src/index.js
@@ -16,4 +16,8 @@ limitations under the License.
 
 'use strict';
 
-module.exports.ComponentBroker = require("./ComponentBroker");
+module.exports.atoms = {};
+module.exports.molecules = {};
+module.exports.organisms = {};
+module.exports.templates = {};
+module.exports.pages = {};