@typinghare/trick 2.1.0 → 2.1.2

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/dist/app.js CHANGED
@@ -8,7 +8,7 @@ import path from 'path';
8
8
  import { colorFilePath, colorTargetName } from './color.js';
9
9
  import { success, warning } from './console.js';
10
10
  const program = new Command();
11
- program.version('2.1.0');
11
+ program.version('2.1.2');
12
12
  program.description('Save credential files to remote safely and easily.');
13
13
  program
14
14
  .command('config')
@@ -113,13 +113,11 @@ program
113
113
  return;
114
114
  }
115
115
  const rootDirectory = getRootDirectory();
116
- const trickRootDirectory = path.resolve(rootDirectory, config.trickRootDirectory);
117
116
  for (const targetName of targetNames) {
118
117
  const target = getTargetFromConfig(config, targetName);
119
118
  const passphrase = getPassphrase(config, targetName);
120
119
  const srcFilePaths = target.files;
121
- fsExtra.ensureDir(trickRootDirectory);
122
- encryptFiles(srcFilePaths, trickRootDirectory, passphrase, config.encryption.iterationCount);
120
+ encryptFiles(config, rootDirectory, srcFilePaths, passphrase);
123
121
  }
124
122
  });
125
123
  });
@@ -137,13 +135,11 @@ program
137
135
  return;
138
136
  }
139
137
  const rootDirectory = getRootDirectory();
140
- const trickRootDirectory = path.resolve(rootDirectory, config.trickRootDirectory);
141
138
  for (const targetName of targetNames) {
142
139
  const target = getTargetFromConfig(config, targetName);
143
140
  const passphrase = getPassphrase(config, targetName);
144
141
  const srcFilePaths = target.files;
145
- fsExtra.ensureDir(trickRootDirectory);
146
- decryptFiles(srcFilePaths, trickRootDirectory, passphrase, config.encryption.iterationCount);
142
+ decryptFiles(config, rootDirectory, srcFilePaths, passphrase);
147
143
  }
148
144
  });
149
145
  });
@@ -172,7 +168,7 @@ program
172
168
  });
173
169
  program
174
170
  .command('list-defaults')
175
- .description('Display the default target name.')
171
+ .description('Display the default target name.')
176
172
  .action(function () {
177
173
  updateConfig((config) => {
178
174
  for (const targetName of config.defaultTargetNames) {
@@ -202,17 +198,17 @@ program
202
198
  const passphraseDirectory = getPassphraseDirectory(config);
203
199
  if (!fsExtra.existsSync(passphraseDirectory)) {
204
200
  fsExtra.ensureDirSync(passphraseDirectory);
205
- console.log(success(`Created passphrase directory: ${passphraseDirectory}`));
201
+ console.log(success(`Created passphrase directory: ${colorFilePath(passphraseDirectory)}`));
206
202
  }
207
203
  const passphraseFile = path.join(passphraseDirectory, targetName);
208
204
  if (!fsExtra.existsSync(passphraseFile)) {
209
205
  fsExtra.createFileSync(passphraseFile);
210
206
  fsExtra.chmodSync(passphraseFile, 0o600);
211
- console.log(success(`Created passphrase file: ${passphraseFile}`));
207
+ console.log(success(`Created passphrase file: ${colorFilePath(passphraseFile)}`));
212
208
  console.log(success(`You have to edit the file to set the passphrase.`));
213
209
  }
214
210
  else {
215
- console.log(warning(`Passphrase file already exists: ${passphraseFile}`));
211
+ console.log(warning(`Passphrase file already exists: ${colorFilePath(passphraseFile)}`));
216
212
  }
217
213
  });
218
214
  });
package/dist/color.d.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export declare function colorTargetName(targetName: string): string;
2
2
  export declare function colorFilePath(filePath: string): string;
3
+ export declare function colorSourceFilePath(filePath: string): string;
4
+ export declare function colorTargetFilePath(message: string): string;
package/dist/color.js CHANGED
@@ -5,3 +5,9 @@ export function colorTargetName(targetName) {
5
5
  export function colorFilePath(filePath) {
6
6
  return chalk.yellow(filePath);
7
7
  }
8
+ export function colorSourceFilePath(filePath) {
9
+ return chalk.italic.green(filePath);
10
+ }
11
+ export function colorTargetFilePath(message) {
12
+ return chalk.italic.magenta(message);
13
+ }
package/dist/encrypt.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Config } from './config.js';
1
2
  /**
2
3
  * Encrypts a file using OpenSSL with AES-256-CBC and PBKDF2 key derivation.
3
4
  *
@@ -36,25 +37,24 @@ export declare function decryptFile(srcFilePath: string, destFilePath: string, p
36
37
  * For each source file path provided, this function constructs the destination file path by
37
38
  * appending `.enc`, then calls `encryptFile` and logs the operation.
38
39
  *
40
+ * @param config The configuration object.
41
+ * @param rootDir The root directory where the trick configuration is located.
39
42
  * @param srcFilePaths An array of file paths to be encrypted.
40
- * @param destDir The directory where the encrypted files will be saved.
41
43
  * @param passphrase The passphrase used for encryption.
42
- * @param iterationCount The number of iterations to use for PBKDF2.
43
44
  * @returns Resolves when all files are successfully encrypted.
44
45
  * @throws {FailToEncryptFileError} If any file fails to encrypt.
45
46
  */
46
- export declare function encryptFiles(srcFilePaths: string[], destDir: string, passphrase: string, iterationCount: number): Promise<void>;
47
- /**
47
+ export declare function encryptFiles(config: Config, rootDir: string, srcFilePaths: string[], passphrase: string): Promise<void>; /**
48
48
  * Decrypts multiple files using OpenSSL with AES-256-CBC and PBKDF2 key derivation.
49
49
  *
50
50
  * For each source file path provided, this function assumes the corresponding encrypted file has
51
51
  * the `.enc` extension and calls `decryptFile`, logging the operation.
52
52
  *
53
+ * @param config The configuration object.
54
+ * @param rootDir The root directory where the trick configuration is located.
53
55
  * @param srcFilePaths An array of original file paths that were encrypted.
54
- * @param destDir The directory containing the encrypted files.
55
56
  * @param passphrase The passphrase used for decryption.
56
- * @param iterationCount The number of iterations used for PBKDF2.
57
57
  * @returns Resolves when all files are successfully decrypted.
58
58
  * @throws {FailToDecryptFileError} If any file fails to decrypt.
59
59
  */
60
- export declare function decryptFiles(srcFilePaths: string[], destDir: string, passphrase: string, iterationCount: number): Promise<void>;
60
+ export declare function decryptFiles(config: Config, rootDir: string, srcFilePaths: string[], passphrase: string): Promise<void>;
package/dist/encrypt.js CHANGED
@@ -3,6 +3,7 @@ import * as path from 'node:path';
3
3
  import fsExtra from 'fs-extra';
4
4
  import { FailToDecryptFileError, FailToEncryptFileError } from './error.js';
5
5
  import { success } from './console.js';
6
+ import { colorSourceFilePath, colorTargetFilePath } from './color.js';
6
7
  /**
7
8
  * Encrypts a file using OpenSSL with AES-256-CBC and PBKDF2 key derivation.
8
9
  *
@@ -106,37 +107,45 @@ export async function decryptFile(srcFilePath, destFilePath, passphrase, iterati
106
107
  * For each source file path provided, this function constructs the destination file path by
107
108
  * appending `.enc`, then calls `encryptFile` and logs the operation.
108
109
  *
110
+ * @param config The configuration object.
111
+ * @param rootDir The root directory where the trick configuration is located.
109
112
  * @param srcFilePaths An array of file paths to be encrypted.
110
- * @param destDir The directory where the encrypted files will be saved.
111
113
  * @param passphrase The passphrase used for encryption.
112
- * @param iterationCount The number of iterations to use for PBKDF2.
113
114
  * @returns Resolves when all files are successfully encrypted.
114
115
  * @throws {FailToEncryptFileError} If any file fails to encrypt.
115
116
  */
116
- export async function encryptFiles(srcFilePaths, destDir, passphrase, iterationCount) {
117
+ export async function encryptFiles(config, rootDir, srcFilePaths, passphrase) {
118
+ const iterationCount = config.encryption.iterationCount;
119
+ const trickRootDirectory = config.trickRootDirectory;
120
+ fsExtra.ensureDir(path.join(rootDir, trickRootDirectory));
117
121
  for (const srcFilePath of srcFilePaths) {
118
- const destFilePath = path.join(destDir, srcFilePath + '.enc');
119
- await encryptFile(srcFilePath, destFilePath, passphrase, iterationCount);
120
- console.log(success(`Encrypted: ${srcFilePath} -> ${destFilePath}`));
122
+ const destFilePath = path.join(trickRootDirectory, srcFilePath + '.enc');
123
+ const absoluteDestFilePath = path.join(rootDir, destFilePath);
124
+ const absoluteSrcFilePath = path.resolve(rootDir, srcFilePath);
125
+ await encryptFile(absoluteSrcFilePath, absoluteDestFilePath, passphrase, iterationCount);
126
+ console.log(success(`Encrypted: ${colorSourceFilePath(srcFilePath)} -> ${colorTargetFilePath(destFilePath)}`));
121
127
  }
122
- }
123
- /**
128
+ } /**
124
129
  * Decrypts multiple files using OpenSSL with AES-256-CBC and PBKDF2 key derivation.
125
130
  *
126
131
  * For each source file path provided, this function assumes the corresponding encrypted file has
127
132
  * the `.enc` extension and calls `decryptFile`, logging the operation.
128
133
  *
134
+ * @param config The configuration object.
135
+ * @param rootDir The root directory where the trick configuration is located.
129
136
  * @param srcFilePaths An array of original file paths that were encrypted.
130
- * @param destDir The directory containing the encrypted files.
131
137
  * @param passphrase The passphrase used for decryption.
132
- * @param iterationCount The number of iterations used for PBKDF2.
133
138
  * @returns Resolves when all files are successfully decrypted.
134
139
  * @throws {FailToDecryptFileError} If any file fails to decrypt.
135
140
  */
136
- export async function decryptFiles(srcFilePaths, destDir, passphrase, iterationCount) {
141
+ export async function decryptFiles(config, rootDir, srcFilePaths, passphrase) {
142
+ const iterationCount = config.encryption.iterationCount;
143
+ const trickRootDirectory = config.trickRootDirectory;
137
144
  for (const srcFilePath of srcFilePaths) {
138
- const destFilePath = path.join(destDir, srcFilePath + '.enc');
139
- await decryptFile(srcFilePath, destFilePath, passphrase, iterationCount);
140
- console.log(success(`Decrypted: ${destFilePath} -> ${srcFilePath}`));
145
+ const destFilePath = path.join(trickRootDirectory, srcFilePath + '.enc');
146
+ const absoluteDestFilePath = path.join(rootDir, destFilePath);
147
+ const absoluteSrcFilePath = path.resolve(rootDir, srcFilePath);
148
+ await decryptFile(absoluteSrcFilePath, absoluteDestFilePath, passphrase, iterationCount);
149
+ console.log(success(`Decrypted: ${colorTargetFilePath(destFilePath)} -> ${colorSourceFilePath(srcFilePath)}`));
141
150
  }
142
151
  }
package/package.json CHANGED
@@ -1,19 +1,37 @@
1
1
  {
2
2
  "name": "@typinghare/trick",
3
3
  "description": "Save credential files to remote safely and easily.",
4
- "version": "2.1.0",
4
+ "version": "2.1.2",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "type": "module",
8
8
  "bin": {
9
9
  "trick": "bin/trick"
10
10
  },
11
+ "scripts": {
12
+ "build": "node_modules/typescript/bin/tsc"
13
+ },
11
14
  "author": {
12
15
  "name": "James Chen",
13
16
  "email": "jameschan312.cn@gmail.com"
14
17
  },
15
18
  "homepage": "https://github.com/typinghare/trick",
16
19
  "license": "MIT",
20
+ "keywords": [
21
+ "cli",
22
+ "credentials",
23
+ "secrets",
24
+ "secret-management",
25
+ "config",
26
+ "dotenv",
27
+ "security",
28
+ "encryption",
29
+ "remote-storage",
30
+ "backup",
31
+ "sync",
32
+ "devops",
33
+ "tool"
34
+ ],
17
35
  "dependencies": {
18
36
  "chalk": "^5.6.2",
19
37
  "commander": "^14.0.2",
@@ -36,7 +54,5 @@
36
54
  "endOfLine": "lf",
37
55
  "printWidth": 100
38
56
  },
39
- "scripts": {
40
- "build": "node_modules/typescript/bin/tsc"
41
- }
42
- }
57
+ "packageManager": "pnpm@10.27.0"
58
+ }
package/src/app.ts CHANGED
@@ -15,7 +15,7 @@ import { colorFilePath, colorTargetName } from './color.js'
15
15
  import { success, warning } from './console.js'
16
16
 
17
17
  const program = new Command()
18
- program.version('2.1.0')
18
+ program.version('2.1.2')
19
19
  program.description('Save credential files to remote safely and easily.')
20
20
 
21
21
  program
@@ -136,14 +136,12 @@ program
136
136
  }
137
137
 
138
138
  const rootDirectory = getRootDirectory()
139
- const trickRootDirectory = path.resolve(rootDirectory, config.trickRootDirectory)
140
139
  for (const targetName of targetNames) {
141
140
  const target: Target = getTargetFromConfig(config, targetName)
142
141
  const passphrase: string = getPassphrase(config, targetName)
143
142
  const srcFilePaths: string[] = target.files
144
143
 
145
- fsExtra.ensureDir(trickRootDirectory)
146
- encryptFiles(srcFilePaths, trickRootDirectory, passphrase, config.encryption.iterationCount)
144
+ encryptFiles(config, rootDirectory, srcFilePaths, passphrase)
147
145
  }
148
146
  })
149
147
  })
@@ -164,14 +162,12 @@ program
164
162
  }
165
163
 
166
164
  const rootDirectory = getRootDirectory()
167
- const trickRootDirectory = path.resolve(rootDirectory, config.trickRootDirectory)
168
165
  for (const targetName of targetNames) {
169
166
  const target: Target = getTargetFromConfig(config, targetName)
170
167
  const passphrase: string = getPassphrase(config, targetName)
171
168
  const srcFilePaths: string[] = target.files
172
169
 
173
- fsExtra.ensureDir(trickRootDirectory)
174
- decryptFiles(srcFilePaths, trickRootDirectory, passphrase, config.encryption.iterationCount)
170
+ decryptFiles(config, rootDirectory, srcFilePaths, passphrase)
175
171
  }
176
172
  })
177
173
  })
@@ -205,7 +201,7 @@ program
205
201
 
206
202
  program
207
203
  .command('list-defaults')
208
- .description('Display the default target name.')
204
+ .description('Display the default target name.')
209
205
  .action(function (): void {
210
206
  updateConfig((config) => {
211
207
  for (const targetName of config.defaultTargetNames) {
@@ -237,17 +233,17 @@ program
237
233
  const passphraseDirectory = getPassphraseDirectory(config)
238
234
  if (!fsExtra.existsSync(passphraseDirectory)) {
239
235
  fsExtra.ensureDirSync(passphraseDirectory)
240
- console.log(success(`Created passphrase directory: ${passphraseDirectory}`))
236
+ console.log(success(`Created passphrase directory: ${colorFilePath(passphraseDirectory)}`))
241
237
  }
242
238
 
243
239
  const passphraseFile = path.join(passphraseDirectory, targetName)
244
240
  if (!fsExtra.existsSync(passphraseFile)) {
245
241
  fsExtra.createFileSync(passphraseFile)
246
242
  fsExtra.chmodSync(passphraseFile, 0o600)
247
- console.log(success(`Created passphrase file: ${passphraseFile}`))
243
+ console.log(success(`Created passphrase file: ${colorFilePath(passphraseFile)}`))
248
244
  console.log(success(`You have to edit the file to set the passphrase.`))
249
245
  } else {
250
- console.log(warning(`Passphrase file already exists: ${passphraseFile}`))
246
+ console.log(warning(`Passphrase file already exists: ${colorFilePath(passphraseFile)}`))
251
247
  }
252
248
  })
253
249
  })
package/src/color.ts CHANGED
@@ -7,3 +7,11 @@ export function colorTargetName(targetName: string): string {
7
7
  export function colorFilePath(filePath: string): string {
8
8
  return chalk.yellow(filePath)
9
9
  }
10
+
11
+ export function colorSourceFilePath(filePath: string): string {
12
+ return chalk.italic.green(filePath)
13
+ }
14
+
15
+ export function colorTargetFilePath(message: string): string {
16
+ return chalk.italic.magenta(message)
17
+ }
package/src/encrypt.ts CHANGED
@@ -3,6 +3,8 @@ import * as path from 'node:path'
3
3
  import fsExtra from 'fs-extra'
4
4
  import { FailToDecryptFileError, FailToEncryptFileError } from './error.js'
5
5
  import { success } from './console.js'
6
+ import { colorSourceFilePath, colorTargetFilePath } from './color.js'
7
+ import { Config } from './config.js'
6
8
 
7
9
  /**
8
10
  * Encrypts a file using OpenSSL with AES-256-CBC and PBKDF2 key derivation.
@@ -121,48 +123,65 @@ export async function decryptFile(
121
123
  * For each source file path provided, this function constructs the destination file path by
122
124
  * appending `.enc`, then calls `encryptFile` and logs the operation.
123
125
  *
126
+ * @param config The configuration object.
127
+ * @param rootDir The root directory where the trick configuration is located.
124
128
  * @param srcFilePaths An array of file paths to be encrypted.
125
- * @param destDir The directory where the encrypted files will be saved.
126
129
  * @param passphrase The passphrase used for encryption.
127
- * @param iterationCount The number of iterations to use for PBKDF2.
128
130
  * @returns Resolves when all files are successfully encrypted.
129
131
  * @throws {FailToEncryptFileError} If any file fails to encrypt.
130
132
  */
131
133
  export async function encryptFiles(
134
+ config: Config,
135
+ rootDir: string,
132
136
  srcFilePaths: string[],
133
- destDir: string,
134
- passphrase: string,
135
- iterationCount: number
137
+ passphrase: string
136
138
  ): Promise<void> {
139
+ const iterationCount: number = config.encryption.iterationCount
140
+ const trickRootDirectory: string = config.trickRootDirectory
141
+
142
+ fsExtra.ensureDir(path.join(rootDir, trickRootDirectory))
137
143
  for (const srcFilePath of srcFilePaths) {
138
- const destFilePath: string = path.join(destDir, srcFilePath + '.enc')
139
- await encryptFile(srcFilePath, destFilePath, passphrase, iterationCount)
140
- console.log(success(`Encrypted: ${srcFilePath} -> ${destFilePath}`))
144
+ const destFilePath: string = path.join(trickRootDirectory, srcFilePath + '.enc')
145
+ const absoluteDestFilePath: string = path.join(rootDir, destFilePath)
146
+ const absoluteSrcFilePath: string = path.resolve(rootDir, srcFilePath)
147
+ await encryptFile(absoluteSrcFilePath, absoluteDestFilePath, passphrase, iterationCount)
148
+ console.log(
149
+ success(
150
+ `Encrypted: ${colorSourceFilePath(srcFilePath)} -> ${colorTargetFilePath(destFilePath)}`
151
+ )
152
+ )
141
153
  }
142
- }
143
-
144
- /**
154
+ } /**
145
155
  * Decrypts multiple files using OpenSSL with AES-256-CBC and PBKDF2 key derivation.
146
156
  *
147
157
  * For each source file path provided, this function assumes the corresponding encrypted file has
148
158
  * the `.enc` extension and calls `decryptFile`, logging the operation.
149
159
  *
160
+ * @param config The configuration object.
161
+ * @param rootDir The root directory where the trick configuration is located.
150
162
  * @param srcFilePaths An array of original file paths that were encrypted.
151
- * @param destDir The directory containing the encrypted files.
152
163
  * @param passphrase The passphrase used for decryption.
153
- * @param iterationCount The number of iterations used for PBKDF2.
154
164
  * @returns Resolves when all files are successfully decrypted.
155
165
  * @throws {FailToDecryptFileError} If any file fails to decrypt.
156
166
  */
157
167
  export async function decryptFiles(
168
+ config: Config,
169
+ rootDir: string,
158
170
  srcFilePaths: string[],
159
- destDir: string,
160
- passphrase: string,
161
- iterationCount: number
171
+ passphrase: string
162
172
  ): Promise<void> {
173
+ const iterationCount: number = config.encryption.iterationCount
174
+ const trickRootDirectory: string = config.trickRootDirectory
175
+
163
176
  for (const srcFilePath of srcFilePaths) {
164
- const destFilePath: string = path.join(destDir, srcFilePath + '.enc')
165
- await decryptFile(srcFilePath, destFilePath, passphrase, iterationCount)
166
- console.log(success(`Decrypted: ${destFilePath} -> ${srcFilePath}`))
177
+ const destFilePath: string = path.join(trickRootDirectory, srcFilePath + '.enc')
178
+ const absoluteDestFilePath: string = path.join(rootDir, destFilePath)
179
+ const absoluteSrcFilePath: string = path.resolve(rootDir, srcFilePath)
180
+ await decryptFile(absoluteSrcFilePath, absoluteDestFilePath, passphrase, iterationCount)
181
+ console.log(
182
+ success(
183
+ `Decrypted: ${colorTargetFilePath(destFilePath)} -> ${colorSourceFilePath(srcFilePath)}`
184
+ )
185
+ )
167
186
  }
168
187
  }