Permalink
Browse files

Compatibility with Node.js 0.8

  • Loading branch information...
1 parent f682452 commit 05933d041af0877fd8caec7952eda1921da5f1d5 @wdavidw committed Jul 27, 2012
View
@@ -12,6 +12,7 @@ module.exports = class NullStream extends events.EventEmitter
write: (data) ->
@emit 'data', data
end: ->
+ @emit 'close'
# Shared API
destroy: ->
destroySoon: ->
View
@@ -26,6 +26,7 @@ module.exports = class Request
q = "#{question.name} "
q += "[#{question.value}] " if question.value
@shell.interface().question q, (answer) ->
+ answer = answer.substr(0, answer.length - 1) if answer.substr(-1, 1) is '\n'
answers[question.name] =
if answer is '' then question.value else answer
next()
@@ -51,6 +52,7 @@ module.exports = class Request
question = @shell.styles.raw( msg, {color: 'green'})
@shell.interface().question question, (answer) =>
accepted = ['', key_true, key_false]
+ answer = answer.substr(0, answer.length - 1) if answer.substr(-1, 1) is '\n'
answer = answer.toLowerCase()
valid = accepted.indexOf(answer) isnt -1
return @confirm.apply(@, args) unless valid
@@ -1,6 +1,5 @@
fs = require 'fs'
-path = require 'path'
crypto = require 'crypto'
Interface = require('readline').Interface
@@ -29,9 +28,9 @@ module.exports = (settings) ->
settings.name ?= hash process.argv[1]
file = "#{settings.dir}/#{settings.file}"
# Create store directory
- fs.mkdirSync settings.dir, 0o0700 unless path.existsSync settings.dir
+ fs.mkdirSync settings.dir, 0o0700 unless fs.existsSync settings.dir
# Look for previous history
- if path.existsSync file
+ if fs.existsSync file
try
json = fs.readFileSync(file, 'utf8') or '[]'
settings.shell.interface().history = JSON.parse json
View
@@ -1,5 +1,7 @@
+fs = require 'fs'
path = require 'path'
+existsSync = fs.existsSync or path.existsSync
start_stop = require '../start_stop'
###
@@ -50,13 +52,13 @@ module.exports = () ->
searchs = if settings.path then [settings.path] else ['app', 'server', 'lib/app', 'lib/server']
for search in searchs
search = path.resolve settings.workspace, search
- if path.existsSync "#{search}"
+ if existsSync "#{search}"
if search.substr(-4) is '.coffee'
then return "coffee #{search}"
else return "node #{search}"
- if path.existsSync "#{search}.js"
+ if existsSync "#{search}.js"
return "node #{search}.js"
- else if path.existsSync "#{search}.coffee"
+ else if existsSync "#{search}.coffee"
return "coffee #{search}.coffee"
throw new Error 'Failed to discover a "server.js" or "app.js" file'
http = null
@@ -1,5 +1,4 @@
-fs = require 'fs'
path = require 'path'
start_stop = require '../start_stop'
View
@@ -1,5 +1,7 @@
+fs = require 'fs'
path = require 'path'
+existsSync = fs.existsSync or path.existsSync
exec = require('child_process').exec
module.exports = (settings) ->
@@ -30,7 +32,7 @@ module.exports = (settings) ->
res.prompt()
paths = [].concat module.paths, require.paths
for p in paths
- if path.existsSync p + '/expresso/bin/expresso'
+ if existsSync p + '/expresso/bin/expresso'
return run p
res.magenta('Expresso not found').ln()
res.prompt()
View
@@ -3,6 +3,7 @@ crypto = require 'crypto'
{exec} = require 'child_process'
fs = require 'fs'
path = require 'path'
+exists = fs.exists or path.exists
md5 = (cmd) -> crypto.createHash('md5').update(cmd).digest('hex')
@@ -58,7 +59,7 @@ module.exports = start_stop =
if code isnt 0
msg = "Process exit with code #{code}"
return callback new Error msg
- path.exists path.dirname(options.pidfile), (exists) ->
+ exists path.dirname(options.pidfile), (exists) ->
return callback new Error "Pid directory does not exist" unless exists
fs.writeFile options.pidfile, '' + pid, (err) ->
callback null, pid
@@ -202,14 +203,22 @@ module.exports = start_stop =
###
file: (options, callback) ->
return callback null, null, false if options.attach
- unless options.pidfile
+ start = ->
+ return pidFileExists() if options.pidfile
dir = path.resolve process.env['HOME'], '.node_shell'
file = md5 options.cmd
- createDir = not path.existsSync dir
- fs.mkdirSync dir, 0o0700 if createDir
options.pidfile = "#{dir}/#{file}.pid"
- path.exists options.pidfile, (exists) ->
- callback null, options.pidfile, exists
+ exists dir, (dirExists) ->
+ return createDir() unless dirExists
+ pidFileExists()
+ createDir = ->
+ fs.mkdir dir, 0o0700, (err) ->
+ return callback err if err
+ pidFileExists()
+ pidFileExists = ->
+ exists options.pidfile, (pidFileExists) ->
+ callback null, options.pidfile, pidFileExists
+ start()
###
View
@@ -1,5 +1,7 @@
+fs = require 'fs'
path = require 'path'
+existsSync = fs.existsSync or path.existsSync
module.exports =
flatten: (arr, ret) ->
@@ -15,7 +17,7 @@ module.exports =
#dirs = require('module')._nodeModulePaths process.cwd()
dirs = require('module')._nodeModulePaths process.argv[1]
for dir in dirs
- if path.existsSync(dir) || path.existsSync(path.normalize(dir + '/../package.json'))
+ if existsSync(dir) || existsSync(path.normalize(dir + '/../package.json'))
return path.normalize dir + '/..'
checkPort: (port, host, callback) ->
cmd = exec "nc #{host} #{port} < /dev/null"
View
@@ -14,7 +14,7 @@
"email": "https://github.com/russfrank"
}],
"engines": {
- "node": ">= 0.1.90"
+ "node": ">= 0.8.0"
},
"dependencies": {
"coffee-script": "latest",
View
0 samples/cloud9/sample.js 100755 → 100644
No changes.
View
0 samples/coffee/lib/.gitignore 100644 → 100755
No changes.
View
0 samples/coffee/sample.coffee 100755 → 100644
No changes.
View
0 samples/error/sample.js 100755 → 100644
No changes.
View
0 samples/http/app.js 100755 → 100644
No changes.
View
0 samples/http/db/.gitignore 100644 → 100755
No changes.
View
0 samples/http/logs/.gitignore 100644 → 100755
No changes.
View
0 samples/http/sample.js 100755 → 100644
No changes.
View
0 samples/http/tmp/.gitignore 100644 → 100755
No changes.
View
4 samples/params/ami.js 100755 → 100644
@@ -1,9 +1,9 @@
#!/usr/bin/env node
- var shell = require('shell');
+ var shell = require('../..');
var exec = require('child_process').exec;
- var app = new shell();
+ var app = shell();
app.configure(function(){
app.use(shell.router({
shell: app
View
2 samples/params/sample.js 100755 → 100644
@@ -1,6 +1,6 @@
#!/usr/bin/env node
- var shell = require('shell'),
+ var shell = require('../..'),
app = shell(),
users = {
'1': 'lulu',
View
@@ -0,0 +1,9 @@
+
+shell = require '..'
+app = shell()
+app.configure ->
+ app.use shell.router shell: app
+
+app.cmd 'test', (req, res, next) ->
+ req.question 'Are you sure [yes|no]', (answer) ->
+ console.log 'answer is: ', answer
View
0 samples/redis/sample.js 100755 → 100644
No changes.
View
0 samples/routes/sample.js 100755 → 100644
No changes.
View
@@ -10,8 +10,8 @@ describe 'req confirm', ->
stdout.on 'data', (data) ->
return unless data.trim()
styles.unstyle(data).should.eql 'Do u confirm? [Yn] '
- @answer = !@answer
- stdin.emit 'data', if @answer then 'y' else 'N'
+ @answer = not @answer
+ stdin.emit 'data', new Buffer(if @answer then 'y\n' else 'N\n')
app = shell
workspace: "#{__dirname}/plugins_http"
command: 'test string'
View
@@ -9,7 +9,7 @@ describe 'Request question', ->
stdout.on 'data', (data) ->
return unless data.trim()
data.should.eql 'My question: '
- stdin.emit 'data', 'My answer'
+ stdin.emit 'data', 'My answer\n'
app = shell
workspace: "#{__dirname}/plugins_http"
command: 'test string'
@@ -28,7 +28,7 @@ describe 'Request question', ->
stdout.on 'data', (data) ->
return unless data.trim()
data.should.eql expects.shift()
- stdin.emit 'data', "Value #{2 - expects.length}"
+ stdin.emit 'data', "Value #{2 - expects.length}\n"
app = shell
workspace: "#{__dirname}/plugins_http"
command: 'test array'
@@ -54,7 +54,7 @@ describe 'Request question', ->
stdout.on 'data', (data) ->
return unless data.trim()
data.should.eql expects.shift()
- stdin.emit 'data', "Value #{3 - expects.length}"
+ stdin.emit 'data', "Value #{3 - expects.length}\n"
app = shell
workspace: "#{__dirname}/plugins_http"
command: 'test object'
View
@@ -1,6 +1,7 @@
fs = require 'fs'
path = require 'path'
+exists = fs.exists or path.exists
should = require 'should'
start_stop = require '../lib/start_stop'
@@ -12,16 +13,16 @@ describe 'StartStop', ->
should.not.exist err
pid.should.be.a 'number'
# Check if process started
- start_stop.running pid, (err, exists) ->
+ start_stop.running pid, (err, running) ->
should.not.exist err
- exists.should.be.true
+ running.should.be.true
# Stop process
start_stop.stop cmd: cmd, (err) ->
should.not.exist err
# Check if process stoped
- start_stop.running pid, (err, exists) ->
+ start_stop.running pid, (err, running) ->
should.not.exist err
- exists.should.be.false
+ running.should.be.false
next()
it 'Test daemon # stop inactive process', (next) ->
cmd = "node #{__dirname}/start_stop/server.js"
@@ -35,31 +36,31 @@ describe 'StartStop', ->
pidfile = "#{__dirname}/start_stop/pidfile"
fs.writeFile pidfile, "1234567", (err) ->
# Check process doesnt exists
- start_stop.running 1234567, (err, exists) ->
+ start_stop.running 1234567, (err, running) ->
should.not.exist err
- exists.should.be.false
+ running.should.be.false
# Stop process
start_stop.stop {cmd:cmd, pidfile: pidfile}, (err, stoped) ->
should.not.exist err
stoped.should.be.false
# Pidfile shall be removed even if pid is invalid
- path.exists pidfile, (exists) ->
- exists.should.be.false
+ exists pidfile, (running) ->
+ running.should.be.false
next()
it 'Test daemon # stop inactive process with pidfile # strict', (next) ->
cmd = "node #{__dirname}/start_stop/server.js"
pidfile = "#{__dirname}/start_stop/pidfile"
fs.writeFile pidfile, "1234567", (err) ->
# Check process doesnt exists
- start_stop.running 1234567, (err, exists) ->
+ start_stop.running 1234567, (err, running) ->
should.not.exist err
- exists.should.be.false
+ running.should.be.false
# Stop process
start_stop.stop {cmd:cmd, pidfile: pidfile, strict: true}, (err, stoped) ->
err.should.be.an.instanceof Error
# Pidfile shall be removed even if pid is invalid
- path.exists pidfile, (exists) ->
- exists.should.be.false
+ exists pidfile, (running) ->
+ running.should.be.false
next()
it 'should throw an error if pidfile is not in a existing directory', (next) ->
cmd = "node #{__dirname}/start_stop/server.js"
@@ -69,21 +70,21 @@ describe 'StartStop', ->
err.message.should.eql 'Pid directory does not exist'
next()
it 'Test attach', (next) ->
- cmd = "#{__dirname}/start_stop/server.js"
+ cmd = "node #{__dirname}/start_stop/server.js"
# Start the process
start_stop.start {cmd: cmd, attach: true}, (err, pid) ->
should.not.exist err
pid.should.be.a 'number'
# Check if process started
- start_stop.running pid, (err, exists) ->
+ start_stop.running pid, (err, running) ->
should.not.exist err
- exists.should.be.true
+ running.should.be.true
# Stop process
start_stop.stop pid, (err) ->
should.not.exist err
# Check if process stoped
- start_stop.running pid, (err, exists) ->
+ start_stop.running pid, (err, running) ->
should.not.exist err
- exists.should.be.false
+ running.should.be.false
next()
View
0 test/start_stop/server.js 100755 → 100644
No changes.
View
No changes.

0 comments on commit 05933d0

Please sign in to comment.