@@ -39,63 +39,85 @@ var cp = require('child_process');
3939var fs = require ( 'fs-extra' ) ;
4040var stripJsonComments = require ( 'strip-json-comments' ) ;
4141
42- // Verify that `.npmscriptrc` exists.
42+ var buildEntry = 'build' ;
43+
44+ // Potentially set a new testEntry.
45+ if ( typeof process . argv [ 2 ] === 'string' )
46+ {
47+ buildEntry = process . argv [ 2 ] ;
48+ }
49+
50+ // Verify that `Babel` exists.
4351/* istanbul ignore next */
4452try
4553{
46- if ( ! fs . statSync ( './.npmscriptrc ' ) . isFile ( ) )
54+ if ( ! fs . statSync ( './node_modules/.bin/babel ' ) . isFile ( ) )
4755 {
48- throw new Error ( "'.npmscriptrc' not found in root path ." ) ;
56+ throw new Error ( "could not locate Babel at './node_modules/.bin/babel' ." ) ;
4957 }
5058}
5159catch ( err )
5260{
5361 throw new Error ( "typhonjs-npm-scripts-build-babel error: " + err ) ;
5462}
5563
56- // Verify that `Babel` exists.
64+ var configInfo ;
65+
66+ // Attempt to require `.npmscriptrc.js`
5767/* istanbul ignore next */
5868try
5969{
60- if ( ! fs . statSync ( './node_modules/.bin/babel ' ) . isFile ( ) )
70+ if ( fs . statSync ( './.npmscriptrc.js ' ) . isFile ( ) )
6171 {
62- throw new Error ( "could not locate Babel at './node_modules/.bin/babel'." ) ;
72+ configInfo = require ( './.npmscriptrc.js' ) ;
6373 }
6474}
65- catch ( err )
75+ catch ( err ) { /* nop */ }
76+
77+ // Attempt to load `.npmscriptrc` and strip comments.
78+ /* istanbul ignore next */
79+ try
6680{
67- throw new Error ( "typhonjs-npm-scripts-build-babel error: " + err ) ;
81+ if ( fs . statSync ( './.npmscriptrc' ) . isFile ( ) )
82+ {
83+ configInfo = JSON . parse ( stripJsonComments ( fs . readFileSync ( './.npmscriptrc' , 'utf-8' ) ) ) ;
84+ }
6885}
86+ catch ( err ) { /* nop */ }
6987
70- // Load `.npmscriptrc` and strip comments.
71- var configInfo = JSON . parse ( stripJsonComments ( fs . readFileSync ( './.npmscriptrc' , 'utf-8' ) ) ) ;
88+ // Exit now if no configInfo object has been loaded.
89+ if ( typeof configInfo !== 'object' )
90+ {
91+ throw new Error ( "TyphonJS NPM script (build-babel) could not load `./npmscriptrc.js` or `./npmscriptrc`." ) ;
92+ }
7293
7394// Verify that `build` entry is an object.
7495/* istanbul ignore if */
75- if ( typeof configInfo . build !== 'object' )
96+ if ( typeof configInfo [ buildEntry ] !== 'object' )
7697{
7798 throw new Error (
78- "typhonjs-npm-scripts-build-babel error: 'build' entry is not an object or is missing in '.npmscriptrc'." ) ;
99+ "typhonjs-npm-scripts-build-babel error: '" + buildEntry +
100+ "' entry is not an object or is missing in config file." ) ;
79101}
80102
81-
82103// Verify that `build.babel` entry is an object.
83104/* istanbul ignore if */
84- if ( typeof configInfo . build . babel !== 'object' )
105+ if ( typeof configInfo [ buildEntry ] . babel !== 'object' )
85106{
86107 throw new Error (
87- "typhonjs-npm-scripts-build-babel error: 'build.babel' entry is not an object or is missing in '.npmscriptrc'." ) ;
108+ "typhonjs-npm-scripts-build-babel error: '" + buildEntry +
109+ ".babel' entry is not an object or is missing in config file." ) ;
88110}
89111
90- var babelConfig = configInfo . build . babel ;
112+ var babelConfig = configInfo [ buildEntry ] . babel ;
91113
92114// Verify that source entry is a string.
93115/* istanbul ignore if */
94116if ( typeof babelConfig . source !== 'string' )
95117{
96118 throw new Error (
97- "typhonjs-npm-scripts-build-babel error: 'build .babel.source' entry is not a string or is missing in "
98- + "'.npmscriptrc' ." ) ;
119+ "typhonjs-npm-scripts-build-babel error: '" + buildEntry + " .babel.source' entry is not a string or is missing in "
120+ + "config file ." ) ;
99121}
100122
101123// Verify that destination entry is a string.
@@ -104,7 +126,7 @@ if (typeof babelConfig.destination !== 'string')
104126{
105127 throw new Error (
106128 "typhonjs-npm-scripts-build-babel error: 'build.babel.destination' entry is not a string or is missing in "
107- + "'.npmscriptrc' ." ) ;
129+ + "config file ." ) ;
108130}
109131
110132// Verify that source entry is a directory.
113135{
114136 if ( ! fs . statSync ( babelConfig . source ) . isDirectory ( ) )
115137 {
116- throw new Error ( "'build .babel.source' entry is not a directory: " + babelConfig . source ) ;
138+ throw new Error ( "'" + buildEntry + " .babel.source' entry is not a directory: " + babelConfig . source ) ;
117139 }
118140}
119141catch ( err )
130152{
131153 if ( ! fs . statSync ( babelConfig . destination ) . isDirectory ( ) )
132154 {
133- throw new Error ( "'build. babel.destination' entry is not a directory: " + babelConfig . destination ) ;
155+ throw new Error ( "'" + buildEntry + " babel.destination' entry is not a directory: " + babelConfig . destination ) ;
134156 }
135157}
136158catch ( err )
@@ -148,7 +170,8 @@ if (typeof babelConfig.options !== 'undefined')
148170 if ( ! Array . isArray ( babelConfig . options ) )
149171 {
150172 throw new Error (
151- "typhonjs-npm-scripts-build-babel error: 'build.babel.options' entry is not an `array` in '.npmscriptrc'." ) ;
173+ "typhonjs-npm-scripts-build-babel error: '" + buildEntry +
174+ ".babel.options' entry is not an `array` in '.npmscriptrc'." ) ;
152175 }
153176
154177 exec += ' ' + babelConfig . options . join ( ' ' ) ;
@@ -164,26 +187,26 @@ var cntr;
164187// Copy files -------------------------------------------------------------------------------------------------------
165188
166189// Potentially copy files if a copy array is present
167- if ( Array . isArray ( configInfo . build . copy ) )
190+ if ( Array . isArray ( configInfo [ buildEntry ] . copy ) )
168191{
169- for ( cntr = 0 ; cntr < configInfo . build . copy . length ; cntr ++ )
192+ for ( cntr = 0 ; cntr < configInfo [ buildEntry ] . copy . length ; cntr ++ )
170193 {
171- var copyEntry = configInfo . build . copy [ cntr ] ;
194+ var copyEntry = configInfo [ buildEntry ] . copy [ cntr ] ;
172195
173196 /* istanbul ignore if */
174197 if ( typeof copyEntry !== 'object' )
175198 {
176199 throw new Error (
177- "typhonjs-npm-scripts-build-babel error: 'build .copy' entry `" + cntr
178- + "` is not an `object` in '.npmscriptrc' ." ) ;
200+ "typhonjs-npm-scripts-build-babel error: '" + buildEntry + " .copy' entry `" + cntr
201+ + "` is not an `object` in config file ." ) ;
179202 }
180203
181204 /* istanbul ignore if */
182205 if ( typeof copyEntry . source !== 'string' || typeof copyEntry . destination !== 'string' )
183206 {
184207 throw new Error (
185- "typhonjs-npm-scripts-build-babel error: 'build .copy' entry `" + cntr
186- + "` has improperly formatted `source` or `destination` fields in '.npmscriptrc' ." ) ;
208+ "typhonjs-npm-scripts-build-babel error: '" + buildEntry + " .copy' entry `" + cntr
209+ + "` has improperly formatted `source` or `destination` fields in config file ." ) ;
187210 }
188211
189212 console . log ( 'typhonjs-npm-scripts-build-babel copy: ' + copyEntry . source + ' ' + copyEntry . destination ) ;
@@ -195,36 +218,36 @@ if (Array.isArray(configInfo.build.copy))
195218// Commands / scripts -----------------------------------------------------------------------------------------------
196219
197220// Potentially run commands or scripts if a scripts array is present
198- if ( Array . isArray ( configInfo . build . scripts ) )
221+ if ( Array . isArray ( configInfo [ buildEntry ] . scripts ) )
199222{
200223 var runner = require ( 'typhonjs-npm-scripts-runner' ) ;
201224
202- runner . run ( '.npmscriptrc' , 'build .scripts' , 'typhonjs-npm-scripts-build-babel' ) ;
225+ runner . run ( [ '.npmscriptrc' , '.npmscriptrc.js' ] , buildEntry + ' .scripts', 'typhonjs-npm-scripts-build-babel' ) ;
203226}
204227
205228// Chmod files ------------------------------------------------------------------------------------------------------
206229
207230// Potentially chmod files if a chmod array is present
208- if ( Array . isArray ( configInfo . build . chmod ) )
231+ if ( Array . isArray ( configInfo [ buildEntry ] . chmod ) )
209232{
210- for ( cntr = 0 ; cntr < configInfo . build . chmod . length ; cntr ++ )
233+ for ( cntr = 0 ; cntr < configInfo [ buildEntry ] . chmod . length ; cntr ++ )
211234 {
212- var chmodEntry = configInfo . build . chmod [ cntr ] ;
235+ var chmodEntry = configInfo [ buildEntry ] . chmod [ cntr ] ;
213236
214237 /* istanbul ignore if */
215238 if ( typeof chmodEntry !== 'object' )
216239 {
217240 throw new Error (
218- "typhonjs-npm-scripts-build-babel error: 'build.cntr' chmod `" + cntr
219- + "` is not an `object` in '.npmscriptrc' ." ) ;
241+ "typhonjs-npm-scripts-build-babel error: '" + buildEntry + ". chmod' `" + cntr
242+ + "` is not an `object` in config file ." ) ;
220243 }
221244
222245 /* istanbul ignore if */
223246 if ( typeof chmodEntry . path !== 'string' || typeof chmodEntry . mode !== 'string' )
224247 {
225248 throw new Error (
226- "typhonjs-npm-scripts-build-babel error: 'build .chmod' entry `" + cntr
227- + "` has improperly formatted `path` or `mode` fields in '.npmscriptrc' ." ) ;
249+ "typhonjs-npm-scripts-build-babel error: '" + buildEntry + " .chmod' entry `" + cntr
250+ + "` has improperly formatted `path` or `mode` fields in config file ." ) ;
228251 }
229252
230253 console . log ( 'typhonjs-npm-scripts-build-babel chmod: ' + chmodEntry . path + ' ' + chmodEntry . mode ) ;
0 commit comments