2013-03-30 13 views
29

Hubot è il robot di chatroom di Github. È un ottimo strumento, tranne per il fatto che nessuno nella nostra azienda vuole scrivere in Coffeescript .... ma sembra che non possiamo scrivere script per Hubot in un semplice vecchio Javascript.
È vero? C'è qualcosa che mi manca qui? Coffeescript è "solo javascript" ma non posso usare Javascript con esso?
EDIT
stavo facendo 2 errori assurdamente semplice:
- ho copiato il commento sintassi CoffeeScript nel mio file JS
- ho avuto la sceneggiatura sotto le hubot-scripts node_module, invece di appena sotto le/scripts/directory nel progetto principale.Posso scrivere script per hubot in Javascript?

Funziona perfettamente ora.

risposta

20

CoffeeScript è compilato in JavaScript, ma non è un superset di JavaScript, quindi il codice JavaScript non è necessariamente codice CoffeeScript valido.

Tuttavia, dopo aver guardato at the source, sembra che Hubot può accettare sia:

# Public: Loads a file in path. 
    # 
    # path - A String path on the filesystem. 
    # file - A String filename in path on the filesystem. 
    # 
    # Returns nothing. 
    loadFile: (path, file) -> 
    ext = Path.extname file 
    full = Path.join path, Path.basename(file, ext) 
    if ext is '.coffee' or ext is '.js' 
     try 
     require(full) @ 
     @parseHelp "#{path}/#{file}" 
     catch error 
     @logger.error "Unable to load #{full}: #{error.stack}" 
     process.exit(1) 

Questo metodo viene chiamato da loadHubotScripts.

+0

Grazie, che il codice (che ho avuto difficoltà a trovare) mi ha portato al mio problema. Avevo i file nel posto sbagliato e ho copiato la sintassi dei commenti di Coffeescript nel mio file JS. –

28

Sì, è possibile scrivere gli script di hubot in puro JavaScript. Di seguito è riportato un semplice script hubot scritto in puro JavaScript e mettere sotto la /scripts/ directory del mio hubot personalizzato:

// Description: 
// holiday detector script 
// 
// Dependencies: 
// None 
// 
// Configuration: 
// None 
// 
// Commands: 
// hubot is it weekend ? - returns whether is it weekend or not 
// hubot is it holiday ? - returns whether is it holiday or not 

module.exports = function(robot) { 
    robot.respond(/is it (weekend|holiday)\s?\?/i, function(msg){ 
     var today = new Date(); 

     msg.reply(today.getDay() === 0 || today.getDay() === 6 ? "YES" : "NO"); 
    }); 
} 
+2

Usa questo sito per convertire i tuoi script di caffè in js http://js2.coffee/ :) –

Problemi correlati