diff --git a/src/index.ts b/src/index.ts index ef898d5..c28f5d7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,3 +16,4 @@ export * from './resources'; export * from './types'; export * from './context'; export * from './handlers'; +export * from './lifecycle'; diff --git a/src/lifecycle/index.ts b/src/lifecycle/index.ts new file mode 100644 index 0000000..c79db7c --- /dev/null +++ b/src/lifecycle/index.ts @@ -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 './lifecycle'; diff --git a/src/lifecycle/lifecycle.ts b/src/lifecycle/lifecycle.ts new file mode 100644 index 0000000..584f7e1 --- /dev/null +++ b/src/lifecycle/lifecycle.ts @@ -0,0 +1,83 @@ +// 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 LifecycleStage { + // 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 getCurrentLifecycle = (): LifecycleStage => { + const lifecycle = process.env[NITRIC_ENVIRONMENT]; + if ( + !lifecycle || + !Object.values(LifecycleStage).includes(lifecycle as LifecycleStage) + ) { + throw new Error( + `Unable to determine the current Nitric lifecycle, please ensure the ${NITRIC_ENVIRONMENT} environment variable is set` + ); + } + return lifecycle as LifecycleStage; +}; + +// Check if the current environment is one of the provided stages +const isInLifecycle = (stage: LifecycleStage[]) => { + const currentStage = getCurrentLifecycle(); + return stage.includes(currentStage); +}; + +// If the current environment is one of the provided stages, execute the provided callback +const whenInLifecycles = ( + stage: LifecycleStage[], + callback: Lifecycle +): T | undefined => { + if (isInLifecycle(stage)) { + return callback(); + } +}; + +const whenRunning = (callback: Lifecycle) => + whenInLifecycles([LifecycleStage.LocalRun, LifecycleStage.Cloud], callback); + +const whenCollecting = (callback: Lifecycle) => + whenInLifecycles([LifecycleStage.Build], callback); + +const isRunning = () => + isInLifecycle([LifecycleStage.LocalRun, LifecycleStage.Cloud]); + +const isCollecting = () => isInLifecycle([LifecycleStage.Build]); + +type Lifecycle = () => T; + +export const Lifecycle = { + // Check if the current environment is one of the provided stages + is: isInLifecycle, + // Check if the current lifecycle is collecting application requirements + isCollecting, + // Check if the current lifecycle is running the app + isRunning, + // If the current environment is one of the provided stages, execute the provided callback + when: whenInLifecycles, + // If the current environment is collecting application requirements + whenCollecting, + // If the current environment is a cloud environment, execute the provided callback + whenRunning, +};