@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/src/error.ts CHANGED
@@ -1,82 +1,95 @@
1
- import chalk from 'chalk'
1
+ import { error, warning } from './console.js'
2
2
 
3
+ /**
4
+ * Thrown when the root directory is not found.
5
+ */
6
+ export class RootDirectoryNotFoundError extends Error {
7
+ public constructor() {
8
+ super('Root directory not found')
9
+ }
10
+ }
11
+
12
+ /**
13
+ * Thrown when fail to write the configuration file.
14
+ */
3
15
  export class WriteConfigError extends Error {
4
- public constructor(err: any) {
5
- super(`Fail to write the configuration file. ${err}`)
6
- }
16
+ public constructor(err: any) {
17
+ super(`Fail to write the configuration file. ${err}`)
18
+ }
7
19
  }
8
20
 
21
+ /**
22
+ * Thrown when fail to read the configuration file.
23
+ */
9
24
  export class ReadConfigError extends Error {
10
- public constructor(err: any) {
11
- super(`Fail to read the configuration file: ${err}`)
12
- }
25
+ public constructor(err: any) {
26
+ super(`Fail to read the configuration file: ${err}`)
27
+ }
13
28
  }
14
29
 
30
+ /**
31
+ * Thrown when the specified target is not found in the configuration.
32
+ */
15
33
  export class TargetNotFoundError extends Error {
16
- public constructor(public readonly targetName: string) {
17
- super(`Target not found: ${targetName}`)
18
- }
34
+ public constructor(public readonly targetName: string) {
35
+ super(`Target not found: ${targetName}`)
36
+ }
19
37
  }
20
38
 
39
+ /**
40
+ * Thrown when fail to encrypt the source file.
41
+ */
21
42
  export class FailToEncryptFileError extends Error {
22
- public constructor(
23
- public readonly srcFilePath: string,
24
- public readonly opensslErrMessage?: string
25
- ) {
26
- super(`Fail to encrypt source file: ${srcFilePath}`)
27
- }
43
+ public constructor(
44
+ public readonly srcFilePath: string,
45
+ public readonly opensslErrMessage?: string
46
+ ) {
47
+ super(`Fail to encrypt source file: ${srcFilePath}`)
48
+ }
28
49
  }
29
50
 
51
+ /**
52
+ * Thrown when fail to decrypt the source file.
53
+ */
30
54
  export class FailToDecryptFileError extends Error {
31
- public constructor(
32
- public readonly destFilePath: string,
33
- public readonly opensslErrMessage?: string
34
- ) {
35
- super(`Fail to decrypt destination file: ${destFilePath}`)
36
- }
55
+ public constructor(
56
+ public readonly destFilePath: string,
57
+ public readonly opensslErrMessage?: string
58
+ ) {
59
+ super(`Fail to decrypt destination file: ${destFilePath}`)
60
+ }
37
61
  }
38
62
 
63
+ /**
64
+ * Thrown when the passphrase file is not found.
65
+ */
39
66
  export class PassphraseFileNotFoundError extends Error {
40
- public constructor(public readonly passphraseFilePath: string) {
41
- super(`Passphrase file not found: ${passphraseFilePath}`)
42
- }
67
+ public constructor(public readonly passphraseFilePath: string) {
68
+ super(`Passphrase file not found: ${passphraseFilePath}`)
69
+ }
43
70
  }
44
71
 
45
- export class PassphraseNotFoundError extends Error {
46
- public constructor(
47
- public readonly passphraseFilePath: string,
48
- public readonly targetName: string
49
- ) {
50
- super(
51
- `Passphrase for target ${targetName} is not found in the passphrase file: ${passphraseFilePath}`
52
- )
53
- }
54
- }
72
+ export function resolveError(err: any): void {
73
+ if (!(err instanceof Error)) {
74
+ console.error(error(`Unknown error: ${err}`))
75
+ process.exit(2)
76
+ }
55
77
 
56
- export function resolve_error(err: any): void {
57
- if (!(err instanceof Error)) {
58
- console.error(`Unknown error: ${err}`)
59
- process.exit(2)
60
- }
78
+ if (err.message) {
79
+ console.error(error(err.message))
80
+ }
61
81
 
62
- if (err.message) {
63
- console.log(chalk.red(err.message))
64
- }
65
-
66
- if (
67
- err instanceof FailToEncryptFileError ||
68
- err instanceof FailToDecryptFileError
69
- ) {
70
- if (err.opensslErrMessage) {
71
- console.error(chalk.red(err.opensslErrMessage))
72
- }
82
+ if (err instanceof PassphraseFileNotFoundError) {
83
+ console.error(warning(`Use the "set-passphrase" command to set the passphrase file.`))
84
+ }
73
85
 
74
- console.error(
75
- chalk.yellow(
76
- 'Make sure the file exists and you have enough permission to access it.'
77
- )
78
- )
86
+ if (err instanceof FailToEncryptFileError || err instanceof FailToDecryptFileError) {
87
+ if (err.opensslErrMessage) {
88
+ console.error(error(err.opensslErrMessage))
79
89
  }
80
90
 
81
- process.exit(1)
91
+ console.error(warning('Make sure the file exists and you have enough permission to access it.'))
92
+ }
93
+
94
+ process.exit(1)
82
95
  }
package/src/index.ts CHANGED
@@ -1,4 +1,7 @@
1
1
  export * from './app.js'
2
+ export * from './color.js'
2
3
  export * from './config.js'
4
+ export * from './console.js'
3
5
  export * from './encrypt.js'
6
+ export * from './error.js'
4
7
  export * from './passphrase.js'
package/src/passphrase.ts CHANGED
@@ -1,39 +1,37 @@
1
1
  import { Config } from './config.js'
2
2
  import fsExtra from 'fs-extra'
3
- import {
4
- PassphraseFileNotFoundError,
5
- PassphraseNotFoundError,
6
- } from './error.js'
3
+ import { PassphraseFileNotFoundError } from './error.js'
7
4
  import os from 'node:os'
5
+ import path from 'node:path'
6
+
7
+ /**
8
+ * Gets the passphrase directory from the configuration.
9
+ *
10
+ * This function replaces any tilde (`~`) in the directory path with the user's home directory.
11
+ *
12
+ * @param config The configuration to use.
13
+ * @return The resolved passphrase directory path.
14
+ */
15
+ export function getPassphraseDirectory(config: Config): string {
16
+ return config.passphraseDirectory.replaceAll(/~/g, os.homedir())
17
+ }
8
18
 
9
19
  /**
10
20
  * Retrieves the passphrase from the passphrase file.
11
21
  *
12
- * First, gets the passphrase object from the passphrase file path specified in
13
- * the configuration object. Second, gets and returns the passphrase associated
14
- * with the given target name.
22
+ * The passphrase file is located in the directory specified by `config.passphraseDirectory`
23
+ * and is named after the `targetName`.
15
24
  *
16
25
  * @param config The configuration to use.
17
26
  * @param targetName The name of the target.
18
27
  * @return The passphrase associated with the target name.
19
28
  */
20
29
  export function getPassphrase(config: Config, targetName: string): string {
21
- const passphraseFilePath = config.passphrase_file_path.replaceAll(
22
- /~/g,
23
- os.homedir()
24
- )
25
- if (!fsExtra.existsSync(passphraseFilePath)) {
26
- throw new PassphraseFileNotFoundError(passphraseFilePath)
27
- }
28
-
29
- const passphraseObject = fsExtra.readJsonSync(passphraseFilePath) as {
30
- [name: string]: string
31
- }
32
- const passphrase = passphraseObject[targetName] || null
30
+ const passphraseFilePath = path.join(getPassphraseDirectory(config), targetName)
33
31
 
34
- if (passphrase === null) {
35
- throw new PassphraseNotFoundError(passphraseFilePath, targetName)
36
- }
32
+ if (!fsExtra.existsSync(passphraseFilePath)) {
33
+ throw new PassphraseFileNotFoundError(passphraseFilePath)
34
+ }
37
35
 
38
- return passphrase
36
+ return fsExtra.readFileSync(passphraseFilePath, 'utf-8').trim()
39
37
  }
@@ -0,0 +1,4 @@
1
+ response:
2
+ message: Ok
3
+ taskId: 12345
4
+ taskStatus: "in_progress"
@@ -0,0 +1,5 @@
1
+ {
2
+ "message": "Ok",
3
+ "userId": 123456,
4
+ "username": "TypingHare"
5
+ }
@@ -1,13 +1,18 @@
1
1
  {
2
- "targets": {
3
- "MyTestTarget": {
4
- "files": ["resources/really.json", "resources/task.yml"]
5
- }
6
- },
7
- "default_target_name": "MyTestTarget",
8
- "root_directory": ".trick",
9
- "passphrase_file_path": "~/.config/trick_passphrase.json",
10
- "encryption": {
11
- "iteration_count": 100000
2
+ "targets": {
3
+ "TRICK_TEST_ONE": {
4
+ "files": [
5
+ "resources/task_info.yml",
6
+ "resources/user_info.json"
7
+ ]
12
8
  }
13
- }
9
+ },
10
+ "trickRootDirectory": ".trick",
11
+ "passphraseDirectory": "~/.config/trick/passphrases",
12
+ "defaultTargetNames": [
13
+ "TRICK_TEST_ONE"
14
+ ],
15
+ "encryption": {
16
+ "iterationCount": 100000
17
+ }
18
+ }
@@ -1,4 +0,0 @@
1
- {
2
- "message": "Ok",
3
- "userId": 123456
4
- }
@@ -1,3 +0,0 @@
1
- response:
2
- message: Ok
3
- userId: 123456