Permalink
Browse files

Makes the first page of the Main Menu dynamic.

The data is filled in by metadata found in each package's
pine-package.json file.
  • Loading branch information...
1 parent d10e94e commit 9898d8ca2a5add09b73638955c98ef8783f85911 @jeremyckahn committed Nov 6, 2012
View
10 ui/dev-server.js
@@ -1,9 +1,17 @@
var fs = require('fs');
var express = require('express');
+var mustache = require('mustache');
+var packageMeta = require(__dirname + '/lib/package-meta.js');
+
+
var server = express();
server.get('/', function (req, res) {
- var html = fs.readFileSync(__dirname + '/index.html', 'utf8');
+ var template = fs.readFileSync(__dirname + '/index.html', 'utf8');
+ var packageData = {
+ 'packageList': packageMeta.getPackageList()
+ };
+ var html = mustache.to_html(template, packageData);
res.send(html);
});
View
85 ui/index.html
@@ -20,90 +20,11 @@ <h1 class="welcome">Hello. Welcome to Pine.</h1>
<tr>
<td class="menu main-menu">
<ul class="main-menu-btn-array menu-btn-array">
+ {{#packageList}}
<li>
- <button class="btn main-menu-btn game-init" data-game-url="packages/sample_game/?dev=1">Sample game</button>
- </li>
- <li>
- <button class="btn main-menu-btn game-init" data-game-url="packages/benchmark/?benchmark=idle&dev=1">Rotation Benchmark</button>
- </li>
- <li>
- <button class="btn main-menu-btn game-init" data-game-url="packages/gamepad/demo.html?dev=1">Gamepad Demo</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 1</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 2</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 3</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 4</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 5</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 6</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 7</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 8</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 9</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 10</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 11</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 12</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 13</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 14</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 15</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 16</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 17</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 18</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 19</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 20</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 21</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 22</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 23</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 24</button>
- </li>
- <li>
- <button class="btn main-menu-btn">Item 25</button>
+ <button class="btn main-menu-btn game-init" data-game-url="{{path}}/index.html">{{name}}</button>
</li>
+ {{/packageList}}
</ul>
</td>
View
61 ui/lib/package-meta.js
@@ -0,0 +1,61 @@
+require('shelljs/global');
+
+var fs = require('fs');
+var jsonParse = require('json-parse');
+var _ = require('underscore');
+
+
+var PACKAGE_DIRECTORY = 'packages';
+var JSON_FILE_NAME = 'pine-package.json';
+
+function getJsonList () {
+ var jsonList = [];
+ var packageList = ls(PACKAGE_DIRECTORY);
+
+ packageList.forEach(function (packageDir) {
+ var jsonPath = [PACKAGE_DIRECTORY, packageDir, JSON_FILE_NAME].join('/');
+
+ // "-e" tests for path existence.
+ if (test('-e', jsonPath)) {
+ jsonList.push(jsonPath);
+ }
+ });
+
+ return jsonList;
+}
+
+
+function getPackageData () {
+ var jsonList = getJsonList();
+ var packageData = {};
+
+ jsonList.forEach(function (jsonPath) {
+ try {
+ packageData[jsonPath] = jsonParse.sync(jsonPath);
+ _.extend(packageData[jsonPath], {
+ 'path': jsonPath.split('/').slice(0, -1).join('/')
+ });
+ } catch (ex) {
+ console.log(ex);
+ }
+ });
+
+ return packageData;
+}
+
+
+function getPackageList () {
+ var packageData = getPackageData();
+ var packageKeys = _.keys(packageData);
+ var sortedKeys = packageKeys.sort();
+ var packageList = [];
+
+ sortedKeys.forEach(function (package) {
+ packageList.push(packageData[package]);
+ });
+
+ return packageList;
+}
+
+
+exports.getPackageList = getPackageList;
View
6 ui/package.json
@@ -10,7 +10,11 @@
"url": "https://github.com/jeremyckahn/pine.git"
},
"devDependencies": {
- "express": "*"
+ "express": "*",
+ "shelljs": "*",
+ "json-parse": "*",
+ "underscore": "*",
+ "mustache": "*"
},
"author": "Jeremy Kahn"
}
View
4 ui/packages/benchmark/pine-package.json
@@ -0,0 +1,4 @@
+{
+ "name": "Rotation Benchmark",
+ "version": "0.0.1"
+}
View
0 ui/packages/gamepad/demo.html → ui/packages/gamepad/index.html
File renamed without changes.
View
4 ui/packages/gamepad/pine-package.json
@@ -0,0 +1,4 @@
+{
+ "name": "Gamepad Test",
+ "version": "0.0.1"
+}
View
4 ui/packages/sample_game/pine-package.json
@@ -0,0 +1,4 @@
+{
+ "name": "Sample Game",
+ "version": "0.0.1"
+}

0 comments on commit 9898d8c

Please sign in to comment.