Last active
December 11, 2015 23:59
-
-
Save gawaooooo/4680776 to your computer and use it in GitHub Desktop.
Cakefile 更新版
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs = require 'fs' | |
{exec, spawn} = require 'child_process' | |
{parser, uglify} = require 'uglify-js' | |
files = [] | |
# 指定されたパスのファイルを取得する | |
get_file = (path) -> | |
text = '' | |
stat = fs.statSync(path) | |
if stat.isDirectory() | |
f = fs.readdirSync(path).sort() | |
path_base = path.replace(/\/$/, '') + '/' | |
for file in f | |
get_file(path_base + file) | |
else if stat.isFile() | |
# js or coffee | |
files.push path if path.match(/^(.+)(\.js$|\.coffee$)/) | |
files | |
# defaultのディレクトリ設定 | |
def = | |
src: './src' | |
dist: './dist' | |
# commandを実行 | |
# TODO execより spwanの方がいいのかもしれない | |
run = (cmd, args, cb, err_cb) -> | |
console.log "call run: cmd:#{cmd} , args: #{args.join(' ')}" | |
exec "#{cmd} #{args.join(' ')}", (err, sout, serr) -> | |
if err isnt null | |
console.error serr | |
if typeof err_cb is 'function' | |
err_cb() | |
else | |
throw "Failed command execution (#{err})." | |
else | |
cb() if typeof cb is 'function' | |
# ファイルorディレクトリを指定してコンパイル | |
# src -> ディレクトリorファイル path | |
# dist -> ディレクトリ | |
compile = (src, dist, bare, cb) -> | |
src = src or def.src | |
dist = dist or def.dist | |
bare = bare or '' | |
console.log "\n Compile...: #{src} to #{dist}. option is #{bare} \n\n" | |
run 'coffee', ['-o', dist, bare, '-c', src], cb, (sout) -> | |
console.log 'finish compile', sout | |
# ファイルorディレクトリを指定してminify | |
# src -> minifyするファイルorディレクトリ | |
minify = (src, cb) -> | |
src = src or './dist' | |
console.log "\n Compressing...: #{src}\n\n" | |
# fileのlistを取得 | |
minFiles = get_file(src) | |
for f in minFiles | |
if f.match(/\.min\.js$/) | |
continue | |
if f.match(/^(.+)\.js$/) and f | |
console.log "compress #{f}" | |
minFile = "#{RegExp.$1}.min.js" | |
### | |
exec "uglifyjs -o #{minFile} #{f}", (err, sout, serr) -> | |
console.log "finish compress... " | |
### | |
### | |
run 'uglifyjs', ['-o', minFile, f], cb, (sout) -> | |
console.log "finish compress...", sout | |
### | |
# closure compiler | |
run 'java', ['-jar', 'compiler.jar', '--js', f, '--js_output_file', minFile], cb, (sout) -> | |
console.log "finish compress...", sout | |
# TODO watch | |
watch = (options, cb) -> | |
src = options.src or def.src | |
dist = options.dist or def.dist | |
if options.bare? then bare = '-b' | |
else bare = '' | |
console.log "\n Start Watch changes \n\n" | |
watchFiles = get_file(src) | |
console.log ' watch files start... \n' | |
for file in watchFiles then do (file) -> | |
console.log "#{file}" | |
fs.watchFile file, (curr, prev) -> | |
if +curr.mtime isnt +prev.mtime | |
console.log "\n Saw change in #{file} \n" | |
file.match /.*\/(.+)$/ | |
distDir = file.replace(RegExp.$1, '').replace(src, dist) | |
console.log "#{file} compile to #{distDir} directory" | |
compile file, distDir, bare, -> | |
console.log "#{distDir}#{RegExp.$1}".replace('coffee', 'js') | |
cb("#{distDir}#{RegExp.$1}".replace('coffee', 'js')) if typeof cb is 'function' | |
# TODO docco | |
doc = (src) -> | |
src = src or options.src | |
console.log "\n Generation of a document is started... \n\n" | |
files = get_file(src) | |
run 'docco', [files.join(' ')], (sout) -> | |
console.log "\n Document generation is completed. Please check the 'docs' folder." | |
# optionでsrcとdistディレクトリを指定出来るようにする | |
option '-s', '--src [DIR]', 'ソースとなるCoffeeScriptが格納されたディレクトリ またはファイル' | |
option '-o', '--dist [DIR]', '出力先のディレクトリ' | |
option '-b', '--bare', '-b指定でセーフティ関数を使わない' | |
# TODO ディレクトリのコンパイルも出来るし、ファイルのコンパイルも出来るようにしたい | |
task 'compile', '対象ディレクトリのcoffeeファイルをコンパイルする(default >> -s ./src -o ./dist)', (options) -> | |
src = options.src or def.src | |
dist = options.dist or def.dist | |
if options.bare? then bare = '-b' | |
else bare = '' | |
### | |
exec "coffee -o #{dist} #{bare} -c #{src}", (err, sout, serr) -> | |
console.log 'finish compile' | |
### | |
### | |
run 'coffee', ['-o', dist, bare, '-c', src], (sout) -> | |
console.log 'finish compile', sout | |
### | |
compile src, dist, bare | |
task 'min', '対象ディレクトリのファイルを圧縮(default >> -s ./dist)', (options) -> | |
# TODO ディレクトリに、*.min.js ファイルがある場合は、そのファイルも圧縮されてしまうので注意 | |
src = options.src or './dist' | |
minify src | |
task 'cam', '対象ディレクトリのファイルをコンパイル&圧縮(default >> -s ./src -o ./dist) ', (options) -> | |
src = options.src or def.src | |
dist = options.dist or def.dist | |
if options.bare? then bare = '-b' | |
else bare = '' | |
compile src, dist, bare, -> | |
minify dist | |
# CoffeeScriptの監視 | |
# TODO -cをしないでwだけやって、変更されたsrcをcompileするようにしたらいいのかも | |
task 'watch', '対象ディレクトリのファイルの変更を監視&コンパイル(default >> -s ./src -o ./dist)', (options) -> | |
src = options.src or def.src | |
dist = options.dist or def.dist | |
if options.bare? then bare = '-b' | |
else bare = '' | |
### | |
run 'coffee', ['-o', dist, bare, '-c', '-w', src], (sout) -> | |
console.log 'finish compile', sout | |
### | |
### | |
watch = spawn 'coffee', ['-w', src] | |
watch.stderr.on 'data', (data) -> | |
process.stderr.write data.toString() | |
watch.stdout.on 'data', (data) -> | |
console.log data.toString() | |
console.log 'file changed' | |
console.log watch | |
### | |
compile src, dist, bare, -> | |
watch options | |
task 'wam', '対象ディレクトリのファイルの変更を監視&コンパイル&圧縮(default >> -s ./src -o ./dist)', (options) -> | |
src = options.src or def.src | |
dist = options.dist or def.dist | |
if options.bare? then bare = '-b' | |
else bare = '' | |
compile src, dist, bare, -> | |
watch options, minify | |
# document | |
task 'doc', 'doccoを使用してdocsにドキュメントを作成(default >> -s ./src)', (options) -> | |
src = options.src or def.src | |
doc src |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment