UNPKG

gulp-resx-convert

Version:

A .resx file converter for gulp. Supports multiple output formats, including JSON, Typescript and C# constants class

236 lines 14.1 kB
"use strict"; var ES; (function (ES) { var FX; (function (FX) { var JS; (function (JS) { var Gulp; (function (Gulp) { var Resx; (function (Resx) { "use strict"; var Converter = (function () { function Converter($file, $options) { var _this = this; this.$file = $file; this.$options = $options; this.$dictionary = {}; this.$culture = null; this.convert = function () { $gutil.log("Converting file '" + _this.$file.path + "' | Properties found: " + Object.keys(_this.$dictionary).length + " | Culture: '" + (_this.$culture || "") + "'"); var result = []; if (_this.$options == null) { $gutil.log("No options provided. No conversion possible"); return result; } if (_this.$options.json) { result = result.concat(_this.convertToJson()); } if (_this.$options.typescript) { result = result.concat(_this.convertToTypeScript()); } if (_this.$options.csharp) { result = result.concat(_this.convertToCSharp()); } return result; }; this.$filePath = $path.parse(this.$file.path); var cultureRegex = new RegExp(/.*\.([a-zA-Z]{2}-[a-zA-Z]{2}|[a-zA-Z]{2})/g); var cultureMatches = cultureRegex.exec(this.$filePath.name); if (cultureMatches != null && cultureMatches.length > 1) { this.$culture = cultureMatches[1]; } (new $xml.Parser()).parseString(this.$file.contents, function (err, result) { if (err != null || result == null) throw new Error("Could not parse XML in file " + _this.$file.path); if (result.root.data != null) { for (var _i = 0, _a = result.root.data; _i < _a.length; _i++) { var resource = _a[_i]; var key = resource.$.name; var value = resource.value.toString(); var type = { csharp: "string", typescript: "string", raw: false }; if (resource.comment) { var comment = resource.comment; try { var commentType = JSON.parse(comment); if (commentType.csharp) type.csharp = commentType.csharp; if (commentType.typescript) type.typescript = commentType.typescript; if (commentType.raw === true) type.raw = commentType.raw; } catch (Error) { } } _this.$dictionary[key] = { value: value, type: type }; } } }); } Converter.prototype.convertToJson = function () { var source = "{" + $os.EOL; var keys = Object.keys(this.$dictionary); for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) { var key = keys_1[_i]; var property = this.$dictionary[key]; var value = property.type.raw ? property.value : JSON.stringify(property.value); var comma = keys.indexOf(key) !== keys.length - 1 ? "," : ""; source += "\t\"" + key + "\": " + value + comma + $os.EOL; } source += "}"; var file = this.$file.clone(); file.path = $path.join(this.$filePath.dir, this.$filePath.name + ".json"); file.contents = new $Buffer(source); return [file]; }; Converter.prototype.convertToTypeScript = function () { var result = []; if (!this.$options.typescript.source && !this.$options.typescript.declaration) return result; var namespace = this.$options.typescript.namespace ? this.$options.typescript.namespace : "Resx"; var interfaceName = this.$filePath.name; var variableName = "$Resx_" + interfaceName; if (!this.$options.typescript.culturesDisabled && this.$culture !== null) { interfaceName = interfaceName.substring(0, interfaceName.lastIndexOf("." + this.$culture)); variableName = "$Resx_" + interfaceName + "_" + this.$culture.replace("-", "_"); } if (this.$options.typescript.declaration) { var declaration = "declare namespace " + namespace + " { " + $os.EOL; declaration += "\tinterface " + interfaceName + " { " + $os.EOL; for (var _i = 0, _a = Object.keys(this.$dictionary); _i < _a.length; _i++) { var key = _a[_i]; var property = this.$dictionary[key]; declaration += "\t\t" + key + ": " + property.type.typescript + ";" + $os.EOL; } declaration += "\t}" + $os.EOL; if (this.$options.typescript.source) { declaration += "\tvar " + variableName + ": " + interfaceName + ";" + $os.EOL; } declaration += "}" + $os.EOL; var declarationFile = this.$file.clone(); declarationFile.path = $path.join(this.$filePath.dir, this.$filePath.name + ".d.ts"); declarationFile.contents = new $Buffer(declaration); result.push(declarationFile); } if (this.$options.typescript.source) { var source = "namespace " + namespace + " { " + $os.EOL; source += "\texport interface " + interfaceName + " { " + $os.EOL; for (var _b = 0, _c = Object.keys(this.$dictionary); _b < _c.length; _b++) { var key = _c[_b]; var property = this.$dictionary[key]; source += "\t\t" + key + ": " + property.type.typescript + ";" + $os.EOL; } source += "\t}" + $os.EOL; source += "\texport var " + variableName + ": " + interfaceName + " = <" + interfaceName + ">{ " + $os.EOL; var keys = Object.keys(this.$dictionary); for (var _d = 0, keys_2 = keys; _d < keys_2.length; _d++) { var key = keys_2[_d]; var property = this.$dictionary[key]; var value = property.type.raw ? property.value : JSON.stringify(property.value); var comma = keys.indexOf(key) !== keys.length - 1 ? "," : ""; source += "\t\t" + key + ": " + value + comma + $os.EOL; } source += "\t}" + $os.EOL; source += "}" + $os.EOL; var sourceFile = this.$file.clone(); sourceFile.path = $path.join(this.$filePath.dir, this.$filePath.name + ".ts"); sourceFile.contents = new $Buffer(source); result.push(sourceFile); } return result; }; Converter.prototype.convertToCSharp = function () { var result = []; var namespace = this.$options.csharp.namespace ? this.$options.csharp.namespace : "Resx"; var className = "" + this.$filePath.name.replace("-", "_").replace(".", "_"); var source = "using System; " + $os.EOL + " " + $os.EOL; source += "namespace " + namespace + $os.EOL; source += "{" + $os.EOL; source += "\tpublic " + (this.$options.csharp.partial ? "partial " : "") + "class " + className + $os.EOL; source += "\t{" + $os.EOL; var keys = Object.keys(this.$dictionary); for (var _i = 0, keys_3 = keys; _i < keys_3.length; _i++) { var key = keys_3[_i]; var property = this.$dictionary[key]; var value = property.type.raw ? property.value : JSON.stringify(property.value); var equal = this.$options.csharp.properties ? "=>" : "="; var memberType = this.$options.csharp.properties ? "static" : "const"; source += "\t\tpublic " + memberType + " " + property.type.csharp + " " + key + " " + equal + " " + value.toString() + ";" + $os.EOL; } source += "\t}" + $os.EOL; source += "}" + $os.EOL; var sourceFile = this.$file.clone(); sourceFile.path = $path.join(this.$filePath.dir, this.$filePath.name + ".cs"); sourceFile.contents = new $Buffer(source); result.push(sourceFile); return result; }; return Converter; }()); Resx.Converter = Converter; })(Resx = Gulp.Resx || (Gulp.Resx = {})); })(Gulp = JS.Gulp || (JS.Gulp = {})); })(JS = FX.JS || (FX.JS = {})); })(FX = ES.FX || (ES.FX = {})); })(ES || (ES = {})); var ES; (function (ES) { var FX; (function (FX) { var JS; (function (JS) { var Gulp; (function (Gulp) { var Resx; (function (Resx) { "use strict"; })(Resx = Gulp.Resx || (Gulp.Resx = {})); })(Gulp = JS.Gulp || (JS.Gulp = {})); })(JS = FX.JS || (FX.JS = {})); })(FX = ES.FX || (ES.FX = {})); })(ES || (ES = {})); var ES; (function (ES) { var FX; (function (FX) { var JS; (function (JS) { var Gulp; (function (Gulp) { var Resx; (function (Resx) { "use strict"; })(Resx = Gulp.Resx || (Gulp.Resx = {})); })(Gulp = JS.Gulp || (JS.Gulp = {})); })(JS = FX.JS || (FX.JS = {})); })(FX = ES.FX || (ES.FX = {})); })(ES || (ES = {})); var $os = require("os"); var $through2 = require("through2"); var $Buffer = require("buffer").Buffer; var $gutil = require("gulp-util"); var $PluginError = $gutil.PluginError; var $path = require("path"); var $xml = require("xml2js"); exports.convert = function (options) { return $through2.obj(function (file, enc, cb) { if (file.isStream()) { this.emit("error", new $PluginError("moonstone-resx", "Streaming not supported")); } else if (file.isBuffer()) { var convertedFiles = (new ES.FX.JS.Gulp.Resx.Converter(file, options)).convert(); for (var _i = 0, convertedFiles_1 = convertedFiles; _i < convertedFiles_1.length; _i++) { var convertedFile = convertedFiles_1[_i]; this.push(convertedFile); } } return cb(); }); }; //# sourceMappingURL=gulp-resx-convert.js.map