|
| 1 | +// Copyright 2021, Nitric Technologies Pty Ltd. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// The environment variable key that will be used to determine the current Nitric lifecycle/executing environment |
| 16 | +const NITRIC_ENVIRONMENT = 'NITRIC_ENVIRONMENT'; |
| 17 | + |
| 18 | +// Possible nitric execution environments |
| 19 | +enum LifecycleStage { |
| 20 | + // Local development run (using nitric run/start) |
| 21 | + LocalRun = 'run', |
| 22 | + // Local development requirements building/collection (using nitric up) |
| 23 | + Build = 'build', |
| 24 | + // When the code is running in a deployed environment |
| 25 | + Cloud = 'cloud', |
| 26 | +} |
| 27 | + |
| 28 | +const getCurrentLifecycle = (): LifecycleStage => { |
| 29 | + const lifecycle = process.env[NITRIC_ENVIRONMENT]; |
| 30 | + if ( |
| 31 | + !lifecycle || |
| 32 | + !Object.values(LifecycleStage).includes(lifecycle as LifecycleStage) |
| 33 | + ) { |
| 34 | + throw new Error( |
| 35 | + `Unable to determine the current Nitric lifecycle, please ensure the ${NITRIC_ENVIRONMENT} environment variable is set` |
| 36 | + ); |
| 37 | + } |
| 38 | + return lifecycle as LifecycleStage; |
| 39 | +}; |
| 40 | + |
| 41 | +// Check if the current environment is one of the provided stages |
| 42 | +const isInLifecycle = (stage: LifecycleStage[]) => { |
| 43 | + const currentStage = getCurrentLifecycle(); |
| 44 | + return stage.includes(currentStage); |
| 45 | +}; |
| 46 | + |
| 47 | +// If the current environment is one of the provided stages, execute the provided callback |
| 48 | +const whenInLifecycles = <T>( |
| 49 | + stage: LifecycleStage[], |
| 50 | + callback: Lifecycle<T> |
| 51 | +): T | undefined => { |
| 52 | + if (isInLifecycle(stage)) { |
| 53 | + return callback(); |
| 54 | + } |
| 55 | +}; |
| 56 | + |
| 57 | +const whenRunning = <T>(callback: Lifecycle<T>) => |
| 58 | + whenInLifecycles([LifecycleStage.LocalRun, LifecycleStage.Cloud], callback); |
| 59 | + |
| 60 | +const whenCollecting = <T>(callback: Lifecycle<T>) => |
| 61 | + whenInLifecycles([LifecycleStage.Build], callback); |
| 62 | + |
| 63 | +const isRunning = () => |
| 64 | + isInLifecycle([LifecycleStage.LocalRun, LifecycleStage.Cloud]); |
| 65 | + |
| 66 | +const isCollecting = () => isInLifecycle([LifecycleStage.Build]); |
| 67 | + |
| 68 | +type Lifecycle<T> = () => T; |
| 69 | + |
| 70 | +export const Lifecycle = { |
| 71 | + // Check if the current environment is one of the provided stages |
| 72 | + is: isInLifecycle, |
| 73 | + // Check if the current lifecycle is collecting application requirements |
| 74 | + isCollecting, |
| 75 | + // Check if the current lifecycle is running the app |
| 76 | + isRunning, |
| 77 | + // If the current environment is one of the provided stages, execute the provided callback |
| 78 | + when: whenInLifecycles, |
| 79 | + // If the current environment is collecting application requirements |
| 80 | + whenCollecting, |
| 81 | + // If the current environment is a cloud environment, execute the provided callback |
| 82 | + whenRunning, |
| 83 | +}; |
0 commit comments