Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@
node_modules
checkstyle.xml
loopback-phase-*.tgz

intl/*
!intl/en/
4 changes: 3 additions & 1 deletion example/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

var g = require('strong-globalize')();

var PhaseList = require('../lib/phase-list');

var phaseOrder = [
Expand All @@ -27,7 +29,7 @@ app.use(function(req, res, next) {

function createLoggerForPhase(name) {
return function logger(req, res, next) {
console.log('Phase: %s, url: %s', name, req.url);
g.log('Phase: %s, url: %s', name, req.url);
next();
};
}
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

var SG = require('strong-globalize');
SG.SetRootDir(__dirname);

exports.PhaseList = require('./lib/phase-list');
exports.Phase = require('./lib/phase');
exports.mergePhaseNameLists = require('./lib/merge-name-lists');
9 changes: 9 additions & 0 deletions intl/en/messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"a19f59787426e8f41b321911dd39f8ec": "Phase: {0}, url: {1}",
"6447e6b342a2c51ab0bc53b3cbdf3742": "Ordering conflict: cannot add \"{0}\" after \"{1}\", because the opposite order was already specified",
"2bdb39e93f74c523a8ccee628fb9599a": "Phase \"{0}\" already exists.",
"58545fed6db146f2b8576d723560cb47": "Unknown phase: %",
"7c9ffc9635ec7f4de62b0955d2b8a474": "Unknown phase: {0}",
"9f7cabafa3a0e4a1e27acc839534e34d": "Unknown phase {0}",
"f2151dad01f583c644dd2e7e7fa062be": "Cannot add a non phase object to a {{PhaseList}}"
}
8 changes: 5 additions & 3 deletions lib/merge-name-lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

var g = require('strong-globalize')();

/**
* Extend the list of builtin phases by merging in an array of phases
* requested by a user while preserving the relative order of phases
Expand Down Expand Up @@ -63,9 +65,9 @@ module.exports = function mergePhaseNameLists(currentNames, namesToMerge) {
// A new phase - try to add it after the last one,
// unless it was already registered
if (targetArray.indexOf(valueToAdd) !== -1) {
throw new Error('Ordering conflict: cannot add "' + valueToAdd +
'" after "' + previousValue + '", because the opposite order was ' +
' already specified');
throw new Error(g.f('Ordering conflict: cannot add "%s' +
'" after "%s", because the opposite order was ' +
' already specified', valueToAdd, previousValue));
}
var previousIx = targetArray.indexOf(previousValue);
targetArray.splice(previousIx + 1, 0, valueToAdd);
Expand Down
12 changes: 7 additions & 5 deletions lib/phase-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

var g = require('strong-globalize')();

var Phase = require('./phase');
var zipMerge = require('./merge-name-lists');
var async = require('async');
Expand Down Expand Up @@ -75,12 +77,12 @@ PhaseList.prototype._resolveNameAndAddToMap = function(phaseOrName) {
}

if (phase.id in this._phaseMap) {
throw new Error('Phase "' + phase.id + '" already exists.');
throw new Error(g.f('Phase "%s" already exists.', phase.id));
}


if(!phase.__isPhase__) {
throw new Error('Cannot add a non phase object to a PhaseList');
throw new Error(g.f('Cannot add a non phase object to a {{PhaseList}}'));
}

this._phaseMap[phase.id] = phase;
Expand Down Expand Up @@ -110,7 +112,7 @@ PhaseList.prototype.addAt = function(index, phase) {
PhaseList.prototype.addAfter = function(after, phase) {
var ix = this.getPhaseNames().indexOf(after);
if (ix === -1) {
throw new Error('Unknown phase: "' + after + '"');
throw new Error(g.f('Unknown phase: %s', after));
}
return this.addAt(ix+1, phase);
};
Expand All @@ -125,7 +127,7 @@ PhaseList.prototype.addAfter = function(after, phase) {
PhaseList.prototype.addBefore = function(before, phase) {
var ix = this.getPhaseNames().indexOf(before);
if (ix === -1) {
throw new Error('Unknown phase: "' + before + '"');
throw new Error(g.f('Unknown phase: %s', before));
}
return this.addAt(ix, phase);
};
Expand Down Expand Up @@ -296,6 +298,6 @@ PhaseList.prototype.registerHandler = function(phaseName, handler) {
subphase = m[2];
}
var phase = this.find(phaseName);
if (!phase) throw new Error('Unknown phase ' + phaseName);
if (!phase) throw new Error(g.f('Unknown phase %s', phaseName));
phase[subphase](handler);
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"license": "MIT",
"dependencies": {
"async": "^0.9.0",
"debug": "^2.1.0"
"debug": "^2.1.0",
"strong-globalize": "^2.6.2"
},
"devDependencies": {
"chai": "~1.10.0",
Expand Down
2 changes: 1 addition & 1 deletion test/phase-list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe('PhaseList', function() {

it('throws when the "after" phase was not found', function() {
expect(function() { phaseList.addAfter('unknown-phase', 'a-name'); })
.to.throw(/unknown-phase/);
.to.throw(/unknown phase/i);
});
});

Expand Down