-
-
Notifications
You must be signed in to change notification settings - Fork 5
feat: Add lifecycle hooks for nitric. #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b79ca87
feat: Add lifecycle hooks for nitric runs.
tjholm 0bde205
passthrough return value of callback.
tjholm d5854ac
correct return type of whenInEnvironments.
tjholm 6705621
additional passthrough fixes
tjholm 79bd212
chore: add missing license header.
tjholm 7de2c35
Apply suggestions from code review
tjholm 0b44a16
Update src/environment/environment.ts
tjholm dce5996
fix exports
tjholm 019fb55
prettier
tjholm 37cc6b0
update naming.
tjholm fb98af0
lint.
tjholm 97898e7
update naming.
tjholm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Copyright 2021, Nitric Technologies Pty Ltd. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // The environment variable key that will be used to determine the current Nitric lifecycle/executing environment | ||
| const NITRIC_ENVIRONMENT = "NITRIC_ENVIRONMENT"; | ||
|
|
||
| // Possible nitric execution environments | ||
| enum EnvironmentStage { | ||
| // Local development run (using nitric run/start) | ||
| LocalRun = "run", | ||
| // Local development requirements building/collection (using nitric up) | ||
| Build = "build", | ||
| // When the code is running in a deployed environment | ||
| Cloud = "cloud", | ||
| } | ||
|
|
||
| const getCurrentEnvironment = (): EnvironmentStage => { | ||
| const lifecycle = process.env[NITRIC_ENVIRONMENT]; | ||
| if (!lifecycle || !Object.values(EnvironmentStage).includes(lifecycle as EnvironmentStage)) { | ||
| throw new Error(`Unable to determine the current Nitric lifecycle, please ensure the ${NITRIC_ENVIRONMENT} environment variable is set`); | ||
| } | ||
| return lifecycle as EnvironmentStage; | ||
| } | ||
|
|
||
| // Check if the current environment is one of the provided stages | ||
| const isInEnvironment = (stage: EnvironmentStage[]) => { | ||
| const currentStage = getCurrentEnvironment(); | ||
| return stage.includes(currentStage); | ||
| } | ||
|
|
||
| // If the current environment is one of the provided stages, execute the provided callback | ||
| const whenInEnvironments = <T>(stage: EnvironmentStage[], callback: EnvCallback<T>): T | undefined => { | ||
| if (isInEnvironment(stage)) { | ||
| return callback(); | ||
| } | ||
| } | ||
|
|
||
| const whenLocalRun = <T>(callback: EnvCallback<T>) => whenInEnvironments([EnvironmentStage.LocalRun], callback); | ||
|
|
||
|
|
||
| const whenBuild = <T>(callback: EnvCallback<T>) => whenInEnvironments([EnvironmentStage.Build], callback); | ||
|
|
||
| const whenCloud = <T>(callback: EnvCallback<T>) => whenInEnvironments([EnvironmentStage.Cloud], callback); | ||
tjholm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| type EnvCallback<T> = () => T; | ||
|
|
||
| export const Environment = { | ||
| // Check if the current environment is one of the provided stages | ||
| is: isInEnvironment, | ||
| // Check if the current environment is a local run | ||
| isLocalRun: () => isInEnvironment([EnvironmentStage.LocalRun]), | ||
| // Check if the current environment is a build | ||
| isBuild: () => isInEnvironment([EnvironmentStage.Build]), | ||
| // Check if the current environment is a cloud environment | ||
| isCloud: () => isInEnvironment([EnvironmentStage.Cloud]), | ||
| // If the current environment is one of the provided stages, execute the provided callback | ||
| when: whenInEnvironments, | ||
| // If the current environment is a local run, execute the provided callback | ||
| whenLocalRun, | ||
| // If the current environment is a build, execute the provided callback | ||
| whenBuild, | ||
| // If the current environment is a cloud environment, execute the provided callback | ||
| whenCloud, | ||
tjholm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Copyright 2021, Nitric Technologies Pty Ltd. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| export * from "./environment"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.