Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 136 lines (110 sloc) 4.702 kb
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 135
"use strict"

fs = require 'fs'
path = require 'path'

logger = require 'logmimosa'
wrench = require "wrench"
_ = require 'lodash'

requireRegister = require './lib/register'
optimizer = require './lib/optimize'
builder = require './lib/builder'

exports.registration = (config, register) ->

  return unless config.require.verify.enabled or config.isOptimize
  e = config.extensions
  register ['postClean'], 'init', _clean
  register ['add','update','buildFile'], 'betweenCompileWrite', _requireRegister, [e.javascript...]
  register ['add','update','buildExtension'], 'betweenCompileWrite', _requireRegister, [e.template...]
  register ['remove'], 'afterDelete', _requireDelete, [e.javascript...]
  register ['postBuild'], 'beforeOptimize', _buildDone

  if config.isOptimize
    register ['add','update','remove'], 'beforeOptimize', _buildOptimizeConfigsFile, [e.javascript..., e.template...]
    register ['add','update','remove'], 'optimize', _requireOptimize, [e.javascript..., e.template...]
    register ['postBuild'], 'beforeOptimize', _buildOptimizeConfigs
    register ['postBuild'], 'optimize', _requireOptimize

    if config.isBuild
      register ['add','update','remove'], 'afterOptimize', _removeCombined, [e.javascript..., e.template...]
      register ['postBuild'], 'optimize', _removeCombined

  requireRegister.setConfig(config)

exports.aliasForPath = (libPath) ->
  requireRegister.aliasForPath(libPath)

_clean = (config, options, next) ->
  jsDir = path.join config.watch.compiledDir, config.watch.javascriptDir
  if fs.existsSync jsDir
    files = wrench.readdirSyncRecursive(jsDir)
      .filter (f) ->
        /-built.js$/.test(f)
      .map (f) ->
        f = path.join jsDir, f
        fs.unlinkSync f
        logger.success "Deleted file [[ #{f} ]]"
  next()

_requireDelete = (config, options, next) ->
  return next() unless options.files?.length > 0
  requireRegister.remove(options.files[0].inputFileName)
  next()

_requireRegister = (config, options, next) ->
  return next() unless options.files?.length > 0
  return next() if options.isVendor
  options.files.forEach (file) ->
    if file.outputFileName and file.outputFileText
      if config.isVirgin
        requireRegister.process(file.inputFileName, file.outputFileText)
      else
        requireRegister.process(file.outputFileName, file.outputFileText)

  next()

_buildOptimizeConfigsFile = (config, options, next) ->
  return next() unless options.files?.length > 0

  filesDone = 0
  allRunConfigs = []
  done = (runConfigs) ->
    if runConfigs
      allRunConfigs = allRunConfigs.concat runConfigs
    if options.files.length is ++filesDone
      if allRunConfigs.length > 0
        options.runConfigs = allRunConfigs
      logger.debug "Total of [[ #{allRunConfigs.length} ]] r.js run configs generated."
      next()

  options.files.forEach (file) ->
    if file.outputFileName and file.outputFileText
      builder.buildRunConfig config, file.outputFileName, done

_buildOptimizeConfigs = (config, options, next) ->
  builder.buildRunConfig config, null, (runConfigs) ->
    if runConfigs
      options.runConfigs = runConfigs
      logger.debug "Total of [[ #{runConfigs.length} ]] r.js run configs generated."
    else
      logger.debug "No r.js run configs generated."
    next()

_requireOptimize = (config, options, done) ->
  return done() unless options.runConfigs
  return done() if options.runConfigs.length is 0

  i = 0
  next = =>
    if i < options.runConfigs.length
      optimizer.execute options.runConfigs[i++], next
    else
      done()
  next()

_removeCombined = (config, options, next) ->
  return next() unless options.runConfigs

  for runConfig in options.runConfigs
    if runConfig.filesUsed? and Array.isArray(runConfig.filesUsed)
      for f in runConfig.filesUsed
        if fs.existsSync(f)
          logger.debug "Removing [[ #{f} ]]"
          fs.unlinkSync(f)

  jsDir = config.watch.compiledJavascriptDir
  directories = wrench.readdirSyncRecursive(jsDir).map (f) -> path.join jsDir, f
  directories = directories.filter (f) -> fs.statSync(f).isDirectory()

  _.sortBy(directories, 'length').reverse().forEach (dirPath) ->
    if fs.existsSync dirPath
      try
        fs.rmdirSync dirPath
        logger.debug "Deleted directory [[ #{dirPath} ]]"
      catch err
        if err?.code is not "ENOTEMPTY"
          logger.error "Unable to delete directory, #{dirPath}"
          logger.error err

  next()

_buildDone = (config, options, next) ->
  requireRegister.buildDone()
  next()
Something went wrong with that request. Please try again.