Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support GASKET_ENV with process exposure #387

Merged
merged 5 commits into from
Jun 30, 2022
Merged
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
3 changes: 3 additions & 0 deletions packages/gasket-cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# `@gasket/cli`

- Support for `GASKET_ENV` with fallback to `NODE_ENV`
- expose Gasket settings on process.env

### 6.24.3

- Support for --require flag to load modules before Gasket initializes ([#370])
Expand Down
6 changes: 6 additions & 0 deletions packages/gasket-cli/src/config/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ function getEnvironment(flags, commandId, warn) {
return 'local';
}

const { NODE_ENV } = process.env;
if (NODE_ENV) {
warn(`No env specified, falling back to NODE_ENV: "${ NODE_ENV }".`);
return NODE_ENV;
}

warn('No env specified, falling back to "development".');
return 'development';
}
Expand Down
6 changes: 6 additions & 0 deletions packages/gasket-cli/src/hooks/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ async function initHook({ id, config: oclifConfig, argv }) {

try {
const env = getEnvironment(flags, id, warn);

// expose Gasket settings on process
process.env.GASKET_ENV = env;
process.env.GASKET_CONFIG = flags.config;
process.env.GASKET_ROOT = flags.root;

const gasketConfig = await getGasketConfig(flags, env, id);

if (gasketConfig) {
Expand Down
19 changes: 16 additions & 3 deletions packages/gasket-cli/test/unit/config/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ describe('config utils', () => {
});
});

after(function () {
afterEach(function () {
sinon.restore();
delete process.env.NODE_ENV;
});

describe('getGasketConfig', () => {
Expand Down Expand Up @@ -110,12 +111,24 @@ describe('config utils', () => {
assume(results).equals('local');
});

it('returns development when no flag or local command', function () {
it('returns NODE_ENV when no flag or local command', function () {
process.env.NODE_ENV = 'fake';
const results = utils.getEnvironment(flags, commandId, warnStub);
assume(results).equals('fake');
});

it('warns for NODE_ENV when no flag or local command', function () {
process.env.NODE_ENV = 'fake';
utils.getEnvironment(flags, commandId, warnStub);
assume(warnStub).calledWith('No env specified, falling back to NODE_ENV: "fake".');
});

it('returns development when no flag, NODE_ENV, or local command', function () {
const results = utils.getEnvironment(flags, commandId, warnStub);
assume(results).equals('development');
});

it('warns when no flag or local command', function () {
it('warns when no flag, NODE_ENV, or local command', function () {
utils.getEnvironment(flags, commandId, warnStub);
assume(warnStub).calledWith('No env specified, falling back to "development".');
});
Expand Down
15 changes: 15 additions & 0 deletions packages/gasket-cli/test/unit/hooks/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ describe('init hook', () => {

afterEach(function () {
sinon.restore();
delete process.env.GASKET_ENV;
delete process.env.GASKET_CONFIG;
delete process.env.GASKET_ROOT;
});

it('ends early for create command', async () => {
Expand All @@ -54,6 +57,18 @@ describe('init hook', () => {
assume(parseStub).called();
});

it('set env vars from flags', async () => {
assume(process.env).not.property('GASKET_ENV');
assume(process.env).not.property('GASKET_ROOT');
assume(process.env).not.property('GASKET_CONFIG');

await initHook({ id: 'build', argv: [] });

assume(process.env.GASKET_ENV).equals('development');
assume(process.env.GASKET_ROOT).equals('/path/to/app');
assume(process.env.GASKET_CONFIG).equals('gasket.config');
});

it('gets the gasket.config', async () => {
await initHook({ id: 'build', argv: [] });
assume(getGasketConfigStub).called();
Expand Down
2 changes: 2 additions & 0 deletions packages/gasket-plugin-command/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# `@gasket/plugin-command`

- Support for `GASKET_ENV` with fallback to `NODE_ENV`

### 6.24.3

- Support for --require flag to load modules before Gasket initializes ([#370])
Expand Down
4 changes: 2 additions & 2 deletions packages/gasket-plugin-command/lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class GasketCommand extends Command {
* @type {object} flags
* @property {string} config - Fully qualified gasket config to load (default: `'gasket.config'`)
* @property {string} root - Top-level app directory (default: `process.cwd()`)
* @property {string} env - Target runtime environment (default: `NODE_ENV` or `'development'`)
* @property {string} env - Target runtime environment (default: `GASKET_ENV` or `'development'`)
*/
GasketCommand.flags = {
config: flags.string({
Expand All @@ -111,7 +111,7 @@ GasketCommand.flags = {
description: 'Top-level app directory'
}),
env: flags.string({
env: 'NODE_ENV',
env: 'GASKET_ENV',
description: 'Target runtime environment'
}),
require: flags.string({
Expand Down
2 changes: 2 additions & 0 deletions packages/gasket-plugin-start/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# `@gasket/plugin-start`

- Support for `GASKET_ENV` with fallback to `NODE_ENV`

### 6.13.0

- Add `--exit` flag to build command ([#325])
Expand Down
2 changes: 1 addition & 1 deletion packages/gasket-plugin-start/lib/get-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = function getCommands(gasket, { GasketCommand, flags }) {
LocalCommand.description = 'Build then start your app in local environment';
LocalCommand.flags = {
env: flags.string({
env: 'NODE_ENV',
env: 'GASKET_ENV',
description: 'Target runtime environment',
default: 'local'
})
Expand Down