New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@biesbjerg/ng2-translate-extract

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@biesbjerg/ng2-translate-extract - npm Package Compare versions

Comparing version 0.2.8 to 0.2.9

56

dist/cli/extract.js
"use strict";
const extractor_1 = require("../utils/extractor");
const pipe_parser_1 = require("../parsers/pipe.parser");
const directive_parser_1 = require("../parsers/directive.parser");
const service_parser_1 = require("../parsers/service.parser");
const json_compiler_1 = require("../compilers/json.compiler");
const po_compiler_1 = require("../compilers/po.compiler");
const fs = require("fs");
const path = require("path");
const cli = require("cli");
const options = cli.parse({
var extractor_1 = require("../utils/extractor");
var pipe_parser_1 = require("../parsers/pipe.parser");
var directive_parser_1 = require("../parsers/directive.parser");
var service_parser_1 = require("../parsers/service.parser");
var json_compiler_1 = require("../compilers/json.compiler");
var po_compiler_1 = require("../compilers/po.compiler");
var fs = require("fs");
var path = require("path");
var cli = require("cli");
var options = cli.parse({
dir: ['d', 'Directory path you would like to extract strings from', 'dir', process.env.PWD],

@@ -18,10 +18,10 @@ output: ['o', 'Directory path you would like to save extracted strings to', 'dir', process.env.PWD],

});
[options.dir, options.output].forEach(dir => {
[options.dir, options.output].forEach(function (dir) {
if (!fs.existsSync(dir)) {
cli.fatal(`The directory path you supplied was not found: '${dir}'`);
cli.fatal("The directory path you supplied was not found: '" + dir + "'");
}
});
const filename = 'template.' + options.format;
const dest = path.join(options.output, filename);
const parsers = [
var filename = 'template.' + options.format;
var dest = path.join(options.output, filename);
var parsers = [
new pipe_parser_1.PipeParser(),

@@ -31,3 +31,3 @@ new directive_parser_1.DirectiveParser(),

];
const patterns = [
var patterns = [
'/**/*.html',

@@ -38,8 +38,8 @@ '/**/*.ts',

try {
const extractor = new extractor_1.Extractor(parsers, patterns);
cli.info(`Extracting strings from '${options.dir}'`);
const extracted = extractor.process(options.dir);
cli.ok(`* Extracted ${extracted.count()} strings`);
let collection = extracted;
let compiler = new json_compiler_1.JsonCompiler();
var extractor = new extractor_1.Extractor(parsers, patterns);
cli.info("Extracting strings from '" + options.dir + "'");
var extracted = extractor.process(options.dir);
cli.ok("* Extracted " + extracted.count() + " strings");
var collection = extracted;
var compiler = new json_compiler_1.JsonCompiler();
if (options.format === 'pot') {

@@ -49,13 +49,13 @@ compiler = new po_compiler_1.PoCompiler();

if (!options.replace && fs.existsSync(dest)) {
const existing = compiler.parse(fs.readFileSync(dest, 'utf-8'));
var existing = compiler.parse(fs.readFileSync(dest, 'utf-8'));
if (existing.count() > 0) {
collection = extracted.union(existing);
cli.ok(`* Merged ${existing.count()} existing strings`);
cli.ok("* Merged " + existing.count() + " existing strings");
}
if (options.clean) {
const collectionCount = collection.count();
var collectionCount = collection.count();
collection = collection.intersect(extracted);
const removeCount = collectionCount - collection.count();
var removeCount = collectionCount - collection.count();
if (removeCount > 0) {
cli.ok(`* Removed ${removeCount} unused strings`);
cli.ok("* Removed " + removeCount + " unused strings");
}

@@ -65,3 +65,3 @@ }

fs.writeFileSync(dest, compiler.compile(collection));
cli.ok(`* Saved to '${dest}'`);
cli.ok("* Saved to '" + dest + "'");
}

@@ -68,0 +68,0 @@ catch (e) {

"use strict";
const translation_collection_1 = require("../utils/translation.collection");
class JsonCompiler {
compile(collection) {
var translation_collection_1 = require("../utils/translation.collection");
var JsonCompiler = (function () {
function JsonCompiler() {
}
JsonCompiler.prototype.compile = function (collection) {
return JSON.stringify(collection.values, null, '\t');
}
parse(contents) {
};
JsonCompiler.prototype.parse = function (contents) {
return new translation_collection_1.TranslationCollection(JSON.parse(contents));
}
}
};
return JsonCompiler;
}());
exports.JsonCompiler = JsonCompiler;
//# sourceMappingURL=json.compiler.js.map
"use strict";
const translation_collection_1 = require("../utils/translation.collection");
const gettext = require("gettext-parser");
class PoCompiler {
constructor() {
var translation_collection_1 = require("../utils/translation.collection");
var gettext = require("gettext-parser");
var PoCompiler = (function () {
function PoCompiler() {
this.domain = '';
}
compile(collection) {
const data = {
PoCompiler.prototype.compile = function (collection) {
var data = {
charset: 'utf-8',

@@ -16,4 +16,4 @@ headers: {

},
translations: {
[this.domain]: Object.keys(collection.values).reduce((translations, key) => {
translations: (_a = {},
_a[this.domain] = Object.keys(collection.values).reduce(function (translations, key) {
translations[key] = {

@@ -24,23 +24,26 @@ msgid: key,

return translations;
}, {})
}
}, {}),
_a)
};
return gettext.po.compile(data, 'utf-8');
}
parse(contents) {
const collection = new translation_collection_1.TranslationCollection();
const po = gettext.po.parse(contents, 'utf-8');
var _a;
};
PoCompiler.prototype.parse = function (contents) {
var _this = this;
var collection = new translation_collection_1.TranslationCollection();
var po = gettext.po.parse(contents, 'utf-8');
if (!po.translations.hasOwnProperty(this.domain)) {
return collection;
}
const values = Object.keys(po.translations[this.domain])
.filter(key => key.length > 0)
.reduce((values, key) => {
values[key] = po.translations[this.domain][key].msgstr.pop();
var values = Object.keys(po.translations[this.domain])
.filter(function (key) { return key.length > 0; })
.reduce(function (values, key) {
values[key] = po.translations[_this.domain][key].msgstr.pop();
return values;
}, {});
return new translation_collection_1.TranslationCollection(values);
}
}
};
return PoCompiler;
}());
exports.PoCompiler = PoCompiler;
//# sourceMappingURL=po.compiler.js.map
"use strict";
class AbstractTemplateParser {
_isAngularComponent(path) {
var AbstractTemplateParser = (function () {
function AbstractTemplateParser() {
}
AbstractTemplateParser.prototype._isAngularComponent = function (path) {
return new RegExp(/\.ts|js$/, 'i').test(path);
}
_extractInlineTemplate(contents) {
const match = new RegExp(/template\s*:\s*(["'`])([^\1]*?)\1/).exec(contents);
};
AbstractTemplateParser.prototype._extractInlineTemplate = function (contents) {
var match = new RegExp(/template\s*:\s*(["'`])([^\1]*?)\1/).exec(contents);
if (match !== null) {

@@ -12,8 +14,9 @@ return match[2];

return '';
}
_normalizeTemplateAttributes(template) {
};
AbstractTemplateParser.prototype._normalizeTemplateAttributes = function (template) {
return template.replace(/\[([^\]]+)\]="'([^']*)'"/g, '$1="$2"');
}
}
};
return AbstractTemplateParser;
}());
exports.AbstractTemplateParser = AbstractTemplateParser;
//# sourceMappingURL=abstract-template.parser.js.map
"use strict";
const abstract_template_parser_1 = require("./abstract-template.parser");
const translation_collection_1 = require("../utils/translation.collection");
const $ = require("cheerio");
class DirectiveParser extends abstract_template_parser_1.AbstractTemplateParser {
extract(contents, path) {
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var abstract_template_parser_1 = require("./abstract-template.parser");
var translation_collection_1 = require("../utils/translation.collection");
var $ = require("cheerio");
var DirectiveParser = (function (_super) {
__extends(DirectiveParser, _super);
function DirectiveParser() {
return _super.apply(this, arguments) || this;
}
DirectiveParser.prototype.extract = function (contents, path) {
if (path && this._isAngularComponent(path)) {

@@ -11,13 +20,13 @@ contents = this._extractInlineTemplate(contents);

return this._parseTemplate(contents);
}
_parseTemplate(template) {
let collection = new translation_collection_1.TranslationCollection();
};
DirectiveParser.prototype._parseTemplate = function (template) {
var collection = new translation_collection_1.TranslationCollection();
template = this._normalizeTemplateAttributes(template);
const selector = '[translate], [ng2-translate]';
var selector = '[translate], [ng2-translate]';
$(template)
.find(selector)
.addBack(selector)
.each((i, element) => {
const $element = $(element);
const attr = $element.attr('translate') || $element.attr('ng2-translate');
.each(function (i, element) {
var $element = $(element);
var attr = $element.attr('translate') || $element.attr('ng2-translate');
if (attr) {

@@ -30,12 +39,13 @@ collection = collection.add(attr);

.toArray()
.filter(node => node.type === 'text')
.map(node => node.nodeValue.trim())
.filter(text => text.length > 0)
.forEach(text => collection = collection.add(text));
.filter(function (node) { return node.type === 'text'; })
.map(function (node) { return node.nodeValue.trim(); })
.filter(function (text) { return text.length > 0; })
.forEach(function (text) { return collection = collection.add(text); });
}
});
return collection;
}
}
};
return DirectiveParser;
}(abstract_template_parser_1.AbstractTemplateParser));
exports.DirectiveParser = DirectiveParser;
//# sourceMappingURL=directive.parser.js.map
"use strict";
const abstract_template_parser_1 = require("./abstract-template.parser");
const translation_collection_1 = require("../utils/translation.collection");
class PipeParser extends abstract_template_parser_1.AbstractTemplateParser {
extract(contents, path) {
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var abstract_template_parser_1 = require("./abstract-template.parser");
var translation_collection_1 = require("../utils/translation.collection");
var PipeParser = (function (_super) {
__extends(PipeParser, _super);
function PipeParser() {
return _super.apply(this, arguments) || this;
}
PipeParser.prototype.extract = function (contents, path) {
if (path && this._isAngularComponent(path)) {

@@ -10,7 +19,7 @@ contents = this._extractInlineTemplate(contents);

return this._parseTemplate(contents);
}
_parseTemplate(template) {
let collection = new translation_collection_1.TranslationCollection();
const regExp = new RegExp(/(['"`])([^\1]*)\1\s*\|\s*translate/, 'g');
let matches;
};
PipeParser.prototype._parseTemplate = function (template) {
var collection = new translation_collection_1.TranslationCollection();
var regExp = new RegExp(/(['"`])([^\1]*)\1\s*\|\s*translate/, 'g');
var matches;
while (matches = regExp.exec(template)) {

@@ -20,5 +29,6 @@ collection = collection.add(matches[2]);

return collection;
}
}
};
return PipeParser;
}(abstract_template_parser_1.AbstractTemplateParser));
exports.PipeParser = PipeParser;
//# sourceMappingURL=pipe.parser.js.map
"use strict";
const translation_collection_1 = require("../utils/translation.collection");
class ServiceParser {
extract(contents, path) {
let collection = new translation_collection_1.TranslationCollection();
const translateServiceVar = this._extractTranslateServiceVar(contents);
var translation_collection_1 = require("../utils/translation.collection");
var ServiceParser = (function () {
function ServiceParser() {
}
ServiceParser.prototype.extract = function (contents, path) {
var collection = new translation_collection_1.TranslationCollection();
var translateServiceVar = this._extractTranslateServiceVar(contents);
if (!translateServiceVar) {
return collection;
}
const methodRegExp = new RegExp(/(?:get|instant)\s*\(\s*(\[?\s*(['"`])([^\1\r\n]*)\2\s*\]?)/);
const regExp = new RegExp(`${translateServiceVar}\.${methodRegExp.source}`, 'g');
let matches;
var methodRegExp = new RegExp(/(?:get|instant)\s*\(\s*(\[?\s*(['"`])([^\1\r\n]*)\2\s*\]?)/);
var regExp = new RegExp(translateServiceVar + "." + methodRegExp.source, 'g');
var matches;
while (matches = regExp.exec(contents)) {

@@ -22,5 +24,5 @@ if (this._stringContainsArray(matches[1])) {

return collection;
}
_extractTranslateServiceVar(contents) {
const matches = contents.match(/([a-z0-9_]+)\s*:\s*TranslateService/i);
};
ServiceParser.prototype._extractTranslateServiceVar = function (contents) {
var matches = contents.match(/([a-z0-9_]+)\s*:\s*TranslateService/i);
if (matches === null) {

@@ -30,7 +32,7 @@ return '';

return matches[1];
}
_stringContainsArray(input) {
};
ServiceParser.prototype._stringContainsArray = function (input) {
return input.startsWith('[') && input.endsWith(']');
}
_stringToArray(input) {
};
ServiceParser.prototype._stringToArray = function (input) {
if (this._stringContainsArray(input)) {

@@ -40,5 +42,6 @@ return eval(input);

return [];
}
}
};
return ServiceParser;
}());
exports.ServiceParser = ServiceParser;
//# sourceMappingURL=service.parser.js.map
"use strict";
const translation_collection_1 = require("./translation.collection");
const glob = require("glob");
const fs = require("fs");
class Extractor {
constructor(parsers, patterns) {
var translation_collection_1 = require("./translation.collection");
var glob = require("glob");
var fs = require("fs");
var Extractor = (function () {
function Extractor(parsers, patterns) {
this.parsers = parsers;
this.patterns = patterns;
}
process(dir) {
let collection = new translation_collection_1.TranslationCollection();
this._readDir(dir, this.patterns).forEach(path => {
const contents = fs.readFileSync(path, 'utf-8');
this.parsers.forEach((parser) => {
Extractor.prototype.process = function (dir) {
var _this = this;
var collection = new translation_collection_1.TranslationCollection();
this._readDir(dir, this.patterns).forEach(function (path) {
var contents = fs.readFileSync(path, 'utf-8');
_this.parsers.forEach(function (parser) {
collection = collection.union(parser.extract(contents, path));

@@ -19,12 +20,13 @@ });

return collection;
}
_readDir(dir, patterns) {
return patterns.reduce((results, pattern) => {
};
Extractor.prototype._readDir = function (dir, patterns) {
return patterns.reduce(function (results, pattern) {
return glob.sync(dir + pattern)
.filter(path => fs.statSync(path).isFile())
.filter(function (path) { return fs.statSync(path).isFile(); })
.concat(results);
}, []);
}
}
};
return Extractor;
}());
exports.Extractor = Extractor;
//# sourceMappingURL=extractor.js.map
"use strict";
;
class TranslationCollection {
constructor(values = {}) {
var TranslationCollection = (function () {
function TranslationCollection(values) {
if (values === void 0) { values = {}; }
this.values = {};
this.values = values;
}
add(key, val = '') {
return new TranslationCollection(Object.assign({}, this.values, { [key]: val }));
}
addKeys(keys) {
const values = keys.reduce((results, key) => {
TranslationCollection.prototype.add = function (key, val) {
if (val === void 0) { val = ''; }
return new TranslationCollection(Object.assign({}, this.values, (_a = {}, _a[key] = val, _a)));
var _a;
};
TranslationCollection.prototype.addKeys = function (keys) {
var values = keys.reduce(function (results, key) {
results[key] = '';

@@ -17,14 +20,16 @@ return results;

return new TranslationCollection(Object.assign({}, this.values, values));
}
remove(key) {
return this.filter(k => key !== k);
}
forEach(callback) {
Object.keys(this.values).forEach(key => callback.call(this, key, this.values[key]));
};
TranslationCollection.prototype.remove = function (key) {
return this.filter(function (k) { return key !== k; });
};
TranslationCollection.prototype.forEach = function (callback) {
var _this = this;
Object.keys(this.values).forEach(function (key) { return callback.call(_this, key, _this.values[key]); });
return this;
}
filter(callback) {
let values = {};
this.forEach((key, val) => {
if (callback.call(this, key, val)) {
};
TranslationCollection.prototype.filter = function (callback) {
var _this = this;
var values = {};
this.forEach(function (key, val) {
if (callback.call(_this, key, val)) {
values[key] = val;

@@ -34,31 +39,32 @@ }

return new TranslationCollection(values);
}
union(collection) {
};
TranslationCollection.prototype.union = function (collection) {
return new TranslationCollection(Object.assign({}, this.values, collection.values));
}
intersect(collection) {
let values = {};
this.filter(key => collection.has(key))
.forEach((key, val) => {
};
TranslationCollection.prototype.intersect = function (collection) {
var values = {};
this.filter(function (key) { return collection.has(key); })
.forEach(function (key, val) {
values[key] = val;
});
return new TranslationCollection(values);
}
has(key) {
};
TranslationCollection.prototype.has = function (key) {
return this.values.hasOwnProperty(key);
}
get(key) {
};
TranslationCollection.prototype.get = function (key) {
return this.values[key];
}
keys() {
};
TranslationCollection.prototype.keys = function () {
return Object.keys(this.values);
}
count() {
};
TranslationCollection.prototype.count = function () {
return Object.keys(this.values).length;
}
isEmpty() {
};
TranslationCollection.prototype.isEmpty = function () {
return Object.keys(this.values).length === 0;
}
}
};
return TranslationCollection;
}());
exports.TranslationCollection = TranslationCollection;
//# sourceMappingURL=translation.collection.js.map
{
"name": "@biesbjerg/ng2-translate-extract",
"version": "0.2.8",
"version": "0.2.9",
"description": "Extract strings from projects using ng2-translate",

@@ -42,3 +42,3 @@ "main": "dist/index.js",

"engines": {
"node": ">=6.9.0"
"node": ">=4.3.2"
},

@@ -45,0 +45,0 @@ "config": {},

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc