Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"@oclif/plugin-help": "^3.2.2",
"chalk": "^4.1.0",
"cosmiconfig": "^7.0.0",
"lodash.pick": "^4.4.0",
"staged-git-files": "^1.2.0"
},
"devDependencies": {
Expand Down
5 changes: 2 additions & 3 deletions src/commands/generate.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import fs from 'fs';
import path from 'path';
import pick from 'lodash.pick';
import chalk from 'chalk';

import { cosmiconfig } from 'cosmiconfig';
import { Command, flags as flagTypes } from '@oclif/command';

import { replaceIWA } from '../helpers';
import { replaceIWA, getOverridesFromProcessEnv } from '../helpers';

const explorer = cosmiconfig('iwa');

Expand Down Expand Up @@ -48,7 +47,7 @@ class GenerateCommand extends Command {
const cosmicDefaultData = cosmic.config.env.all || {};
const cosmicData = cosmic.config.env[env];

const processOverrideData = pick(process.env, Object.keys(cosmicData));
const processOverrideData = getOverridesFromProcessEnv(cosmicData);
const iwaConfig = {
...cosmicDefaultData,
...cosmicData,
Expand Down
7 changes: 7 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ export function hasIWAConfig(input: string) {

return match && match[0] !== '';
}

export function getOverridesFromProcessEnv(cosmicData: { [key: string]: string }) {
return Object.keys(cosmicData)
.filter((key: string) => process.env[key])
.map((key: string) => ({ [key]: process.env[key] }))
.reduce((prev, curr) => ({ ...prev, ...curr }), {});
}
25 changes: 25 additions & 0 deletions test/commands/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,29 @@ describe('iwa generate', () => {

expect(result2).toBe(expected2);
});

it('should overide iwarc value using process.env', async () => {
const overrideValue = 'OVERRIDE_VALUE';
process.env.KEY = overrideValue;
await GenerateCommand.run(['./test/index.html', '-c', iwarcFile, '-e', 'production']);

const result = fs.readFileSync(entryFilePath, {
encoding: 'utf-8',
});

const expected = html`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<script id="iwa">window.env = {"KEY":"${overrideValue}"}</script>
</body>
</html>
`;

expect(result).toBe(expected);
});
});
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3451,11 +3451,6 @@ lodash.merge@^4.6.2:
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==

lodash.pick@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3"
integrity sha1-UvBWEP/53tQiYRRB7R/BI6AwAbM=

lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
Expand Down