1 | import * as fs from 'fs-extra'
|
2 | import globby = require('globby')
|
3 | import * as path from 'path'
|
4 | import { dimGrey, yellow } from '../colors'
|
5 | import { encryptRandomIVBuffer } from '../security/crypto.util'
|
6 |
|
7 | export interface EncryptCLIOptions {
|
8 | pattern: string[]
|
9 | encKey: string
|
10 | algorithm?: string
|
11 | del?: boolean
|
12 | }
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | export function secretsEncrypt(
|
19 | pattern: string[],
|
20 | encKey: string,
|
21 | algorithm?: string,
|
22 | del?: boolean,
|
23 | ): void {
|
24 | const patterns = [
|
25 | ...pattern,
|
26 | `!**/*.enc`,
|
27 | ]
|
28 | const filenames = globby.sync(patterns)
|
29 |
|
30 | filenames.forEach(filename => {
|
31 | const plain = fs.readFileSync(filename)
|
32 | const enc = encryptRandomIVBuffer(plain, encKey, algorithm)
|
33 |
|
34 | const encFilename = `${filename}.enc`
|
35 | fs.writeFileSync(encFilename, enc)
|
36 |
|
37 | if (del) {
|
38 | fs.unlinkSync(filename)
|
39 | }
|
40 |
|
41 | console.log(` ${path.basename(filename)} > ${path.basename(encFilename)}`)
|
42 | })
|
43 |
|
44 | console.log(`encrypted ${yellow(filenames.length)} files in (${dimGrey(pattern.join(' '))})`)
|
45 | }
|