@node-cli/secret 1.0.7 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,15 +2,38 @@
2
2
 
3
3
  ![npm](https://img.shields.io/npm/v/@node-cli/secret?label=version&logo=npm)
4
4
 
5
- > Secret is a command line tool that can encode or decode a file with a password.
5
+ > Secret is a command line tool that can encode or decode a file with a password. It also exposes a library to be used in other projects.
6
6
 
7
- ## Installation
7
+ ## Installation as a CLI
8
8
 
9
9
  ```sh
10
10
  > npm install --global @node-cli/secret
11
11
  ```
12
12
 
13
- ## Examples
13
+ ## Installation as a library
14
+
15
+ ```sh
16
+ > cd your-project
17
+ > npm install @node-cli/secret
18
+ ```
19
+
20
+ ## Usage as a library
21
+
22
+ ### Encrypt a string with a password
23
+
24
+ ```js
25
+ import { encrypt } from "@node-cli/secret";
26
+ const encrypted = encrypt("password", "Hello World");
27
+ ```
28
+
29
+ ### Decrypt a string with a password
30
+
31
+ ```js
32
+ import { decrypt } from "@node-cli/secret";
33
+ const decrypted = decrypt("password", encrypted);
34
+ ```
35
+
36
+ ## Usage as a CLI
14
37
 
15
38
  NOTE: The password is not stored anywhere, it is only used to encrypt or decrypt the file and will be prompted each time.
16
39
 
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  * @file Automatically generated by barrelsby.
3
3
  */
4
4
 
5
+ export * from "./lib";
5
6
  export * from "./parse";
6
7
  export * from "./secret";
7
8
  export * from "./utilities";
package/dist/lib.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ import { Logger } from "@node-cli/logger";
2
+ export declare const logger: Logger;
3
+ /**
4
+ * Create an hexadecimal hash from a given string. The default
5
+ * algorithm is md5 but it can be changed to anything that
6
+ * crypto.createHash allows.
7
+ * @param {String} string the string to hash
8
+ * @param {String} [algorithm='md5'] the algorithm to use or hashing
9
+ * @return {String} the hashed string in hexa format
10
+ */
11
+ export declare const createHash: (string: string, algorithm?: string) => string;
12
+ /**
13
+ * Encrypts a string or a buffer using AES-256-CTR
14
+ * algorithm.
15
+ * @param {String} password a unique password
16
+ * @param {String} data a string to encrypt
17
+ * @return {String} the encrypted data in hexa
18
+ * encoding, followed by a dollar sign ($) and by a
19
+ * unique random initialization vector.
20
+ */
21
+ export declare const encrypt: (password: string, data: string) => string;
22
+ /**
23
+ * Decrypts a string that was encrypted using the
24
+ * AES-256-CRT algorithm via `encrypt`. It expects
25
+ * the encrypted string to have the corresponding
26
+ * initialization vector appended at the end, after
27
+ * a dollar sign ($) - which was done via the
28
+ * corresponding `encrypt` method.
29
+ * @param {String} password a unique password
30
+ * @param {String} data a string to decrypt
31
+ * @return {String} the decrypted data
32
+ */
33
+ export declare const decrypt: (password: string, data: string) => string;
package/dist/lib.js ADDED
@@ -0,0 +1,67 @@
1
+ import crypto from "node:crypto";
2
+ import { Logger } from "@node-cli/logger";
3
+ export const logger = new Logger({
4
+ boring: process.env.NODE_ENV === "test"
5
+ });
6
+ const HEX = "hex";
7
+ const UTF8 = "utf8";
8
+ const DEFAULT_CRYPTO_ALGO = "aes-256-ctr";
9
+ const DEFAULT_HASH_ALGO = "md5";
10
+ const DEFAULT_BYTES_FOR_IV = 16;
11
+ /**
12
+ * Create an hexadecimal hash from a given string. The default
13
+ * algorithm is md5 but it can be changed to anything that
14
+ * crypto.createHash allows.
15
+ * @param {String} string the string to hash
16
+ * @param {String} [algorithm='md5'] the algorithm to use or hashing
17
+ * @return {String} the hashed string in hexa format
18
+ */ export const createHash = (string, algorithm = DEFAULT_HASH_ALGO)=>{
19
+ return crypto.createHash(algorithm).update(string, UTF8).digest(HEX);
20
+ };
21
+ /**
22
+ * Encrypts a string or a buffer using AES-256-CTR
23
+ * algorithm.
24
+ * @param {String} password a unique password
25
+ * @param {String} data a string to encrypt
26
+ * @return {String} the encrypted data in hexa
27
+ * encoding, followed by a dollar sign ($) and by a
28
+ * unique random initialization vector.
29
+ */ export const encrypt = (password, data)=>{
30
+ // Ensure that the initialization vector (IV) is random.
31
+ const iv = crypto.randomBytes(DEFAULT_BYTES_FOR_IV);
32
+ // Hash the given password (result is always the same).
33
+ const key = createHash(password);
34
+ // Create a cipher.
35
+ const cipher = crypto.createCipheriv(DEFAULT_CRYPTO_ALGO, key, iv);
36
+ // Encrypt the data using the newly created cipher.
37
+ const encrypted = cipher.update(data, UTF8, HEX) + cipher.final(HEX);
38
+ /*
39
+ * Append the IV at the end of the encrypted data
40
+ * to reuse it for decryption (IV is not a key,
41
+ * it can be public).
42
+ */ return `${encrypted}$${iv.toString(HEX)}`;
43
+ };
44
+ /**
45
+ * Decrypts a string that was encrypted using the
46
+ * AES-256-CRT algorithm via `encrypt`. It expects
47
+ * the encrypted string to have the corresponding
48
+ * initialization vector appended at the end, after
49
+ * a dollar sign ($) - which was done via the
50
+ * corresponding `encrypt` method.
51
+ * @param {String} password a unique password
52
+ * @param {String} data a string to decrypt
53
+ * @return {String} the decrypted data
54
+ */ export const decrypt = (password, data)=>{
55
+ // Extract encrypted data and initialization vector (IV).
56
+ const [encrypted, ivHex] = data.split("$");
57
+ // Create a buffer out of the raw hex IV
58
+ const iv = Buffer.from(ivHex, HEX);
59
+ // Hash the given password (result is always the same).
60
+ const hash = createHash(password);
61
+ // Create a cipher.
62
+ const decipher = crypto.createDecipheriv(DEFAULT_CRYPTO_ALGO, hash, iv);
63
+ // Return the decrypted data using the newly created cipher.
64
+ return decipher.update(encrypted, HEX, UTF8) + decipher.final("utf8");
65
+ };
66
+
67
+ //# sourceMappingURL=lib.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/lib.ts"],"sourcesContent":["import crypto from \"node:crypto\";\nimport { Logger } from \"@node-cli/logger\";\n\nexport const logger = new Logger({\n\tboring: process.env.NODE_ENV === \"test\",\n});\n\nconst HEX = \"hex\";\nconst UTF8 = \"utf8\";\n\nconst DEFAULT_CRYPTO_ALGO = \"aes-256-ctr\";\nconst DEFAULT_HASH_ALGO = \"md5\";\nconst DEFAULT_BYTES_FOR_IV = 16;\n\n/**\n * Create an hexadecimal hash from a given string. The default\n * algorithm is md5 but it can be changed to anything that\n * crypto.createHash allows.\n * @param {String} string the string to hash\n * @param {String} [algorithm='md5'] the algorithm to use or hashing\n * @return {String} the hashed string in hexa format\n */\nexport const createHash = (\n\tstring: string,\n\talgorithm: string = DEFAULT_HASH_ALGO,\n): string => {\n\treturn crypto.createHash(algorithm).update(string, UTF8).digest(HEX);\n};\n\n/**\n * Encrypts a string or a buffer using AES-256-CTR\n * algorithm.\n * @param {String} password a unique password\n * @param {String} data a string to encrypt\n * @return {String} the encrypted data in hexa\n * encoding, followed by a dollar sign ($) and by a\n * unique random initialization vector.\n */\nexport const encrypt = (password: string, data: string): string => {\n\t// Ensure that the initialization vector (IV) is random.\n\tconst iv = crypto.randomBytes(DEFAULT_BYTES_FOR_IV);\n\t// Hash the given password (result is always the same).\n\tconst key = createHash(password);\n\t// Create a cipher.\n\tconst cipher = crypto.createCipheriv(DEFAULT_CRYPTO_ALGO, key, iv);\n\t// Encrypt the data using the newly created cipher.\n\tconst encrypted = cipher.update(data, UTF8, HEX) + cipher.final(HEX);\n\t/*\n\t * Append the IV at the end of the encrypted data\n\t * to reuse it for decryption (IV is not a key,\n\t * it can be public).\n\t */\n\treturn `${encrypted}$${iv.toString(HEX)}`;\n};\n\n/**\n * Decrypts a string that was encrypted using the\n * AES-256-CRT algorithm via `encrypt`. It expects\n * the encrypted string to have the corresponding\n * initialization vector appended at the end, after\n * a dollar sign ($) - which was done via the\n * corresponding `encrypt` method.\n * @param {String} password a unique password\n * @param {String} data a string to decrypt\n * @return {String} the decrypted data\n */\nexport const decrypt = (password: string, data: string): string => {\n\t// Extract encrypted data and initialization vector (IV).\n\tconst [encrypted, ivHex] = data.split(\"$\");\n\t// Create a buffer out of the raw hex IV\n\tconst iv = Buffer.from(ivHex, HEX);\n\t// Hash the given password (result is always the same).\n\tconst hash = createHash(password);\n\t// Create a cipher.\n\tconst decipher = crypto.createDecipheriv(DEFAULT_CRYPTO_ALGO, hash, iv);\n\t// Return the decrypted data using the newly created cipher.\n\treturn decipher.update(encrypted, HEX, UTF8) + decipher.final(\"utf8\");\n};\n"],"names":["crypto","Logger","logger","boring","process","env","NODE_ENV","HEX","UTF8","DEFAULT_CRYPTO_ALGO","DEFAULT_HASH_ALGO","DEFAULT_BYTES_FOR_IV","createHash","string","algorithm","update","digest","encrypt","password","data","iv","randomBytes","key","cipher","createCipheriv","encrypted","final","toString","decrypt","ivHex","split","Buffer","from","hash","decipher","createDecipheriv"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,OAAOA,YAAY,cAAc;AACjC,SAASC,MAAM,QAAQ,mBAAmB;AAE1C,OAAO,MAAMC,SAAS,IAAID,OAAO;IAChCE,QAAQC,QAAQC,GAAG,CAACC,QAAQ,KAAK;AAClC,GAAG;AAEH,MAAMC,MAAM;AACZ,MAAMC,OAAO;AAEb,MAAMC,sBAAsB;AAC5B,MAAMC,oBAAoB;AAC1B,MAAMC,uBAAuB;AAE7B;;;;;;;CAOC,GACD,OAAO,MAAMC,aAAa,CACzBC,QACAC,YAAoBJ,iBAAiB;IAErC,OAAOV,OAAOY,UAAU,CAACE,WAAWC,MAAM,CAACF,QAAQL,MAAMQ,MAAM,CAACT;AACjE,EAAE;AAEF;;;;;;;;CAQC,GACD,OAAO,MAAMU,UAAU,CAACC,UAAkBC;IACzC,wDAAwD;IACxD,MAAMC,KAAKpB,OAAOqB,WAAW,CAACV;IAC9B,uDAAuD;IACvD,MAAMW,MAAMV,WAAWM;IACvB,mBAAmB;IACnB,MAAMK,SAASvB,OAAOwB,cAAc,CAACf,qBAAqBa,KAAKF;IAC/D,mDAAmD;IACnD,MAAMK,YAAYF,OAAOR,MAAM,CAACI,MAAMX,MAAMD,OAAOgB,OAAOG,KAAK,CAACnB;IAChE;;;;EAIC,GACD,OAAO,CAAC,EAAEkB,UAAU,CAAC,EAAEL,GAAGO,QAAQ,CAACpB,KAAK,CAAC;AAC1C,EAAE;AAEF;;;;;;;;;;CAUC,GACD,OAAO,MAAMqB,UAAU,CAACV,UAAkBC;IACzC,yDAAyD;IACzD,MAAM,CAACM,WAAWI,MAAM,GAAGV,KAAKW,KAAK,CAAC;IACtC,wCAAwC;IACxC,MAAMV,KAAKW,OAAOC,IAAI,CAACH,OAAOtB;IAC9B,uDAAuD;IACvD,MAAM0B,OAAOrB,WAAWM;IACxB,mBAAmB;IACnB,MAAMgB,WAAWlC,OAAOmC,gBAAgB,CAAC1B,qBAAqBwB,MAAMb;IACpE,4DAA4D;IAC5D,OAAOc,SAASnB,MAAM,CAACU,WAAWlB,KAAKC,QAAQ0B,SAASR,KAAK,CAAC;AAC/D,EAAE"}
@@ -1,36 +1,5 @@
1
1
  import { Logger } from "@node-cli/logger";
2
2
  export declare const logger: Logger;
3
- /**
4
- * Create an hexadecimal hash from a given string. The default
5
- * algorithm is md5 but it can be changed to anything that
6
- * crypto.createHash allows.
7
- * @param {String} string the string to hash
8
- * @param {String} [algorithm='md5'] the algorithm to use or hashing
9
- * @return {String} the hashed string in hexa format
10
- */
11
- export declare const createHash: (string: string, algorithm?: string) => string;
12
- /**
13
- * Encrypts a string or a buffer using AES-256-CTR
14
- * algorithm.
15
- * @param {String} password a unique password
16
- * @param {String} data a string to encrypt
17
- * @return {String} the encrypted data in hexa
18
- * encoding, followed by a dollar sign ($) and by a
19
- * unique random initialization vector.
20
- */
21
- export declare const encrypt: (password: string, data: string) => string;
22
- /**
23
- * Decrypts a string that was encrypted using the
24
- * AES-256-CRT algorithm via `encrypt`. It expects
25
- * the encrypted string to have the corresponding
26
- * initialization vector appended at the end, after
27
- * a dollar sign ($) - which was done via the
28
- * corresponding `encrypt` method.
29
- * @param {String} password a unique password
30
- * @param {String} data a string to decrypt
31
- * @return {String} the decrypted data
32
- */
33
- export declare const decrypt: (password: string, data: string) => string;
34
3
  /**
35
4
  * Process a file with a given password. The file can be
36
5
  * encoded or decoded depending on the `encode` flag.
package/dist/utilities.js CHANGED
@@ -1,71 +1,12 @@
1
- import crypto from "node:crypto";
2
1
  import { Logger } from "@node-cli/logger";
3
2
  import fs from "fs-extra";
4
3
  import inquirer from "inquirer";
4
+ import { decrypt, encrypt } from "./lib.js";
5
5
  export const logger = new Logger({
6
6
  boring: process.env.NODE_ENV === "test"
7
7
  });
8
- const HEX = "hex";
9
8
  const UTF8 = "utf8";
10
- const DEFAULT_CRYPTO_ALGO = "aes-256-ctr";
11
- const DEFAULT_HASH_ALGO = "md5";
12
- const DEFAULT_BYTES_FOR_IV = 16;
13
9
  const DEFAULT_FILE_ENCODING = UTF8;
14
- /**
15
- * Create an hexadecimal hash from a given string. The default
16
- * algorithm is md5 but it can be changed to anything that
17
- * crypto.createHash allows.
18
- * @param {String} string the string to hash
19
- * @param {String} [algorithm='md5'] the algorithm to use or hashing
20
- * @return {String} the hashed string in hexa format
21
- */ export const createHash = (string, algorithm = DEFAULT_HASH_ALGO)=>{
22
- return crypto.createHash(algorithm).update(string, UTF8).digest(HEX);
23
- };
24
- /**
25
- * Encrypts a string or a buffer using AES-256-CTR
26
- * algorithm.
27
- * @param {String} password a unique password
28
- * @param {String} data a string to encrypt
29
- * @return {String} the encrypted data in hexa
30
- * encoding, followed by a dollar sign ($) and by a
31
- * unique random initialization vector.
32
- */ export const encrypt = (password, data)=>{
33
- // Ensure that the initialization vector (IV) is random.
34
- const iv = crypto.randomBytes(DEFAULT_BYTES_FOR_IV);
35
- // Hash the given password (result is always the same).
36
- const key = createHash(password);
37
- // Create a cipher.
38
- const cipher = crypto.createCipheriv(DEFAULT_CRYPTO_ALGO, key, iv);
39
- // Encrypt the data using the newly created cipher.
40
- const encrypted = cipher.update(data, UTF8, HEX) + cipher.final(HEX);
41
- /*
42
- * Append the IV at the end of the encrypted data
43
- * to reuse it for decryption (IV is not a key,
44
- * it can be public).
45
- */ return `${encrypted}$${iv.toString(HEX)}`;
46
- };
47
- /**
48
- * Decrypts a string that was encrypted using the
49
- * AES-256-CRT algorithm via `encrypt`. It expects
50
- * the encrypted string to have the corresponding
51
- * initialization vector appended at the end, after
52
- * a dollar sign ($) - which was done via the
53
- * corresponding `encrypt` method.
54
- * @param {String} password a unique password
55
- * @param {String} data a string to decrypt
56
- * @return {String} the decrypted data
57
- */ export const decrypt = (password, data)=>{
58
- // Extract encrypted data and initialization vector (IV).
59
- const [encrypted, ivHex] = data.split("$");
60
- // Create a buffer out of the raw hex IV
61
- const iv = Buffer.from(ivHex, HEX);
62
- // Hash the given password (result is always the same).
63
- const hash = createHash(password);
64
- // Create a cipher.
65
- const decipher = crypto.createDecipheriv(DEFAULT_CRYPTO_ALGO, hash, iv);
66
- // Return the decrypted data using the newly created cipher.
67
- return decipher.update(encrypted, HEX, UTF8) + decipher.final("utf8");
68
- };
69
10
  export const processFileWithPassword = async (options)=>{
70
11
  const { encode, input, output, password } = options;
71
12
  const fileProcessor = encode ? encrypt : decrypt;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/utilities.ts"],"sourcesContent":["import crypto from \"node:crypto\";\nimport { Logger } from \"@node-cli/logger\";\nimport fs from \"fs-extra\";\nimport inquirer from \"inquirer\";\n\nexport const logger = new Logger({\n\tboring: process.env.NODE_ENV === \"test\",\n});\n\nconst HEX = \"hex\";\nconst UTF8 = \"utf8\";\n\nconst DEFAULT_CRYPTO_ALGO = \"aes-256-ctr\";\nconst DEFAULT_HASH_ALGO = \"md5\";\nconst DEFAULT_BYTES_FOR_IV = 16;\nconst DEFAULT_FILE_ENCODING = UTF8;\n\n/**\n * Create an hexadecimal hash from a given string. The default\n * algorithm is md5 but it can be changed to anything that\n * crypto.createHash allows.\n * @param {String} string the string to hash\n * @param {String} [algorithm='md5'] the algorithm to use or hashing\n * @return {String} the hashed string in hexa format\n */\nexport const createHash = (\n\tstring: string,\n\talgorithm: string = DEFAULT_HASH_ALGO,\n): string => {\n\treturn crypto.createHash(algorithm).update(string, UTF8).digest(HEX);\n};\n\n/**\n * Encrypts a string or a buffer using AES-256-CTR\n * algorithm.\n * @param {String} password a unique password\n * @param {String} data a string to encrypt\n * @return {String} the encrypted data in hexa\n * encoding, followed by a dollar sign ($) and by a\n * unique random initialization vector.\n */\nexport const encrypt = (password: string, data: string): string => {\n\t// Ensure that the initialization vector (IV) is random.\n\tconst iv = crypto.randomBytes(DEFAULT_BYTES_FOR_IV);\n\t// Hash the given password (result is always the same).\n\tconst key = createHash(password);\n\t// Create a cipher.\n\tconst cipher = crypto.createCipheriv(DEFAULT_CRYPTO_ALGO, key, iv);\n\t// Encrypt the data using the newly created cipher.\n\tconst encrypted = cipher.update(data, UTF8, HEX) + cipher.final(HEX);\n\t/*\n\t * Append the IV at the end of the encrypted data\n\t * to reuse it for decryption (IV is not a key,\n\t * it can be public).\n\t */\n\treturn `${encrypted}$${iv.toString(HEX)}`;\n};\n\n/**\n * Decrypts a string that was encrypted using the\n * AES-256-CRT algorithm via `encrypt`. It expects\n * the encrypted string to have the corresponding\n * initialization vector appended at the end, after\n * a dollar sign ($) - which was done via the\n * corresponding `encrypt` method.\n * @param {String} password a unique password\n * @param {String} data a string to decrypt\n * @return {String} the decrypted data\n */\nexport const decrypt = (password: string, data: string): string => {\n\t// Extract encrypted data and initialization vector (IV).\n\tconst [encrypted, ivHex] = data.split(\"$\");\n\t// Create a buffer out of the raw hex IV\n\tconst iv = Buffer.from(ivHex, HEX);\n\t// Hash the given password (result is always the same).\n\tconst hash = createHash(password);\n\t// Create a cipher.\n\tconst decipher = crypto.createDecipheriv(DEFAULT_CRYPTO_ALGO, hash, iv);\n\t// Return the decrypted data using the newly created cipher.\n\treturn decipher.update(encrypted, HEX, UTF8) + decipher.final(\"utf8\");\n};\n\n/**\n * Process a file with a given password. The file can be\n * encoded or decoded depending on the `encode` flag.\n * @param {Boolean} encode whether to encode or decode the file\n * @param {String} input the input file path\n * @param {String} [output] the output file path\n * @param {String} password the password to use\n * @return {Promise} a promise that resolves when\n * the file has been processed.\n */\nexport type ProcessFileOptions = {\n\tencode: boolean;\n\tinput: string;\n\toutput?: string;\n\tpassword: string;\n};\nexport const processFileWithPassword = async (\n\toptions: ProcessFileOptions,\n): Promise<void> => {\n\tconst { encode, input, output, password } = options;\n\tconst fileProcessor = encode ? encrypt : decrypt;\n\tconst data = await fs.readFile(input, DEFAULT_FILE_ENCODING);\n\n\tif (output) {\n\t\t// Save data to output file\n\t\tawait fs.outputFile(output, fileProcessor(password, data));\n\t} else {\n\t\t// Print to stdout directly\n\t\tlogger.log(fileProcessor(password, data));\n\t}\n};\n\n/* istanbul ignore next */\nexport const displayConfirmation = async (message: string) => {\n\tconst questions = {\n\t\tdefault: true,\n\t\tmessage: message || \"Do you want to continue?\",\n\t\tname: \"goodToGo\",\n\t\ttype: \"confirm\",\n\t};\n\tlogger.log();\n\tconst answers = await inquirer.prompt(questions);\n\treturn answers.goodToGo;\n};\n\n/* istanbul ignore next */\nexport const displayPromptWithPassword = async (message: string) => {\n\tconst questions = {\n\t\tmessage: message,\n\t\tname: \"password\",\n\t\ttype: \"password\",\n\t\tvalidate(value: string) {\n\t\t\tif (!value) {\n\t\t\t\treturn \"Password cannot be empty...\";\n\t\t\t}\n\t\t\treturn true;\n\t\t},\n\t};\n\tconst answers = await inquirer.prompt(questions);\n\treturn answers.password;\n};\n\n/* istanbul ignore next */\nexport const shouldContinue = (goodToGo: boolean) => {\n\tif (!goodToGo) {\n\t\tlogger.log(\"\\nBye then!\");\n\t\t// eslint-disable-next-line unicorn/no-process-exit\n\t\tprocess.exit(0);\n\t}\n\treturn true;\n};\n"],"names":["crypto","Logger","fs","inquirer","logger","boring","process","env","NODE_ENV","HEX","UTF8","DEFAULT_CRYPTO_ALGO","DEFAULT_HASH_ALGO","DEFAULT_BYTES_FOR_IV","DEFAULT_FILE_ENCODING","createHash","string","algorithm","update","digest","encrypt","password","data","iv","randomBytes","key","cipher","createCipheriv","encrypted","final","toString","decrypt","ivHex","split","Buffer","from","hash","decipher","createDecipheriv","processFileWithPassword","options","encode","input","output","fileProcessor","readFile","outputFile","log","displayConfirmation","message","questions","default","name","type","answers","prompt","goodToGo","displayPromptWithPassword","validate","value","shouldContinue","exit"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"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"}
1
+ {"version":3,"sources":["../src/utilities.ts"],"sourcesContent":["import { Logger } from \"@node-cli/logger\";\nimport fs from \"fs-extra\";\nimport inquirer from \"inquirer\";\n\nimport { decrypt, encrypt } from \"./lib.js\";\n\nexport const logger = new Logger({\n\tboring: process.env.NODE_ENV === \"test\",\n});\n\nconst UTF8 = \"utf8\";\nconst DEFAULT_FILE_ENCODING = UTF8;\n\n/**\n * Process a file with a given password. The file can be\n * encoded or decoded depending on the `encode` flag.\n * @param {Boolean} encode whether to encode or decode the file\n * @param {String} input the input file path\n * @param {String} [output] the output file path\n * @param {String} password the password to use\n * @return {Promise} a promise that resolves when\n * the file has been processed.\n */\nexport type ProcessFileOptions = {\n\tencode: boolean;\n\tinput: string;\n\toutput?: string;\n\tpassword: string;\n};\nexport const processFileWithPassword = async (\n\toptions: ProcessFileOptions,\n): Promise<void> => {\n\tconst { encode, input, output, password } = options;\n\tconst fileProcessor = encode ? encrypt : decrypt;\n\tconst data = await fs.readFile(input, DEFAULT_FILE_ENCODING);\n\n\tif (output) {\n\t\t// Save data to output file\n\t\tawait fs.outputFile(output, fileProcessor(password, data));\n\t} else {\n\t\t// Print to stdout directly\n\t\tlogger.log(fileProcessor(password, data));\n\t}\n};\n\n/* istanbul ignore next */\nexport const displayConfirmation = async (message: string) => {\n\tconst questions = {\n\t\tdefault: true,\n\t\tmessage: message || \"Do you want to continue?\",\n\t\tname: \"goodToGo\",\n\t\ttype: \"confirm\",\n\t};\n\tlogger.log();\n\tconst answers = await inquirer.prompt(questions);\n\treturn answers.goodToGo;\n};\n\n/* istanbul ignore next */\nexport const displayPromptWithPassword = async (message: string) => {\n\tconst questions = {\n\t\tmessage: message,\n\t\tname: \"password\",\n\t\ttype: \"password\",\n\t\tvalidate(value: string) {\n\t\t\tif (!value) {\n\t\t\t\treturn \"Password cannot be empty...\";\n\t\t\t}\n\t\t\treturn true;\n\t\t},\n\t};\n\tconst answers = await inquirer.prompt(questions);\n\treturn answers.password;\n};\n\n/* istanbul ignore next */\nexport const shouldContinue = (goodToGo: boolean) => {\n\tif (!goodToGo) {\n\t\tlogger.log(\"\\nBye then!\");\n\t\t// eslint-disable-next-line unicorn/no-process-exit\n\t\tprocess.exit(0);\n\t}\n\treturn true;\n};\n"],"names":["Logger","fs","inquirer","decrypt","encrypt","logger","boring","process","env","NODE_ENV","UTF8","DEFAULT_FILE_ENCODING","processFileWithPassword","options","encode","input","output","password","fileProcessor","data","readFile","outputFile","log","displayConfirmation","message","questions","default","name","type","answers","prompt","goodToGo","displayPromptWithPassword","validate","value","shouldContinue","exit"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,SAASA,MAAM,QAAQ,mBAAmB;AAC1C,OAAOC,QAAQ,WAAW;AAC1B,OAAOC,cAAc,WAAW;AAEhC,SAASC,OAAO,EAAEC,OAAO,QAAQ,WAAW;AAE5C,OAAO,MAAMC,SAAS,IAAIL,OAAO;IAChCM,QAAQC,QAAQC,GAAG,CAACC,QAAQ,KAAK;AAClC,GAAG;AAEH,MAAMC,OAAO;AACb,MAAMC,wBAAwBD;AAkB9B,OAAO,MAAME,0BAA0B,OACtCC;IAEA,MAAM,EAAEC,MAAM,EAAEC,KAAK,EAAEC,MAAM,EAAEC,QAAQ,EAAE,GAAGJ;IAC5C,MAAMK,gBAAgBJ,SAASV,UAAUD;IACzC,MAAMgB,OAAO,MAAMlB,GAAGmB,QAAQ,CAACL,OAAOJ;IAEtC,IAAIK,QAAQ;QACX,2BAA2B;QAC3B,MAAMf,GAAGoB,UAAU,CAACL,QAAQE,cAAcD,UAAUE;IACrD,OAAO;QACN,2BAA2B;QAC3Bd,OAAOiB,GAAG,CAACJ,cAAcD,UAAUE;IACpC;AACD,EAAE;AAEF,wBAAwB,GACxB,OAAO,MAAMI,sBAAsB,OAAOC;IACzC,MAAMC,YAAY;QACjBC,SAAS;QACTF,SAASA,WAAW;QACpBG,MAAM;QACNC,MAAM;IACP;IACAvB,OAAOiB,GAAG;IACV,MAAMO,UAAU,MAAM3B,SAAS4B,MAAM,CAACL;IACtC,OAAOI,QAAQE,QAAQ;AACxB,EAAE;AAEF,wBAAwB,GACxB,OAAO,MAAMC,4BAA4B,OAAOR;IAC/C,MAAMC,YAAY;QACjBD,SAASA;QACTG,MAAM;QACNC,MAAM;QACNK,UAASC,KAAa;YACrB,IAAI,CAACA,OAAO;gBACX,OAAO;YACR;YACA,OAAO;QACR;IACD;IACA,MAAML,UAAU,MAAM3B,SAAS4B,MAAM,CAACL;IACtC,OAAOI,QAAQZ,QAAQ;AACxB,EAAE;AAEF,wBAAwB,GACxB,OAAO,MAAMkB,iBAAiB,CAACJ;IAC9B,IAAI,CAACA,UAAU;QACd1B,OAAOiB,GAAG,CAAC;QACX,mDAAmD;QACnDf,QAAQ6B,IAAI,CAAC;IACd;IACA,OAAO;AACR,EAAE"}
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@node-cli/secret",
3
- "version": "1.0.7",
3
+ "version": "1.1.0",
4
4
  "license": "MIT",
5
5
  "author": "Arno Versini",
6
6
  "description": "Secret is a CLI tool that can encode or decode a file with a password",
7
7
  "type": "module",
8
8
  "types": "./dist/index.d.ts",
9
- "exports": "./dist/secret.js",
9
+ "exports": "./dist/lib.js",
10
10
  "bin": "dist/secret.js",
11
11
  "files": [
12
12
  "dist"
@@ -21,6 +21,7 @@
21
21
  "lint": "biome lint src",
22
22
  "test": "cross-env-shell NODE_OPTIONS=--experimental-vm-modules TZ=UTC jest",
23
23
  "test:coverage": "npm run test -- --coverage",
24
+ "test:watch": "npm run test -- --watch",
24
25
  "watch": "swc --strip-leading-paths --watch --out-dir dist src"
25
26
  },
26
27
  "dependencies": {
@@ -32,5 +33,5 @@
32
33
  "publishConfig": {
33
34
  "access": "public"
34
35
  },
35
- "gitHead": "e511453b89f180c52ca60e10bd1dfb3c8675cbec"
36
+ "gitHead": "323308c9e797d9626c06dafa2a5213dcd64e19bb"
36
37
  }