I play around with dotenv cross-var in npm scripts. .env contains REPO_PULL_URL and REPO_PUSH_AUTHOR values.
{
"scripts": {
"testScript3": "shx rm -rf ./temp && dotenv cross-var git clone %REPO_PULL_URL% ./temp",
"testScript5": "cd ./temp && dotenv cross-var git config user.email %REPO_PUSH_AUTHOR%",
},
"devDependencies": {
"cross-env": "^7.0.3",
"cross-var": "^1.1.0",
"dotenv-cli": "^6.0.0",
"shx": "^0.3.4"
}
}
testScript3 - works fine: it creates temp folder and pulls the repo from the valid url, that was taken from .env.
testScript5 - writes email = %REPO_PUSH_AUTHOR%
to the config, which means the value was not extracted properly.
My latest finding is that .env should be in the root, which was probably broken by the cd command. It seems I have to use config for that ( e.g. dotenv is not loading properly ) but I don't really want to move the script to *.js (not yet, at least).
So, the question is: is there a way to pass the config, or should I consider extracting to js? Or any other, better options?
CodePudding user response:
So, I ended up with switching to *.js. I've realized that the case is a bit too specific, the existing libs are not quite fit.
Used shelljs and dotenv libs to get env vars and manage FS. Works perfectly on both Mac and Windows.
var shell = require('shelljs');
require('dotenv').config();
const REPO_PUSH_URL = process.env.REPO_PUSH_URL;
const REPO_PUSH_AUTHOR = process.env.REPO_PUSH_AUTHOR;
shell.cd('./project');
if (shell.exec('git init').code !== 0) {
shell.echo('Error: Git init failed');
shell.exit(1);
}