From c46dfaf58b0b047fde6b3e267259a912ee3f63d0 Mon Sep 17 00:00:00 2001 From: Esiur Project Date: Mon, 19 Jun 2023 22:30:44 +0300 Subject: [PATCH] global --- bin/esiur.cjs | 6 +++++- build/esiur.js | 32 ++++++++++++++++++----------- package-lock.json | 37 +++++++++++++++++++++------------- package.json | 2 +- src/Proxy/TemplateGenerator.js | 10 +++++++-- 5 files changed, 57 insertions(+), 30 deletions(-) diff --git a/bin/esiur.cjs b/bin/esiur.cjs index 178617d..8e17305 100644 --- a/bin/esiur.cjs +++ b/bin/esiur.cjs @@ -12,6 +12,8 @@ if (process.argv.length == 2) { console.log("\t-u, --username\tAuthentication username."); console.log("\t-p, --password\tAuthentication password."); console.log("\t-d, --dir\tName of the directory to generate model inside."); + console.log("\t-g, --global\tDeclare a global variable for the module with the specified name."); + process.exit(); } @@ -38,6 +40,8 @@ else if (cmd == "get-template") // get username let username = getOption(args, "--username", "-u"); let password = getOption(args, "--password", "-p"); + let globalName = getOption(args, "--global", "-g"); + let dir = getOption(args, "--dir", "-d"); if (args.length == 0){ @@ -51,7 +55,7 @@ else if (cmd == "get-template") let getTemplate = async () => { let Esiur = (await import('../src/esiur.js')).default; - await Esiur.Proxy.TemplateGenerator.getTemplate(url, dir, username, password, true); + await Esiur.Proxy.TemplateGenerator.getTemplate(url, dir, username, password, true, globalName); process.exit(); } diff --git a/build/esiur.js b/build/esiur.js index b5ef0e5..2d42c98 100644 --- a/build/esiur.js +++ b/build/esiur.js @@ -10938,6 +10938,7 @@ var TemplateGenerator = /*#__PURE__*/function () { var _this3 = this; var asyncSetters, + globalName, fs, _urlRegex, path, @@ -10954,47 +10955,48 @@ var TemplateGenerator = /*#__PURE__*/function () { switch (_context2.prev = _context2.next) { case 0: asyncSetters = _args2.length > 4 && _args2[4] !== undefined ? _args2[4] : true; - _context2.next = 3; + globalName = _args2.length > 5 && _args2[5] !== undefined ? _args2[5] : null; + _context2.next = 4; return Promise.resolve().then(function () { return _interopRequireWildcard(require("fs")); }); - case 3: + case 4: fs = _context2.sent; // var fs = require('fs'); _urlRegex = /^(?:([^\s|:]*):\/\/([^/]*)\/?(.*))/; // /^(?:([^\s|:]*):\/\/([^/]*)\/?)/; if (_urlRegex.test(url)) { - _context2.next = 7; + _context2.next = 8; break; } throw Error("Invalid IIP URL '".concat(url, "'")); - case 7: + case 8: path = url.split(_urlRegex); - _context2.next = 10; + _context2.next = 11; return _Warehouse["default"].get(path[1] + "://" + path[2], username != null ? { "username": username, "password": password !== null && password !== void 0 ? password : "" } : null); - case 10: + case 11: con = _context2.sent; if (!(con == null)) { - _context2.next = 13; + _context2.next = 14; break; } throw Error("Can't connect to server"); - case 13: + case 14: if (dir == null || dir == "") dir = path[2].replaceAll(":", "_"); - _context2.next = 16; + _context2.next = 17; return con.getLinkTemplates(path[3]); - case 16: + case 17: templates = _context2.sent; // no longer needed @@ -11063,11 +11065,17 @@ var TemplateGenerator = /*#__PURE__*/function () { module += "Esiur.define(module, ".concat(typeName, ", '").concat(tmp.className, "');\r\n"); module += "new Esiur.Resource.Template.TypeTemplate(".concat(typeName, ", true);\r\n"); }); - module += "\r\nexport default module;"; + module += "\r\nexport default module;\r\n"; + + if (globalName != null) { + module += "\r\nif (typeof window !== 'undefined') window[\"".concat(globalName, "\"] = module;\r\n"); + module += "\r\nelse if (typeof global !== 'undefined') global[\"".concat(globalName, "\"] = module;\r\n"); + } + fs.writeFileSync(modulePath, module); return _context2.abrupt("return", dstDir); - case 28: + case 30: case "end": return _context2.stop(); } diff --git a/package-lock.json b/package-lock.json index e3f5d27..9762446 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,20 +1,19 @@ { "name": "esiur", - "version": "2.1.3", + "version": "2.2.8", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "esiur", - "version": "2.1.3", + "version": "2.2.8", "license": "MIT", "dependencies": { "mongodb": "^3.6.9", "ws": "^7.5.0" }, "bin": { - "esiur": "bin/esiur.cjs", - "esiur2": "node -i -e \"import('./src/esiur.js');\"" + "esiur": "bin/esiur.cjs" }, "devDependencies": { "@babel/core": "^7.14.6", @@ -2206,14 +2205,24 @@ } }, "node_modules/caniuse-lite": { - "version": "1.0.30001315", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001315.tgz", - "integrity": "sha512-5v7LFQU4Sb/qvkz7JcZkvtSH1Ko+1x2kgo3ocdBeMGZSOFpuE1kkm0kpTwLtWeFrw5qw08ulLxJjVIXIS8MkiQ==", + "version": "1.0.30001504", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001504.tgz", + "integrity": "sha512-5uo7eoOp2mKbWyfMXnGO9rJWOGU8duvzEiYITW+wivukL7yHH4gX9yuRaobu6El4jPxo6jKZfG+N6fB621GD/Q==", "dev": true, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/browserslist" - } + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/browserslist" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/caniuse-lite" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ] }, "node_modules/chalk": { "version": "2.4.2", @@ -6696,9 +6705,9 @@ "dev": true }, "caniuse-lite": { - "version": "1.0.30001315", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001315.tgz", - "integrity": "sha512-5v7LFQU4Sb/qvkz7JcZkvtSH1Ko+1x2kgo3ocdBeMGZSOFpuE1kkm0kpTwLtWeFrw5qw08ulLxJjVIXIS8MkiQ==", + "version": "1.0.30001504", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001504.tgz", + "integrity": "sha512-5uo7eoOp2mKbWyfMXnGO9rJWOGU8duvzEiYITW+wivukL7yHH4gX9yuRaobu6El4jPxo6jKZfG+N6fB621GD/Q==", "dev": true }, "chalk": { diff --git a/package.json b/package.json index fefd1f0..e8df772 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "esiur", - "version": "2.2.7", + "version": "2.2.8", "description": "Distributed Object Framework", "main": "esiur.js", "type": "module", diff --git a/src/Proxy/TemplateGenerator.js b/src/Proxy/TemplateGenerator.js index 4b0e2f2..eb41525 100644 --- a/src/Proxy/TemplateGenerator.js +++ b/src/Proxy/TemplateGenerator.js @@ -238,7 +238,7 @@ export default class TemplateGenerator { return v == null || v == ""; } - static async getTemplate(url, dir, username, password, asyncSetters = true) { + static async getTemplate(url, dir, username, password, asyncSetters = true, globalName = null) { const fs = await import("fs"); @@ -318,7 +318,13 @@ export default class TemplateGenerator { module += `new Esiur.Resource.Template.TypeTemplate(${typeName}, true);\r\n` }); - module += "\r\nexport default module;"; + module += "\r\nexport default module;\r\n"; + + if (globalName != null) { + module += `\r\nif (typeof window !== 'undefined') window["${globalName}"] = module;\r\n`; + module += `\r\nelse if (typeof global !== 'undefined') global["${globalName}"] = module;\r\n`; + } + fs.writeFileSync(modulePath, module); return dstDir;