Skip to content

Commit 94c2bd4

Browse files
authored
Merge pull request #8 from kunalpanchal/refactor/standard-fix
Fixes based on https://standardjs.com/
2 parents 4aec4f9 + 61bda80 commit 94c2bd4

File tree

8 files changed

+461
-435
lines changed

8 files changed

+461
-435
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@ typings/
5656

5757
# dotenv environment variables file
5858
.env
59+
60+
# Build directories
61+
dist

lib/cli.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#! /usr/bin/env node
22

3-
/* Arguments that can be passed are
3+
/* Arguments that can be passed are
44
* --secret <secretKey> | -s <secretKey>
55
* --out <file-path> | -o <file-path>
6-
* --algo <algoName> | -a <algoName>
6+
* --algo <algoName> | -a <algoName>
77
*/
88

9-
const argv = require('minimist')(process.argv.slice(2));
10-
const outputFile = argv.outputFile || argv.o;
11-
const inputFile = argv._[0];
12-
const secret = argv.secret || argv.s;
13-
const encryptionAlgo = argv.algo || argv.a;
9+
const argv = require('minimist')(process.argv.slice(2))
10+
const outputFile = argv.outputFile || argv.o
11+
const inputFile = argv._[0]
12+
const secret = argv.secret || argv.s
13+
const encryptionAlgo = argv.algo || argv.a
1414

15-
const cryptography = require('./cryptography');
15+
const cryptography = require('./cryptography')
1616

17-
cryptography.encrypt({ secret, inputFile, outputFile, encryptionAlgo });
17+
cryptography.encrypt({ secret, inputFile, outputFile, encryptionAlgo })

lib/cryptography.js

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,52 @@
1-
"use strict"
1+
'use strict'
22

3-
const crypto = require('crypto');
4-
const fs = require('fs');
5-
const log = require('./utils/log');
3+
const crypto = require('crypto')
4+
const fs = require('fs')
5+
const log = require('./utils/log')
66

7-
/* Arguments that can be passed are
7+
/* Arguments that can be passed are
88
* --secret <secretKey> | -s <secretKey>
99
* --out <file-path> | -o <file-path>
10-
* --algo <algoName> | -a <algoName>
10+
* --algo <algoName> | -a <algoName>
1111
*/
1212

1313
module.exports.decrypt = (options) => {
14-
try {
15-
const secret = options.secret || 'mySecret';
16-
const inputFile = options.file || '.env.enc';
17-
const decryptionAlgo = options.decryptionAlgo || `aes192`;
18-
19-
if (!fs.existsSync(inputFile)) throw `${inputFile} does not exist.`
20-
if (!secret || typeof (secret) !== 'string') throw 'No SecretKey provided.';
21-
22-
const decipher = crypto.createDecipher(decryptionAlgo, secret);
23-
let decrypted = decipher.update(fs.readFileSync(inputFile), 'hex', 'utf8');
24-
decrypted += decipher.final('utf8');
25-
return decrypted;
26-
} catch (e) {
27-
log(e, 'error');
28-
}
29-
};
14+
try {
15+
const secret = options.secret || 'mySecret'
16+
const inputFile = options.file || '.env.enc'
17+
const decryptionAlgo = options.decryptionAlgo || 'aes192'
18+
19+
if (!fs.existsSync(inputFile)) throw `${inputFile} does not exist.`
20+
if (!secret || typeof (secret) !== 'string') throw 'No SecretKey provided.'
21+
22+
const decipher = crypto.createDecipher(decryptionAlgo, secret)
23+
let decrypted = decipher.update(fs.readFileSync(inputFile), 'hex', 'utf8')
24+
decrypted += decipher.final('utf8')
25+
return decrypted
26+
} catch (e) {
27+
log(e, 'error')
28+
}
29+
}
3030

3131
module.exports.encrypt = (options) => {
32-
try {
33-
const secret = options.secret || 'mySecret';
34-
const inputFile = options.inputFile || '.env';
35-
const outputFilePath = options.outputFile || `${inputFile}.enc`;
36-
const encryptionAlgo = options.encryptionAlgo || `aes192`;
37-
38-
if (!fs.existsSync(inputFile)) throw `Error: ${inputFile} does not exist.`
39-
if (!secret || typeof (secret) !== 'string') throw 'No SecretKey provided.Use -s option to specify secret';
40-
41-
const cipher = crypto.createCipher(encryptionAlgo, secret);
42-
const output = fs.createWriteStream(outputFilePath);
43-
fs.createReadStream(inputFile).pipe(cipher).pipe(output);
44-
45-
output.on('finish', () => {
46-
log(`The Environment file "${inputFile}" has been encrypted to "${outputFilePath}".`, 'info');
47-
log(`Make sure to delete "${inputFile}" for production use.`, 'warn');
48-
});
49-
50-
} catch (e) {
51-
log(e, 'error');
52-
}
53-
};
32+
try {
33+
const secret = options.secret || 'mySecret'
34+
const inputFile = options.inputFile || '.env'
35+
const outputFilePath = options.outputFile || `${inputFile}.enc`
36+
const encryptionAlgo = options.encryptionAlgo || 'aes192'
37+
38+
if (!fs.existsSync(inputFile)) throw `Error: ${inputFile} does not exist.`
39+
if (!secret || typeof (secret) !== 'string') throw 'No SecretKey provided.Use -s option to specify secret'
40+
41+
const cipher = crypto.createCipher(encryptionAlgo, secret)
42+
const output = fs.createWriteStream(outputFilePath)
43+
fs.createReadStream(inputFile).pipe(cipher).pipe(output)
44+
45+
output.on('finish', () => {
46+
log(`The Environment file "${inputFile}" has been encrypted to "${outputFilePath}".`, 'info')
47+
log(`Make sure to delete "${inputFile}" for production use.`, 'warn')
48+
})
49+
} catch (e) {
50+
log(e, 'error')
51+
}
52+
}

lib/index.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
'use strict'
22

3-
const fs = require('fs');
4-
const cryptography = require('./cryptography');
5-
const log = require('./utils/log');
6-
const parser = require('./parser');
3+
const cryptography = require('./cryptography')
4+
const log = require('./utils/log')
5+
const parser = require('./parser')
76

87
/*
98
* Options Expected
@@ -13,22 +12,21 @@ const parser = require('./parser');
1312
*/
1413

1514
module.exports = (options) => {
16-
try {
17-
let decryptedContent = cryptography.decrypt({
18-
secret: options.secret,
19-
file: options.path,
20-
decryptionAlgo: options.enc_algo
21-
});
22-
if (decryptedContent) {
23-
let env = {};
24-
let parsedObj = parser(decryptedContent);
25-
Object.keys(parsedObj).forEach(key => {
26-
if (!env.hasOwnProperty(key))
27-
env[key] = parsedObj[key]
28-
})
29-
return env;
30-
}
31-
} catch (e) {
32-
log(e, 'error');
15+
try {
16+
const decryptedContent = cryptography.decrypt({
17+
secret: options.secret,
18+
file: options.path,
19+
decryptionAlgo: options.enc_algo
20+
})
21+
if (decryptedContent) {
22+
const env = {}
23+
const parsedObj = parser(decryptedContent)
24+
Object.keys(parsedObj).forEach(key => {
25+
if (!env.hasOwnProperty(key)) { env[key] = parsedObj[key] }
26+
})
27+
return env
3328
}
34-
}
29+
} catch (e) {
30+
log(e, 'error')
31+
}
32+
}

lib/parser.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@
33
*/
44

55
module.exports = (src) => {
6-
let obj = {}
7-
// convert Buffers before splitting into lines and processing
8-
src.toString().split('\n').forEach(function (line) {
9-
// matching "KEY' and 'VAL' in 'KEY=VAL'
10-
const keyValueArr = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/)
11-
// matched?
12-
if (keyValueArr != null) {
13-
const key = keyValueArr[1]
14-
// default undefined or missing values to empty string
15-
let value = keyValueArr[2] || ''
16-
// expand newlines in quoted values
17-
let len = value ? value.length : 0
18-
if (len > 0 && value.charAt(0) === '"' && value.charAt(len - 1) === '"') {
19-
value = value.replace(/\\n/gm, '\n')
20-
}
21-
// remove any surrounding quotes and extra spaces
22-
value = value.replace(/(^['"]|['"]$)/g, '').trim()
23-
obj[key] = value
24-
}
25-
})
26-
return obj;
27-
}
6+
const obj = {}
7+
// convert Buffers before splitting into lines and processing
8+
src.toString().split('\n').forEach(function (line) {
9+
// matching "KEY' and 'VAL' in 'KEY=VAL'
10+
const keyValueArr = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/)
11+
// matched?
12+
if (keyValueArr != null) {
13+
const key = keyValueArr[1]
14+
// default undefined or missing values to empty string
15+
let value = keyValueArr[2] || ''
16+
// expand newlines in quoted values
17+
const len = value ? value.length : 0
18+
if (len > 0 && value.charAt(0) === '"' && value.charAt(len - 1) === '"') {
19+
value = value.replace(/\\n/gm, '\n')
20+
}
21+
// remove any surrounding quotes and extra spaces
22+
value = value.replace(/(^['"]|['"]$)/g, '').trim()
23+
obj[key] = value
24+
}
25+
})
26+
return obj
27+
}

lib/utils/log.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
const colorCodes = {
2-
BLACK: "\x1b[30m",
3-
RED: "\x1b[31m",
4-
GREEN: "\x1b[32m",
5-
YELLOW: "\x1b[33m",
6-
BLUE: "\x1b[34m",
7-
MAGENTA: "\x1b[35m",
8-
CYAN: "\x1b[36m",
9-
RESET: "\x1b[0m"
2+
BLACK: '\x1b[30m',
3+
RED: '\x1b[31m',
4+
GREEN: '\x1b[32m',
5+
YELLOW: '\x1b[33m',
6+
BLUE: '\x1b[34m',
7+
MAGENTA: '\x1b[35m',
8+
CYAN: '\x1b[36m',
9+
RESET: '\x1b[0m'
1010
}
1111

12-
function getColorCode(colorName) {
13-
let colorCode = colorCodes[colorName.toUpperCase()];
14-
return colorCode || colorCodes.RESET;
12+
function getColorCode (colorName) {
13+
const colorCode = colorCodes[colorName.toUpperCase()]
14+
return colorCode || colorCodes.RESET
1515
}
1616

1717
module.exports = (data, type, color) => {
18-
if (data) {
19-
let logData = `${colorCodes.CYAN}Secure-env : ${colorCodes.RESET}`;
20-
switch (type.toUpperCase()) {
21-
case `ERROR`:
22-
logData += `${colorCodes.RED} ERROR OCCURED ${colorCodes.RESET}`
23-
break;
24-
case `INFO`:
25-
logData += `${colorCodes.GREEN} INFO ${colorCodes.RESET}`
26-
break;
27-
case `WARN`:
28-
logData += `${colorCodes.YELLOW} WARNING ${colorCodes.RESET}`
29-
break;
30-
}
31-
32-
logData += color ? `${getColorCode(color)}${data}${colorCodes.RESET}` : data;
33-
console.log(logData);
18+
if (data) {
19+
let logData = `${colorCodes.CYAN}Secure-env : ${colorCodes.RESET}`
20+
switch (type.toUpperCase()) {
21+
case 'ERROR':
22+
logData += `${colorCodes.RED} ERROR OCCURED ${colorCodes.RESET}`
23+
break
24+
case 'INFO':
25+
logData += `${colorCodes.GREEN} INFO ${colorCodes.RESET}`
26+
break
27+
case 'WARN':
28+
logData += `${colorCodes.YELLOW} WARNING ${colorCodes.RESET}`
29+
break
3430
}
35-
}
31+
32+
logData += color ? `${getColorCode(color)}${data}${colorCodes.RESET}` : data
33+
console.log(logData)
34+
}
35+
}

0 commit comments

Comments
 (0)