Skip to content

Commit 137b8fa

Browse files
move to bin/, update package.json
1 parent f88b754 commit 137b8fa

File tree

5 files changed

+181
-24
lines changed

5 files changed

+181
-24
lines changed

main.js renamed to bin/compile.js

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
module.exports = compile
2+
13
var fs = require('fs')
24
, handlebars = require('handlebars')
3-
, _ = require('underscore')
4-
, marked = require('marked');
5+
, marked = require('marked')
56

67
var cities = [
78
{
@@ -32,7 +33,7 @@ var cities = [
3233
, about_me: "I'm a Node engineer for New Relic who has contributed significantly to their\ninstrumentation for Node.js. I've been using Node professionally since 2011,\nco-ran one of the session tracks at NodeConf 2013, and have contributed some\n small pieces to Node core. My most significant public project aside from\n [node-newrelic](https://github.com/newrelic/node-newrelic) is probably\n [continuation-local storage](https://github.com/othiym23/node-continuation-local-storage),\nof which I am the primary maintainer. No server-side JavaScript is too gross\n for me to be interested in.\n \nNode is a great platform but it can be scary to get started with, and it can\ncontinue to be scary to plumb its depths. I'm comfortable with Node's (and\n JavaScript's) many idiosyncracies and awkward design tradeoffs and really enjoy\nhelping other people get more comfortable as well. Having done all of free-form\ntalks with questions, formal presentations, and hands-on workshops, I enjoy all\nthree, although I do better when there are frequent opportunities for\nquestions.\n "
3334
}]
3435
}
35-
];
36+
]
3637

3738
function get_speakers() {
3839
return speaker_profile({cities: cities})
@@ -45,36 +46,50 @@ function get_speakers() {
4546
})
4647
return city_map
4748
}
48-
};
49+
}
4950

5051
/**
5152
* A mapping of template name (which lives in the templates/ directory) to
5253
* functions which return a context the template is rendered with.
5354
*/
5455
var template_contextfn = {
55-
'speakers.hbs': get_speakers,
56-
'index.hbs': function () {}
57-
};
56+
'speakers.hbs': get_speakers
57+
, 'index.hbs': Function()
58+
}
59+
60+
function compile(ready) {
61+
var keys = Object.keys(template_contextfn)
62+
, pending = keys.length
5863

59-
function compile () {
60-
_.each(template_contextfn, function (ctxFn, template) {
64+
keys.forEach(function(template) {
6165
var outputFile = template.replace('hbs', 'html')
62-
, ctx = template_contextfn[template]()
66+
, ctxFn = template_contextfn[template]
67+
, ctx = template_contextfn[template]()
6368

64-
fs.readFile('templates/' + template, 'utf8', function (err, txt) {
65-
if (err) {
66-
throw err
69+
fs.readFile('templates/' + template, 'utf8', function(err, txt) {
70+
if(err) {
71+
return ready(err)
6772
}
6873
var output = handlebars.compile(txt)(ctx)
6974

70-
fs.writeFile('output/' + outputFile, output, function (err) {
71-
if (err) {
72-
throw err
75+
fs.writeFile('output/' + outputFile, output, function(err) {
76+
if(err) {
77+
return ready(err)
7378
}
74-
console.log("Wrote", template, 'to', outputFile);
79+
console.log('Wrote %s to %s', template, outputFile)
80+
81+
!--pending && ready(null)
7582
})
7683
})
7784
})
78-
};
85+
}
7986

80-
compile();
87+
if(require.main === module) {
88+
compile(function(err) {
89+
if(err) {
90+
console.error(err.stack || err)
91+
}
92+
93+
process.exit(err ? 1 : 0)
94+
})
95+
}

bin/install.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
module.exports = main
2+
3+
var compile = require('./compile.js')
4+
5+
var mkdirp = require('mkdirp')
6+
, path = require('path')
7+
, cpr = require('cpr')
8+
9+
var base_dir = path.join(__dirname, '..', 'output')
10+
11+
var targets = {
12+
'stylesheets': path.join(__dirname, '..', 'stylesheets')
13+
, 'javascripts': path.join(__dirname, '..', 'javascripts')
14+
, 'images': path.join(__dirname, '..', 'images')
15+
, 'style': path.join(__dirname, '..', 'node_modules', 'knode-style')
16+
}
17+
18+
function main(ready) {
19+
mkdirp(path.join(__dirname, '..', 'output'), onmkdirp)
20+
21+
function onmkdirp(err) {
22+
if(err) {
23+
throw err
24+
}
25+
26+
var target_keys = Object.keys(targets)
27+
, pending = target_keys.length
28+
29+
target_keys.forEach(function(target) {
30+
var source = targets[target]
31+
32+
cpr(source, subdir(target), oncpr)
33+
})
34+
35+
function oncpr(err) {
36+
if(err) {
37+
return onerror(err)
38+
}
39+
40+
!--pending && compile(ready)
41+
}
42+
43+
function onerror(err) {
44+
var cb = ready
45+
46+
ready = Function()
47+
cb(err)
48+
}
49+
}
50+
}
51+
52+
function subdir(dir) {
53+
return path.join(base_dir, dir)
54+
}
55+
56+
if(require.main === module) {
57+
main(function(err) {
58+
if(err) {
59+
console.error(err.stack || err)
60+
}
61+
62+
process.exit(err ? 1 : 0)
63+
})
64+
}

bin/serve.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
module.exports = main
2+
3+
var portfinder = require('portfinder')
4+
, ecstatic = require('ecstatic')
5+
, watchr = require('watchr')
6+
, open = require('opener')
7+
, path = require('path')
8+
, http = require('http')
9+
10+
var compile = require('./compile.js')
11+
12+
var dir = path.join(__dirname, '..', 'output')
13+
14+
function main(ready) {
15+
var server = ecstatic({root: dir})
16+
, compiling = false
17+
, timeout
18+
19+
watchr.watch({
20+
path: path.join(__dirname, '..')
21+
, listener: recompile
22+
, ignoreHiddenFiles: true
23+
})
24+
25+
compile(function(err) {
26+
if(err) {
27+
return ready(err)
28+
}
29+
30+
ready(null, http.createServer(server))
31+
})
32+
33+
function recompile() {
34+
clearTimeout(timeout)
35+
36+
timeout = setTimeout(function iter() {
37+
if(compiling) {
38+
return timeout = setTimeout(iter, 500)
39+
}
40+
41+
compiling = true
42+
compile(function() {
43+
compiling = false
44+
})
45+
})
46+
}
47+
}
48+
49+
if(require.main === module) {
50+
main(function(err, server) {
51+
if(err) {
52+
console.error(err.stack || err)
53+
}
54+
55+
portfinder.getPort(function(err, port) {
56+
if(err) {
57+
console.error(err.stack || err)
58+
}
59+
60+
server.listen(port)
61+
open('http://127.0.0.1:' + port + '/')
62+
})
63+
})
64+
}

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
'install': require('./bin/install.js')
3+
, 'compile': require('./bin/compile.js')
4+
, 'serve': require('./bin/serve.js')
5+
}

package.json

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,24 @@
22
"name": "knode.github.io",
33
"version": "0.0.0",
44
"description": "Website for knode community portal",
5-
"main": "main.js",
5+
"main": "index.js",
66
"dependencies": {
77
"handlebars": "~1.3.0",
8-
"underscore": "~1.5.2",
9-
"marked": "~0.3.1"
8+
"marked": "~0.3.1",
9+
"knode-style": "git://github.com/knode/style.git#HEAD",
10+
"mkdirp": "~0.3.5",
11+
"cpr": "~0.1.1"
12+
},
13+
"devDependencies": {
14+
"portfinder": "~0.2.1",
15+
"ecstatic": "~0.4.13",
16+
"watchr": "~2.4.11",
17+
"opener": "~1.3.0"
1018
},
11-
"devDependencies": {},
1219
"scripts": {
13-
"test": "echo \"Error: no test specified\" && exit 1"
20+
"update": "node ./bin/install.js",
21+
"test": "echo \"Error: no test specified\" && exit 1",
22+
"start": "node ./bin/serve.js"
1423
},
1524
"repository": {
1625
"type": "git",

0 commit comments

Comments
 (0)