0

Is there any way to create React App build from a command like? So far I have in the shell script:

npm install;
npm run-script build:test;

where build:test is

bash -ac '. .env.test; react-scripts build'

but that will refer to build:test script from package.json but when I want to use specific .env file in the shell script (without editing or adding anything to package.json) as follow

npm run .env.my_box react-scripts build;

that will not execute build for .env.my_box

What I have set incorrectly?

JackTheKnife
  • 3,795
  • 8
  • 57
  • 117
  • I'm curious why you would store the various env configs along side each other vs having your .env file created in the stage you're at. IE: in circle-ci the automated builder I use, I inject the env variables into the container at each stage vs checking something like that in. Would that work for you? – Petrogad Jun 18 '20 at 22:31
  • @Petrogad we are using Ansible for CD – JackTheKnife Jun 19 '20 at 13:25

2 Answers2

0

you should try this library https://github.com/toddbluhm/env-cmd

Update your package.json

   "scripts": {
   "start": "react-scripts start",
   "build": "react-scripts build",
   "test": "react-scripts test",
   "eject": "react-scripts eject",
   "build:stage": "env-cmd -f ./.env.stage npm run-script build" 
}

then npm run build:stage

Yoandry Collazo
  • 1,122
  • 9
  • 16
  • But this is the same I have above. Question is if I can use `env-cmd -f ./.env.stage npm run-script build` from the command like or shell script – JackTheKnife Jun 18 '20 at 21:20
  • Apparently `npm run env-cmd -f ./.env.stage npm run-script build` from a command line is no working – JackTheKnife Jun 18 '20 at 21:30
0

You could do something like this:

on package.json

"scripts": {
    "server": "cd server && npm start",
    "client": "cd client && npm start",
    "server-dep": "cd server && npm install",
    "client-dep": "cd client && npm install",
    "install-dep": "npm install && concurrently \"npm run server-dep\" \"npm run client-dep\"",
    "dev": "concurrently \"npm run server\" \"npm run client\"",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

then npm run start for example, this is monorepo so we run server and frontend with one command

  • I'm trying to automate build. That solution is not going to work from a command line when need to pass `.env` file to the automation script – JackTheKnife Jun 18 '20 at 21:31