UNPKG

1.15 kBPlain TextView Raw
1import * as fs from 'fs-extra'
2import globby = require('globby')
3import * as path from 'path'
4import { dimGrey, yellow } from '../colors'
5import { decryptRandomIVBuffer } from '../security/crypto.util'
6
7export interface DecryptCLIOptions {
8 dir: string[]
9 encKey: string
10 algorithm?: string
11 del?: boolean
12}
13
14/**
15 * Decrypts all files in given directory (*.enc), saves decrypted versions without ending `.enc`.
16 * Using provided encKey.
17 */
18export function secretsDecrypt(
19 dir: string[],
20 encKey: string,
21 algorithm?: string,
22 del?: boolean,
23): void {
24 const patterns = dir.map(d => `${d}/**/*.enc`)
25
26 const filenames = globby.sync(patterns)
27
28 filenames.forEach(filename => {
29 const enc = fs.readFileSync(filename)
30 const plain = decryptRandomIVBuffer(enc, encKey, algorithm)
31
32 const plainFilename = filename.slice(0, filename.length - '.enc'.length)
33 fs.writeFileSync(plainFilename, plain)
34
35 if (del) {
36 fs.unlinkSync(filename)
37 }
38
39 console.log(` ${path.basename(filename)} > ${path.basename(plainFilename)}`)
40 })
41
42 console.log(`decrypted ${yellow(filenames.length)} files in ${dimGrey(dir.join(' '))}`)
43}