grunt.util.spawn doesn't give output - coffeescript

Grunt.util.spawn is not calling the done function. Here is the code. when the command gets executed from command prompt it is failing and throws an error message which is not captured in gruntjs..what is happening
module.exports = (grunt)->
grunt.initConfig
concurrent:
dev: ['watch']
slim:
dev:
expand: true
src: 'views'
dest: 'views/'
watch:
slim:
files: 'views/**.slim'
tasks:['slim']
grunt.registerMultiTask 'slim', 'Server Slim Compiler', ->
console.log 'In the slim'
options = {}
options.cmd = 'dirld'
options.grunt = true
console.log options
grunt.util.spawn options, (err,res,cod)->
console.log 'in the spawn'
if err
grunt.log.error err
else
grunt.log.oklns 'success'
grunt.log.writeln res
grunt.loadNpmTasks 'grunt-contrib-watch'
grunt.loadNpmTasks 'grunt-concurrent'
grunt.registerTask 'default', ['concurrent:dev']

grunt.util.spawn is asynchronous so you need to make your slim task async. See: http://gruntjs.com/frequently-asked-questions#why-doesn-t-my-asynchronous-task-complete
grunt.registerMultiTask 'slim', 'Server Slim Compiler', ->
done = #async()
grunt.util.spawn options, (err,res,cod) ->
done() # Im done, continue onto the next task

Related

Grunt task for uploading a file to CouchDB using request

In my Gruntfile.coffee, I have set up a task:
grunt.registerTask 'couch_upload', 'CouchDB upload', ->
done = #async()
assets = ['combined.min.js', 'screen.min.css']
ddoc = 'http://localhost:5984/mydb/_design/app'
request = require 'request'
fs = require 'fs'
grunt.log.writeln 'Uploading assets...'
upload = (asset) ->
# get the revision of the design document
request.get {json: yes, url: ddoc}, (err, resp, body) ->
rev = body._rev
# stream the file and pipe it, so it has the correct Content-Type
fs.createReadStream('build/' + asset).pipe request.put
auth: user: 'myusername', password: 'mypass'
headers: 'If-Match': rev
url: [ddoc, asset].join '/'
, -> grunt.log.write ' ' + asset
upload asset for asset in assets
done()
but I cannot see anything uploaded, neither on Grunt's log, nor Couch's log.
Can you tell if I'm doing something wrong?
Sounds like you need to open a feature request to CouchDB-Lucene: https://github.com/rnewson/couchdb-lucene/issues — it doesn’t look like it has that feature enabled yet, if it is at all possible.

Coffeescript member is undefined

I get the following error:
Uncaught TypeError: Cannot call method 'push' of undefined
In the next code:
class classDemo
names : ['t1', 't2']
methodM1: () ->
# This works:
#names.push 't3'
console.log #names.toString()
#socket = io.connect()
#socket.on 'connect', () ->
# This raise the error:
#names.push 't4'
console.log #names.toString()
Does anyone know how to push into "names" inside the socket.on method? (How to push 't4' correctly?
Thanks
EDIT: The solution proposed by #Sven works for one level of chaining. It seems to fail for two chained calls. Please consider the following example:
methodM1: () ->
_this = #
#socket = io.connect() # connect with no args does auto-discovery
#socket.on 'connect', () ->
# This works:
_this.names.push 'inside connect'
console.log _this.names.toString()
#socket.emit 'getModels', (data) ->
# This does not work:
_this.names.push 'inside emit'
console.log _this.names.toString()
I tried to apply the same solution again inside connect and before emit (see below) but I get no output:
_this2 = _this
#socket.emit 'getModels', (data) ->
_this2.names.push "inside emit"
console.log _this2.names.toString()
Thanks.
your emit is never fired because emit sends data and requieres therefore a datastructure.
Please change your code like this
a) use the fat arrow
b) emit a data structure
methodM1: ->
#socket = io.connect()
#here use the fat arrow it does the '_this = # automatically'
#socket.on 'connect', =>
#names.push 'inside connect'
console.log _this.names.toString()
#socket.emit 'getModels', yo: "got your message"
The fat arrow always binds to the outer instance (see When does the "fat arrow" (=>) bind to "this" instance)
I am not sure (well, I am pretty sure but havent tried it) that you can send a closure over the wire.

CoffeeScript and Cake Error

I try to get the cake example from http://arcturo.github.io/library/coffeescript/05_compiling.html to run. But that leads to a strange error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: spawn ENOENT
at errnoException (child_process.js:980:11)
at Process.ChildProcess._handle.onexit (child_process.js:771:34)
This is my Cakefile (just copied from "Little book on CoffeeSCript")
fs = require 'fs'
{print} = require 'sys'
{spawn} = require 'child_process'
build = (callback) ->
coffee = spawn 'coffee', ['-c', '-o', 'lib', 'src']
coffee.stderr.on 'data', (data) ->
process.stderr.write data.toString()
coffee.stdout.on 'data', (data) ->
print data.toString()
coffee.on 'exit', (code) ->
callback?() if code is 0
task 'build', 'Build lib/ from src/', ->
build()
I'm using Coffee 1.6.3 and node 0.10.20.
Does anyone know what I'm doing wrong?
Thanks!
ENOENT typically means "I looked for the thing you told me to find and I didn't find it". From the example page:
For example, create a file called Cakefile, and two directories, lib and src.
Do you have both of those?
I've found an explanation for what is happening here:
Using nodejs's spawn causes "unknown option -- " and "[Error: spawn ENOENT]" errors
The solution was to use exec instead of spawn
On Windows, spawn doesn't handle '.cmd' or '.bat' without file extension.
repalce
coffee = spawn 'coffee', ['-c', '-o', 'lib', 'src']
with
coffee = spwan 'coffee.cmd', ['-c','-o','lib','src']

How to test a Controller which relies on an ngResource Service

I have a controller which relies on a service built through ngResource. I am having trouble testing this(although both appear to work like a charm in the actual application). The following is the (sanitized) Controller
MyApp.Controller.MyCarsController = (scope, http, typeService) ->
if scope.context==undefined
scope.ferrari_or_porshe =""
scope.id = ""
else if scope.context=="ferrari"
scope.country_or_pi ="Ferrari"
else if scope.context=="porshe"
scope.country_or_pi ="Porshe"
typeService.index
ferrari_or_porshe: scope.ferrari_or_porshe
id: scope.id
, (response) ->
scope.type = response
scope.loading = false
MyApp.Controller.MyCarsController.$inject = ['$scope', '$http', 'Type']
And this is the Service:
MyApp.MyModule.factory 'Type', ['$resource', ($resource) ->
TypeResource = $resource("/api/types/:ferrari_or_porshe/:id", {},
index:
method: "GET"
isArray: true
)
return TypeResource
]
Finally, some test code:
describe 'MyApp.Controller.MyCarsController', ->
beforeEach module('MyModule')
beforeEach inject ($rootScope, $http, $controller, Type) ->
#scope = $rootScope.$new()
#typeService = Type
#scope.context = undefined
$controller 'MyApp.Controller.MyCarsController', $scope: #scope
describe '#home-page', ->
it 'contains a list of types', ->
expect(#scope.types.length).toBeGreaterThan 0
it "sets instance variables correctly", ->
expect(#scope.ferrari_or_porshe).toBe ""
expect(#scope.id).toBe ""
Which fails with:
No more request expected in helpers/angular-mocks.js on line 889
TypeError: 'undefined' is not an object (evaluating 'this.scope.types.length') in controllers/my_cars_controller_spec.js
By judicious application of console.logs, I have discovered that the issue is that the final callback on response is never reached. TypeResource comes back as [Function].
My questions is:
How do I drive the Jasmine Tests to correctly enter the Service and fetch a response? And is there any way to create direct Unit Tests for Services?
Any and all help is appreciated
The Solution is as follows: for the Service, use $httpBackend which is bundled as part of ngMock:
http://code.google.com/p/google-drive-sdk-samples/source/browse/ruby/app/lib/angular-1.0.0/angular-mocks-1.0.0.js?r=c43c943e32be395b7abca8150deb301d3cbc0dbe
Use this to mock the Rest responses. Since in my case I only cared about verifying that a GET request goes out:
describe 'Type', ->
describe '#index', ->
beforeEach module('MyModule')
beforeEach inject(($httpBackend) ->
$httpBackend.whenGET('/api/types/ferrari/1').respond([])
)
it 'for a ferrari scope', inject((Type) ->
ferrari_or_porsche = 'ferrari'
id = '1'
expect( Type.index('ferrari_or_porsche': ferrari_or_porsche, 'id': id) ).toEqual([ ])
)
And then for the controller, mock the service using jasmine spies and use jasmine.any(Function) to warn of the callback.
describe 'MyApp.Controller.MyCarsController', ->
beforeEach module('myModule')
beforeEach inject ($rootScope, $http, $controller, Type) ->
#scope = $rootScope.$new()
#typeService = Type
#scope.context = undefined
spyOn(#typeService, 'index')
describe '#home-page', ->
beforeEach inject ($controller) ->
$controller 'MyApp.Controller.MyCarsController', $scope: #scope
it 'contains a list of types', ->
expect(#typeService.index).toHaveBeenCalledWith({ ferrari_or_porsche : '', id : '' }, jasmine.any(Function))
it "sets instance variables correctly", ->
expect(#scope.ferrari_or_porsche).toBe ""
expect(#scope.id).toBe ""
Note: I make no claims as to the "canonicalness" of this solution. But it works.
Note: The API endpoints are of course tested extensively elsewhere.

How can I do continuous build/unit testing in Coffeescript?

I'm writing a (ever larger) set of unit tests using Coffeescript and node.js. I build the files using the coffee "watch" option (-w)
coffee -w -b -c -o web/ src/
My problem is that running the unit tests takes 20 secs (I'm assuming for the compile to .js).
If possible, I'd like automatically run the unit tests on a (compiled .js) file change, which would eliminate the long wait for the results.
My current Cakefile:
fs = require 'fs'
{print} = require 'sys'
{spawn, exec} = require 'child_process'
build = (watch, callback) ->
if typeof watch is 'function'
callback = watch
watch = false
options = ['-c', '-b', '-o', 'web', 'src']
options.unshift '-w' if watch
coffee = spawn 'coffee', options
coffee.stdout.on 'data', (data) -> print data.toString()
coffee.stderr.on 'data', (data) -> print data.toString()
coffee.on 'exit', (status) -> callback?() if status is 0
task 'test', 'Run the test suite', ->
build ->
require.paths.unshift __dirname + "/lib"
{reporters} = require 'nodeunit'
process.chdir __dirname
reporters.default.run ['test']
Take a look at the Cakefile for my connect-assets project: https://github.com/adunkman/connect-assets/blob/master/Cakefile
It's a bit more complex than sstephenson's (which I assume your example is derived from), but it shows how you can watch a directory of files for changes and respond to those changes by re-running tests.