0.0.2 • Published 9 months ago

env-git v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

env-git

Description

The project is a library designed to facilitate the retrieval and parsing of environment variables from various hosting platforms. It provides functions to extract environment variables from environment files stored on GitLab, GitHub, and Bitbucket.

Overall, this library simplifies the process of retrieving and parsing environment variables from environment files stored on different hosting platforms, making it easier to manage and utilize those variables within an application.

Installation

Either through cloning with git or by using npm (the recommended way):

npm install env-git # or yarn install env-git

Parameters

  • hostingPlatform (string): The hosting platform where the repository is hosted. It accepts three possible values: "gitlab", "github", or "bitbucket". This parameter determines the platform-specific logic for retrieving the environment file.

  • config (object): Additional configuration options for the hosting platform. This parameter is optional and can be used to provide specific configuration settings based on the hosting platform.

  • If the hostingPlatform is "bitbucket", the config object can include properties like workspace (string) to specify the GitLab workspace or organization associated with the repository.

  • If the hostingPlatform is "github", the config object can include properties like owner (string) to specify the GitHub owner or organization associated with the repository.

  • If the hostingPlatform is "gitlab", the config object can include properties like host_url (string) to specify the GitHub owner or organization associated with the repository. -->

Usage

To represent both ways of providing configuration in the usage section, you can update the code examples as follows:

1. Using a Environment Variable

The library supports configuration using various file formats such as .env, .json, or .yaml. By providing a configuration file in one of these formats.

To utilize the env-git library, you can create a .env file in your repository. The .env file should contain the environment variables you want to retrieve and use in your application.

Here's an example of a .env file:

GIT_ENV_PLATFORM=gitlab
GIT_ENV_ACCESS_TOKEN=your-access-token
GIT_ENV_REPO=your-repository /* project-id for gitlab, repo-slug for bitbucket */ 
GIT_ENV_BRANCH=your-repo-branch
GIT_ENV_FILE_PATH=.env

GIT_ENV_OWNER=your-github-username  # Only for GitHub
GIT_ENV_HOST_URL=your-gitlab-host-url  # Only for GitLab
GIT_ENV_WORKSPACE=your-bitbucket-workspace  # Only for Bitbucket

Note: Assign the appropriate values to each environment variable without changing the key names. This configuration enables the library to fetch and load the environment variables from the corresponding hosting platform based on the provided values.

Once you have the .env file in your repository, you can use the loadEnvFromFile function to load the environment variables directly from the file:

/* Import the required function from the library */
import GitENV, { PLATFORM } from "env-git";

async function main() {
    const readENV = new GitENV();
    /* This function is used to fetch environment variables from a repository 
    hosted on different platforms and load them into the process.env. */
    await readENV.loadEnv();

   /* This function is used to fetch environment variables from a repository hosted 
   on different platforms and returns them as an object but without directly 
   modifying the process.env. */
  // await readENV.fetchEnv();
}

main();

Users can choose the approach that suits their needs and provide the necessary configuration accordingly.

2. Using Function Arguments

The library provides two functions, loadEnv and fetchEnv, that allow you to retrieve and load environment variables using function arguments.

Here an example demonstrates how to directly specify the configuration using function arguments, allowing users to provide the necessary details.

/* Import the required functions and constants from the library */
import GitENV, { PLATFORM } from "env-git";


const platform = PLATFORM.GITHUB;
const config = { 
    access_token: "your-access-token",
    repository: "your-repository", /* project-id for gitlab, repo-slug for bitbucket */ 
    repo_branch: "your-repo-branch",
    file_path: ".env",
    /* Additional platform-specific configuration options */
    host_url: "your-gitlab-host-url", # Only for GitHub
    owner: "your-github-username", # Only for GitLab
    workspace: "your-bitbucket-workspace", # Only for Bitbucket
}


async function main() {
    const readENV = new GitENV({platform, config});
    /* This function is used to fetch environment variables from a repository 
    hosted on different platforms and load them into the process.env. */
    await readENV.loadEnv();

  /* This function is used to fetch environment variables from a repository hosted 
  on different platforms and returns them as an object but without directly 
  modifying the process.env. */
  // await readENV.fetchEnv();
}

main();

Obtaining Repository Access Token Platform Wise

GitLab

To obtain the GitLab repository access token and information, follow these steps:

GitHub

To obtain the GitHub repository access token and information, follow these steps:

Bitbucket

To obtain the Bitbucket repository access token and information, follow these steps:

0.0.1

9 months ago

0.0.2

9 months ago

1.2.0

3 years ago

1.1.0

3 years ago

1.9.0

3 years ago

1.8.1

3 years ago

1.8.0

3 years ago

1.7.0

3 years ago

1.6.0

3 years ago

1.4.2

3 years ago

1.5.0

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.0.0

3 years ago