diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index 0ac36a9..f622004 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -15,6 +15,7 @@ * [Deploy destinations](./in-depth/deploy-destinations.md) * [TypeScript](./in-depth/typescript.md) * [Cookbook](./in-depth/cookbook.md) + * [Environment variables](./in-depth/cookbook/environment-variables.md) --- diff --git a/docs/in-depth/cookbook/environment-variables.md b/docs/in-depth/cookbook/environment-variables.md new file mode 100644 index 0000000..07a5b3e --- /dev/null +++ b/docs/in-depth/cookbook/environment-variables.md @@ -0,0 +1,61 @@ +# Managing deploys with environment variables + +Environment variables provide a more streamlined way to manage your build process. We can also use it to define "build toggles", or environment-based variables that will be injected into your scripts to be used during runtime. + +## Setting it up + +Let's say that we want to set `NODE_ENV` to `production` for uploading to our main branch, and `development` for uploading to our Simulation branch. First we'll catch the environment variable and assign the compile target. + +```js +// rollup.config.js + +const isProduction = process.env.NODE_ENV === 'production' + +// use the `main` target from `screeps.json` in production mode +const cfg = isProduction ? 'main' : 'sim'; + +export default { + // ... + plugins: [ + // ... + screeps({ + config: require("./screeps")[cfg], + // if `NODE_ENV` is local, perform a dry run + dryRun: process.env.NODE_ENV === 'local' + }) + ] +} +``` + +## Running a deploy + +Then we'll change the build tasks on `package.json` to pass the environment variable before running the rollup command. + +```json +{ + "tasks": { + "deploy-prod": "NODE_ENV=production rollup -c", + "deploy-dev": "NODE_ENV=development rollup -c", + } +} +``` + +**Note:** On Windows, setting the environment variables as defined above will not work. For a cross-platform solution to define environment variables, use `cross-env`. + +```bash +npm install --save-dev cross-env +``` +```json +{ + "tasks": { + "deploy-prod": "cross-env NODE_ENV=production rollup -c", + "deploy-dev": "cross-env NODE_ENV=development rollup -c", + } +} +``` + +Now let's give it a try! Run `npm run deploy-dev` or `npm run deploy-prod` and see if your code is uploaded properly. + +## Setting up build toggles + +[TODO]