|
| 1 | +path = require 'path' |
| 2 | +fs = require 'fs' |
| 3 | +{exec} = require 'child_process' |
| 4 | +{log, error} = console |
| 5 | +{inspect} = require 'util' |
| 6 | + |
| 7 | +coffee = require '../../node_modules/coffee-script' |
| 8 | +_ = require '../../node_modules/underscore' |
| 9 | +colors = require '../../node_modules/colors' |
| 10 | + |
| 11 | +r = |
| 12 | + # ### exec(cmd, options, callback) |
| 13 | + # |
| 14 | + # Overrides `child_process.r.exec` to log to console and exit if there's an error |
| 15 | + # |
| 16 | + # bakerhelper.exec 'rm *.js', {cwd:'./lib'}, (err, stdout, stderr)-> |
| 17 | + # |
| 18 | + # `options` : See (`child_process`)[http://nodejs.org/api/child_process.html#child_process_child_process_r.exec_command_options_callback] documentation |
| 19 | + # |
| 20 | + # The callback is passed three arguments (`err`, `stdout`, `stderr`) |
| 21 | + exec : (cmd, options, callback=null)-> |
| 22 | + [callback, options] = [options, {}] if not callback? |
| 23 | + log "#{'Executing'.bold.green} `#{cmd}`" |
| 24 | + exec cmd, options, (err, stdout, stderr)-> |
| 25 | + if err? |
| 26 | + error stderr.red |
| 27 | + process.exit 1 |
| 28 | + |
| 29 | + log stdout.grey if stdout? && stdout != '' |
| 30 | + callback?(err, stdout, stderr) |
| 31 | + |
| 32 | + # ### compileCoffeescripts(directory, option={}) |
| 33 | + # |
| 34 | + # Compile all the coffeescripts into javascript files from a directory (not recursive) |
| 35 | + # |
| 36 | + # bakerhelper.compileCoffeescripts './lib/' |
| 37 | + # bakerhelper.compileCoffeescripts './bin/', {shebang:true} |
| 38 | + # |
| 39 | + # `options.shebang` : If it should add a shebang at the top of the file |
| 40 | + compileCoffeescripts: (directory, options={})-> |
| 41 | + # Compile each coffee in js in bin |
| 42 | + directory = path.resolve directory |
| 43 | + fs.readdir directory, (err, files)-> |
| 44 | + files.forEach (file)-> |
| 45 | + # Check if it's a coffee file |
| 46 | + if path.extname(file) is '.coffee' |
| 47 | + filename = "#{directory}/#{file}" |
| 48 | + log "#{'Read'.bold} #{filename.italic}" |
| 49 | + fs.readFile filename, (err, data)-> |
| 50 | + console.error err and process.exit 1 if err? |
| 51 | + |
| 52 | + log "#{'Compile'.bold} #{filename.italic}" |
| 53 | + js = coffee.compile(data.toString()) |
| 54 | + |
| 55 | + # Add a shebang on top |
| 56 | + js = "#!/usr/bin/env node\n#{js}" if options.shebang |
| 57 | + |
| 58 | + # Save to file |
| 59 | + filename = filename.replace /\.coffee$/, '.js' |
| 60 | + log "#{'Write'.bold} to #{filename.italic}" |
| 61 | + |
| 62 | + fs.writeFile filename, js, 'utf8', (err)-> |
| 63 | + error err and process.exit 1 if err? |
| 64 | + |
| 65 | + # ### generateDoccoHusky(directories=[]) |
| 66 | + # |
| 67 | + # Generate doc with [docco-husky](https://github.com/mbrevoort/docco-husky) |
| 68 | + # and push it to the `gh-pages` branch. |
| 69 | + # |
| 70 | + # bakerhelper.compileCoffeescripts ['./lib/', './bin'] |
| 71 | + generateDoccoHusky: (directories)-> |
| 72 | + directories = (_.flatten([directories])).join ' ' |
| 73 | + directory = path.resolve './' |
| 74 | + |
| 75 | + # Create a tmp directoryectory |
| 76 | + r.exec 'mktemp -d', (err, stdout, stderr)-> |
| 77 | + error err if err? |
| 78 | + tmp = stdout.replace(/^\s*|\s*$/g, '') |
| 79 | + |
| 80 | + # clone the git in that temp directory |
| 81 | + r.exec "git clone #{directory} #{tmp}", (err, stdout, stderr)-> |
| 82 | + error err if err? |
| 83 | + log stdout |
| 84 | + |
| 85 | + # Change branch to gh-pages |
| 86 | + r.exec "git checkout gh-pages", {cwd:tmp}, (err, stdout, stderr)-> |
| 87 | + error err if err? |
| 88 | + log stdout |
| 89 | + |
| 90 | + # Create doc |
| 91 | + r.exec "docco-husky #{directories}", (err, stdout, stderr)-> |
| 92 | + error err if err? |
| 93 | + log stdout |
| 94 | + |
| 95 | + # Move the doc to the tmp directory |
| 96 | + r.exec "cp #{directory}/docs/* #{tmp} -r", (err, stdout, stderr)-> |
| 97 | + error err if err? |
| 98 | + log stdout |
| 99 | + |
| 100 | + # Commit to gh-pages |
| 101 | + r.exec "git add . && git commit -am 'Generated automatically'", {cwd:tmp}, (err, stdout, stderr)-> |
| 102 | + error err if err? |
| 103 | + log stdout |
| 104 | + |
| 105 | + # Push to gh-pages |
| 106 | + r.exec "git push origin gh-pages", {cwd:tmp}, (err, stdout, stderr)-> |
| 107 | + error err if err? |
| 108 | + log stdout |
| 109 | + |
| 110 | + # Remove the docs directory |
| 111 | + r.exec "rm -r #{directory}/docs/" |
| 112 | + |
| 113 | +module.exports = r |
0 commit comments