@typinghare/trick 2.0.1 → 2.1.1
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/LICENSE +22 -0
- package/README.md +175 -41
- package/dist/app.js +157 -65
- package/dist/color.d.ts +2 -0
- package/dist/color.js +7 -0
- package/dist/config.d.ts +45 -26
- package/dist/config.js +70 -29
- package/dist/console.d.ts +3 -0
- package/dist/console.js +9 -0
- package/dist/encrypt.d.ts +24 -32
- package/dist/encrypt.js +31 -38
- package/dist/error.d.ts +25 -6
- package/dist/error.js +36 -17
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/passphrase.d.ts +11 -3
- package/dist/passphrase.js +17 -11
- package/env.sh +1 -0
- package/package.json +27 -12
- package/src/app.ts +232 -155
- package/src/color.ts +9 -0
- package/src/config.ts +112 -69
- package/src/console.ts +11 -0
- package/src/encrypt.ts +104 -127
- package/src/error.ts +70 -57
- package/src/index.ts +3 -0
- package/src/passphrase.ts +21 -23
- package/test/resources/task_info.yml +4 -0
- package/test/resources/user_info.json +5 -0
- package/test/trick.config.json +16 -11
- package/test/resources/really.json +0 -4
- package/test/resources/task.yml +0 -3
package/dist/config.d.ts
CHANGED
|
@@ -3,21 +3,26 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export declare const CONFIG_FILE_NAME: string;
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* A list of root markers.
|
|
7
|
+
*/
|
|
8
|
+
export declare const ROOT_MARKERS: string[];
|
|
9
|
+
/**
|
|
10
|
+
* Represents Trick configuration type.
|
|
7
11
|
*
|
|
8
12
|
* @property targets Mapping from target names to target objects.
|
|
9
|
-
* @property
|
|
10
|
-
* @property
|
|
11
|
-
* @property
|
|
13
|
+
* @property trickRootDirectory The name of the Trick root directory under the project root.
|
|
14
|
+
* @property passphraseDirectory The path to the passphrase directory.
|
|
15
|
+
* @property defaultTargetNames A list of default target names. If no target name is specified
|
|
16
|
+
* when running Trick commands, these target names will be used.
|
|
12
17
|
* @property encryption Encryption configuration.
|
|
13
18
|
*/
|
|
14
19
|
export interface Config {
|
|
15
20
|
targets: {
|
|
16
21
|
[name: string]: Target;
|
|
17
22
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
23
|
+
trickRootDirectory: string;
|
|
24
|
+
passphraseDirectory: string;
|
|
25
|
+
defaultTargetNames: string[];
|
|
21
26
|
encryption: Encryption;
|
|
22
27
|
}
|
|
23
28
|
/**
|
|
@@ -31,44 +36,58 @@ export interface Target {
|
|
|
31
36
|
/**
|
|
32
37
|
* Encryption configuration.
|
|
33
38
|
*
|
|
34
|
-
* @property
|
|
39
|
+
* @property iterationCount The number of iteration.
|
|
35
40
|
*/
|
|
36
41
|
export interface Encryption {
|
|
37
|
-
|
|
42
|
+
iterationCount: number;
|
|
38
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Recursively searches for the root directory of a project based on specified root markers.
|
|
46
|
+
*
|
|
47
|
+
* @param directory - The current directory to check. Defaults to the current working directory.
|
|
48
|
+
* @returns The path to the root directory.
|
|
49
|
+
* @throws {RootDirectoryNotFoundError} If the root directory cannot be found.
|
|
50
|
+
*/
|
|
51
|
+
export declare function getRootDirectory(directory?: string | null): string;
|
|
52
|
+
/**
|
|
53
|
+
* Gets the full path to the configuration file based on the root directory.
|
|
54
|
+
*
|
|
55
|
+
* @return The full path to the configuration file.
|
|
56
|
+
* @throws {RootDirectoryNotFoundError} If the root directory cannot be found.
|
|
57
|
+
*/
|
|
58
|
+
export declare function getConfigFilePath(): string;
|
|
39
59
|
/**
|
|
40
60
|
* Writes a configuration object to the configuration file.
|
|
41
61
|
*
|
|
42
62
|
* @param config The configuration to write.
|
|
43
|
-
* @
|
|
44
|
-
*
|
|
63
|
+
* @param createInRoot Whether to create the configuration file in the root directory if it
|
|
64
|
+
* doesn't exist. Defaults to false.
|
|
65
|
+
* @throws {WriteConfigError} If error occurs when writing to the configuration file.
|
|
45
66
|
*/
|
|
46
|
-
export declare function writeConfig(config: Config):
|
|
67
|
+
export declare function writeConfig(config: Config, createInRoot?: boolean): void;
|
|
47
68
|
/**
|
|
48
69
|
* Retrieves the configuration object from the configuration file.
|
|
49
70
|
*
|
|
50
|
-
* @return The configuration object retrieved from the configuration object;
|
|
51
|
-
*
|
|
52
|
-
* @throws {ReadConfigError} If error occurs when reading the configuration
|
|
53
|
-
* file.
|
|
71
|
+
* @return The configuration object retrieved from the configuration object; null if the
|
|
72
|
+
* configuration file doesn't exist.
|
|
73
|
+
* @throws {ReadConfigError} If error occurs when reading the configuration file.
|
|
54
74
|
*/
|
|
55
|
-
export declare function readConfig():
|
|
75
|
+
export declare function readConfig(): Config | null;
|
|
56
76
|
/**
|
|
57
77
|
* Updates the configuration object.
|
|
58
78
|
*
|
|
59
|
-
* This function first retrieves the configuration object fromthe configuration
|
|
60
|
-
*
|
|
61
|
-
*
|
|
79
|
+
* This function first retrieves the configuration object fromthe configuration file. If the
|
|
80
|
+
* configuration file doesn't exist, the default configuration will be used instead.
|
|
81
|
+
*
|
|
82
|
+
* Then it calls the callback function by passing on the configuration object. If the callback
|
|
83
|
+
* function returns `true`, then the object will be written to the configuration file.
|
|
62
84
|
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
* the configuration file.
|
|
85
|
+
* @param callback The callback function taking the configuraition object retrieved from the
|
|
86
|
+
* configuration file.
|
|
66
87
|
*
|
|
67
|
-
* @param callback The callback function taking the configuraition object
|
|
68
|
-
* retrieved from the configuration file.
|
|
69
88
|
* @see DEFAULT_CONFIG
|
|
70
89
|
*/
|
|
71
|
-
export declare function updateConfig(callback: (Config: Config) => boolean | void):
|
|
90
|
+
export declare function updateConfig(callback: (Config: Config) => boolean | void, createInRoot?: boolean): void;
|
|
72
91
|
/**
|
|
73
92
|
* Gets a target object from a specified configuration object.
|
|
74
93
|
*
|
package/dist/config.js
CHANGED
|
@@ -1,31 +1,75 @@
|
|
|
1
1
|
import fsExtra from 'fs-extra';
|
|
2
|
-
import { ReadConfigError, TargetNotFoundError, WriteConfigError, } from './error.js';
|
|
2
|
+
import { ReadConfigError, TargetNotFoundError, WriteConfigError, RootDirectoryNotFoundError, } from './error.js';
|
|
3
|
+
import { join, resolve } from 'path';
|
|
3
4
|
/**
|
|
4
5
|
* The name of the configuration file to look for in the root directory.
|
|
5
6
|
*/
|
|
6
7
|
export const CONFIG_FILE_NAME = 'trick.config.json';
|
|
8
|
+
/**
|
|
9
|
+
* A list of root markers.
|
|
10
|
+
*/
|
|
11
|
+
export const ROOT_MARKERS = ['.git', CONFIG_FILE_NAME];
|
|
7
12
|
/**
|
|
8
13
|
* Default configuration.
|
|
9
14
|
*/
|
|
10
15
|
const DEFAULT_CONFIG = {
|
|
11
16
|
targets: {},
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
17
|
+
trickRootDirectory: '.trick',
|
|
18
|
+
passphraseDirectory: '~/.config/trick/passphrases',
|
|
19
|
+
defaultTargetNames: [],
|
|
15
20
|
encryption: {
|
|
16
|
-
|
|
21
|
+
iterationCount: 100_000,
|
|
17
22
|
},
|
|
18
23
|
};
|
|
24
|
+
/**
|
|
25
|
+
* Recursively searches for the root directory of a project based on specified root markers.
|
|
26
|
+
*
|
|
27
|
+
* @param directory - The current directory to check. Defaults to the current working directory.
|
|
28
|
+
* @returns The path to the root directory.
|
|
29
|
+
* @throws {RootDirectoryNotFoundError} If the root directory cannot be found.
|
|
30
|
+
*/
|
|
31
|
+
export function getRootDirectory(directory = null) {
|
|
32
|
+
if (!directory) {
|
|
33
|
+
return getRootDirectory(process.cwd());
|
|
34
|
+
}
|
|
35
|
+
if (directory === '/') {
|
|
36
|
+
throw new RootDirectoryNotFoundError();
|
|
37
|
+
}
|
|
38
|
+
for (const marker of ROOT_MARKERS) {
|
|
39
|
+
if (fsExtra.existsSync(join(directory, marker))) {
|
|
40
|
+
return directory;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return getRootDirectory(resolve(join(directory, '..')));
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Gets the full path to the configuration file based on the root directory.
|
|
47
|
+
*
|
|
48
|
+
* @return The full path to the configuration file.
|
|
49
|
+
* @throws {RootDirectoryNotFoundError} If the root directory cannot be found.
|
|
50
|
+
*/
|
|
51
|
+
export function getConfigFilePath() {
|
|
52
|
+
return resolve(getRootDirectory() + '/' + CONFIG_FILE_NAME);
|
|
53
|
+
}
|
|
19
54
|
/**
|
|
20
55
|
* Writes a configuration object to the configuration file.
|
|
21
56
|
*
|
|
22
57
|
* @param config The configuration to write.
|
|
23
|
-
* @
|
|
24
|
-
*
|
|
58
|
+
* @param createInRoot Whether to create the configuration file in the root directory if it
|
|
59
|
+
* doesn't exist. Defaults to false.
|
|
60
|
+
* @throws {WriteConfigError} If error occurs when writing to the configuration file.
|
|
25
61
|
*/
|
|
26
|
-
export
|
|
62
|
+
export function writeConfig(config, createInRoot = true) {
|
|
27
63
|
try {
|
|
28
|
-
|
|
64
|
+
if (createInRoot) {
|
|
65
|
+
const configFilePath = join(getRootDirectory(), CONFIG_FILE_NAME);
|
|
66
|
+
fsExtra.writeFileSync(configFilePath, JSON.stringify(config, null, 2));
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
// Always create in the current working directory
|
|
70
|
+
const configFilePath = join(process.cwd(), CONFIG_FILE_NAME);
|
|
71
|
+
fsExtra.writeFileSync(configFilePath, JSON.stringify(config, null, 2));
|
|
72
|
+
}
|
|
29
73
|
}
|
|
30
74
|
catch (err) {
|
|
31
75
|
throw new WriteConfigError(err);
|
|
@@ -34,17 +78,17 @@ export async function writeConfig(config) {
|
|
|
34
78
|
/**
|
|
35
79
|
* Retrieves the configuration object from the configuration file.
|
|
36
80
|
*
|
|
37
|
-
* @return The configuration object retrieved from the configuration object;
|
|
38
|
-
*
|
|
39
|
-
* @throws {ReadConfigError} If error occurs when reading the configuration
|
|
40
|
-
* file.
|
|
81
|
+
* @return The configuration object retrieved from the configuration object; null if the
|
|
82
|
+
* configuration file doesn't exist.
|
|
83
|
+
* @throws {ReadConfigError} If error occurs when reading the configuration file.
|
|
41
84
|
*/
|
|
42
|
-
export
|
|
43
|
-
|
|
85
|
+
export function readConfig() {
|
|
86
|
+
const configFilePath = getConfigFilePath();
|
|
87
|
+
if (!fsExtra.existsSync(configFilePath)) {
|
|
44
88
|
return null;
|
|
45
89
|
}
|
|
46
90
|
try {
|
|
47
|
-
return
|
|
91
|
+
return fsExtra.readJSONSync(configFilePath);
|
|
48
92
|
}
|
|
49
93
|
catch (err) {
|
|
50
94
|
throw new ReadConfigError(err);
|
|
@@ -53,23 +97,20 @@ export async function readConfig() {
|
|
|
53
97
|
/**
|
|
54
98
|
* Updates the configuration object.
|
|
55
99
|
*
|
|
56
|
-
* This function first retrieves the configuration object fromthe configuration
|
|
57
|
-
*
|
|
58
|
-
*
|
|
100
|
+
* This function first retrieves the configuration object fromthe configuration file. If the
|
|
101
|
+
* configuration file doesn't exist, the default configuration will be used instead.
|
|
102
|
+
*
|
|
103
|
+
* Then it calls the callback function by passing on the configuration object. If the callback
|
|
104
|
+
* function returns `true`, then the object will be written to the configuration file.
|
|
59
105
|
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* the configuration file.
|
|
106
|
+
* @param callback The callback function taking the configuraition object retrieved from the
|
|
107
|
+
* configuration file.
|
|
63
108
|
*
|
|
64
|
-
* @param callback The callback function taking the configuraition object
|
|
65
|
-
* retrieved from the configuration file.
|
|
66
109
|
* @see DEFAULT_CONFIG
|
|
67
110
|
*/
|
|
68
|
-
export
|
|
69
|
-
const config =
|
|
70
|
-
|
|
71
|
-
await writeConfig(config);
|
|
72
|
-
}
|
|
111
|
+
export function updateConfig(callback, createInRoot = true) {
|
|
112
|
+
const config = readConfig() || DEFAULT_CONFIG;
|
|
113
|
+
Boolean(callback(config)) && writeConfig(config, createInRoot);
|
|
73
114
|
}
|
|
74
115
|
/**
|
|
75
116
|
* Gets a target object from a specified configuration object.
|
package/dist/console.js
ADDED
package/dist/encrypt.d.ts
CHANGED
|
@@ -1,68 +1,60 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Encrypts a file using OpenSSL with AES-256-CBC and PBKDF2 key derivation.
|
|
3
3
|
*
|
|
4
|
-
* This function checks whether the source file exists, constructs an OpenSSL
|
|
5
|
-
*
|
|
6
|
-
* encryption command.
|
|
4
|
+
* This function checks whether the source file exists, constructs an OpenSSL command, ensures the
|
|
5
|
+
* destination directory exists, and then executes the encryption command.
|
|
7
6
|
*
|
|
8
7
|
* @param srcFilePath The path to the source file that needs to be encrypted.
|
|
9
8
|
* @param destFilePath The path where the encrypted file will be saved.
|
|
10
9
|
* @param passphrase The passphrase used for encryption.
|
|
11
|
-
* @param
|
|
10
|
+
* @param iterationCount The number of iterations to use for PBKDF2.
|
|
12
11
|
* @returns Resolves when the file is successfully encrypted.
|
|
13
|
-
* @throws {FailToEncryptFileError} If the source file does not exist or if
|
|
14
|
-
*
|
|
15
|
-
* @throws {FailToDecryptFileError} If an unknown error occurs during
|
|
16
|
-
* encryption.
|
|
12
|
+
* @throws {FailToEncryptFileError} If the source file does not exist or if OpenSSL returns an error
|
|
13
|
+
* during encryption.
|
|
14
|
+
* @throws {FailToDecryptFileError} If an unknown error occurs during encryption.
|
|
17
15
|
*/
|
|
18
|
-
export declare function encryptFile(srcFilePath: string, destFilePath: string, passphrase: string,
|
|
16
|
+
export declare function encryptFile(srcFilePath: string, destFilePath: string, passphrase: string, iterationCount: number): Promise<void>;
|
|
19
17
|
/**
|
|
20
18
|
* Decrypts a file using OpenSSL with AES-256-CBC and PBKDF2 key derivation.
|
|
21
19
|
*
|
|
22
|
-
* This function checks whether the encrypted file exists, constructs an OpenSSL
|
|
23
|
-
*
|
|
24
|
-
* executes the decryption command.
|
|
20
|
+
* This function checks whether the encrypted file exists, constructs an OpenSSL decryption command,
|
|
21
|
+
* ensures the destination directory exists, and then executes the decryption command.
|
|
25
22
|
*
|
|
26
23
|
* @param srcFilePath The path where the decrypted file will be saved.
|
|
27
24
|
* @param destFilePath The path to the encrypted source file.
|
|
28
25
|
* @param passphrase The passphrase used for decryption.
|
|
29
|
-
* @param
|
|
26
|
+
* @param iterationCount The number of iterations used for PBKDF2.
|
|
30
27
|
* @returns Resolves when the file is successfully decrypted.
|
|
31
|
-
* @throws {FailToDecryptFileError} If the encrypted file does not exist or if
|
|
32
|
-
*
|
|
33
|
-
* @throws {FailToDecryptFileError} If an unknown error occurs during
|
|
34
|
-
* decryption.
|
|
28
|
+
* @throws {FailToDecryptFileError} If the encrypted file does not exist or if OpenSSL returns an
|
|
29
|
+
* error during decryption.
|
|
30
|
+
* @throws {FailToDecryptFileError} If an unknown error occurs during decryption.
|
|
35
31
|
*/
|
|
36
|
-
export declare function decryptFile(srcFilePath: string, destFilePath: string, passphrase: string,
|
|
32
|
+
export declare function decryptFile(srcFilePath: string, destFilePath: string, passphrase: string, iterationCount: number): Promise<void>;
|
|
37
33
|
/**
|
|
38
|
-
* Encrypts multiple files using OpenSSL with AES-256-CBC and PBKDF2 key
|
|
39
|
-
* derivation.
|
|
34
|
+
* Encrypts multiple files using OpenSSL with AES-256-CBC and PBKDF2 key derivation.
|
|
40
35
|
*
|
|
41
|
-
* For each source file path provided, this function constructs the destination
|
|
42
|
-
*
|
|
43
|
-
* operation.
|
|
36
|
+
* For each source file path provided, this function constructs the destination file path by
|
|
37
|
+
* appending `.enc`, then calls `encryptFile` and logs the operation.
|
|
44
38
|
*
|
|
45
39
|
* @param srcFilePaths An array of file paths to be encrypted.
|
|
46
40
|
* @param destDir The directory where the encrypted files will be saved.
|
|
47
41
|
* @param passphrase The passphrase used for encryption.
|
|
48
|
-
* @param
|
|
42
|
+
* @param iterationCount The number of iterations to use for PBKDF2.
|
|
49
43
|
* @returns Resolves when all files are successfully encrypted.
|
|
50
44
|
* @throws {FailToEncryptFileError} If any file fails to encrypt.
|
|
51
45
|
*/
|
|
52
|
-
export declare function encryptFiles(srcFilePaths: string[], destDir: string, passphrase: string,
|
|
46
|
+
export declare function encryptFiles(srcFilePaths: string[], destDir: string, passphrase: string, iterationCount: number): Promise<void>;
|
|
53
47
|
/**
|
|
54
|
-
* Decrypts multiple files using OpenSSL with AES-256-CBC and PBKDF2 key
|
|
55
|
-
* derivation.
|
|
48
|
+
* Decrypts multiple files using OpenSSL with AES-256-CBC and PBKDF2 key derivation.
|
|
56
49
|
*
|
|
57
|
-
* For each source file path provided, this function assumes the corresponding
|
|
58
|
-
*
|
|
59
|
-
* operation.
|
|
50
|
+
* For each source file path provided, this function assumes the corresponding encrypted file has
|
|
51
|
+
* the `.enc` extension and calls `decryptFile`, logging the operation.
|
|
60
52
|
*
|
|
61
53
|
* @param srcFilePaths An array of original file paths that were encrypted.
|
|
62
54
|
* @param destDir The directory containing the encrypted files.
|
|
63
55
|
* @param passphrase The passphrase used for decryption.
|
|
64
|
-
* @param
|
|
56
|
+
* @param iterationCount The number of iterations used for PBKDF2.
|
|
65
57
|
* @returns Resolves when all files are successfully decrypted.
|
|
66
58
|
* @throws {FailToDecryptFileError} If any file fails to decrypt.
|
|
67
59
|
*/
|
|
68
|
-
export declare function decryptFiles(srcFilePaths: string[], destDir: string, passphrase: string,
|
|
60
|
+
export declare function decryptFiles(srcFilePaths: string[], destDir: string, passphrase: string, iterationCount: number): Promise<void>;
|
package/dist/encrypt.js
CHANGED
|
@@ -2,24 +2,23 @@ import { execa } from 'execa';
|
|
|
2
2
|
import * as path from 'node:path';
|
|
3
3
|
import fsExtra from 'fs-extra';
|
|
4
4
|
import { FailToDecryptFileError, FailToEncryptFileError } from './error.js';
|
|
5
|
+
import { success } from './console.js';
|
|
5
6
|
/**
|
|
6
7
|
* Encrypts a file using OpenSSL with AES-256-CBC and PBKDF2 key derivation.
|
|
7
8
|
*
|
|
8
|
-
* This function checks whether the source file exists, constructs an OpenSSL
|
|
9
|
-
*
|
|
10
|
-
* encryption command.
|
|
9
|
+
* This function checks whether the source file exists, constructs an OpenSSL command, ensures the
|
|
10
|
+
* destination directory exists, and then executes the encryption command.
|
|
11
11
|
*
|
|
12
12
|
* @param srcFilePath The path to the source file that needs to be encrypted.
|
|
13
13
|
* @param destFilePath The path where the encrypted file will be saved.
|
|
14
14
|
* @param passphrase The passphrase used for encryption.
|
|
15
|
-
* @param
|
|
15
|
+
* @param iterationCount The number of iterations to use for PBKDF2.
|
|
16
16
|
* @returns Resolves when the file is successfully encrypted.
|
|
17
|
-
* @throws {FailToEncryptFileError} If the source file does not exist or if
|
|
18
|
-
*
|
|
19
|
-
* @throws {FailToDecryptFileError} If an unknown error occurs during
|
|
20
|
-
* encryption.
|
|
17
|
+
* @throws {FailToEncryptFileError} If the source file does not exist or if OpenSSL returns an error
|
|
18
|
+
* during encryption.
|
|
19
|
+
* @throws {FailToDecryptFileError} If an unknown error occurs during encryption.
|
|
21
20
|
*/
|
|
22
|
-
export async function encryptFile(srcFilePath, destFilePath, passphrase,
|
|
21
|
+
export async function encryptFile(srcFilePath, destFilePath, passphrase, iterationCount) {
|
|
23
22
|
if (!(await fsExtra.pathExists(srcFilePath))) {
|
|
24
23
|
throw new FailToEncryptFileError(srcFilePath);
|
|
25
24
|
}
|
|
@@ -30,7 +29,7 @@ export async function encryptFile(srcFilePath, destFilePath, passphrase, iterati
|
|
|
30
29
|
'-salt',
|
|
31
30
|
'-pbkdf2',
|
|
32
31
|
'-iter',
|
|
33
|
-
Number(
|
|
32
|
+
Number(iterationCount),
|
|
34
33
|
'-in',
|
|
35
34
|
srcFilePath,
|
|
36
35
|
'-out',
|
|
@@ -55,21 +54,19 @@ export async function encryptFile(srcFilePath, destFilePath, passphrase, iterati
|
|
|
55
54
|
/**
|
|
56
55
|
* Decrypts a file using OpenSSL with AES-256-CBC and PBKDF2 key derivation.
|
|
57
56
|
*
|
|
58
|
-
* This function checks whether the encrypted file exists, constructs an OpenSSL
|
|
59
|
-
*
|
|
60
|
-
* executes the decryption command.
|
|
57
|
+
* This function checks whether the encrypted file exists, constructs an OpenSSL decryption command,
|
|
58
|
+
* ensures the destination directory exists, and then executes the decryption command.
|
|
61
59
|
*
|
|
62
60
|
* @param srcFilePath The path where the decrypted file will be saved.
|
|
63
61
|
* @param destFilePath The path to the encrypted source file.
|
|
64
62
|
* @param passphrase The passphrase used for decryption.
|
|
65
|
-
* @param
|
|
63
|
+
* @param iterationCount The number of iterations used for PBKDF2.
|
|
66
64
|
* @returns Resolves when the file is successfully decrypted.
|
|
67
|
-
* @throws {FailToDecryptFileError} If the encrypted file does not exist or if
|
|
68
|
-
*
|
|
69
|
-
* @throws {FailToDecryptFileError} If an unknown error occurs during
|
|
70
|
-
* decryption.
|
|
65
|
+
* @throws {FailToDecryptFileError} If the encrypted file does not exist or if OpenSSL returns an
|
|
66
|
+
* error during decryption.
|
|
67
|
+
* @throws {FailToDecryptFileError} If an unknown error occurs during decryption.
|
|
71
68
|
*/
|
|
72
|
-
export async function decryptFile(srcFilePath, destFilePath, passphrase,
|
|
69
|
+
export async function decryptFile(srcFilePath, destFilePath, passphrase, iterationCount) {
|
|
73
70
|
if (!(await fsExtra.pathExists(destFilePath))) {
|
|
74
71
|
throw new FailToDecryptFileError(destFilePath);
|
|
75
72
|
}
|
|
@@ -81,7 +78,7 @@ export async function decryptFile(srcFilePath, destFilePath, passphrase, iterati
|
|
|
81
78
|
'-salt',
|
|
82
79
|
'-pbkdf2',
|
|
83
80
|
'-iter',
|
|
84
|
-
Number(
|
|
81
|
+
Number(iterationCount),
|
|
85
82
|
'-in',
|
|
86
83
|
destFilePath,
|
|
87
84
|
'-out',
|
|
@@ -104,46 +101,42 @@ export async function decryptFile(srcFilePath, destFilePath, passphrase, iterati
|
|
|
104
101
|
}
|
|
105
102
|
}
|
|
106
103
|
/**
|
|
107
|
-
* Encrypts multiple files using OpenSSL with AES-256-CBC and PBKDF2 key
|
|
108
|
-
* derivation.
|
|
104
|
+
* Encrypts multiple files using OpenSSL with AES-256-CBC and PBKDF2 key derivation.
|
|
109
105
|
*
|
|
110
|
-
* For each source file path provided, this function constructs the destination
|
|
111
|
-
*
|
|
112
|
-
* operation.
|
|
106
|
+
* For each source file path provided, this function constructs the destination file path by
|
|
107
|
+
* appending `.enc`, then calls `encryptFile` and logs the operation.
|
|
113
108
|
*
|
|
114
109
|
* @param srcFilePaths An array of file paths to be encrypted.
|
|
115
110
|
* @param destDir The directory where the encrypted files will be saved.
|
|
116
111
|
* @param passphrase The passphrase used for encryption.
|
|
117
|
-
* @param
|
|
112
|
+
* @param iterationCount The number of iterations to use for PBKDF2.
|
|
118
113
|
* @returns Resolves when all files are successfully encrypted.
|
|
119
114
|
* @throws {FailToEncryptFileError} If any file fails to encrypt.
|
|
120
115
|
*/
|
|
121
|
-
export async function encryptFiles(srcFilePaths, destDir, passphrase,
|
|
116
|
+
export async function encryptFiles(srcFilePaths, destDir, passphrase, iterationCount) {
|
|
122
117
|
for (const srcFilePath of srcFilePaths) {
|
|
123
118
|
const destFilePath = path.join(destDir, srcFilePath + '.enc');
|
|
124
|
-
await encryptFile(srcFilePath, destFilePath, passphrase,
|
|
125
|
-
console.log(`
|
|
119
|
+
await encryptFile(srcFilePath, destFilePath, passphrase, iterationCount);
|
|
120
|
+
console.log(success(`Encrypted: ${srcFilePath} -> ${destFilePath}`));
|
|
126
121
|
}
|
|
127
122
|
}
|
|
128
123
|
/**
|
|
129
|
-
* Decrypts multiple files using OpenSSL with AES-256-CBC and PBKDF2 key
|
|
130
|
-
* derivation.
|
|
124
|
+
* Decrypts multiple files using OpenSSL with AES-256-CBC and PBKDF2 key derivation.
|
|
131
125
|
*
|
|
132
|
-
* For each source file path provided, this function assumes the corresponding
|
|
133
|
-
*
|
|
134
|
-
* operation.
|
|
126
|
+
* For each source file path provided, this function assumes the corresponding encrypted file has
|
|
127
|
+
* the `.enc` extension and calls `decryptFile`, logging the operation.
|
|
135
128
|
*
|
|
136
129
|
* @param srcFilePaths An array of original file paths that were encrypted.
|
|
137
130
|
* @param destDir The directory containing the encrypted files.
|
|
138
131
|
* @param passphrase The passphrase used for decryption.
|
|
139
|
-
* @param
|
|
132
|
+
* @param iterationCount The number of iterations used for PBKDF2.
|
|
140
133
|
* @returns Resolves when all files are successfully decrypted.
|
|
141
134
|
* @throws {FailToDecryptFileError} If any file fails to decrypt.
|
|
142
135
|
*/
|
|
143
|
-
export async function decryptFiles(srcFilePaths, destDir, passphrase,
|
|
136
|
+
export async function decryptFiles(srcFilePaths, destDir, passphrase, iterationCount) {
|
|
144
137
|
for (const srcFilePath of srcFilePaths) {
|
|
145
138
|
const destFilePath = path.join(destDir, srcFilePath + '.enc');
|
|
146
|
-
await decryptFile(srcFilePath, destFilePath, passphrase,
|
|
147
|
-
console.log(`
|
|
139
|
+
await decryptFile(srcFilePath, destFilePath, passphrase, iterationCount);
|
|
140
|
+
console.log(success(`Decrypted: ${destFilePath} -> ${srcFilePath}`));
|
|
148
141
|
}
|
|
149
142
|
}
|
package/dist/error.d.ts
CHANGED
|
@@ -1,30 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown when the root directory is not found.
|
|
3
|
+
*/
|
|
4
|
+
export declare class RootDirectoryNotFoundError extends Error {
|
|
5
|
+
constructor();
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Thrown when fail to write the configuration file.
|
|
9
|
+
*/
|
|
1
10
|
export declare class WriteConfigError extends Error {
|
|
2
11
|
constructor(err: any);
|
|
3
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Thrown when fail to read the configuration file.
|
|
15
|
+
*/
|
|
4
16
|
export declare class ReadConfigError extends Error {
|
|
5
17
|
constructor(err: any);
|
|
6
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Thrown when the specified target is not found in the configuration.
|
|
21
|
+
*/
|
|
7
22
|
export declare class TargetNotFoundError extends Error {
|
|
8
23
|
readonly targetName: string;
|
|
9
24
|
constructor(targetName: string);
|
|
10
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Thrown when fail to encrypt the source file.
|
|
28
|
+
*/
|
|
11
29
|
export declare class FailToEncryptFileError extends Error {
|
|
12
30
|
readonly srcFilePath: string;
|
|
13
31
|
readonly opensslErrMessage?: string | undefined;
|
|
14
32
|
constructor(srcFilePath: string, opensslErrMessage?: string | undefined);
|
|
15
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Thrown when fail to decrypt the source file.
|
|
36
|
+
*/
|
|
16
37
|
export declare class FailToDecryptFileError extends Error {
|
|
17
38
|
readonly destFilePath: string;
|
|
18
39
|
readonly opensslErrMessage?: string | undefined;
|
|
19
40
|
constructor(destFilePath: string, opensslErrMessage?: string | undefined);
|
|
20
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Thrown when the passphrase file is not found.
|
|
44
|
+
*/
|
|
21
45
|
export declare class PassphraseFileNotFoundError extends Error {
|
|
22
46
|
readonly passphraseFilePath: string;
|
|
23
47
|
constructor(passphraseFilePath: string);
|
|
24
48
|
}
|
|
25
|
-
export declare
|
|
26
|
-
readonly passphraseFilePath: string;
|
|
27
|
-
readonly targetName: string;
|
|
28
|
-
constructor(passphraseFilePath: string, targetName: string);
|
|
29
|
-
}
|
|
30
|
-
export declare function resolve_error(err: any): void;
|
|
49
|
+
export declare function resolveError(err: any): void;
|
package/dist/error.js
CHANGED
|
@@ -1,14 +1,31 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { error, warning } from './console.js';
|
|
2
|
+
/**
|
|
3
|
+
* Thrown when the root directory is not found.
|
|
4
|
+
*/
|
|
5
|
+
export class RootDirectoryNotFoundError extends Error {
|
|
6
|
+
constructor() {
|
|
7
|
+
super('Root directory not found');
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Thrown when fail to write the configuration file.
|
|
12
|
+
*/
|
|
2
13
|
export class WriteConfigError extends Error {
|
|
3
14
|
constructor(err) {
|
|
4
15
|
super(`Fail to write the configuration file. ${err}`);
|
|
5
16
|
}
|
|
6
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Thrown when fail to read the configuration file.
|
|
20
|
+
*/
|
|
7
21
|
export class ReadConfigError extends Error {
|
|
8
22
|
constructor(err) {
|
|
9
23
|
super(`Fail to read the configuration file: ${err}`);
|
|
10
24
|
}
|
|
11
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Thrown when the specified target is not found in the configuration.
|
|
28
|
+
*/
|
|
12
29
|
export class TargetNotFoundError extends Error {
|
|
13
30
|
targetName;
|
|
14
31
|
constructor(targetName) {
|
|
@@ -16,6 +33,9 @@ export class TargetNotFoundError extends Error {
|
|
|
16
33
|
this.targetName = targetName;
|
|
17
34
|
}
|
|
18
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Thrown when fail to encrypt the source file.
|
|
38
|
+
*/
|
|
19
39
|
export class FailToEncryptFileError extends Error {
|
|
20
40
|
srcFilePath;
|
|
21
41
|
opensslErrMessage;
|
|
@@ -25,6 +45,9 @@ export class FailToEncryptFileError extends Error {
|
|
|
25
45
|
this.opensslErrMessage = opensslErrMessage;
|
|
26
46
|
}
|
|
27
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Thrown when fail to decrypt the source file.
|
|
50
|
+
*/
|
|
28
51
|
export class FailToDecryptFileError extends Error {
|
|
29
52
|
destFilePath;
|
|
30
53
|
opensslErrMessage;
|
|
@@ -34,6 +57,9 @@ export class FailToDecryptFileError extends Error {
|
|
|
34
57
|
this.opensslErrMessage = opensslErrMessage;
|
|
35
58
|
}
|
|
36
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Thrown when the passphrase file is not found.
|
|
62
|
+
*/
|
|
37
63
|
export class PassphraseFileNotFoundError extends Error {
|
|
38
64
|
passphraseFilePath;
|
|
39
65
|
constructor(passphraseFilePath) {
|
|
@@ -41,29 +67,22 @@ export class PassphraseFileNotFoundError extends Error {
|
|
|
41
67
|
this.passphraseFilePath = passphraseFilePath;
|
|
42
68
|
}
|
|
43
69
|
}
|
|
44
|
-
export
|
|
45
|
-
passphraseFilePath;
|
|
46
|
-
targetName;
|
|
47
|
-
constructor(passphraseFilePath, targetName) {
|
|
48
|
-
super(`Passphrase for target ${targetName} is not found in the passphrase file: ${passphraseFilePath}`);
|
|
49
|
-
this.passphraseFilePath = passphraseFilePath;
|
|
50
|
-
this.targetName = targetName;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
export function resolve_error(err) {
|
|
70
|
+
export function resolveError(err) {
|
|
54
71
|
if (!(err instanceof Error)) {
|
|
55
|
-
console.error(`Unknown error: ${err}`);
|
|
72
|
+
console.error(error(`Unknown error: ${err}`));
|
|
56
73
|
process.exit(2);
|
|
57
74
|
}
|
|
58
75
|
if (err.message) {
|
|
59
|
-
console.
|
|
76
|
+
console.error(error(err.message));
|
|
77
|
+
}
|
|
78
|
+
if (err instanceof PassphraseFileNotFoundError) {
|
|
79
|
+
console.error(warning(`Use the "set-passphrase" command to set the passphrase file.`));
|
|
60
80
|
}
|
|
61
|
-
if (err instanceof FailToEncryptFileError ||
|
|
62
|
-
err instanceof FailToDecryptFileError) {
|
|
81
|
+
if (err instanceof FailToEncryptFileError || err instanceof FailToDecryptFileError) {
|
|
63
82
|
if (err.opensslErrMessage) {
|
|
64
|
-
console.error(
|
|
83
|
+
console.error(error(err.opensslErrMessage));
|
|
65
84
|
}
|
|
66
|
-
console.error(
|
|
85
|
+
console.error(warning('Make sure the file exists and you have enough permission to access it.'));
|
|
67
86
|
}
|
|
68
87
|
process.exit(1);
|
|
69
88
|
}
|