Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

grunt-envpreprocess

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

grunt-envpreprocess

A task for replacing environment-specific variables in a specified file or directory. Environment variables are specified in an external .json file.

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
26
decreased by-68.29%
Maintainers
1
Weekly downloads
 
Created
Source

envpreprocess

preprocess environment variables

Getting Started

This plugin requires Grunt ~0.4.5

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install envpreprocess --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('envpreprocess');

The "envpreprocess" task

Text search and replacement in files is powered by the core of grunt-text-replace.

Overview

In your project's Gruntfile, add a section named envpreprocess to the data object passed into grunt.initConfig().

grunt.initConfig({
   envpreprocess:{
        dev:{
            files:{
                src:  'config/env.json'
            },
            options:{
                replacePath: ['dev_build/**/*.*'],
                environment: 'dev'
            }
        },
        prod:{
            files:{
                src:  'config/env.json'
            },
            options:{
                replacePath: ['dist_build/**/*.*'],
                environment: 'prod'
            }
        }
    }
});

When the task is run, you'll see a log output like this

Running "envpreprocess:prod" (envpreprocess) task
Reading ENV variables from config/env.json
Replaced all ENV variables in dev_build/**/*.*

Options

options.replacePath

Type: String

The path of files to have ENV variables replaced with the preprocessor.

options.environment

Type: String Default value: dev'

Which environment to use when retrieving ENV variables from the .json file. If given "*", any environment that isn't specified will use that value.

Example Usage

Environment variables are defined in the config file located at the specified 'files.src' file. For each variable you must define a value for each environment, or use "*" to apply it to any environments that don't have a value specified. This is an example of such file

{
	"APP_NAME": {
		"dev": "AppDev",
		"prod": "AppProd"
	},
	"APP_VERSION": {
        "*"
	},
    "API_BASE": {
        "dev": "http://localhost:8000",
        "prod": "https://www.mysite.com/api"
    }
}

Then in a file you want an ENV variable to be replaced, use something like this

    <head>
        <title>ENV.APP_NAME</title>
    </head>
    <script src="ENV.API_BASE/user/create">
    var version = "ENV.APP_VERSION";
    alert(version);

Which will produce

    <head>
        <title>AppDev</title>
    </head>
    <script src="http://localhost:8000/user/create">
    var version = "0.1.0";
    alert(version);

Note that this is going to modify the files that you point it to. This means that you shouldn't point this task at your actual source files, but at a separate build folder containing a copy of the source.

Release History

0.1.0 - Initial release. Functionality is working. Not tested.

Keywords

FAQs

Package last updated on 09 Feb 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc