From ef8c10f0dd4148dbcaf0c002f84a08b857027996 Mon Sep 17 00:00:00 2001 From: icfr Date: Mon, 26 Sep 2016 18:49:18 +0200 Subject: [PATCH] [fix] minor fix --- Commands/init.js | 23 ++++++------ Commands/laravelConfig.js | 79 +++++++++++++++++++++++---------------- lib/Mail.js | 48 ++++++++++++------------ lib/shell/index.js | 6 ++- 4 files changed, 88 insertions(+), 68 deletions(-) diff --git a/Commands/init.js b/Commands/init.js index 85b1efb..11108c5 100644 --- a/Commands/init.js +++ b/Commands/init.js @@ -1,5 +1,6 @@ 'use strict'; /* global app,appdir */ + var fs = require('fs-promise'); var shelljs = require('shelljs'); var path = require('path'); @@ -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')); }); }; @@ -56,7 +57,7 @@ var askCommandFolder = function askCommandFolder() { }); }); -} +}; var cpCommand = function cpCommand(laravelCommandPath) { return new Promise(function promiseCpCommand(resolve, reject) { var here = __dirname; @@ -66,7 +67,7 @@ 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); @@ -74,7 +75,7 @@ var cpCommand = function cpCommand(laravelCommandPath) { return reject(resCp.stderr); } } catch (error) { - reject(error); + return reject(error); } response.green('command copied to ' + laravelCommandPath).ln(); @@ -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"; @@ -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'); @@ -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'); - }) + }); }) diff --git a/Commands/laravelConfig.js b/Commands/laravelConfig.js index e52a6ad..432b35c 100644 --- a/Commands/laravelConfig.js +++ b/Commands/laravelConfig.js @@ -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'); @@ -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() { @@ -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() { @@ -28,7 +29,7 @@ var prepareTmpFolder = function prepareTmpFolder() { }); }); -} +}; var cmd = function cmd(command, dontParse) { return new Promise(function promiseCmd(resolve, reject) { @@ -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) { @@ -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 { @@ -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); - }); }); @@ -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) { @@ -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) { diff --git a/lib/Mail.js b/lib/Mail.js index 83ba34c..86a241c 100644 --- a/lib/Mail.js +++ b/lib/Mail.js @@ -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 @@ -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; @@ -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); @@ -75,3 +76,4 @@ Mail.prototype.send = function sendMail(callback) { }); }; module.exports = Mail; + diff --git a/lib/shell/index.js b/lib/shell/index.js index 237dd9d..3875406 100644 --- a/lib/shell/index.js +++ b/lib/shell/index.js @@ -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 };