• 跳至… +
    browser.coffee cake.coffee coffeescript.coffee command.coffee grammar.coffee helpers.coffee index.coffee lexer.coffee nodes.coffee optparse.coffee register.coffee repl.coffee rewriter.coffee scope.litcoffee sourcemap.litcoffee
  • cake.coffee

  • §

    cake 是 Make (Rake、Jake) 的簡化版,適用於 CoffeeScript。您可以在 Cakefile 中定義帶有名稱和說明的任務,並可從命令列呼叫它們,或從其他任務呼叫它們。

    執行 cake 而沒有參數會列印出目前目錄的 Cakefile 中所有任務的清單。

  • §

    外部依賴項。

    fs           = require 'fs'
    path         = require 'path'
    helpers      = require './helpers'
    optparse     = require './optparse'
    CoffeeScript = require './'
  • §

    註冊 .coffee 副檔名

    CoffeeScript.register()
  • §

    追蹤已定義任務的清單、已接受的選項等。

    tasks     = {}
    options   = {}
    switches  = []
    oparse    = null
  • §

    混合頂層 Cake 函數,供 Cakefile 直接使用。

    helpers.extend global,
  • §

    定義一個 Cake 任務,帶有簡短名稱、一個選用的句子說明,以及作為動作本身執行的函數。

      task: (name, description, action) ->
        [action, description] = [description, action] unless action
        tasks[name] = {name, description, action}
  • §

    定義 Cakefile 接受的選項。已剖析的選項雜湊,包含已傳遞的所有命令列選項,將會作為動作的第一個參數提供。

      option: (letter, flag, description) ->
        switches.push [letter, flag, description]
  • §

    呼叫目前 Cakefile 中的另一個任務。

      invoke: (name) ->
        missingTask name unless tasks[name]
        tasks[name].action options
  • §

    執行 cake。依序執行您傳遞的所有任務。請注意,Node 的非同步性可能會導致任務以您預期之外的順序執行。如果沒有傳遞任何任務,則列印說明畫面。在從子目錄執行 Cake 任務時,保留原始目錄名稱的參考。

    exports.run = ->
      global.__originalDirname = fs.realpathSync '.'
      process.chdir cakefileDirectory __originalDirname
      args = process.argv[2..]
      CoffeeScript.run fs.readFileSync('Cakefile').toString(), filename: 'Cakefile'
      oparse = new optparse.OptionParser switches
      return printTasks() unless args.length
      try
        options = oparse.parse(args)
      catch e
        return fatalError "#{e}"
      invoke arg for arg in options.arguments
  • §

    以類似於 rake -T 的格式顯示 Cake 任務清單

    printTasks = ->
      relative = path.relative or path.resolve
      cakefilePath = path.join relative(__originalDirname, process.cwd()), 'Cakefile'
      console.log "#{cakefilePath} defines the following tasks:\n"
      for name, task of tasks
        spaces = 20 - name.length
        spaces = if spaces > 0 then Array(spaces + 1).join(' ') else ''
        desc   = if task.description then "# #{task.description}" else ''
        console.log "cake #{name}#{spaces} #{desc}"
      console.log oparse.help() if switches.length
  • §

    在嘗試使用無效的任務/選項時列印錯誤並結束。

    fatalError = (message) ->
      console.error message + '\n'
      console.log 'To see a list of all tasks/options, run "cake"'
      process.exit 1
    
    missingTask = (task) -> fatalError "No such task: #{task}"
  • §

    當呼叫 cake 時,在目前目錄和所有父目錄中搜尋以尋找相關的 Cakefile。

    cakefileDirectory = (dir) ->
      return dir if fs.existsSync path.join dir, 'Cakefile'
      parent = path.normalize path.join dir, '..'
      return cakefileDirectory parent unless parent is dir
      throw new Error "Cakefile not found in #{process.cwd()}"