From 76092b7bcd212837d0ee29dccc3658a862c747c9 Mon Sep 17 00:00:00 2001 From: jlenon7 Date: Mon, 4 Mar 2024 14:30:30 +0000 Subject: [PATCH] feat(rc): reload rc file after changes --- package-lock.json | 4 ++-- package.json | 2 +- src/helpers/Rc.ts | 23 +++++++++++++++++++++++ tests/unit/helpers/RcTest.ts | 20 ++++++++++++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3cb29f3..0065b81 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/config", - "version": "4.18.0", + "version": "4.19.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@athenna/config", - "version": "4.18.0", + "version": "4.19.0", "license": "MIT", "dependencies": { "dotenv": "^16.4.1", diff --git a/package.json b/package.json index d45ff93..1a760ba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/config", - "version": "4.18.0", + "version": "4.19.0", "description": "Cache and handle environment variables and config files of Athenna.", "license": "MIT", "author": "João Lenon ", diff --git a/src/helpers/Rc.ts b/src/helpers/Rc.ts index 5739bf4..f2d7a68 100644 --- a/src/helpers/Rc.ts +++ b/src/helpers/Rc.ts @@ -15,6 +15,23 @@ export class Rc { public static file: File public static content: ObjectBuilder + /** + * Reload the Rc file and content. Useful when + * changes has been made in the Rc file by external + * tools. + */ + public static async reload(): Promise { + if (!this.file) { + debug('RC file still not set, ignoring reload call') + + return this + } + + debug('Reloading RC file: %s', this.file.path) + + return this.setFile(this.file.path) + } + /** * Set the RC file that the Rc class should work with. */ @@ -120,6 +137,8 @@ export class Rc { } as any }) + await this.reload() + return } @@ -130,12 +149,16 @@ export class Rc { await this.file.setContent(this.toStringJson(packageJson)) + await this.reload() + return } const athennaRcJson = this.content.get() await this.file.setContent(this.toStringJson(athennaRcJson)) + + await this.reload() } /** diff --git a/tests/unit/helpers/RcTest.ts b/tests/unit/helpers/RcTest.ts index ea467c7..ac43716 100644 --- a/tests/unit/helpers/RcTest.ts +++ b/tests/unit/helpers/RcTest.ts @@ -250,4 +250,24 @@ export default class RcTest { providers: ['value', 'value', 'value'] }) } + + @Test() + public async shouldBeAbleToReloadTheRcFile({ assert }: Context) { + const path = Path.fixtures('.athennarc.json') + + await Rc.setFile(path) + + const rcFile = new File(path) + const rcFileContent = await rcFile.getContentAsJson() + + rcFileContent.providers = ['#src/providers/AppProvider'] + + await rcFile.setContent(JSON.stringify(rcFileContent)) + + await Rc.reload().then(() => Rc.pushTo('providers', '#src/providers/DbProvider').save()) + + const rcFileContentUpdated = await Rc.file.getContentAsJson() + + assert.deepEqual(rcFileContentUpdated.providers, ['#src/providers/AppProvider', '#src/providers/DbProvider']) + } }