Skip to main content

· 4 min read

Hubo

We (Fred Lin & Ray Lin) have ported Github's popular chat robot framework 'Hubot' from Coffeescript to plain Javascript with ES6 features. Currently I name it webbybot to denote ES6 version of hubot, to better test the port result and avoid the naming confusion with the original hubot.

Now webbybot is fully functional and still support all coffeescript written plugins. If you have an existing bot generated by hubot-generator, you can install webbybot via npm install webbybot command, follow with simple instruction and see your bot works smoothly with webbybot core.

(For experienced developer, the instruction guide you to replace script in generated '/bin' folder from 'hubot' to 'webby'. And change the adapter library import from 'hubot' to 'webbybot'.)

As time goes to 2016, there're less reason to use Coffeescript instead of standard ES6 Javascript. During the porting we do learned something that might helpful for your projects.

1. Use npm script directly instead of gulp or grunt

With npm we can define some scripts directly in the 'script' attribution of 'package.json' file. Webbybot use 'npm run build' command to compile ES6 to plain javascript via Babel. Do style checking via 'npm run lint' command.

2. Use babel directly instead of webpack

At the beginning we count on webpack's babel-loader to convert ES6 to plain javascript. Alas its a beginning of 'Try and Error' journey. Webpack is originally designed for front-end packaging and works very well on that purpose. But for backend program like webbybot that feature is not important for us. Webpack also bring 'require' keyword to the front-end, but its not suit for backend program that depends on dynamic import. As a framework, hubot heavily counts on NODE require to load plugins. Webpack treat all 'require' as its keyword and try hard in vain to find external modules from packed files.

We tried several ways to detour these side effects, and finally replaced the full webpack stack with one line npm build script. Now its easier to debug and no hacks needed in source code.

3. return or not return, its the question

We use the "Try Coffeescript" utility provide form Coffeescript official site, which you can paste Coffeescript the page will convert the source to Javascript instantly. The converted code is... not all pretty for human read, and all converted functions will contain a return statement even its unnecessary. It needs check by hand.

4. test cases matter

Hubot itself contains good coverage of unit tests. So we are able to test one ported script file with one ported test file when we start the porting. The unit test files contain great number of redundant return sentences when convert from coffeescript.

5. class and super

Hubot use Class syntax from Coffeescript。Thanksfully ES6 support the Class syntax, which is a bit different from Coffeescript. You can check how to use Class and super on MDN.

6. Default + rest + spread

Hubot contain several syntax like 'reply(strings...)'. The syntax 'strings...' in Coffeescript is correspondent to "...strings" in ES6(The order of '...' is reversed). '...strings' denotes an array and I feel its a bit hard to figure out when to expand it or not.

7. for..of instead of for..in

To use for..in loop in ES6, we need add 'hasOwnProperty' check to make sure inherited property are not looped. Or we can rewrite for..in loop in Coffeescript to forEach iteration. Though there are some cases that need 'break' or 'return' from a loop.

Now we use ES6 for..of loop in Webbybot to replace forEach and for..in loop. You need wrap object with Object.keys syntax to iterate with object. ex: for (let item of Object.keys(TargetObj)) {...}.

8. Object.assign instead of Extend

We can use Object.assign to extend a object without handmade extend function or lodash!

9. Do you know hubot also support write plugin with plain javascript?

Learning how chat bot works is the main reason we start porting hubot to ES6! A simple plain javascript plugin could be as easy as: (src/simple.js)

> module.exports = function(robot) {
>   robot.respond(/(hello)/i, function(res) {
>     res.send('hi');
>   });
> }You can put it in generated plugin folder and it will just work.

I've created a ticket on hubot issue list to start a discussion if hubot would like to go with ES6 in its future version.

The webbybot source is at https://github.com/gasolin/webbybot

Do you have a project ported from coffeescript to ES6? Welcome to drop by your thoughts.

· 6 min read

Hubo

(English Version available here

用兩周多的時間,路路續續把原本用 Coffeescript 撰寫的 Hubot 聊天機器人框架移植成使用到 ES6 特性的純 Javascript 版本。 現在這個移植版本已經可供使用。移植後依然可以使用 Hubot 原來以 Coffeescript 撰寫的各種擴充套件 (Plugins) 。 https://github.com/gasolin/webbybot (目前的版本完全移植 Hubot 的功能,已開 Issue 詢問 Hubot 是否有興趣 merge 回去,在此之前先放在自己的 webbybot repo 裡) 移植的動機之一是為了了解整個聊天機器人框架,為後續可能的修改打基礎。 其二則是嘗試平常不常用到的 ES6 新特性。 過去 Coffeescript 和 Typescript 等最終編譯成 Javascript 執行的語言,都走在 Javascript 之前,提供了許多語法上的新特性。但在 2015 年 Javascript 開始的新發佈規劃 (一年一版) 下,ES6 (ES2015) 已將 Coffescript 眾多特性都收編了。且現在透過 Babel 可以將 ES6 編譯成現有的 Javascript 直譯器能讀懂的語法。在這樣的趨勢下,過去開發者想為這些新特性多學一套語言的誘因就不再存在了。 移植過程中採用的作法與遇到了一些坑,在此一一列出來。 1. 不用 grunt gulp, 直接使用 npm script 在 package.json 裡可以直接在 "script" 屬性中定義一些要執行的腳本,例如 webbybot 透過 "npm run build" 來將 ES6 轉換成 Javascript 直譯器能讀懂的語法。用 “npm run lint" 來做 style check。 2. 不用 webpack, 直接使用 babel 移植之初決定使用 webpack 的 babel loader 來轉換 ES6,但這是一連串從「錯誤中學習」的開始。 webpack 支援使用 require 命令載入各種檔案,而且 webpack 在轉換的過程中會將檔案打包成一份。但後端程式其實不太需要打包,而且 Hubot 作為一個框架,執行後從外部讀取 Plugin 是非常重要的功能。 使用 webpack 時,它會將程式中出現的 require 都視為它的 require,而嘗試從打包好的檔案中找到我們需要的外部 plugin,結果是徒勞的。當我們換用 npm script 直接呼叫 babel-cli 來打包,原來使用 webpack 時出現的諸多問題也都一併解決了。 3. 該不該 return 在移植的過程中常用到的工具是 Coffeescript 官網的 "Try Coffeescript" 分頁。可以即時將 Coffeescript 結果轉換成 Javascript。 但是真的把程式碼貼上去會發現, Coffeescript 所轉出來的 Javascript 所有的函式都會回傳值。需要手工一個個確認。 4. test cases Hubot 本身有很完整的單元測試。所以每移植一支程式碼時,只要一併移植對應的單元測試,就可以捉對拿來直接測試。單元測試移植時出現最多該不該 return 的問題。 5. class and super Hubot 使用了很多 Coffeescript 的 Class 語法。Class 與 super 的用法在 MDN 上可以找到相關教學。 6. Default + Rest + Spread Hubot 程式碼裡常常會出現諸如 reply (strings...) 的語法。Coffeescript 裡的 "strings..." 可以對應到 ES6 的 "...strings"(... 放置位置剛好相反)。 "...strings" 代表的是一個陣列。在程式中有時要展開有時不用,其實頗令人苦惱。7. 使用 for..of 替換 for.. in 迴圈 碰到 for..in 迴圈有幾種解法,最不會出錯的是將 Array iteration 改寫成 forEach。但是用 forEach ˇ的話無法在執行中使用 break/return 跳出是其缺點。其二是在 for..in 迴圈裡加入 hasOwnProperty 確認不會跑到無關的 function。 在 Webbybot 的第二版中已使用 ES6 支援的 for..of 迴圈 來取代 forEach 和 for..in 迴圈。由於 for..of 迴圈只支援 iteratable ,不支援一般 Object,所以碰到要對 Object 跑迴圈時可以在 Object 外包一層 Object.keys,例如 `for (let item of Object.keys (TargetObj)) {...} 8. 使用 Object.assign 替換 Extend

要擴展一個物件的功能不用再用 lodash 或是自己寫 extend 函式,直接用 Object.assign 吧。

以上是移植過程中碰上的問題。感謝同事 Ray Lin 一起幫忙完成這次的移植。

現在 Webbybot (Hubot ES6 port) 整套都可以用 Javascript 寫了,歡迎試用或上 Patch 喔。

https://github.com/gasolin/webbybot

· 2 min read

前陣子在 Medium 上看到 Tor Bair 寫的 Your Life is Tetris, Stop playing it like Chess, 談到作者認為俄羅斯方塊比起棋局更接近人生。

棋局是種非贏即輸的零和遊戲,而俄羅斯方塊則是不斷重複,幾乎不可能獲得最終勝利的遊戲。

作者的論點如下:

1. 人生中你唯一的對手是你自己

真實的人生遊戲是內化的,而不是靠向外找尋對手或打敗大魔王來獲取積分。

2. 人生中,事情不會變得更難,只會變得更快

3. 人生中,你無法控制接下來會發生什麼

你只知道下一個拿到的方塊是什麼,你只能活在當下,試著組織起最好的結果。

4. 人生中,沒有人告訴你什麼時候你贏了

不是為了贏而遊戲,而是為了樂趣而遊戲。

"人生如俄羅斯方塊",實在是很有趣的人生觀阿!

· 7 min read

20132014年都曬過書架,今年繼續獻曝。(今年能紀錄的共讀 115 本,23539 頁),63 本是電子書(54%),比例首次超過實體書。不過這也許是因為更多原本線上的小說也出了實體書,讓我也得以紀錄下來的關係?

來曬一下今年看過的書,順便從中推薦些好書(由近至遠)

今年的開始似乎是從 Remote Work (中國譯做 重來 2) 開始,看看知名團隊如何遠端工作還蠻新鮮的。

團隊之美
讓我觸動最深的一句話是:「好工具的關鍵不是說不再需要人們,而是讓人們更快樂。」

矽谷之火
全本在 kindle 上看完的矽谷歷史書,

「西部人認為自己是一往無前的牛仔,失敗對於他們來說,不過是一種更快速的獲取經驗的途徑罷了。」

看那些媒體沒告訴我們的個人電腦史,看那些湮沒在時間中,對個人電腦發展留下貢獻的人們。相當精彩

之後看的 黑客列傳 也還可以,沒這本驚豔。

食戟之靈
無論是作者或繪者都超認真地構思與呈現符合每個選手風格的料理

萬曆十五年
禁得起時間考驗,已出版經年的書,讀來還是精彩

頂尖業務員都在用的 3T 筆記術

雖然標題聳動,但內容卻意外地紮實。

書中講到業務要做好的兩個要件:
思考:不斷思考「接下來該做什麼?怎麼做會比較好?」

行動:採取確切行動。

 

和小朋友一起讀童書其實很有趣。當一本書翻多遍時,你可以從中找到許多觀察與講述的方法。

 從 0 到 1

「今天的任務是找到獨一無二的方法創造新事物,不只讓未來變得不一樣,而且要更好,所以我們要從 0 到 1。最重要的第一步是自己獨立思考。唯有重新看待世界,像古人首次見到它那樣覺得新鮮古怪,我們才能重新創造,並將更好的未來留給後世。」

我們已經漸漸實現了過去所想像的未來。現在需要的,是從過去的經驗中,尋找出我們對下個未來的想像。

先讓英雄救貓咪

之前看電影類 yy 小說時反覆被提到的編劇聖經

一個人的會議時間
我還是頗愛看個人時間管理相關的書

價值主張年代
Business Canvas 作為經常被提及與使用的工具,這本書其實不太好讀

今年應該會找日本作者寫的「圖解獲利世代」 看看。

失控

1980 年的大部頭舊書,把人與科技合在一起,當作一個有機生物體來觀察它的發展的想法值得思考。我們現在把手機,Siri 或運用網路的能力視為人的一部分,還能看到個體間更大的差異。

河圖洛書前傳
提到中國上古文化裡其實是有數位基因的,不論真實性如何,就算當作小說看,這樣的立論也很有趣。

計時器讀書法
15min 一段專注力的週期 + 3min 最後的跳躍。
保持非整數時間,讓人有強迫繼續補完的動力,不強制休息時間,讓實行起來更不困難。
都是高明的心理技巧。

這本小書一看完,馬上將手機的倒數計時器設成 18 分鐘。

不賣東西,賣體驗
「這名女性真正想要的並不是卡地亞 Tank Francaise 腕錶這項「商品」,她要的是在聖誕夜,男朋友陪她到銀座卡地亞總店,在裝潢華麗的店裡,接受女店員恭敬的服務,買下女店員用戴著白手套的手,從盒子裡取出的 Tank Francaise 腕錶的那份 ' 體驗 '。」

光看這段就有顛覆三觀之效

為什麼聰明人都用方格筆記本?
看了後腦波很弱地馬上去買了方格筆記本來用。目前使用尚稱順暢。

有沒有 XXX 的八卦
書名不正經,但內容出乎意料的充實

SCRUM 
將敏捷開發法用在各行各業。

有人說過「所有的模型都是錯的,但有些比較好用 (All models are wrong; but some are useful)」,這本有詳細地講解目前科技圈最愛說自己有在用的 SCRUM 開發方法。

小結:

21 世紀,書也已經不是唯一獲取知識的方式。也許該綜合一下,若有方便的方法,也應該把看過的電影 / 按贊過的 Youtube 影片 / SlideShare 等也列一列。

· One min read

I got my Dell XPS 13 (9343) for a while and decide to install Ubuntu 15.10 as duel boot.

By following the guide its pretty easy to get a Ubuntu environment. To setup my daily develop environment, I take the foxbox setup script for 14.04 and everything works fine.

So for windows user who want develop FirefoxOS gaia with real device, I recommend you try foxbox first. If you have more commitment, install the Ubuntu. With duel boot you can keep windows and have a more developer friendly environment to develop connecting devices.