Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@packt/serverless-env-generator

Package Overview
Dependencies
Maintainers
6
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@packt/serverless-env-generator - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

.idea/jsLibraryMappings.xml

223

index.js

@@ -16,83 +16,178 @@ #!/usr/bin/env node

const log = require('debug')('serverless-env-generator');
const commander = require('commander');
const Fs = require('fs');
const fse = require('fs-extra');
const YAML = require('yamljs');
const path = require('path');
// Commander config
commander
.version(require('./package.json').version)
.option('--stage [type]', 'Add deployment stage [stage]', 'dev')
.option(
'--serverless-env-file [filename]',
'Set the serverless env file',
'serverless.env.yml'
)
.option('--env-variables [filename|string]', 'Set the serverless env file')
.option('--local, -l', 'Use .env file')
.parse(process.argv);
// Use local ENV
if (commander.local) {
require('dotenv').load();
}
const loadDotEnv = async (filename) => {
const buffer = await fse.readFile(filename);
const asString = buffer.toString();
log('current: %S', asString);
// Name of the ENV storage file
const serverlessEnvFile = commander.serverlessEnvFile;
const result = {};
asString
.split('\n')
.forEach(record => {
const split = record.split('=');
if (split && split[0] && split[1]) result[split[0]] = split[1];
});
// Hard code the STAGE to dev for initial run through or if not set
const STAGE = commander.stage;
return result;
};
// The ENV variables
if (!commander.envVariables) {
console.log(
'Please provide --env-variables either file containing an array of ENV variable names or a comma (,) separated list of ENV variable names'
);
process.exit(1);
}
const availableEnvs = Fs.existsSync(commander.envVariables)
? require(commander.envVariables)
: commander.envVariables.split(',');
const buildEnvFile = (envVariables) => {
let file = '';
const result = Object.keys(envVariables)
.forEach(key => {
const value = envVariables[key];
// Prepopulate the ENV if a serverless.env.yml exists
const envVariables = Fs.existsSync(serverlessEnvFile)
? YAML.load(serverlessEnvFile)
: {};
file += `${key}=${value}\n`;
});
// If the STAGE doesn't exist, create it
if (!envVariables[STAGE]) {
envVariables[STAGE] = {};
}
return file;
};
// Populate with blank variables for anything not already defined
for (let i = 0; i < availableEnvs.length; i += 1) {
if (!envVariables[STAGE][availableEnvs[i]]) {
envVariables[STAGE][availableEnvs[i]] = '';
const loadFile = (filename) => {
if (!commander.D) return YAML.load(filename);
return loadDotEnv(filename);
};
(async () => {
// Commander config
commander
.version(require('./package.json').version)
.option('--stage [type]', 'Add deployment stage [stage]', 'dev')
.option(
'--serverless-env-file [filename]',
'Set the serverless env file',
'serverless.env.yml'
)
.option('--env-variables [filename|string]', 'Set the serverless env file')
.option('--local, -l', 'Use .env file')
.option('--dotenv, -d', 'Write .env file rather than yaml')
.parse(process.argv);
// Use local ENV
if (commander.local) {
require('dotenv').load();
}
}
// Populate from ENV variables
Object.keys(envVariables[STAGE]).map(key => {
const stageKey = `${STAGE}_${key}`.toUpperCase();
if (process.env[key] || process.env[stageKey]) {
envVariables[STAGE][key] = process.env[stageKey]
? process.env[stageKey]
: process.env[key];
// log('commander: %O', commander);
// Name of the ENV storage file
const serverlessEnvFile = commander.D ? '.env' : commander.serverlessEnvFile;
const CWD = process.cwd();
log('PWD: %O', CWD);
log('Env File: %O', serverlessEnvFile);
// Hard code the STAGE to dev for initial run through or if not set
const STAGE = commander.stage;
log('Stage: %O', STAGE);
// The ENV variables
if (!commander.envVariables) {
console.log(
'Please provide --env-variables either file containing an array of ENV variable names or a comma (,) separated list of ENV variable names'
);
process.exit(1);
}
return true;
});
// Convert the object into YAML
const ymlEnv = YAML.stringify(envVariables);
const envVariablesPath = path.join(CWD, commander.envVariables);
// Write the YAML to disk so it can be read by sls deploy --stage <STAGE>
Fs.writeFile(serverlessEnvFile, ymlEnv, err => {
if (err) {
// eslint-disable-next-line no-console
console.error(err);
process.exit(1);
log('Env Variables: %O', envVariablesPath);
const availableEnvs = Fs.existsSync(envVariablesPath)
? require(envVariablesPath)
: commander.envVariables.split(',');
log('available envs: %O', availableEnvs);
// Prepopulate the ENV if a serverless.env.yml exists
const envVariables = Fs.existsSync(serverlessEnvFile)
? await loadFile(serverlessEnvFile)
: {};
log('current env: %O', envVariables);
// If the STAGE doesn't exist, create it
if (!commander.D && !envVariables[STAGE]) {
envVariables[STAGE] = {};
}
// Populate with blank variables for anything not already defined
if(!commander.D) {
for (let i = 0; i < availableEnvs.length; i += 1) {
if (!envVariables[STAGE][availableEnvs[i]]) {
envVariables[STAGE][availableEnvs[i]] = '';
}
}
} else {
// eslint-disable-next-line no-console
console.log(`${serverlessEnvFile} has been created`);
process.exit(0);
for (let i = 0; i < availableEnvs.length; i += 1) {
if (!envVariables[availableEnvs[i]]) {
envVariables[availableEnvs[i]] = '';
}
}
}
});
// Populate from ENV variables
if(!commander.D) {
Object.keys(envVariables[STAGE]).map(key => {
const stageKey = `${STAGE}_${key}`.toUpperCase();
if (process.env[key] || process.env[stageKey]) {
envVariables[STAGE][key] = process.env[stageKey]
? process.env[stageKey]
: process.env[key];
}
return true;
});
} else {
Object.keys(envVariables).map(key => {
const stageKey = `${STAGE}_${key}`.toUpperCase();
if (process.env[key] || process.env[stageKey]) {
envVariables[key] = process.env[stageKey]
? process.env[stageKey]
: process.env[key];
}
return true;
});
}
// Convert the object into YAML
if (!commander.D) {
const ymlEnv = YAML.stringify(envVariables);
// Write the YAML to disk so it can be read by sls deploy --stage <STAGE>
Fs.writeFile(serverlessEnvFile, ymlEnv, err => {
if (err) {
// eslint-disable-next-line no-console
console.error(err);
process.exit(1);
} else {
// eslint-disable-next-line no-console
console.log(`${serverlessEnvFile} has been created`);
process.exit(0);
}
});
} else {
const envFile = buildEnvFile(envVariables);
fse.writeFile(serverlessEnvFile, envFile)
.then(() => {
console.log(`${serverlessEnvFile} has been created`);
process.exit(0);
})
.catch((error) => {
log('error: %O', error);
console.error(error);
process.exit(1);
})
}
})();
{
"name": "@packt/serverless-env-generator",
"version": "0.0.2",
"description":
"Generate serverless.env.yml for dynamic ENV assignments within serverless.yml",
"version": "0.0.3",
"description": "Generate serverless.env.yml for dynamic ENV assignments within serverless.yml",
"main": "index.js",

@@ -12,4 +11,3 @@ "scripts": {

"type": "git",
"url":
"git+ssh://git@bitbucket.org/packt-internal/serverless-env-generator.git"
"url": "git+ssh://git@bitbucket.org/packt-internal/serverless-env-generator.git"
},

@@ -27,4 +25,3 @@ "author": {

"license": "Apache-2.0",
"homepage":
"https://bitbucket.org/packt-internal/serverless-env-generator#readme",
"homepage": "https://bitbucket.org/packt-internal/serverless-env-generator#readme",
"dependencies": {

@@ -37,3 +34,7 @@ "commander": "^2.15.1",

"serverless-env-generator": "index.js"
},
"devDependencies": {
"debug": "4.1.1",
"fs-extra": "8.1.0"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc