Skip to content

Commit 268f40c

Browse files
committed
Implement notify with error and success message
Implement .everc for custom configuration. Example, specificate the preferred editor.
1 parent afc7bf8 commit 268f40c

File tree

8 files changed

+140
-29
lines changed

8 files changed

+140
-29
lines changed

example/.everc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"editor": "atom"
3+
}

example/config.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"author": "",
77
"license": "",
88
"env": "developer",
9-
"build-date": "2017-4-26",
10-
"API_KEY": "PERSONAL_KEY_FOR_DEV"
9+
"build-date": "2017-04-28",
10+
"API_KEY": "PERSONAL_KEY_FOR_DEV",
11+
"build-date-time": "2017-04-28 17:43:11"
1112
}

example/lib/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ console.log("LoooL");
1111
console.log(a, _.isArray(a) );
1212

1313
$('body').css({ "background": "red", "height":"350px"})
14+
asdasdasd:

icon_error.png

22.2 KB
Loading

lib/build.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const bowerrcFile = path.resolve('.bowerrc');
1414
const factor = require('factor-bundle');
1515
const waterfall = require('async').waterfall;
1616
const junk = require('junk');
17+
const notify = require('./notify');
1718
const UGLIFYJS_OPTIONS = {
1819
compress: {
1920
screw_ie8: true, // Use this flag if you don't wish to support Internet Explorer 6/7/8.
@@ -24,7 +25,7 @@ const UGLIFYJS_OPTIONS = {
2425

2526
var bowerrc = { "directory": "bower_components" };
2627
try{
27-
bowerrc = JSON.parse(fs.readFileSync(bowerrc, 'utf8'));
28+
bowerrc = JSON.parse(fs.readFileSync(bowerrcFile, 'utf8'));
2829
}catch(e){}
2930

3031
function beautifyTime(diff) {
@@ -343,8 +344,10 @@ module.exports = function(type, unit, watcher, options, done) {
343344
], function (err) {
344345
if ( err ){
345346
if ( _.isObject(err) && err.message ){
347+
notify.error('JS error build', err.message);
346348
console.log(err.message.red);
347349
}else {
350+
notify.error('JS error build', err.toString());
348351
console.log(err.toString().red);
349352
}
350353
return;

lib/eve.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/usr/bin/env node
2-
2+
const _ = require('underscore');
33
const program = require('commander');
44
const colors = require('colors');
55
const path = require('path');
66
const pkg = require('../package.json');
77
const pkgApp = require(path.resolve('package.json'));
88
const exec = require('child_process').exec;
99
const ets = require('ets');
10-
const notifier = require('node-notifier');
10+
const notify = require('./notify');
1111

1212
program
1313
.version(pkg.version);
@@ -50,12 +50,12 @@ program
5050

5151
build(type, unit, false, options, function (err, time, type) {
5252
if (err) return handleError(err, 'JS');
53-
notify(`Eve: ${appName}`, 'JS builded', `Time ${time}`);
53+
notify.success('JS builded', `Time ${time}`);
5454
console.log('JS builded with type '.gray + type.cyan + '...'.gray + time.green + '\u0007');
5555
});
5656
scss(type, unit, false, function (err, time) {
5757
if (err) return handleError(err, 'SCSS');
58-
notify(`Eve: ${appName}`, 'Scss builded', `Time ${time}`);
58+
notify.success('Scss builded', `Time ${time}`);
5959
console.log('SCSS builded...'.gray + time.green + '\u0007');
6060
});
6161
});
@@ -80,7 +80,7 @@ program
8080
prepareApp(function(err, platform) {
8181
if (err) return handleError(err, 'CORDOVA');
8282

83-
notify(`Eve: ${appName}`, 'Javascript builded', `Time ${time}`);
83+
notify.success('Javascript builded', `Time ${time}`);
8484
console.log('JS builded...'.gray + time.green + '\u0007');
8585

8686
if (platform)
@@ -93,7 +93,7 @@ program
9393
prepareApp(function(err, platform) {
9494
if (err) return handleError(err, 'CORDOVA');
9595

96-
notify(`Eve: ${appName}`, 'Scss builded', `Time ${time}`);
96+
notify.success('Scss builded', `Time ${time}`);
9797
console.log('SCSS builded...'.gray + time.green + '\u0007');
9898

9999
if (platform)
@@ -131,13 +131,13 @@ program
131131

132132
build('test', unit, true, function (err, time, type) {
133133
if (err) return handleError(err, 'JS');
134-
notify(`Eve: ${appName}`, 'JS builded', `Time ${time}`);
134+
notify.success('JS builded', `Time ${time}`);
135135
console.log('JS builded...'.gray + time.green + '\u0007');
136136
});
137137

138138
scss('test', unit, true, function (err, time) {
139139
if (err) return handleError(err, 'SCSS');
140-
notify(`Eve: ${appName}`, 'Scss builded', `Time ${time}`);
140+
notify.success('Scss builded', `Time ${time}`);
141141
console.log('SCSS builded...'.gray + time.green + '\u0007');
142142
});
143143

@@ -159,28 +159,29 @@ if (!program.args.length) {
159159
program.help();
160160
}
161161

162-
163-
function notify(title, subtitle, message){
164-
notifier.notify({
165-
title: title||'Eve',
166-
subtitle: subtitle, // 'Javascript builded'
167-
message: message, // `Time ${time}`
168-
timeout: 3,
169-
icon: path.join(__dirname, '..', 'icon.png'), // Absolute path (doesn't work on balloons)
170-
appIcon: path.join(__dirname, '..', 'icon.png'), // Absolute path (doesn't work on balloons)
171-
// contentImage: path.join(__dirname, '..', 'icon.png'), // Absolute path (doesn't work on balloons)
172-
sound: false, // Only Notification Center or Windows Toasters
173-
wait: false // Wait with callback, until user action is taken against notification
174-
}, function (err, response) {
175-
// Response is response from notification
176-
});
177-
}
178-
179162
function handleError(err, title) {
180163
if (title) {
181-
console.error(title.red + ' error'.red);
164+
title = `${title} error`;
165+
console.error(title.red);
182166
}
183167
var output = '| '.red + err.toString().replace(/\n/g, '\n' + '| '.red);
168+
169+
// console.log(err);
170+
let filename;
171+
let line;
172+
if ( _.isObject(err)){
173+
filename = err.filename;
174+
line = err.loc && err.loc ? err.loc.line : null;
175+
}
176+
177+
notify.error(
178+
title||'Error on build',
179+
err.toString(),
180+
{
181+
filename: filename,
182+
line: line
183+
}
184+
);
184185
console.error(output);
185186
console.error('');
186187
}

lib/everc.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const path = require('path');
2+
const fs = require('fs');
3+
4+
module.exports = function () {
5+
let configs = {};
6+
try{
7+
configs = JSON.parse(fs.readFileSync(path.resolve('.everc'), 'utf8'));
8+
}catch(e){
9+
configs = {};
10+
}
11+
return configs;
12+
}

lib/notify.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
const _ = require('underscore');
2+
const path = require('path');
3+
const exec = require('child_process').exec;
4+
const NotificationCenter = require('node-notifier').NotificationCenter;
5+
const pkgApp = require(path.resolve('package.json'));
6+
const appName = pkgApp['name'];
7+
const everc = require('./everc')();
8+
const defaultEditor = everc['editor']||'';
9+
10+
const getCommandLine = function getCommandLine() {
11+
switch (process.platform) {
12+
case 'darwin' : return 'open';
13+
case 'win32' : return 'start';
14+
case 'win64' : return 'start';
15+
default : return 'xdg-open';
16+
}
17+
}
18+
19+
const n = function n(options, done) {
20+
if ( _.isFunction(options) ){
21+
done = options;
22+
options = {};
23+
}
24+
if (!_.isFunction(done))
25+
done = function () {};
26+
if (!_.isObject(options))
27+
options = {};
28+
29+
30+
options = _.defaults(options, {
31+
title: `Eve: ${appName}`,
32+
sound: false,
33+
group: 2
34+
});
35+
36+
// console.log(options);
37+
38+
var notifier = new NotificationCenter({
39+
withFallback: false, // Use Growl Fallback if <= 10.8
40+
customPath: void 0 // Relative/Absolute path to binary if you want to use your own fork of terminal-notifier
41+
});
42+
43+
notifier.notify(
44+
options,
45+
function (err, response, metadata) {
46+
return done(err, response);
47+
}
48+
).on('click', function (notifierObject, opt) {
49+
if ( options.filename ){
50+
switch (defaultEditor.toLowerCase()) {
51+
case "atom":
52+
exec(`${defaultEditor} ${options.filename}:${options.line||0}`);
53+
break;
54+
default:
55+
exec(`${getCommandLine()} ${options.filename}`);
56+
break;
57+
}
58+
}
59+
});
60+
61+
}
62+
63+
module.exports = {
64+
notifier: n,
65+
66+
success: function success(subtitle, message, options){
67+
options = _.defaults(options||{}, {
68+
subtitle: subtitle,
69+
message: message,
70+
icon: path.join(__dirname, '..', 'icon.png'),
71+
appIcon: path.join(__dirname, '..', 'icon.png')
72+
});
73+
n(options);
74+
},
75+
76+
error: function error(subtitle, message, options){
77+
options = _.defaults(options||{}, {
78+
subtitle: subtitle,
79+
message: message,
80+
icon: path.join(__dirname, '..', 'icon_error.png'),
81+
appIcon: path.join(__dirname, '..', 'icon.png'),
82+
timeout: 30,
83+
sound: 'Basso', // 'Sosumi'
84+
closeLabel: 'Close',
85+
actions: [ 'Open' ]
86+
});
87+
88+
n(options);
89+
}
90+
}

0 commit comments

Comments
 (0)