Skip to content

Commit

Permalink
feat(rc): reload rc file after changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Mar 4, 2024
1 parent 5fb8315 commit 76092b7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <lenon@athenna.io>",
Expand Down
23 changes: 23 additions & 0 deletions src/helpers/Rc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof Rc> {
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.
*/
Expand Down Expand Up @@ -120,6 +137,8 @@ export class Rc {
} as any
})

await this.reload()

return
}

Expand All @@ -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()
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/helpers/RcTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
}
}

0 comments on commit 76092b7

Please sign in to comment.