Skip to content

Commit

Permalink
[fix] minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
icfr committed Sep 26, 2016
1 parent f9c970d commit ef8c10f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 68 deletions.
23 changes: 12 additions & 11 deletions Commands/init.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
/* global app,appdir */

var fs = require('fs-promise');
var shelljs = require('shelljs');
var path = require('path');
Expand All @@ -20,13 +21,13 @@ var askLaravelFolder = function askLaravelFolder() {
});
};

var CheckArtisan = function checkArtisan(cmdRes) {
var checkArtisan = function checkArtisan(cmdRes) {
return new Promise(function promiseCheckArtisan(resolve, reject) {
var test = cmdRes.match(/\s*Laravel\s+Framework\s+version\s+(.*)/);
if (test) {
return resolve(test);
}
reject('not a laravel framework');
reject(new Error('not a laravel framework'));
});
};

Expand Down Expand Up @@ -56,7 +57,7 @@ var askCommandFolder = function askCommandFolder() {

});
});
}
};
var cpCommand = function cpCommand(laravelCommandPath) {
return new Promise(function promiseCpCommand(resolve, reject) {
var here = __dirname;
Expand All @@ -66,15 +67,15 @@ var cpCommand = function cpCommand(laravelCommandPath) {
var message = 'maybe you need to add "\\App\\Console\\Commands\\NodeConfig::class" to ';
message += path.normalize(path.dirname(laravelCommandPath) + '/../') + 'kernel.php';
response.yellow(message).ln();
return resolve()
return resolve();
}).catch(function catchFsStatCmd() {
try {
var resCp = shelljs.cp(commandPath, laravelCommandPath);
if (resCp.stderr) {
return reject(resCp.stderr);
}
} catch (error) {
reject(error);
return reject(error);
}

response.green('command copied to ' + laravelCommandPath).ln();
Expand All @@ -85,11 +86,11 @@ var cpCommand = function cpCommand(laravelCommandPath) {
});

});
}
};

var writeConf = function writeConf() {
return new Promise(function promiseWrite(resolve, reject) {
var config = app.config.laravel;
var config = app.config('laravel');
config.path = laravelPath;
var data = JSON.stringify(config);
var comment = "/**\n";
Expand Down Expand Up @@ -143,11 +144,11 @@ module.exports = {
return cmd('php ' + result + '/artisan -V --no-ansi');
})
.then(function cmdExecuted(result) {
return CheckArtisan(result);
return checkArtisan(result);
})
.then(function artisanChecked(result) {
res.green('install command on laravel v' + result[1]).ln();
return askCommandFolder()
return askCommandFolder();
})
.then(function commandFolderAsked(cmdPath) {
var laravelCommandPath = path.resolve(laravelPath, cmdPath, 'NodeConfig.php');
Expand All @@ -157,9 +158,9 @@ module.exports = {
}).then(function waitUserInput() {
req.question('when done type enter', function tapeEnter() {
req.shell.isShell = false;
app.config.laravel.path = laravelPath;
app.config().set('laravel.path', laravelPath);
req.shell.run('laravel:config');
})
});

})

Expand Down
79 changes: 47 additions & 32 deletions Commands/laravelConfig.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
/* global app,appdir */
/* eslint global-require: 0 */

var Promise = require('bluebird');
var shelljs = require('shelljs');
var utils = require('../lib/utilsCmd');
Expand All @@ -9,7 +10,7 @@ var includes = require('lodash/includes');
var each = require('lodash/each');
var include = require('include-all');
var tmp = require('tmp');
var request, tmpdir;
var request, tmpdir, console, response;
var toTransforms = [];

var prepareTmpFolder = function prepareTmpFolder() {
Expand All @@ -19,7 +20,7 @@ var prepareTmpFolder = function prepareTmpFolder() {
unsafeCleanup: true
}, function tmpDirCallback(err, name) {
if (err) {
reject(err)
reject(err);
}
fs.mkdir(name + '/Config-laravel')
.then(function tmpdirOk() {
Expand All @@ -28,7 +29,7 @@ var prepareTmpFolder = function prepareTmpFolder() {

});
});
}
};

var cmd = function cmd(command, dontParse) {
return new Promise(function promiseCmd(resolve, reject) {
Expand All @@ -55,7 +56,7 @@ var cmd = function cmd(command, dontParse) {

var getConfig = function getConfig(configs) {
var questions = [];
var laravelConfig = app.config.laravel.config;
var laravelConfig = app.config('laravel.config');
var laravelKeys = Object.keys(laravelConfig);
var cleanAnswers = [];
return new Promise(function promiseConfigs(resolve) {
Expand Down Expand Up @@ -91,9 +92,10 @@ var write = function write(conf) {
var path = '';
var message = null;
return new Promise(function promiseWrite(resolve, reject) {
cmd('php ' + app.config.laravel.path + '/artisan node:config ' + conf, true)
cmd('php ' + app.config('laravel.path') + '/artisan node:config ' + conf, true)
.then(function commandOk(data) {
if (!app.config.laravel.config[conf] || app.config.laravel.config[conf].asis) {
console.debug('transform ', conf, !app.config('laravel.config.' + conf + '.asis', true));
if (app.config('laravel.config.' + conf + '.asis', true)) {
path = appdir + '/Config';
message = conf + ' created';
} else {
Expand Down Expand Up @@ -140,50 +142,61 @@ var nodeConfig = function nodeConfig() {
filter: /(.+)\.js$/
});
var conf, loaded, error;

each(toTransforms, function eachConfig(toTransform) {
error = loaded = conf = null;
var transformer;
try {
conf = require(appdir + '/lib/Config/' + toTransform)(laravelConfig[toTransform]);
transformer = require(appdir + '/lib/Config/' + toTransform);
loaded = true;
} catch (errorAppload) {
if (errorAppload.code) {
error = new Error('cant find /lib/Config/' + toTransform + '.js');
} else {
error = errorAppload
} catch (errorInUserLoad) {
error = 'cant find /lib/Config/' + toTransform + '.js';
}
if (loaded) {
try {
conf = transformer(laravelConfig[toTransform]);
} catch (errorIntransformer) {
error = errorIntransformer.message;
loaded = false;
}
loaded = false;
}
/* istanbul ignore else */
if (!loaded) {
try {
conf = require('../lib/Config/' + toTransform)(laravelConfig[toTransform]);
loaded = true;

} catch (errorCoreload) {
if (errorCoreload.code) {
error = new Error('cant find /lib/Config/' + toTransform + '.js');
} else {
error = errorCoreload
if (!error || error === 'cant find /lib/Config/' + toTransform + '.js') {
/* istanbul ignore else */
if (errorCoreload.code) {
error = 'cant find /lib/Config/' + toTransform + '.js';
} else {
error = errorCoreload.message;
}
}
loaded = false;
}
}
if (!loaded) {
return reject(error);
conf = laravelConfig[toTransform];
response.red('config ' + toTransform + ' not trandformed').ln();
response.red(error).ln();
}
var content = utils.formatConfig(conf);
cmds.push(fs.writeFile(appdir + '/Config/' + toTransform + '.js', content));

})
});

Promise.all(cmds).then(function allWriteOk() {
var result = [];
each(toTransforms, function eachConfig(confWrited) {
result.push(confWrited + ' created');
Promise.all(cmds)
.then(function allWriteOk() {
var result = [];
each(toTransforms, function eachConfig(confWrited) {
result.push(confWrited + ' created');
});
resolve(result);
}).catch(function writeKo(err) {
reject(err);
});
resolve(result);
}).catch(function writeKo(err) {
reject(err);
});

});

Expand All @@ -193,15 +206,17 @@ module.exports = {
pattern: 'laravel:config',
help: 'Get config from laravel',
function: function run(req, res) {
console = app.logger(app.config('core.log.prefix') + ':laravelConfig');
request = req;
response = res;
return prepareTmpFolder()
.then(function folderPrepared(name) {
tmpdir = name
return cmd('php ' + app.config.laravel.path + '/artisan node:config')
tmpdir = name;
return cmd('php ' + app.config('laravel.path') + '/artisan node:config');
})
.then(getConfig)
.then(function questionAsked(answers) {
var laravelKeys = Object.keys(app.config.laravel.config);
var laravelKeys = Object.keys(app.config('laravel.config', []));
var setting = {};
var hasSetting = false;
for (var key in answers) {
Expand All @@ -218,7 +233,7 @@ module.exports = {
res.yellow('add this to config in Config/laravel.js to save this responses').ln();
res.yellow(utils.formatConfig(setting)).ln();
}
return writeConfs(answers)
return writeConfs(answers);
})
.then(function displayConfCreated(result) {
each(result, function eachResult(message) {
Expand Down
48 changes: 25 additions & 23 deletions lib/Mail.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
'use strict';
/* global appdir,config,mailer */
// var console = require('debug-logger')('backworker:sendmail');
/* global appdir,app */
// var console = app.logger(config('core.log.prefix') + ':mail');

var Promise = require('bluebird');
var ECT = require('ect');
var ect = require('ect');
var defaults = require('lodash/defaults');
var nodemailer = require('nodemailer');
var renderer = ECT({
root: appdir + '/resources/views/',
ext: '.ect'
});
var error = new Error('mail not configured');
/**
* @constructor
* @class
Expand All @@ -31,10 +28,14 @@ var renderer = ECT({
* });
*/
var Mail = function Mail(email, subject, data, template, options) {
if (!config.mail) {
if (!app.config('mail')) {
throw new Error('mail not configured');
}
this.mailer = nodemailer.createTransport(config.mail.transporter);
this.mailer = nodemailer.createTransport(app.config('mail.transporter', error));
this.renderer = ect({
root: appdir + '/resources/views/',
ext: '.ect'
});
this.to = email;
this.subject = subject;
this.content = data;
Expand All @@ -43,24 +44,24 @@ var Mail = function Mail(email, subject, data, template, options) {
};
/**
* send email
* @param {Function} callback callback called when send
* @param {Function=} callback callback called when send
* @return {Promise} Bluebird promise
*/
Mail.prototype.send = function sendMail(callback) {
var mailTemplate = 'templates/' + this.template + '.ect';
var html = renderer.render(mailTemplate, this.content);
var mailOptions = {
from: config.mail.from,
to: this.to,
subject: this.subject,
html: html
};
if (this.options) {
defaults(mailOptions, this.options);
}

var self = this;
return new Promise(function sendPromise(resolve, reject) {
mailer.sendMail(mailOptions, function mailerSendMail(err, info) {
var mailTemplate = 'templates/' + self.template + '.ect';
var html = self.renderer.render(mailTemplate, self.content);
var mailOptions = {
from: app.config('mail.from', error),
to: self.to,
subject: self.subject,
html: html
};
if (self.options) {
defaults(mailOptions, self.options);
}
self.mailer.sendMail(mailOptions, function mailerSendMail(err, info) {
if (err) {
if (callback) {
return callback(err);
Expand All @@ -75,3 +76,4 @@ Mail.prototype.send = function sendMail(callback) {
});
};
module.exports = Mail;

6 changes: 4 additions & 2 deletions lib/shell/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ Shell.help = require('./lib/plugins/help');
Shell.history = require('./lib/plugins/history');
Shell.router = require('./lib/plugins/router');

// Routes
// Middlewares
var confirm = require('./lib/routes/confirm');
var shellOnly = require('./lib/routes/shellOnly');
var timeout = require('./lib/routes/timeout');
Shell.routes = {
confirm: confirm,
shellOnly: shellOnly
shellOnly: shellOnly,
timeout: timeout
};

0 comments on commit ef8c10f

Please sign in to comment.