construx-makara-amdify
Version:
amd module compiler for makara languagepacks. a plugin for construx
51 lines (45 loc) • 2.78 kB
JavaScript
/*───────────────────────────────────────────────────────────────────────────*\
│ Copyright (C) 2017 PayPal Software Foundation │
│ │
│hh ,'""`. │
│ / _ _ \ Licensed under the Apache License, Version 2.0 (the "License"); │
│ |(@)(@)| you may not use this file except in compliance with the License. │
│ ) __ ( You may obtain a copy of the License at │
│ /,'))((`.\ │
│(( (( )) )) http://www.apache.org/licenses/LICENSE-2.0 │
│ `\ `)(' /' │
│ │
│ Unless required by applicable law or agreed to in writing, software │
│ distributed under the License is distributed on an "AS IS" BASIS, │
│ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │
│ See the License for the specific language governing permissions and │
│ limitations under the License. │
\*───────────────────────────────────────────────────────────────────────────*/
;
var path = require('path');
var iferr = require('iferr');
var spundle = require('spundle');
var moduleBuilder = function (appRoot, m, cb) {
spundle(path.resolve(appRoot), m[2], m[1], iferr(cb, function (out) {
cb(null, 'define("_languagepack", function () { return ' + JSON.stringify(out) + '; });');
}));
};
module.exports = function (options) {
options.precompile = function (options, cb) {
options.skipRead = true;
cb(null, options);
};
return function (data, args, callback) {
var filePath = args.context.filePath;
var locale = /(.*)-(.*)/.exec(filePath.substr(1,filePath.indexOf('/', 1) - 1));
if (!locale || locale.length !== 3) {
return callback(new Error('The locale part xx-XX was not found in the filePath'));
}
moduleBuilder(args.i18n.contentPath, locale, function (err, out) {
if (err !== null) {
return callback(err);
}
callback(null, out);
});
};
};