Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from kunalpanchal/refactor/standard-fix
Fixes based on https://standardjs.com/
- Loading branch information
Showing
8 changed files
with
461 additions
and
435 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,3 +56,6 @@ typings/ | |
|
||
# dotenv environment variables file | ||
.env | ||
|
||
# Build directories | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
#! /usr/bin/env node | ||
|
||
/* Arguments that can be passed are | ||
/* Arguments that can be passed are | ||
* --secret <secretKey> | -s <secretKey> | ||
* --out <file-path> | -o <file-path> | ||
* --algo <algoName> | -a <algoName> | ||
* --algo <algoName> | -a <algoName> | ||
*/ | ||
|
||
const argv = require('minimist')(process.argv.slice(2)); | ||
const outputFile = argv.outputFile || argv.o; | ||
const inputFile = argv._[0]; | ||
const secret = argv.secret || argv.s; | ||
const encryptionAlgo = argv.algo || argv.a; | ||
const argv = require('minimist')(process.argv.slice(2)) | ||
const outputFile = argv.outputFile || argv.o | ||
const inputFile = argv._[0] | ||
const secret = argv.secret || argv.s | ||
const encryptionAlgo = argv.algo || argv.a | ||
|
||
const cryptography = require('./cryptography'); | ||
const cryptography = require('./cryptography') | ||
|
||
cryptography.encrypt({ secret, inputFile, outputFile, encryptionAlgo }); | ||
cryptography.encrypt({ secret, inputFile, outputFile, encryptionAlgo }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,52 @@ | ||
"use strict" | ||
'use strict' | ||
|
||
const crypto = require('crypto'); | ||
const fs = require('fs'); | ||
const log = require('./utils/log'); | ||
const crypto = require('crypto') | ||
const fs = require('fs') | ||
const log = require('./utils/log') | ||
|
||
/* Arguments that can be passed are | ||
/* Arguments that can be passed are | ||
* --secret <secretKey> | -s <secretKey> | ||
* --out <file-path> | -o <file-path> | ||
* --algo <algoName> | -a <algoName> | ||
* --algo <algoName> | -a <algoName> | ||
*/ | ||
|
||
module.exports.decrypt = (options) => { | ||
try { | ||
const secret = options.secret || 'mySecret'; | ||
const inputFile = options.file || '.env.enc'; | ||
const decryptionAlgo = options.decryptionAlgo || `aes192`; | ||
|
||
if (!fs.existsSync(inputFile)) throw `${inputFile} does not exist.` | ||
if (!secret || typeof (secret) !== 'string') throw 'No SecretKey provided.'; | ||
|
||
const decipher = crypto.createDecipher(decryptionAlgo, secret); | ||
let decrypted = decipher.update(fs.readFileSync(inputFile), 'hex', 'utf8'); | ||
decrypted += decipher.final('utf8'); | ||
return decrypted; | ||
} catch (e) { | ||
log(e, 'error'); | ||
} | ||
}; | ||
try { | ||
const secret = options.secret || 'mySecret' | ||
const inputFile = options.file || '.env.enc' | ||
const decryptionAlgo = options.decryptionAlgo || 'aes192' | ||
|
||
if (!fs.existsSync(inputFile)) throw `${inputFile} does not exist.` | ||
if (!secret || typeof (secret) !== 'string') throw 'No SecretKey provided.' | ||
|
||
const decipher = crypto.createDecipher(decryptionAlgo, secret) | ||
let decrypted = decipher.update(fs.readFileSync(inputFile), 'hex', 'utf8') | ||
decrypted += decipher.final('utf8') | ||
return decrypted | ||
} catch (e) { | ||
log(e, 'error') | ||
} | ||
} | ||
|
||
module.exports.encrypt = (options) => { | ||
try { | ||
const secret = options.secret || 'mySecret'; | ||
const inputFile = options.inputFile || '.env'; | ||
const outputFilePath = options.outputFile || `${inputFile}.enc`; | ||
const encryptionAlgo = options.encryptionAlgo || `aes192`; | ||
|
||
if (!fs.existsSync(inputFile)) throw `Error: ${inputFile} does not exist.` | ||
if (!secret || typeof (secret) !== 'string') throw 'No SecretKey provided.Use -s option to specify secret'; | ||
|
||
const cipher = crypto.createCipher(encryptionAlgo, secret); | ||
const output = fs.createWriteStream(outputFilePath); | ||
fs.createReadStream(inputFile).pipe(cipher).pipe(output); | ||
|
||
output.on('finish', () => { | ||
log(`The Environment file "${inputFile}" has been encrypted to "${outputFilePath}".`, 'info'); | ||
log(`Make sure to delete "${inputFile}" for production use.`, 'warn'); | ||
}); | ||
|
||
} catch (e) { | ||
log(e, 'error'); | ||
} | ||
}; | ||
try { | ||
const secret = options.secret || 'mySecret' | ||
const inputFile = options.inputFile || '.env' | ||
const outputFilePath = options.outputFile || `${inputFile}.enc` | ||
const encryptionAlgo = options.encryptionAlgo || 'aes192' | ||
|
||
if (!fs.existsSync(inputFile)) throw `Error: ${inputFile} does not exist.` | ||
if (!secret || typeof (secret) !== 'string') throw 'No SecretKey provided.Use -s option to specify secret' | ||
|
||
const cipher = crypto.createCipher(encryptionAlgo, secret) | ||
const output = fs.createWriteStream(outputFilePath) | ||
fs.createReadStream(inputFile).pipe(cipher).pipe(output) | ||
|
||
output.on('finish', () => { | ||
log(`The Environment file "${inputFile}" has been encrypted to "${outputFilePath}".`, 'info') | ||
log(`Make sure to delete "${inputFile}" for production use.`, 'warn') | ||
}) | ||
} catch (e) { | ||
log(e, 'error') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,35 @@ | ||
const colorCodes = { | ||
BLACK: "\x1b[30m", | ||
RED: "\x1b[31m", | ||
GREEN: "\x1b[32m", | ||
YELLOW: "\x1b[33m", | ||
BLUE: "\x1b[34m", | ||
MAGENTA: "\x1b[35m", | ||
CYAN: "\x1b[36m", | ||
RESET: "\x1b[0m" | ||
BLACK: '\x1b[30m', | ||
RED: '\x1b[31m', | ||
GREEN: '\x1b[32m', | ||
YELLOW: '\x1b[33m', | ||
BLUE: '\x1b[34m', | ||
MAGENTA: '\x1b[35m', | ||
CYAN: '\x1b[36m', | ||
RESET: '\x1b[0m' | ||
} | ||
|
||
function getColorCode(colorName) { | ||
let colorCode = colorCodes[colorName.toUpperCase()]; | ||
return colorCode || colorCodes.RESET; | ||
function getColorCode (colorName) { | ||
const colorCode = colorCodes[colorName.toUpperCase()] | ||
return colorCode || colorCodes.RESET | ||
} | ||
|
||
module.exports = (data, type, color) => { | ||
if (data) { | ||
let logData = `${colorCodes.CYAN}Secure-env : ${colorCodes.RESET}`; | ||
switch (type.toUpperCase()) { | ||
case `ERROR`: | ||
logData += `${colorCodes.RED} ERROR OCCURED ${colorCodes.RESET}` | ||
break; | ||
case `INFO`: | ||
logData += `${colorCodes.GREEN} INFO ${colorCodes.RESET}` | ||
break; | ||
case `WARN`: | ||
logData += `${colorCodes.YELLOW} WARNING ${colorCodes.RESET}` | ||
break; | ||
} | ||
|
||
logData += color ? `${getColorCode(color)}${data}${colorCodes.RESET}` : data; | ||
console.log(logData); | ||
if (data) { | ||
let logData = `${colorCodes.CYAN}Secure-env : ${colorCodes.RESET}` | ||
switch (type.toUpperCase()) { | ||
case 'ERROR': | ||
logData += `${colorCodes.RED} ERROR OCCURED ${colorCodes.RESET}` | ||
break | ||
case 'INFO': | ||
logData += `${colorCodes.GREEN} INFO ${colorCodes.RESET}` | ||
break | ||
case 'WARN': | ||
logData += `${colorCodes.YELLOW} WARNING ${colorCodes.RESET}` | ||
break | ||
} | ||
} | ||
|
||
logData += color ? `${getColorCode(color)}${data}${colorCodes.RESET}` : data | ||
console.log(logData) | ||
} | ||
} |
Oops, something went wrong.