Skip to content
This repository
Browse code

refactor to reduce nesting & increase clarity

  • Loading branch information...
commit 2081c58230c8a1696afb22512754ffd25cae3db6 1 parent 5eb6f1c
Rod Vagg authored October 15, 2012

Showing 1 changed file with 18 additions and 18 deletions. Show diff stats Hide diff stats

  1. 36  lib/source-build.js
36  lib/source-build.js
@@ -55,8 +55,14 @@ var async           = require('async')
55 55
 
56 56
       , asString: function (options, callback) {
57 57
           //options.type == plain||minified
58  
-          var finish = function (sources) {
59  
-            var data = {
  58
+          var finish = function (err, source) {
  59
+                if (err) return callback(err) // wrapped in template.js
  60
+                if (options.type === 'minified') minify.minify(this.options, source, callback)
  61
+                else callback(null, source)
  62
+              }.bind(this)
  63
+
  64
+            , tmplData = function (sources) {
  65
+                return {
60 66
                     source: sources.join('\n\n')
61 67
                   , context: argsParse.toContextString(this.options)
62 68
                   , sandbox: !!this.options.sandbox
@@ -64,25 +70,19 @@ var async           = require('async')
64 70
                       return p.getIdentifier()
65 71
                     }).join(' ')
66 72
                 }
  73
+              }.bind(this)
67 74
 
68  
-            template.generateSource(templateFile, data, function (err, source) {
69  
-              if (err) return callback(err) // wrapped in template.js
70  
-              if (options.type === 'minified') minify.minify(this.options, source, callback)
71  
-              else callback(null, source)
72  
-            }.bind(this))
73  
-          }.bind(this)
  75
+            , readComplete = function (err, sources) {
  76
+                if (err) return callback(err) // wrapped in source-package.js
  77
+                template.generateSource(templateFile, tmplData(sources), finish)
  78
+              }.bind(this)
74 79
 
75  
-          // async.map, oh my! do an asString() on each SourcePackage async but reassemble them in order
76  
-          async.map(
77  
-              this.packages
78  
-            , function (srcPackage, callback) {
  80
+            , packageToString = function (srcPackage, callback) {
79 81
                 srcPackage.asString(callback)
80 82
               }
81  
-            , function (err, sources) {
82  
-                if (err) return callback(err) // wrapped in source-package.js
83  
-                finish(sources)
84  
-              }
85  
-          )
  83
+
  84
+          // async.map, oh my! do an asString() on each SourcePackage async but reassemble them in order
  85
+          async.map(this.packages, packageToString, readComplete)
86 86
         }
87 87
     }
88 88
 
@@ -97,7 +97,7 @@ var async           = require('async')
97 97
         fs.read(fd, buffer, 0, 2048, null, function (err, bytesRead, buffer) {
98 98
           if (err) return callback(new FilesystemError(err))
99 99
 
100  
-          fs.close(fd, function (err) {
  100
+          fs.close(fd, function () {
101 101
             // err? who cares, we have our data, let's use it and run for the hills!
102 102
             var options
103 103
               , error

0 notes on commit 2081c58

Please sign in to comment.
Something went wrong with that request. Please try again.