Skip to content

feat(core): add Magek.init for programmatic environment setup - #811

Open
o-mid wants to merge 1 commit into
theam:mainfrom
o-mid:feat/magek-init
Open

o-mid wants to merge 1 commit into
theam:mainfrom
o-mid:feat/magek-init

Conversation

@o-mid

@o-mid o-mid commented Sep 28, 2026 •

Copy link
Copy Markdown

Summary

Magek.config was built when @magek/core loaded, so MAGEK_ENV had to be set before the import. Config is now created the first time it is read. Magek.init(environment) sets the name before that read. If init is not called, MAGEK_ENV is still used.

init has to run before any module that uses Magek decorators or Magek.configure is evaluated. ES import declarations are hoisted, so a call under those imports in the same file is too late.

Calling init again with the same name does nothing. A different name throws, including when config was already built from MAGEK_ENV.

Semver: the version policy's next bump is still patch. This adds Magek.init and keeps the MAGEK_ENV path. It is not a silent upgrade for tests: Magek.config is a getter, and sinon replace(Magek, 'config', config) throws. Use replaceGetter(Magek, 'config', () => config). docs/content/advanced/testing.md shows that.

Fixes #664

Test plan

  • cd packages/core && rushx test (337 passing)
  • cd packages/core && rushx lint:fix
  • cd packages/server && rushx test (39 passing)

Magek.config was created when the module loaded, so MAGEK_ENV had to be set before import. Config is now created on first access. Magek.init(environment) sets the name before that. The MAGEK_ENV path still works. sinon replace on Magek.config has to become replaceGetter.

Fixes theam#664

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(core): Add programmatic environment initialization

1 participant