@@ -21,16 +21,13 @@ lineNumber = require('line-number'),
21
21
AddcomponentGenerator = yeoman . generators . NamedBase . extend ( {
22
22
23
23
/**
24
- * Loads package.json and waits for callback to finish. Also tells user what happened and gives a little help .
24
+ * Loads package.json and binds events .
25
25
* @function init
26
26
* @private
27
27
*/
28
28
init : function ( ) {
29
29
30
- this . pkg = this . fs . readJSON ( 'package.json' ) ;
31
-
32
30
this . on ( 'end' , function ( ) {
33
-
34
31
// output a little help
35
32
if ( this . ComponentType === 'standardModule' ) {
36
33
if ( this . includeHTML ) {
@@ -41,8 +38,23 @@ AddcomponentGenerator = yeoman.generators.NamedBase.extend({
41
38
this . log ( 'You can use it in your HTML with ' + chalk . yellow ( '{deferred:{' + string . slugify ( this . name ) + '}}' ) ) ;
42
39
}
43
40
}
41
+ } ) ;
44
42
43
+ // take care of error handling
44
+ this . on ( 'error' , function ( message ) {
45
+ // print error message
46
+ this . log . error ( message ) ;
47
+ // exit shell
48
+ process . exit ( 5 ) ;
45
49
} ) ;
50
+
51
+ // load package.json
52
+ try {
53
+ this . pkg = this . _loadJSON ( 'package.json' ) ;
54
+ } catch ( error ) {
55
+ this . emit ( 'error' , error ) ;
56
+ return ;
57
+ }
46
58
} ,
47
59
48
60
/**
@@ -55,6 +67,23 @@ AddcomponentGenerator = yeoman.generators.NamedBase.extend({
55
67
return this . features && this . features . indexOf ( feature ) !== - 1 ;
56
68
} ,
57
69
70
+ /**
71
+ * Loads JSON files.
72
+ * @function _loadJSON
73
+ * @returns {JSON } file's contents
74
+ * @private
75
+ */
76
+ _loadJSON : function ( name ) {
77
+ var json = this . fs . readJSON ( name ) ;
78
+
79
+ if ( json ) {
80
+ return json ;
81
+ } else {
82
+ throw new Error ( 'Could not open ' + name + '. Are you in root directory of the project?' ) ;
83
+ }
84
+
85
+ } ,
86
+
58
87
/**
59
88
* Asks user questions about the component.
60
89
* @function askForApp
@@ -208,6 +237,7 @@ AddcomponentGenerator = yeoman.generators.NamedBase.extend({
208
237
* @private
209
238
*/
210
239
addStyling : function ( ) {
240
+
211
241
if ( this . includeSCSS ) {
212
242
213
243
var
@@ -220,6 +250,12 @@ AddcomponentGenerator = yeoman.generators.NamedBase.extend({
220
250
path = 'components/_' + this . pkg . name + '.scss' ;
221
251
}
222
252
253
+ this . log ( 'PATH: ' + path ) ;
254
+
255
+ if ( ! this . fs . exists ( path ) ) {
256
+ this . log . error ( 'FILE DOESNT EXIST' ) ;
257
+ }
258
+
223
259
// read
224
260
var
225
261
file = this . fs . read ( path ) ,
0 commit comments