From 68ef936b4b89f2d43140f9acc153c95873b1c059 Mon Sep 17 00:00:00 2001 From: Pranav C <61551451+pranavxc@users.noreply.github.com> Date: Tue, 20 Jul 2021 16:34:36 +0530 Subject: [PATCH] feat: Add latest release info spi Signed-off-by: Pranav C <61551451+pranavxc@users.noreply.github.com> --- packages/nocodb/package-lock.json | 8 +- packages/nocodb/package.json | 2 +- .../nocodb/src/lib/noco/meta/NcMetaMgr.ts | 84 +++++++++++-------- .../noco/plugins/adapters/cache/XcCache.ts | 4 +- 4 files changed, 57 insertions(+), 41 deletions(-) diff --git a/packages/nocodb/package-lock.json b/packages/nocodb/package-lock.json index 2aca8d3b30..9de4770e3f 100644 --- a/packages/nocodb/package-lock.json +++ b/packages/nocodb/package-lock.json @@ -1,6 +1,6 @@ { "name": "nocodb", - "version": "0.10.5", + "version": "0.10.6", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -11800,9 +11800,9 @@ "integrity": "sha512-3AryS9uwa5NfISLxMciUonrH7YfXp+nlahB9T7girXIsLQrmwX4MdnuKs32akduCOGpKmjTJSWmATULbuMkbfw==" }, "nc-help": { - "version": "0.2.4", - "resolved": "https://registry.npmjs.org/nc-help/-/nc-help-0.2.4.tgz", - "integrity": "sha512-kwNlE+2U1ZLIp+ct3yh5ihQO81dXqiqUpJ5JezzJ00y5pu6P5R9gdY1LiVTrLawZoHXxsNlFQVTWw67P07Y5AQ==", + "version": "0.2.7", + "resolved": "https://registry.npmjs.org/nc-help/-/nc-help-0.2.7.tgz", + "integrity": "sha512-31wxoJmfZHJ4aP/YiI+GgZRqwulyB6Z5BamcCUw/NDdNVoze+zHX0CtmoxfSlyi/U4zUOG9rhennKPv6QmmsMQ==", "requires": { "axios": "^0.21.1", "boxen": "^4.2.0", diff --git a/packages/nocodb/package.json b/packages/nocodb/package.json index 8abdfa6b7d..ffd83070a3 100644 --- a/packages/nocodb/package.json +++ b/packages/nocodb/package.json @@ -144,7 +144,7 @@ "mysql2": "^2.2.5", "nanoid": "^3.1.20", "nc-common": "0.0.6", - "nc-help": "^0.2.4", + "nc-help": "^0.2.7", "nc-lib-gui": "^0.2.7", "nc-plugin": "^0.1.1", "nodemailer": "^6.4.10", diff --git a/packages/nocodb/src/lib/noco/meta/NcMetaMgr.ts b/packages/nocodb/src/lib/noco/meta/NcMetaMgr.ts index 1063490b15..06df82411e 100644 --- a/packages/nocodb/src/lib/noco/meta/NcMetaMgr.ts +++ b/packages/nocodb/src/lib/noco/meta/NcMetaMgr.ts @@ -34,17 +34,12 @@ import EmailFactory from "../plugins/adapters/email/EmailFactory"; import Twilio from "../plugins/adapters/twilio/Twilio"; import {RestApiBuilder} from "../rest/RestApiBuilder"; import RestAuthCtrl from "../rest/RestAuthCtrlEE"; - +import {packageVersion} from 'nc-help'; import NcMetaIO, {META_TABLES} from "./NcMetaIO"; const XC_PLUGIN_DET = 'XC_PLUGIN_DET'; - -let packageInfo: any = {}; -try { - packageInfo = JSON.parse(fs.readFileSync('package.json', 'utf8')); -} catch (_e) { -} +const NOCO_RELEASE = 'NOCO_RELEASE'; export default class NcMetaMgr { public projectConfigs = {}; @@ -60,7 +55,7 @@ export default class NcMetaMgr { protected projectMgr: any; // @ts-ignore protected isEe = false; - + 4 constructor(app: Noco, config: NcConfig, xcMeta: NcMetaIO) { this.app = app; @@ -78,8 +73,6 @@ export default class NcMetaMgr { await this.pluginMgr?.init(); - await this.initStorage(); - await this.initEmail(); await this.initTwilio(); await this.initCache(); this.eeVerify(); @@ -223,7 +216,7 @@ export default class NcMetaMgr { githubAuthEnabled: !!(process.env.NC_GITHUB_CLIENT_ID && process.env.NC_GITHUB_CLIENT_SECRET), oneClick: !!process.env.NC_ONE_CLICK, connectToExternalDB: !process.env.NC_CONNECT_TO_EXTERNAL_DB_DISABLED, - version: packageInfo?.version + version: packageVersion }) } if (this.config.auth.masterKey) { @@ -964,28 +957,6 @@ export default class NcMetaMgr { } } - protected async initStorage(_overwrite = false): Promise { - // - // const activeStorage = await this.xcMeta.metaGet(null, null, 'nc_plugins', { - // active: true, - // category: 'Storage' - // }); - // - // this.storageAdapter = StorageFactory.create(activeStorage, overwrite); - // await this.storageAdapter?.init(); - } - - protected async initEmail(_overwrite = false): Promise { - - // const activeStorage = await this.xcMeta.metaGet(null, null, 'nc_plugins', { - // active: true, - // category: 'Email' - // }); - // - // this.emailAdapter = EmailFactory.create(activeStorage, overwrite); - // await this.emailAdapter?.init(); - } - protected async initTwilio(overwrite = false): Promise { const activeStorage = await this.xcMeta.metaGet(null, null, 'nc_plugins', { @@ -1018,6 +989,9 @@ export default class NcMetaMgr { case 'getSharedViewData': result = await this.getSharedViewData(req, args); break; + case 'xcRelease': + result = await this.xcRelease(); + break; default: return next(); @@ -3475,7 +3449,7 @@ export default class NcMetaMgr { Database: config.envs?.[process.env.NODE_ENV || 'dev']?.db?.[0]?.client, 'ProjectOnRootDB': !!config?.prefix, 'RootDB': this.config?.meta?.db?.client, - 'PackageVersion': packageInfo?.version + 'PackageVersion': packageVersion } } @@ -3576,6 +3550,48 @@ export default class NcMetaMgr { return this.pluginMgr?.webhookNotificationAdapters; } + private async xcRelease() { + const cachedResult = XcCache.get(NOCO_RELEASE); + if (cachedResult) { + return cachedResult; + } + + const result: any = { + current: packageVersion, + isDocker: isDocker() + }; + try { + const dockerTags = (await axios({ + url: 'https://registry.hub.docker.com/v1/repositories/nocodb/nocodb/tags' + })).data; + const verPattern = /^(\d+)\.(\d+)\.(\d+)$/; + result.docker = dockerTags.sort((a, b): any => { + const m1: any = a.name.match(verPattern); + const m2: any = b.name.match(verPattern); + if (m1 && m2) { + return (m2[1] - m1[1]) || (m2[2] - m1[2]) || (m2[3] - m1[3]); + } else if (m1) { + return -Infinity + } else if (m2) { + return Infinity + } + return 0; + })?.[0]; + + result.docker.name = '0.10.7' + + if (result.docker && result.docker.name !== packageVersion) { + result.docker.upgrade = true; + } + + XcCache.set(NOCO_RELEASE, result, 60 * 60 * 1000); + } catch (e) { + console.log(e); + } + + return result; + } + } diff --git a/packages/nocodb/src/lib/noco/plugins/adapters/cache/XcCache.ts b/packages/nocodb/src/lib/noco/plugins/adapters/cache/XcCache.ts index 344ebb71e6..cd855502c1 100644 --- a/packages/nocodb/src/lib/noco/plugins/adapters/cache/XcCache.ts +++ b/packages/nocodb/src/lib/noco/plugins/adapters/cache/XcCache.ts @@ -27,8 +27,8 @@ export default class XcCache { return this.instance?.get(key); } - public static set(key, val): boolean { - return this.instance?.set(key, val); + public static set(key, val, maxAge = 1000 * 60 * 60): boolean { + return this.instance?.set(key, val, maxAge); } public static del(key): void {