Skip to content

Commit

Permalink
Adds dist folder
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler-boyd committed Jan 23, 2020
1 parent c981da3 commit 5a8a328
Show file tree
Hide file tree
Showing 15 changed files with 6,455 additions and 2 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
i18n
node_modules
.tmp
dist
89 changes: 89 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
'use strict';

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _mkdirp = require('mkdirp');

var _mkdirp2 = _interopRequireDefault(_mkdirp);

var _loaderUtils = require('loader-utils');

var _loaderUtils2 = _interopRequireDefault(_loaderUtils);

var _po2json = require('po2json');

var _po2json2 = _interopRequireDefault(_po2json);

var _ramda = require('ramda');

var _utils = require('./utils');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

var DEFAULT_GETTEXT = '__';

var root = process.env.PWD;
var config = require(_path2.default.join(root, 'gettext.config.js'));

module.exports = function (source) {

if (this.cacheable) {
this.cacheable();
}
var relativeFilename = _path2.default.relative(root, this.resourcePath);

if (relativeFilename.indexOf('..') >= 0) {
// don't consider files outside of root
return source;
}

var output = {
path: root + '/' + (config.output.replace('[filename]', relativeFilename) || 'en.po')
};

var methodNames = config.methods || [DEFAULT_GETTEXT];

var AST = (0, _utils.parseECMA)(source);
var translations = _utils.extractTranslations.apply(undefined, _toConsumableArray(methodNames))(AST);

if (!translations.length) {
return source;
}

var formatTranslations = (0, _utils.formatWithRequest)(this.request);

try {
var buffer = _fs2.default.readFileSync(output.path);
var current = _po2json2.default.parse(buffer);
var newStrings = function newStrings(node) {
return !current[(0, _ramda.prop)('text')(node)];
};
var found = (0, _ramda.filter)(newStrings)(translations);

if (found.length) {

console.log(found.length + ' new translations found in ' + (0, _utils.getFilename)(this.resourcePath));

output.source = formatTranslations(found);
_fs2.default.appendFileSync(output.path, output.source);
}
} catch (error) {
var header_prefix = config.header_prefix || '';
var header = (0, _utils.formatHeader)(config.header);
var body = formatTranslations(translations);
output.source = header_prefix + '\n' + header + '\n' + body;

_mkdirp2.default.sync((0, _utils.getFolderPath)(output.path));
_fs2.default.writeFileSync(output.path, output.source);
}

return source;
};
16 changes: 16 additions & 0 deletions dist/utils/addFilePath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _ramda = require('ramda');

function addFilePath(path, translations) {
var addPath = function addPath(translation) {
return (0, _ramda.merge)(translation)({ path: path });
};
return (0, _ramda.map)(addPath, translations);
}

exports.default = (0, _ramda.curry)(addFilePath);
49 changes: 49 additions & 0 deletions dist/utils/buildMessageBlocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.formatMessageBlock = exports.getNumPlurals = exports.buildMsgstrs = exports.buildMsgstr = undefined;

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _makeRelativePath = require('./makeRelativePath');

var _makeRelativePath2 = _interopRequireDefault(_makeRelativePath);

var _isPluralForm = require('./isPluralForm');

var _isPluralForm2 = _interopRequireDefault(_isPluralForm);

var _ramda = require('ramda');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var root = process.env.PWD;
var config = require(_path2.default.join(root, 'gettext.config.js'));

var buildMsgstr = exports.buildMsgstr = (0, _ramda.map)(function (num) {
return 'msgstr[' + num + '] ""\n';
});
var buildMsgstrs = exports.buildMsgstrs = function buildMsgstrs(num) {
return (0, _ramda.compose)((0, _ramda.join)(''), buildMsgstr)((0, _ramda.range)(0, num));
};
var getNumPlurals = exports.getNumPlurals = (0, _ramda.compose)(parseInt, _ramda.last, _ramda.head, (0, _ramda.split)(';'));
var formatMessageBlock = exports.formatMessageBlock = function formatMessageBlock(accum, translation) {
var path = (0, _makeRelativePath2.default)(translation.path);

var translationBlock = (0, _ramda.concat)('#: ' + path + ' ' + translation.loc.line + ':' + translation.loc.column + '\n', 'msgid "' + translation.text + '"');

if ((0, _isPluralForm2.default)(translation.text)) {
translationBlock += '\nmsgid_plural ""';
var msgstrs = (0, _ramda.compose)(buildMsgstrs, getNumPlurals)(config.header['Plural-Forms']);

return (0, _ramda.compose)((0, _ramda.concat)(accum), (0, _ramda.concat)(translationBlock), (0, _ramda.concat)('\n'), (0, _ramda.concat)(msgstrs))('\n');
}

return (0, _ramda.compose)((0, _ramda.concat)(accum), (0, _ramda.concat)(translationBlock), (0, _ramda.concat)('\n'), (0, _ramda.concat)('msgstr ""\n'))('\n');
};

exports.default = (0, _ramda.reduce)(formatMessageBlock, '');
50 changes: 50 additions & 0 deletions dist/utils/extractTranslations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _ramda = require('ramda');

var _estreeUtils = require('estree-utils');

var extractTranslations = function extractTranslations() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}

return function (ast) {

var gettextFunctions = _estreeUtils.filterTreeForMethodsAndFunctionsNamed.apply(undefined, args)(ast);

if (!gettextFunctions.length) {
return [];
}

var gettextLocations = (0, _ramda.map)(function (node) {
return node.loc.start;
})(gettextFunctions);
var firstArgument = (0, _ramda.compose)((0, _ramda.prop)('value'), _ramda.head, (0, _ramda.prop)('arguments'));
var translationStrings = (0, _ramda.map)(firstArgument)(gettextFunctions).filter(function (x) {
return x;
});

var addLocation = function addLocation(string) {
var location = gettextLocations[translationStrings.indexOf(string)];
return {
text: string,
loc: {
line: location.line,
column: location.column
}
};
};

var unique = function unique(s) {
return gettextLocations[translationStrings.indexOf(s)];
};
return (0, _ramda.map)(addLocation)((0, _ramda.uniqBy)(unique)(translationStrings));
};
};

exports.default = extractTranslations;
18 changes: 18 additions & 0 deletions dist/utils/formatHeader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = formatHeader;

var _ramda = require('ramda');

function formatHeader(header) {

var headerKeys = (0, _ramda.keys)(header);
var headerValues = (0, _ramda.values)(header);

return (0, _ramda.reduce)(function (accum, key) {
return accum + ('"' + key + ' : ' + headerValues[headerKeys.indexOf(key)] + '"\n');
}, '')(headerKeys);
}
23 changes: 23 additions & 0 deletions dist/utils/formatWithRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _ramda = require('ramda');

var _addFilePath = require('./addFilePath');

var _addFilePath2 = _interopRequireDefault(_addFilePath);

var _buildMessageBlocks = require('./buildMessageBlocks');

var _buildMessageBlocks2 = _interopRequireDefault(_buildMessageBlocks);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var formatWithRequest = function formatWithRequest(request) {
return (0, _ramda.compose)(_buildMessageBlocks2.default, (0, _addFilePath2.default)(request));
};

exports.default = formatWithRequest;
9 changes: 9 additions & 0 deletions dist/utils/getFilename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _ramda = require('ramda');

exports.default = (0, _ramda.compose)((0, _ramda.join)('.'), (0, _ramda.split)('.'), _ramda.last, (0, _ramda.split)('/'));
12 changes: 12 additions & 0 deletions dist/utils/getFolderPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = getFolderPath;

var _ramda = require('ramda');

function getFolderPath(filePath) {
return (0, _ramda.compose)((0, _ramda.join)('/'), (0, _ramda.dropLast)(1), (0, _ramda.split)('/'))(filePath);
}
59 changes: 59 additions & 0 deletions dist/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.parseECMA = exports.makeRelativePath = exports.isPluralForm = exports.getFolderPath = exports.getFilename = exports.formatWithRequest = exports.formatHeader = exports.extractTranslations = exports.buildMessageBlocks = exports.addFilePath = undefined;

var _addFilePath2 = require('./addFilePath');

var _addFilePath3 = _interopRequireDefault(_addFilePath2);

var _buildMessageBlocks2 = require('./buildMessageBlocks');

var _buildMessageBlocks3 = _interopRequireDefault(_buildMessageBlocks2);

var _extractTranslations2 = require('./extractTranslations');

var _extractTranslations3 = _interopRequireDefault(_extractTranslations2);

var _formatHeader2 = require('./formatHeader');

var _formatHeader3 = _interopRequireDefault(_formatHeader2);

var _formatWithRequest2 = require('./formatWithRequest');

var _formatWithRequest3 = _interopRequireDefault(_formatWithRequest2);

var _getFilename2 = require('./getFilename');

var _getFilename3 = _interopRequireDefault(_getFilename2);

var _getFolderPath2 = require('./getFolderPath');

var _getFolderPath3 = _interopRequireDefault(_getFolderPath2);

var _isPluralForm2 = require('./isPluralForm');

var _isPluralForm3 = _interopRequireDefault(_isPluralForm2);

var _makeRelativePath2 = require('./makeRelativePath');

var _makeRelativePath3 = _interopRequireDefault(_makeRelativePath2);

var _parseECMA2 = require('./parseECMA');

var _parseECMA3 = _interopRequireDefault(_parseECMA2);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

exports.addFilePath = _addFilePath3.default;
exports.buildMessageBlocks = _buildMessageBlocks3.default;
exports.extractTranslations = _extractTranslations3.default;
exports.formatHeader = _formatHeader3.default;
exports.formatWithRequest = _formatWithRequest3.default;
exports.getFilename = _getFilename3.default;
exports.getFolderPath = _getFolderPath3.default;
exports.isPluralForm = _isPluralForm3.default;
exports.makeRelativePath = _makeRelativePath3.default;
exports.parseECMA = _parseECMA3.default;
9 changes: 9 additions & 0 deletions dist/utils/isPluralForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _ramda = require('ramda');

exports.default = (0, _ramda.compose)((0, _ramda.any)((0, _ramda.equals)('%')), (0, _ramda.split)(''));
26 changes: 26 additions & 0 deletions dist/utils/makeRelativePath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = makeRelativePath;
exports.getBase = getBase;
exports.splitLastPath = splitLastPath;

var _ramda = require('ramda');

function makeRelativePath(paths) {
var baseDir = getBase(process.env.PWD);
var splitPath = splitLastPath(paths);
var takeIdx = splitPath.length - splitPath.indexOf(baseDir) - 1;

return (0, _ramda.compose)((0, _ramda.join)('/'), (0, _ramda.takeLast)(takeIdx))(splitPath);
}

function getBase(pwd) {
return (0, _ramda.compose)(_ramda.last, (0, _ramda.split)('/'))(pwd);
}

function splitLastPath(paths) {
return (0, _ramda.compose)((0, _ramda.split)('/'), _ramda.last, (0, _ramda.split)('!'))(paths);
}
Loading

0 comments on commit 5a8a328

Please sign in to comment.