Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 71b180b

Browse files
author
Markus Falk
committed
[FEATURE] adds meaningful error handling refs #165
1 parent c21b7b4 commit 71b180b

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

addcomponent/index.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,13 @@ lineNumber = require('line-number'),
2121
AddcomponentGenerator = yeoman.generators.NamedBase.extend({
2222

2323
/**
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.
2525
* @function init
2626
* @private
2727
*/
2828
init: function () {
2929

30-
this.pkg = this.fs.readJSON('package.json');
31-
3230
this.on('end', function () {
33-
3431
// output a little help
3532
if (this.ComponentType === 'standardModule') {
3633
if(this.includeHTML) {
@@ -41,8 +38,23 @@ AddcomponentGenerator = yeoman.generators.NamedBase.extend({
4138
this.log('You can use it in your HTML with ' + chalk.yellow('{deferred:{' + string.slugify(this.name) + '}}'));
4239
}
4340
}
41+
});
4442

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);
4549
});
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+
}
4658
},
4759

4860
/**
@@ -55,6 +67,23 @@ AddcomponentGenerator = yeoman.generators.NamedBase.extend({
5567
return this.features && this.features.indexOf(feature) !== -1;
5668
},
5769

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+
5887
/**
5988
* Asks user questions about the component.
6089
* @function askForApp
@@ -208,6 +237,7 @@ AddcomponentGenerator = yeoman.generators.NamedBase.extend({
208237
* @private
209238
*/
210239
addStyling: function () {
240+
211241
if (this.includeSCSS) {
212242

213243
var
@@ -220,6 +250,12 @@ AddcomponentGenerator = yeoman.generators.NamedBase.extend({
220250
path = 'components/_' + this.pkg.name + '.scss';
221251
}
222252

253+
this.log('PATH: ' + path);
254+
255+
if(!this.fs.exists(path)) {
256+
this.log.error('FILE DOESNT EXIST');
257+
}
258+
223259
// read
224260
var
225261
file = this.fs.read(path),

removecomponent/index.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ RemovecomponentGenerator = yeoman.generators.NamedBase.extend({
2121
* @private
2222
*/
2323
constructor: function () {
24-
// console.log(yeoman.NamedBase);
2524
yeoman.Base.apply(this, arguments);
2625

2726
this.argument('name', {
@@ -30,6 +29,31 @@ RemovecomponentGenerator = yeoman.generators.NamedBase.extend({
3029
desc: 'The component name.'
3130
});
3231

32+
// take care of error handling
33+
this.on('error', function(message) {
34+
// print error message
35+
this.log.error(message);
36+
// exit shell
37+
process.exit(5);
38+
});
39+
40+
},
41+
42+
/**
43+
* Loads JSON files.
44+
* @function _loadJSON
45+
* @returns {JSON} file's contents
46+
* @private
47+
*/
48+
_loadJSON: function(name) {
49+
var json = this.fs.readJSON(name);
50+
51+
if(json) {
52+
return json;
53+
} else {
54+
throw new Error('Could not open ' + name + '. Are you in root directory of the project?');
55+
}
56+
3357
},
3458

3559
/**
@@ -38,7 +62,12 @@ RemovecomponentGenerator = yeoman.generators.NamedBase.extend({
3862
* @private
3963
*/
4064
getPackage: function () {
41-
this.pkg = this.fs.readJSON('package.json');
65+
try {
66+
this.pkg = this._loadJSON('package.json');
67+
} catch (error) {
68+
this.emit('error', error);
69+
return;
70+
}
4271
},
4372

4473
/**

0 commit comments

Comments
 (0)