Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

improved cli, improved 'standalone' example, made some necessary tweaks

  • Loading branch information...
commit 7ff6aedf82dec4aaa51735d7091416ca3d0d04f2 1 parent 9148845
@kadirpekel authored
View
13 examples/standalone/README
@@ -1,8 +1,11 @@
-Simply type 'meryl' in the current working directory
+Simply type 'meryl' in the current working directory.
+You can also pass an argument denoting root directory.
- $ meryl
-
-or execute file 'run'
+ $ meryl <rootpath>
- $ ./run
+You can create a module named 'boot.js' in the root directory
+which exports a function that accepts Meryl instance.
+ module.exports = function (meryl) {
+ // config meryl
+ }
View
15 examples/standalone/boot.js
@@ -0,0 +1,15 @@
+var connect = require('connect');
+
+// Boot file
+
+// export a function accepting Meryl instance
+module.exports = function(meryl) {
+
+ meryl.plug(connect.staticProvider(), connect.logger());
+
+ meryl.options = {
+ templateExt: '.mt', // Default is '.jshtml'
+ port: 3000 // Already the default one
+ }
+
+}
View
6 examples/standalone/config.js
@@ -1,6 +0,0 @@
-// export Meryl options
-module.exports = {
- templateExt: '.mt', // Default is '.jshtml'
- port: 8000
-}
-
View
1  examples/standalone/index.mt
@@ -1,3 +1,4 @@
<h1>Wow, this page rendered automatically</h1>
+<img src="meryl.gif" />
<p><a href="/subpage">Go to subpage</a></p>
View
BIN  examples/standalone/meryl.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
2  examples/standalone/run
@@ -1,2 +0,0 @@
-#! /usr/bin/env meryl
-
View
1  index.js
@@ -8,4 +8,3 @@
* Point meryl
*/
module.exports = require('./lib/meryl');
-
View
35 lib/cli.js
@@ -8,29 +8,34 @@
* Module dependencies
*/
var meryl = require('./meryl'),
+ fs = require('fs'),
path = require('path');
/**
- * Import options
+ * Load boot file
*/
+var configDir = process.ARGV.length == 3 && process.ARGV[2];
+if (configDir) {
+ try {
+ process.chdir(configDir);
+ } catch (e) {
+ throw 'Invalid config path';
+ }
+}
-var options = null;
-
-// TODO: Handle config file location as a cli arg to load from an arbitrary path
-
-try {
- options = require(path.join(process.cwd(), "config"));
- if (options)
- console.log('Config loaded...');
+var configPath = path.join(process.cwd(), 'boot.js');
-} catch (e) {
- // Skip exception
- // TODO: Inform user for config file's internal errors
+var stats = fs.statSync(configPath);
+if (stats || stats.isFile()) {
+ configPath = configPath.replace(/\.js$/, '');
+ require(configPath)(meryl);
+ console.log('Boot file loaded...');
+} else {
+ console.log('Boot file not found, using defaults');
}
/**
* Start Engines
- */
-meryl.run(options);
-
+ */
+meryl.run();
console.log('Running Meryl...');
View
17 lib/meryl.js
@@ -8,6 +8,7 @@
* Module dependencies
*/
var sys = require('sys'),
+ http = require('http'),
fs = require('fs'),
path = require('path');
@@ -27,6 +28,9 @@ var Meryl = function () {
this.h = this.handle;
this.p = this.plug;
+ // Options
+ this.options = {};
+
// Shorthand meryl functions for http methods
['get', 'post', 'put', 'delete'].forEach(function(method) {
self[method] = function () {
@@ -245,7 +249,8 @@ Meryl.prototype = {
*/
cgi: function (opts) {
var self = this;
- self.options = opts || {};
+ for (var i in opts || {})
+ self.options[i] = opts[i];
var infra = self.plugins.concat(self.handlers);
infra.push({pattern: '* /(<templatename>)?', cb: function(req, resp, next) {
try { resp.render(req.params.templatename); }
@@ -303,10 +308,10 @@ Meryl.prototype = {
* @api public
*/
run: function (opts) {
- var opts = opts || {};
- require('http').createServer(this.cgi(opts)).listen(
- opts.port || process.env.PORT || 3000,
- opts.hostname || process.env.HOSTNAME || 'localhost');
+ var server = http.createServer(this.cgi(opts));
+ server.listen(this.options.port || process.env.PORT || 3000,
+ this.options.hostname || process.env.HOSTNAME || 'localhost');
+ return server;
},
/*
@@ -318,7 +323,7 @@ Meryl.prototype = {
function contextCarrier () {
if(!arguments.length
|| (arguments.length == 1 && typeof arguments[0] == 'object')) {
- self.run(arguments[0] || {});
+ return self.run(arguments[0]);
} else {
self.plug.apply(self, arguments);
return contextCarrier;
View
3  package.json
@@ -1,5 +1,5 @@
{ "readme": "http://github.com/coffeemate/meryl#readme"
-, "version": "0.9.0"
+, "version": "0.9.1"
, "description": "Minimalist web framework for NodeJS"
, "main": "index"
, "author": "Kadir Pekel <kadirpekel@gmail.com>"
@@ -12,4 +12,3 @@
, "url": "http://github.com/coffeemate/meryl"
}
}
-
Please sign in to comment.
Something went wrong with that request. Please try again.