file-switch

1.0.7 • Public • Published

file-switch

Switches files based on a given key.

Build Status Coverage Status npm

Purpose

Suppose your final, bundled application uses variables in environment.ts, but those variables might change depending on whether the environment you're working on is development or production.

So you want to keep a few different files around (e.g., environment-dev.ts and environment-prod.ts) and swap them in and out depending on whether you're running in dev or prod.

You might have a folder structure like this:

project
│   package.json
│   ...  
│
├───src
│   │
│   ├───app
│   │   │
│   │   └───configuration
│   │          environment.ts
│   │
│   └───environments
│           environment-dev.ts
│           environment-prod.ts

In this case, you want to copy the appropriate environment-***.ts file into the src/app/config folder, and name it environment.ts.

Install

npm install file-switch --save-dev

Usage

In your build script (or webpack.config.js), you might use the following:

const FileSwitch = require('file-switch');

const isProd = (process.env.NODE_ENV === 'production');

const options = {
    sources: [
        { key: 'dev',  path: 'src/environments/environment-dev.ts'  },
        { key: 'prod', path: 'src/environments/environment-prod.ts' }
    ],
    destination: {
		path: 'src/app/configuration/environment.ts'
    }
};

new FileSwitch(options).useFile(isProd ? 'prod' : 'dev')

API

useFile(key)path

Copies the source file with the given key to the destination path specified in the options, and returns the absolute path of the source file.

Configuration Options

const options = {
	// (optional) - the absolute or relative location of the root folder of the project
	root: '../../..',
	
	// (optional) - determines whether to show additional console logging statements about source, destination, and result.
	debug: false,

	// options related to the source files
	sources: [
	    { 
			// the key to use for this file
			key: 'dev',  
			
			// (optional) - the display name to use in the console output 
			displayName: 'DEVELOPMENT', 

			// the path where this source file resides
			path: 'src/environments/environment-dev.ts'  
		},
	    { key: 'prod', displayName: 'PRODUCTION',  path: 'src/environments/environment-prod.ts' }
	],
	
	// options related to the destination file
	destination: {

		// the full path where you want the source file copied, including the destination filename.
		path: 'src/app/configuration/environment.ts'
    }
}

License

MIT © Curtis Kaler

Package Sidebar

Install

npm i file-switch

Weekly Downloads

2

Version

1.0.7

License

MIT

Unpacked Size

11.8 kB

Total Files

12

Last publish

Collaborators

  • curtis.kaler