Skip to content

Commit

Permalink
Add .env auto-import (with user confirmation)
Browse files Browse the repository at this point in the history
  • Loading branch information
davlgd committed Jan 5, 2024
1 parent 1e8e23c commit 38d036f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/commands/create.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
'use strict';

const fs = require('fs');
const path = require('path');

const Application = require('../models/application.js');
const AppConfig = require('../models/app_configuration.js');
const Interact = require('../models/interact.js');
const Logger = require('../logger.js');
const Env = require('../commands/env.js');

const { toNameEqualsValueString } = require('@clevercloud/client/cjs/utils/env-vars.js');

async function create (params) {
Expand All @@ -25,6 +31,17 @@ async function create (params) {
default:
Logger.println('Your application has been successfully created!');
}

if(fs.existsSync(path.join(process.cwd(), '.env'))) {
const envFileContent = fs.readFileSync(path.join(process.cwd(), '.env'), 'utf8');
await Interact.confirm(
'Theres is a .env file in this directory, do you want to import its content ? [y/n]: ',
'The content of .env file has not been imported',
['yes', 'y', 'YES', 'Y'],
);

await Env.importEnvFromFile(alias || app.name, envFileContent);
}
};

function getGithubDetails (githubOwnerRepo) {
Expand Down
11 changes: 10 additions & 1 deletion src/commands/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ async function importEnv (params) {
Logger.println('Environment variables have been set');
};

async function importEnvFromFile(alias, fileData) {
const { ownerId, appId } = await AppConfig.getAppDetails({ alias });

const envVars = await variables.readVariablesFromFile(fileData);
await application.updateAllEnvVars({ id: ownerId, appId }, envVars).then(sendToApi);

Logger.println('Environment variables have been set from .env file');
};

async function importVarsFromLocalEnv (params) {
const [envNames] = params.args;
const { alias } = params.options;
Expand All @@ -89,4 +98,4 @@ async function importVarsFromLocalEnv (params) {
Logger.println('Your environment variables have been successfully saved');
};

module.exports = { list, set, rm, importEnv, importVarsFromLocalEnv };
module.exports = { list, set, rm, importEnv, importEnvFromFile, importVarsFromLocalEnv };
9 changes: 7 additions & 2 deletions src/models/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function readStdin () {
output: process.stdout,
terminal: false,
});

const lines = [];
rl.on('line', (line) => {
lines.push(line);
Expand Down Expand Up @@ -79,7 +79,7 @@ function parseFromJson (rawStdin) {

function parseFromNameEqualsValue (rawStdin) {
const { variables, errors } = parseRaw(rawStdin);

if (errors.length !== 0) {

const formattedErrors = errors
Expand Down Expand Up @@ -119,6 +119,11 @@ async function readVariablesFromStdin (format) {
}
}

async function readVariablesFromFile (data) {
return parseFromNameEqualsValue(data);
}

module.exports = {
readVariablesFromStdin,
readVariablesFromFile
};

0 comments on commit 38d036f

Please sign in to comment.