Rebase & refactor globalization for shared config

This commit is contained in:
David Cheung 2016-09-15 17:02:27 -04:00
parent 90fda8ab80
commit c032849ce5
12 changed files with 52 additions and 43 deletions

View File

@ -4,8 +4,7 @@
// License text available at https://opensource.org/licenses/MIT
// Strong globalize
var SG = require('strong-globalize');
SG.SetRootDir(__dirname);
var g = require('./lib/globalize');
var PluginBase = require('./lib/plugin-base');
var Bootstrapper = require('./lib/bootstrapper').Bootstrapper;

View File

@ -1,25 +1,16 @@
{
"ec551b6f2fafd8d40af801ebe5bb09f6": "Discarding {{middleware}} instructions, {{loopback}} client does not support {{middleware}}.",
"1e5fea50eef843cbffd1d438494912c8": "Cannot resolve path \"{0}\"",
"34319676975b1abf107da7a056abb434": "Invalid normalization format - \"{0}\"",
"46e3ab0ef1149ce0a171b5fac2612ea3": "{{Middleware}} \"{0}\" not found: {1}",
"79e93b2a95e969788590c14e26bb2c1b": "The data in {{model-config.json}} is in the unsupported 1.x format.",
"978a25819e71602cad691dbe7ba17592": "{0} config must be a valid JSON object",
"be2dcdab7aa493ed8d77287eb45cfec8": "cannot require directory contents without directory name",
"2634623ad4b2c5902f6c6bb25e68b733": "WARNING: Main {{config}} file \"{0}.json\" is missing",
"3a7049e42006e8bc19e0f4fc8df63b6b": "The `app` is powered by an incompatible loopback version {0}. Supported versions: {1}",
"3f93b626dd9a1c33d67490f6e71018b5": "WARNING: Main config file \"{0}{{.json}}\" is missing",
"4d052d84c8620730afd4a30832f11724": "Cannot configure unknown model {0}",
"4ed668e9187650d898acf97707df445a": "The {{phase}} \"{0}\" is not defined in the main config.",
"6de7e97f033f2cf477297b3d05a93608": "The {{middleware}} \"{0}\" in phase \"{1}\"is not defined in the main config.",
"94a0c7d5ab6462f7892b90c63f316f42": "invalid array: {0}",
"ec34cc58612cb654742e4cd0a57aca78": "Cannot apply {0}: {1}",
"0b91d122f6459c8bbe7865be0936fc4a": "{{app.restBasePath}} is required",
"1cda77c9954be299bb7154f73cb6ab74": "{{instructions.middleware.phases}} must be an {{array}}",
"22549489736fb0d7eba5a4b08977505f": "{{app.host}} must be a {{string}}",
"4c581cc529a7aeda620d5c4b4ef5cfa8": "{{app.restApiRoot}} must start with \"/\"",
"6037512314fac9d12af6c654a3804823": "Built-in model {0} should have been defined",
"69746d336c89bf4bb371a6c2fe56304d": "{0} does not resolve to a valid value, returned as {1}. \"{2}\" must be resolvable in Environment variable or by app.get().",
"6447e6b342a2c51ab0bc53b3cbdf3742": "Ordering conflict: cannot add \"{0}\" after \"{1}\", because the opposite order was already specified",
"70654dc6eb565613a33344efed3de998": "Failed loading boot script: {0}\n{1}",
"b078ccd043437a258581e387f93dc1a5": "The `{{app}}` is powered by an incompatible {{loopback}} version {0}. Supported versions: {1}",
"e8d29edfb313cfe64f5c96cc7d3d5b4b": "When using {{loopback-boot}} with {{loopback}} <1.9, the {{loopback}} module must be available for `{{require('loopback')}}`.",
"f48405e7c61c3d665b601c9ba41da424": "{{app.port}} must be a {{string}} or {{number}}",
"fa2a7d5137c8891693f9515d48f5b7d7": "{{app.restApiRoot}} must be a {{string}}"
"7f7bdcadb75abfef1bd8a126d547dd6d": "{0} does not resolve to a valid value, returned as {1}. \"{2}\" must be resolvable in Environment variable or by {{app.get()}}.",
"91a742b7c3568cf6b6755741a70b3c52": "The {{middleware}} \"{0}\" in {{phase}} \"{1}\"is not defined in the main config.",
"a3aa22086ae4976cd013065c9a3ff81c": "Cannot apply {0}: ",
"be2cf2868ba54624fe38e9908dde5e9e": "The data in {{model-config.json}} is in the unsupported {{1.x}} format.",
"ec551b6f2fafd8d40af801ebe5bb09f6": "Discarding {{middleware}} instructions, {{loopback}} client does not support {{middleware}}.",
"fdc23df1bd0fe55fe3faabcc89ff60f3": "Middleware \"{0}\" not found: {1}"
}

View File

@ -7,7 +7,7 @@ var fs = require('fs');
var path = require('path');
var commondir = require('commondir');
var cloneDeep = require('lodash').cloneDeep;
var g = require('strong-globalize')();
var g = require('./globalize');
/**
* Add boot instructions to a browserify bundler.

11
lib/globalize.js Normal file
View File

@ -0,0 +1,11 @@
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
// Node module: loopback-boot
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
var path = require('path');
var SG = require('strong-globalize');
SG.SetRootDir(path.join(__dirname, '..'), { autonomousMsgLoading: 'all' });
module.exports = SG();

View File

@ -9,6 +9,7 @@ var debug = require('debug')('loopback:boot:plugin');
var assert = require('assert');
var _ = require('lodash');
var util = require('./utils');
var g = require('./globalize');
module.exports = PluginBase;
@ -92,7 +93,7 @@ PluginBase.prototype.findConfigFiles = function(rootDir, env, name, exts) {
var master = ifExists(name + '.json');
if (!master && (ifExistsWithAnyExt(name + '.local') ||
ifExistsWithAnyExt(name + '.' + env))) {
console.warn('WARNING: Main config file "' + name + '.json" is missing');
g.warn('WARNING: Main config file "%s{{.json}}" is missing', name);
}
if (!master) return [];
@ -283,8 +284,8 @@ function getConfigVariable(app, param, useEnvVars) {
// it will now return `undefined`, for the use case of
// dynamic datasources url:`undefined` to fallback to other parameters
configVariable = undefined;
console.warn('%s does not resolve to a valid value, returned as %s. ' +
'"%s" must be resolvable in Environment variable or by app.get().',
g.warn('%s does not resolve to a valid value, returned as %s. ' +
'"%s" must be resolvable in Environment variable or by {{app.get()}}.',
param, configVariable, varName);
debug('Dynamic Configuration: Cannot resolve variable for `%s`, ' +
'returned as %s', varName, configVariable);

View File

@ -6,8 +6,8 @@
var util = require('util');
var assert = require('assert');
var semver = require('semver');
var format = require('util').format;
var PluginBase = require('../plugin-base');
var g = require('../globalize');
module.exports = function(options) {
return new Application(options);
@ -28,7 +28,7 @@ function assertLoopBackVersion(app) {
// while loopback-boot treats pre-releases the same way as regular versions
var version = (loopback.version || '1.0.0').replace(/-.*$/, '');
if (!semver.satisfies(version, RANGE)) {
var msg = format(
var msg = g.f(
'The `app` is powered by an incompatible loopback version %s. ' +
'Supported versions: %s',
loopback.version || '(unknown)',

View File

@ -10,6 +10,7 @@ var async = require('async');
var debug = require('debug')('loopback:boot:script');
var PluginBase = require('../plugin-base');
var _ = require('lodash');
var g = require('../globalize');
module.exports = function(options) {
return new Script(options);
@ -67,7 +68,7 @@ function runScripts(app, list, callback) {
});
}
} catch (err) {
console.error('Failed loading boot script: %s\n%s', filepath, err.stack);
g.error('Failed loading boot script: %s\n%s', filepath, err.stack);
throw err;
}
});

View File

@ -12,6 +12,7 @@ var cloneDeep = _.cloneDeep;
var debug = require('debug')('loopback:boot:middleware');
var PluginBase = require('../plugin-base');
var utils = require('../utils');
var g = require('../globalize');
var resolveAppScriptPath = utils.resolveAppScriptPath;
@ -35,10 +36,10 @@ Middleware.prototype.merge = function(target, config, fileName) {
if (phase in target) {
err = this.mergePhaseConfig(target[phase], config[phase], phase);
} else {
err = 'The phase "' + phase + '" is not defined in the main config.';
err = g.f('The {{phase}} "%s" is not defined in the main config.', phase);
}
if (err)
throw new Error('Cannot apply ' + fileName + ': ' + err);
throw new Error(g.f('Cannot apply %s: ', fileName) + err);
}
};
@ -72,8 +73,8 @@ Middleware.prototype.mergePhaseConfig = function(target, config, phase) {
err = this.mergeObjects(targetMiddleware, configMiddleware);
}
} else {
err = 'The middleware "' + mw + '" in phase "' + phase + '"' +
'is not defined in the main config.';
err = g.f('The {{middleware}} "%s" in {{phase}} "%s"' +
'is not defined in the main config.', mw, phase);
}
if (err) return err;
}
@ -96,7 +97,7 @@ Middleware.prototype.buildInstructions = function(context, rootDir, config) {
// if a non-optional middleware is not resolvable, it will throw
// at resolveAppPath() and not reach here
if (!resolved.sourceFile) {
return console.log('Middleware "%s" not found: %s',
return g.log('Middleware "%s" not found: %s',
middleware,
resolved.optional
);

View File

@ -10,6 +10,7 @@ var PluginBase = require('../plugin-base');
var _ = require('lodash');
var debug = require('debug')('loopback:boot:mixin');
var utils = require('../utils');
var g = require('../globalize');
var tryResolveAppPath = utils.tryResolveAppPath;
var getExcludedExtensions = utils.getExcludedExtensions;
@ -162,8 +163,8 @@ function normalizeMixinName(str, options) {
return normalization(str);
}
var err = new Error('Invalid normalization format - "' +
normalization + '"');
var err = new Error(g.f('Invalid normalization format - "%s"',
normalization));
err.code = 'INVALID_NORMALIZATION_FORMAT';
throw err;
}

View File

@ -16,6 +16,7 @@ var tryReadDir = utils.tryReadDir;
var assertIsValidConfig = utils.assertIsValidConfig;
var tryResolveAppPath = utils.tryResolveAppPath;
var fixFileExtension = utils.fixFileExtension;
var g = require('../globalize');
module.exports = function(options) {
return new Model(options);
@ -234,8 +235,9 @@ function assertIsValidModelConfig(config) {
entry.plural || options.plural;
if (unsupported) {
throw new Error(
'The data in model-config.json is in the unsupported 1.x format.');
throw new Error(g.f(
'The data in {{model-config.json}} ' +
'is in the unsupported {{1.x}} format.'));
}
}
}
@ -267,7 +269,7 @@ Model.prototype.start = function(context) {
if (!data.definition) {
model = registry.getModel(name);
if (!model) {
throw new Error('Cannot configure unknown model ' + name);
throw new Error(g.f('Cannot configure unknown model %s', name));
}
debug('Configuring existing model %s', name);
} else if (isBuiltinLoopBackModel(app, data)) {

View File

@ -9,6 +9,7 @@ var Module = require('module');
var fs = require('fs');
var assert = require('assert');
var _ = require('lodash');
var g = require('./globalize');
exports.tryReadDir = tryReadDir;
exports.resolveRelativePaths = resolveRelativePaths;
@ -142,7 +143,7 @@ function fixFileExtension(filepath, files, onlyScriptsExportingFunction) {
function resolveAppPath(rootDir, relativePath, resolveOptions) {
var resolvedPath = tryResolveAppPath(rootDir, relativePath, resolveOptions);
if (resolvedPath === undefined && !resolveOptions.optional) {
var err = new Error('Cannot resolve path "' + relativePath + '"');
var err = new Error(g.f('Cannot resolve path "%s"', relativePath));
err.code = 'PATH_NOT_FOUND';
throw err;
}
@ -314,9 +315,10 @@ 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');
var errMsg = g.f('Ordering conflict: cannot add "%s' +
'" after "%s", because the opposite order was ' +
' already specified', valueToAdd, previousValue);
throw new Error(errMsg);
}
var previousIx = targetArray.indexOf(previousValue);
targetArray.splice(previousIx + 1, 0, valueToAdd);

View File

@ -30,7 +30,7 @@
"debug": "^2.2.0",
"lodash": "^4.13.1",
"semver": "^5.1.0",
"strong-globalize": "^2.6.2",
"strong-globalize": "^2.7.0",
"toposort": "^1.0.0"
},
"devDependencies": {