1 | /* |
---|
2 | * grunt-dojo |
---|
3 | * https://github.com/phated/grunt-dojo |
---|
4 | * |
---|
5 | * Copyright (c) 2013 Blaine Bublitz |
---|
6 | * Licensed under the MIT license. |
---|
7 | */ |
---|
8 | |
---|
9 | module.exports = function(grunt) { |
---|
10 | |
---|
11 | 'use strict'; |
---|
12 | |
---|
13 | grunt.registerMultiTask('dojo', 'build dojo by spawning a child process', function(){ |
---|
14 | |
---|
15 | var done = this.async(); |
---|
16 | |
---|
17 | /** |
---|
18 | * @namespace defaults |
---|
19 | */ |
---|
20 | var options = this.options({ |
---|
21 | /** |
---|
22 | * Path to dojo.js file in Dojo source |
---|
23 | * @type {String} |
---|
24 | * @memberOf defaults |
---|
25 | * @default |
---|
26 | */ |
---|
27 | dojo: null, |
---|
28 | /** |
---|
29 | * Utility to bootstrap |
---|
30 | * @type {String=} |
---|
31 | * @memberOf defaults |
---|
32 | * @default |
---|
33 | */ |
---|
34 | load: 'build', |
---|
35 | /** |
---|
36 | * Profile for the build |
---|
37 | * @type {String=} |
---|
38 | * @memberOf defaults |
---|
39 | * @default |
---|
40 | */ |
---|
41 | profile: null, |
---|
42 | /** |
---|
43 | * Config file for dojox/app |
---|
44 | * @type {String=} |
---|
45 | * @memberOf defaults |
---|
46 | * @default |
---|
47 | */ |
---|
48 | appConfigFile: null, |
---|
49 | /** |
---|
50 | * Location to search for package.json |
---|
51 | * @type {String=} |
---|
52 | * @memberOf defaults |
---|
53 | * @default |
---|
54 | */ |
---|
55 | package: null, |
---|
56 | /** |
---|
57 | * Array of locations to search for package.json files |
---|
58 | * @type {Array=} |
---|
59 | * @memberOf defaults |
---|
60 | * @default |
---|
61 | */ |
---|
62 | packages: null, |
---|
63 | /** |
---|
64 | * Module to require for the build |
---|
65 | * @type {String=} |
---|
66 | * @memberOf defaults |
---|
67 | * @default |
---|
68 | */ |
---|
69 | require: null, |
---|
70 | /** |
---|
71 | * Array of modules to require for the build |
---|
72 | * @type {Array=} |
---|
73 | * @memberOf defaults |
---|
74 | * @default |
---|
75 | */ |
---|
76 | requires: null, |
---|
77 | /** |
---|
78 | * Release directory for the build |
---|
79 | * @type {String=} |
---|
80 | * @memberOf defaults |
---|
81 | * @default |
---|
82 | */ |
---|
83 | releaseDir: null, |
---|
84 | /** |
---|
85 | * Directory to execute build within |
---|
86 | * @type {String=} |
---|
87 | * @memberOf defaults |
---|
88 | * @default |
---|
89 | */ |
---|
90 | cwd: null, |
---|
91 | /** |
---|
92 | * Location of dojoConfig to be used in build |
---|
93 | * @type {String=} |
---|
94 | * @memberOf defaults |
---|
95 | * @default |
---|
96 | */ |
---|
97 | dojoConfig: null, |
---|
98 | /** |
---|
99 | * Base Path to pass at the command line |
---|
100 | * Takes precedence over all other basePaths |
---|
101 | * @type {String=} |
---|
102 | * @memberOf defaults |
---|
103 | * @default |
---|
104 | */ |
---|
105 | basePath: null |
---|
106 | }); |
---|
107 | |
---|
108 | grunt.log.subhead('Building Dojo...'); |
---|
109 | |
---|
110 | var args = []; |
---|
111 | /* |
---|
112 | * Add parameter(s) to the argument list and log them, if in verbose mode. |
---|
113 | */ |
---|
114 | var addParam = function() { |
---|
115 | grunt.verbose.write("\t"); |
---|
116 | grunt.util._.forEach(arguments, function(param) { |
---|
117 | args.push(param); |
---|
118 | grunt.verbose.write(param +" "); |
---|
119 | }); |
---|
120 | grunt.verbose.write("\n"); |
---|
121 | }; |
---|
122 | |
---|
123 | if(options.dojo){ |
---|
124 | grunt.verbose.writeln("Dojo build parameters:"); |
---|
125 | addParam(options.dojo); |
---|
126 | addParam('load=' + options.load); |
---|
127 | |
---|
128 | if(options.basePath){ |
---|
129 | addParam('--basePath', options.basePath); |
---|
130 | } |
---|
131 | if(options.profile){ |
---|
132 | addParam('--profile', options.profile); |
---|
133 | } |
---|
134 | if(options.appConfigFile){ |
---|
135 | addParam('--appConfigFile', options.appConfigFile); |
---|
136 | } |
---|
137 | |
---|
138 | /* |
---|
139 | * Support both the singular and plural form of the 'package' and 'require' parameters |
---|
140 | */ |
---|
141 | ['package', 'require'].forEach(function(dojoParam){ |
---|
142 | if(!Array.isArray(options[dojoParam+'s'])) { |
---|
143 | options[dojoParam+'s'] = []; |
---|
144 | } |
---|
145 | if(options[dojoParam]){ |
---|
146 | options[dojoParam+'s'].push(options[dojoParam]); |
---|
147 | } |
---|
148 | options[dojoParam+'s'].forEach(function(paramValue){ |
---|
149 | addParam('--'+dojoParam, paramValue); |
---|
150 | }); |
---|
151 | }); |
---|
152 | |
---|
153 | |
---|
154 | if(options.dojoConfig){ |
---|
155 | addParam('--dojoConfig', options.dojoConfig); |
---|
156 | } |
---|
157 | |
---|
158 | if(options.releaseDir){ |
---|
159 | addParam('--releaseDir', options.releaseDir); |
---|
160 | } |
---|
161 | } else { |
---|
162 | grunt.log.error('No dojo specified'); |
---|
163 | done(false); |
---|
164 | } |
---|
165 | |
---|
166 | var opts = {}; |
---|
167 | if(options.cwd){ |
---|
168 | opts.cwd = options.cwd; |
---|
169 | } |
---|
170 | |
---|
171 | var child = grunt.util.spawn({ |
---|
172 | cmd: 'node', |
---|
173 | args: args, |
---|
174 | opts: opts |
---|
175 | }, function(err, result){ |
---|
176 | if(err){ |
---|
177 | grunt.log.error(err); |
---|
178 | return done(false); |
---|
179 | } |
---|
180 | |
---|
181 | grunt.log.success('Dojo Successfully Built...'); |
---|
182 | |
---|
183 | done(); |
---|
184 | }); |
---|
185 | |
---|
186 | child.stdout.on('data', function (data) { |
---|
187 | grunt.verbose.write(data); |
---|
188 | }); |
---|
189 | child.stderr.on('data', function (data) { |
---|
190 | grunt.verbose.error(data); |
---|
191 | }); |
---|
192 | }); |
---|
193 | |
---|
194 | }; |
---|