@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/src/error.ts
CHANGED
|
@@ -1,82 +1,95 @@
|
|
|
1
|
-
import
|
|
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
|
-
|
|
5
|
-
|
|
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
|
-
|
|
11
|
-
|
|
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
|
-
|
|
17
|
-
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
41
|
-
|
|
42
|
-
|
|
67
|
+
public constructor(public readonly passphraseFilePath: string) {
|
|
68
|
+
super(`Passphrase file not found: ${passphraseFilePath}`)
|
|
69
|
+
}
|
|
43
70
|
}
|
|
44
71
|
|
|
45
|
-
export
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
process.exit(2)
|
|
60
|
-
}
|
|
78
|
+
if (err.message) {
|
|
79
|
+
console.error(error(err.message))
|
|
80
|
+
}
|
|
61
81
|
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
|
|
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
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
|
-
*
|
|
13
|
-
*
|
|
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
|
-
|
|
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
|
-
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
if (!fsExtra.existsSync(passphraseFilePath)) {
|
|
33
|
+
throw new PassphraseFileNotFoundError(passphraseFilePath)
|
|
34
|
+
}
|
|
37
35
|
|
|
38
|
-
|
|
36
|
+
return fsExtra.readFileSync(passphraseFilePath, 'utf-8').trim()
|
|
39
37
|
}
|
package/test/trick.config.json
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
+
}
|
package/test/resources/task.yml
DELETED