Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
gulp+webpack環境でcoffeeのエラーをGrowlとtmuxへ出力するタスク
#-------------------------------------------------------------------------------
# オプション解析
#-------------------------------------------------------------------------------
minimist = require 'minimist'
OptionUtil = ->
knownOptions =
string: 'env'
default:
env: process.env.NODE_ENV || 'development'
options = minimist process.argv.slice(2), knownOptions
subFuncs =
env: options.env
isProduction: ->
options.env == 'production'
module.exports = OptionUtil
#-------------------------------------------------------------------------------
# ライブラリ読み込み
#-------------------------------------------------------------------------------
gulp = require 'gulp'
gutil = require 'gulp-util'
path = require 'path'
handleErrors = require '../util/handleErrors.coffee'
isProduction = require('../util/checkEnv.coffee')().isProduction()
# コンフィグ
config = require('../config.coffee').coffee
# coffee,JavaScript関連
coffee = require 'gulp-coffee'
webpack = require 'webpack-stream'
plumber = require 'gulp-plumber'
#-------------------------------------------------------------------------------
# coffee
#-------------------------------------------------------------------------------
gulp.task 'coffee', ->
gulp.src config.src
.pipe webpack config.webpack, null, (error, stats) ->
convRelativePath = (filepath) ->
path.relative process.cwd(), filepath
for err in stats.compilation.errors
# エラー時にアラートを表示
handleErrors err, false, (msg) -> # エラーログのフォーマット調整関数
if typeof msg == 'object' && msg.module?
file = convRelativePath msg.module.userRequest
# stderrとgrowl用
message = msg.toString()
.replace(/ModuleError:\s*/m, '')
.replace(new RegExp(process.cwd(), 'g'), '.')
# tmux用
tmuxMessage = ''
for msg in message.replace(/(\u001b|\[[0-9;]+m)/g, '').split '\n'
# エラー行のみ抽出するがcoffee-lintのmax_line_lengthの警告は使わない
if msg.match(/^\s*\d+\s*error\s/)? && !msg.match(/max_line_length$/)?
tmuxMessage += ' // ' if tmuxMessage != ''
tmuxMessage += msg.replace /^\s*\d+\s*error\s+/, ''
{
title: file
file: file
message: message
tmuxMessage: '[' + file + '] : ' + tmuxMessage
}
break
.pipe gulp.dest config.dest
# https://raw.githubusercontent.com/greypants/gulp-starter/master/gulp/util/handleErrors.js
# 上記の物を元に魔改造してます
notify = require 'gulp-notify'
notifier = require 'gulp-notify/node_modules/node-notifier'
gutil = require 'gulp-util'
exec = require('child_process').exec
module.exports = ->
[error, canEnd, formatter] = Array.prototype.slice.call arguments
notifyMessage = { title: 'Compile Error', message: '' }
if formatter? && typeof formatter == 'function'
try
msg = formatter error if formatter?
catch e
gutil.log '[notify formatter] ' + e.toString()
if msg instanceof Object && !(msg instanceof Array)
notifyMessage.title = msg.title if msg.title?
notifyMessage.message = msg.message if msg.message?
notifyMessage.file = msg.file if msg.file?
notifyMessage.tmuxMessage = msg.tmuxMessage if msg.tmuxMessage?
notifyMessage.message = msg.toString() if notifyMessage.message == ''
else
notifyMessage.message = error.toString().trim() if error?
# remove the escape sequence of message
notifyMessage.notifier = (options, callback) ->
# remove the same top line as the title
firstTitleLine = new RegExp('^[\./\s\S]*' + options.title + '\s*\n')
options.message = options.message
.replace(/(\u001b|\[[0-9;]+m)/g, '')
.trim()
.replace(firstTitleLine, '')
notifier.notify options, callback
# Send error to notification center with gulp-notify
notify.onError(notifyMessage).call @, error
# tmux display-message
displayTime = null
exec 'ps alx | grep tmux | grep -v grep | wc -l', (err, stdout, stderr) ->
try
exec 'tmux show-option -g display-time', (err, stdout, stderr) ->
if stdout.match /display-time [\d]+/
displayTime = parseInt stdout.split(' ')[1].trim()
exec 'tmux set-option -g display-time 3500'
message = ''
if notifyMessage.tmuxMessage?
message = notifyMessage.tmuxMessage
.replace(/(\u001b|\[[0-9;]+m|\n)/g, '')
else
message = notifyMessage.message.replace(/(\u001b|\[[0-9;]+m|\n)/g, '')
exec 'tmux display-message \'' + message + '\'', (err, stdout, stderr) ->
exec 'tmux set-option -g display-time' + displayTime if displayTime?
catch
exec 'tmux set-option -g display-time' + displayTime if displayTime?
# Keep gulp from hanging on this task
if !canEnd? || canEnd
@emit 'end'
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment