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

vendure@cli unaware of environment #2802

Closed
Draykee opened this issue Apr 19, 2024 · 0 comments
Closed

vendure@cli unaware of environment #2802

Draykee opened this issue Apr 19, 2024 · 0 comments

Comments

@Draykee
Copy link
Contributor

Draykee commented Apr 19, 2024

Is your feature request related to a problem? Please describe.
Using the npx vendure migrate command ignores configuration for the database. For simple projects this might not be a problem, but for production ready code that is usually configured by environment variables this becomes a problem.

Describe the solution you'd like
A env file parameter. Even better might be an option to configure the CLI once like: vendure env .env.
I image if the vendure cli might support triggering e2e test at one point it might be cool to even configure a test environment like this as well.

Describe alternatives you've considered
I added a JS script to handle that:

const fs = require('fs');
const { spawn } = require('child_process');
const dotenv = require('dotenv');

let args = process.argv.slice(2);

let envFile;
// Add default environment file
if (args.length >= 1) {
    if (fs.existsSync(args[0])) {
        envFile = args[0];
    } else if (fs.existsSync(`.env.${args[0]}`)) {
        envFile = `.env.${args[0]}`;
    }
}

if (!envFile) {
    envFile = '.env';
    args = [envFile, ...args];
}

if (fs.existsSync(envFile)) {
    console.log(`[withEnv]`, `Loading environment from: ${envFile}`);

    dotenv.config({
        path: envFile,
    });
    if (args.length >= 2) {
        const command = args[1];
        const commandArgs = args.slice(2);
        const options = { stdio: 'inherit', shell: true }; // This will allow the child process to use the parent's stdin, stdout, and stderr.

        console.log(`[withEnv]`, `Execute shell command: ${command} ${commandArgs.join(' ')}`);
        const child = spawn(command, commandArgs, options);
        child.on('error', error => {
            console.error(`[withEnv]`, `Failed to start subprocess.`, error);
        });
        child.on('exit', function (code, signal) {
            console.log(`child process exited with code ${code} and signal ${signal}`);
        });
    }
} else {
    console.error(`[withEnv]`, `Environment file: ${envFile} not found!`);
    process.exit(1);
}

Using it like: node dev/spawnWithEnv.js npx vendure migrate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant