Skip to content

Commit f9afa96

Browse files
authored
Merge pull request #199 from nullstack/multi-env
✨ add support for named environments
2 parents 2fdbc5b + 17ca95a commit f9afa96

File tree

9 files changed

+35
-8
lines changed

9 files changed

+35
-8
lines changed

scripts/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,26 @@ function logTrace(stats, showCompiling) {
5050
lastTrace = '';
5151
}
5252

53-
function start({ input, port }) {
53+
function start({ input, port, env }) {
5454
const environment = 'development';
5555
const compiler = getCompiler({ environment, input });
5656
if (port) {
5757
process.env['NULLSTACK_SERVER_PORT'] = port;
5858
}
59+
if (env) {
60+
process.env['NULLSTACK_ENVIRONMENT_NAME'] = env;
61+
}
5962
console.log(` 🚀️ Starting your application in ${environment} mode...`);
6063
console.log();
6164
compiler.watch({}, (error, stats) => logTrace(stats, true));
6265
}
6366

64-
function build({ input, output, cache, mode = 'ssr' }) {
67+
function build({ input, output, cache, env, mode = 'ssr' }) {
6568
const environment = 'production';
6669
const compiler = getCompiler({ environment, input, cache });
70+
if (env) {
71+
process.env['NULLSTACK_ENVIRONMENT_NAME'] = env;
72+
}
6773
console.log(` 🚀️ Building your application in ${mode} mode...`);
6874
compiler.run((error, stats) => {
6975
logTrace(stats, false);
@@ -78,6 +84,7 @@ program
7884
.description('Start application in development environment')
7985
.option('-p, --port <port>', 'Port number to run the server')
8086
.option('-i, --input <input>', 'Path to project that will be started')
87+
.option('-e, --env <name>', 'Name of the environment file that should be loaded')
8188
.helpOption('-h, --help', 'Learn more about this command')
8289
.action(start)
8390

@@ -89,6 +96,7 @@ program
8996
.option('-i, --input <input>', 'Path to project that will be built')
9097
.option('-o, --output <output>', 'Path to build output folder')
9198
.option('-c, --cache', 'Cache build results in .production folder')
99+
.option('-e, --env <name>', 'Name of the environment file that should be loaded')
92100
.helpOption('-h, --help', 'Learn more about this command')
93101
.action(build)
94102

server/dotenv.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import dotenv from 'dotenv'
2+
3+
let path = '.env'
4+
5+
if (process.env.NULLSTACK_ENVIRONMENT_NAME) {
6+
path += `.${process.env.NULLSTACK_ENVIRONMENT_NAME}`
7+
}
8+
9+
dotenv.config({ path })

server/environment.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ environment.mode = process.env.NULLSTACK_ENVIRONMENT_MODE || 'ssr';
99

1010
environment.key = "{{NULLSTACK_ENVIRONMENT_KEY}}"
1111

12+
environment.name = process.env.NULLSTACK_ENVIRONMENT_NAME || '';
13+
1214
Object.freeze(environment);
1315

1416
export default environment;

server/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import 'dotenv/config';
1+
import './dotenv';
22
import { normalize } from 'path';
33
import element from '../shared/element';
44
import fragment from '../shared/fragment';
File renamed without changes.

tests/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
"purgecss-webpack-plugin": "^4.1.3"
1515
},
1616
"scripts": {
17-
"start": "npx nullstack start --input=./tests --port=6969",
18-
"build": "npx nullstack build --input=./tests --mode=ssr",
17+
"start": "npx nullstack start --input=./tests --port=6969 --env=test",
18+
"build": "npx nullstack build --input=./tests --mode=ssr --env=test",
1919
"test": "npm run build && jest",
2020
"script": "node src/scripts/run.js"
2121
}
22-
}
22+
}

tests/src/ContextEnvironment.njs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import Nullstack from 'nullstack';
22

33
class ContextEnvironment extends Nullstack {
4-
5-
render({environment}) {
4+
5+
render({ environment }) {
66
return (
77
<div>
88
<div data-environment={!!environment} />
@@ -11,6 +11,7 @@ class ContextEnvironment extends Nullstack {
1111
<div data-development={environment.development.toString()} />
1212
<div data-production={environment.production.toString()} />
1313
<div data-static={(environment.mode === 'ssg').toString()} />
14+
<div data-name={environment.name} />
1415
<div data-key={environment.key} />
1516
</div>
1617
)

tests/src/ContextEnvironment.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,9 @@ describe('ContextEnvironment', () => {
4141
expect(element).toBeTruthy();
4242
});
4343

44+
test('has a key with the environment name', async () => {
45+
const element = await page.$('[data-name="test"]');
46+
expect(element).toBeTruthy();
47+
});
48+
4449
});

tests/src/scripts/run.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
process.env.NULLSTACK_ENVIRONMENT_NAME = 'test'
2+
13
const { default: application } = require('../../.development/server.js');
24

35
async function getProjectName() {

0 commit comments

Comments
 (0)