Skip to content

Instantly share code, notes, and snippets.

@nenjiru
Created March 12, 2013 11:29
Show Gist options
  • Select an option

  • Save nenjiru/5142190 to your computer and use it in GitHub Desktop.

Select an option

Save nenjiru/5142190 to your computer and use it in GitHub Desktop.
Cakefile for coffee script
# http://qiita.com/items/57ccb570f02799efd9d3
fs = require 'fs'
path = require 'path'
{spawn, exec} = require 'child_process'
option '-o', '--output [DIR]', 'Output directory.'
option '-t', '--target [DIR]', 'Watch target directory.'
#-------------------------------------------------------------------------------
# Settings
#-------------------------------------------------------------------------------
#パスは Cakefile からの相対
COFFEE_DIRECTORY = './coffee/'
#sourcemap や結合前のJSを出力
TEMPORARY_DIRECTORY = './tmp/'
#-------------------------------------------------------------------------------
# Import files
#-------------------------------------------------------------------------------
SOURCE_FILES = [
#common
'cmn/geom/M22.coffee',
'cmn/display/Graphic.coffee',
#project
'pantira/geom/Vertex.coffee',
#only dev
{ 'dev' : 'dev.coffee' },
#entry point
'main.coffee',
]
#-------------------------------------------------------------------------------
# Method
#-------------------------------------------------------------------------------
#Remove directory
makeDirectory = (directory) ->
try
fs.statSync directory
catch error
fs.mkdirSync directory
return directory
#Compile and create souremap
mapCompile = (filelist, output, watch = false) ->
for file in filelist
copy = "#{output}#{file.replace /\//g, '.'}"
copy = copy.replace /\.\./g, ''
fs.writeFileSync copy, fs.readFileSync file
options = ['-cbm', copy]
coffee = spawn 'coffee', options
console.log 'compile: ' + copy
#TODO 更新されたファイル名をしりたい
if watch is true
fs.watchFile file, { interval: 0 }, () ->
mapCompile filelist, output, false
console.log 'complete'
return
# Inculude js file
inculude = (target, files, attach) ->
code = '<!-- INCLUDE-SCRIPT -->\n'
files = []
for file in SOURCE_FILES
if typeof file is 'string'
code += "<script src=\"#{TEMPORARY_DIRECTORY + file}\"></script>\n"
else if file[attach]
code += "<script src=\"#{TEMPORARY_DIRECTORY + file}\"></script>\n"
code += '<!-- //INCLUDE-SCRIPT -->'
# fs.readFile target, (err, data) ->
# console.log arguments
stat = fs.statSync target
console.log(stat, target);
# fd = fs.openSync target, 'r'
# bytes = fs.readSync fd, stat.size, 0, "ascii"
# fileContent = bytes[0]
# fs.closeSync fd
#
# fd = fs.openSync target, 'w'
# console.log fd
# fs.writeSync fd, str, 0, "ascii"
# fs.closeSync fd
# fs.createReadStream target, fs.createWriteStream target
#var html = file.read(target).replace(/<!-- INCLUDE-SCRIPT -->[\s\S]*<!-- \/\/INCLUDE-SCRIPT -->/m, code);
#file.write(target, html);
return
###
変更分だけコンパイルする目的だが、リネームしてるためソースマップのリンクが切れる
#Compile and Soure map
mapCompile = (filelist, output, watch = false) ->
for file in filelist
options = ['-cmb', file]
if watch is true
options[0] = '-cmbw'
coffee = spawn 'coffee', options
coffee.stdout.on 'data', (data) ->
filename = data.toString().split(' ')[3].replace /\n/g, ''
tmpfile = filename.replace /\//g, '.'
tmpfile = tmpfile.replace '..', TEMPORARY_DIRECTORY
fs.rename filename.replace('.coffee', '.js'), tmpfile.replace('.coffee', '.js')
fs.rename filename.replace('.coffee', '.map'), tmpfile.replace('.coffee', '.map')
return
console.log 'complete'
return
###
#Inculude js file
devInculude = (targetHTML) ->
files = []
for file in SOURCE_FILES
files.push COFFEE_DIRECTORY + file
makeDirectory(TEMPORARY_DIRECTORY)
#mapCompile files, TEMPORARY_DIRECTORY, true
inculude targetHTML, SOURCE_FILES, 'dev'
return
#Lump Build 先頭から結合
#lumpBuild = (watch, output = './js', target = './coffee') ->
#
# makeDirectory(output)
# mapCompile getCoffeePaht output, source, true
#
# return
# Gat coffee file index
getCoffeePaht = (directory) ->
pathlist = []
filelist = fs.readdirSync directory
#ファイルを再帰的に取得
for item in filelist
file = path.join directory, item
stat = fs.statSync file
if stat.isFile() and /.*\.coffee$/.test file
#拡張子 .coffee$ ならファイルリストに追加
pathlist.push file
else if stat.isDirectory()
#ディレクトリなら再帰呼び出し
pathlist = pathlist.concat getCoffeePaht file
return pathlist
#style = (watch) ->
# console.log 'Watching compass files.'
#
# options = ['compile']
#
# if watch is true
# options = ['watch']
#
# compass = spawn 'compass', options
# compass.stdout.on 'data', (data) -> stdout_handler
# compass.stderr.on 'data', (data) -> stdout_handler
# Dump
stdout_handler = (data) ->
console.log data.toString().trim()
#-------------------------------------------------------------------------------
# Tasks
#-------------------------------------------------------------------------------
task 'dev', 'develop build', ->
devInculude('../index.html')
#task 'watch', 'watch for changes and rebuild', (options) ->
# build true, options.output, options.target
# style true
#
#task 'copy', 'copy coffee file.', ->
# fileCopy()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment