all files / lib/ util.coffee

0% Statements 0/81
0% Branches 0/23
0% Functions 0/7
0% Lines 0/59
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134                                                                                                                                                                                                                                                                           
 
###
  utils for node.js scripts, cake tasks, grunt tasks
 
  TODO : dry up - this file is indentical between htdoc_ems and react-datum
  maybe opensource separately as an NPM module?
 
###
 
 
child_process = require "child_process"
path = require 'path'
fs = require 'fs'
_ = require 'underscore'
moment = require 'moment'
 
 
HOME_DIR = process.env.HOME
 
systemCmd = (cmd, options={}) ->
  options = _.defaults options,
    failOnError: true
    echo: true
    showOutput: true
 
  console.log("$ " + cmd) if options.echo
  try
    out = child_process.execSync(cmd)
    process.stdout.write(out) if options.showOutput
  catch e
    out = e.stderr.toString()
    console.error out if options.showOutput
    if options.failOnError
      throw e
 
  return out.toString()
 
 
 
handleError = (error) ->
  return unless error
  console.error(error)
  process.exit(1)
 
 
LAST_NPM_INSTALL_FILE = './.lastNpmInstall'
# runs an `npm install` if we see that the package.json is newer than the last time called
#   or if the node_modules directory doesn't exist
npmInstall = () ->
  return unless fs.existsSync('package.json')
  packageFileMtime = moment(fs.statSync('package.json').mtime)
 
  try lastTimeStamp = moment(parseInt(fs.readFileSync(LAST_NPM_INSTALL_FILE)))
  # console.log 'lastTimeStamp: ', lastTimeStamp
  # console.log 'node_modules exists: ', fs.existsSync('node_modules')
  # console.log 'packageFileMtime: ', packageFileMtime
 
  if !lastTimeStamp? || !fs.existsSync('node_modules') || packageFileMtime.isAfter(lastTimeStamp)
    console.log 'running npm install (this may take a while the first time)'
    systemCmd 'npm install'
    fs.writeFileSync(LAST_NPM_INSTALL_FILE, packageFileMtime.valueOf())
 
 
# only installs if not alread installed
installNodePackage = (packageName, options={}) ->
  options = _.defaults options,
    global: false
 
  [flags, sudo] = if options.global then ['-g', 'sudo'] else ['', '']
 
  unless fs.existsSync("/usr/local/lib/node_modules/#{packageName}") ||
         fs.existsSync("/opt/nodejs/current/lib/node_modules/#{packageName}")
    if options.global
      console.log 'you may be asked to enter your sudo password (and this may take a few seconds)'
    systemCmd "#{sudo} npm install #{flags} #{packageName}"
 
 
openTerminalTab = (cdPath = './', cmd='')->
  cdPath = path.resolve(cdPath)
  console.log "opening terminal tab. maybe. to #{cdPath}. TERM_PROGRAM='#{process.env.TERM_PROGRAM}'"
  switch process.env.TERM_PROGRAM
    when 'iTerm.app'
      systemCmd """osascript 2>/dev/null -e '
        tell application "iTerm"
          tell current terminal
            launch session "Default Session"
            delay .5
            tell the last session
              write text "cd #{cdPath}"
              if "#{cmd}" is not equal "" then
                write text "#{cmd}"
              end if
            end tell
          end tell
        end tell
      '""", echo: false
    when 'Apple_Terminal'
      systemCmd """osascript 2>/dev/null -e '
        tell application "Terminal"
          activate
          tell application "System Events" to keystroke "t" using command down
          repeat while contents of selected tab of window 1 starts with linefeed
            delay 0.01
          end repeat
          do script "cd #{cdPath}" in window 1
          if "#{cmd}" is not equal "" then
            do script "#{cmd}" in window 1
          end if
        end tell
      '""", echo: false
    else
      console.log "Sorry... unknown terminal type: #{process.env.TERM_PROGRAM}"
 
 
pressAnyKeyToContinue = (method) ->
  console.log "\nPress any key to continue..."
  process.stdin.setRawMode(true);
  process.stdin.resume();
  process.stdin.on('data', method);
  
  
parseJsonFile = (file) ->
  return JSON.parse(fs.readFileSync(file))
  
 
module.exports =
  systemCmd: systemCmd
  handleError: handleError
  npmInstall: npmInstall
  installNodePackage: installNodePackage
  openTerminalTab: openTerminalTab
  pressAnyKeyToContinue: pressAnyKeyToContinue
  parseJsonFile: parseJsonFile