@typinghare/trick 1.0.1 → 1.0.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.
@@ -0,0 +1 @@
1
+ Salted__�;��a��S�K�9ℑ���2�����4�'Q���\0]�G~G01�߯G���
@@ -0,0 +1 @@
1
+ Salted__�)�"�K 7�bk��aQ��ܰ^�B9��������.3.�g�!g�$�XjM�q�~q�c76C�i����0�S
package/dist/app.d.ts CHANGED
@@ -1 +1 @@
1
- export {};
1
+ export declare function resolve_error(err: any): void;
package/dist/app.js CHANGED
@@ -1,10 +1,10 @@
1
1
  import { Command } from 'commander';
2
- import { getTargetFromConfig, updateConfig } from './config.js';
3
- import { decryptFiles, encryptFiles } from './encrypt.js';
4
- import { getSecret } from './secret.js';
2
+ import { getTargetFromConfig, ReadConfigError, TargetNotFoundError, updateConfig, WriteConfigError, } from './config.js';
3
+ import { decryptFiles, encryptFiles, FailToDecryptFileError, FailToEncryptFileError, } from './encrypt.js';
4
+ import { getSecret, SecretNotFoundError } from './secret.js';
5
5
  import { TRICK_ENCRYPTED_DIR } from './constant.js';
6
6
  import fsExtra from 'fs-extra';
7
- import { resolve_error } from './utility.js';
7
+ import chalk from 'chalk';
8
8
  const program = new Command();
9
9
  program.version('Trick v1.0.0 \nby James Chen (jameschan312.cn@gmail.com)');
10
10
  program.description('Save credential files to remote safely.');
@@ -19,6 +19,7 @@ program
19
19
  getTargetFromConfig(config, secretName);
20
20
  }
21
21
  catch (err) {
22
+ config.default_secret_name = secretName;
22
23
  config.targets.push({
23
24
  secret_name: secretName,
24
25
  files,
@@ -30,12 +31,22 @@ program
30
31
  process.exit(1);
31
32
  });
32
33
  });
34
+ function checkSecretName(secretName, defaultSecretName) {
35
+ if (!secretName) {
36
+ secretName = defaultSecretName;
37
+ }
38
+ if (!secretName) {
39
+ throw new Error('No secret name given, and the default secret name is not set.');
40
+ }
41
+ return secretName;
42
+ }
33
43
  program
34
44
  .command('encrypt')
35
45
  .description('Encrypt the credential files.')
36
- .argument('<secret-name>', 'The name of secret in the environment')
46
+ .argument('[secret-name]', 'The name of secret in the environment', undefined)
37
47
  .action(async (secretName) => {
38
48
  await updateConfig((config) => {
49
+ secretName = checkSecretName(secretName, config.default_secret_name);
39
50
  const target = getTargetFromConfig(config, secretName);
40
51
  const secret = getSecret(target.secret_name);
41
52
  const srcFilePaths = target.files;
@@ -47,9 +58,10 @@ program
47
58
  program
48
59
  .command('decrypt')
49
60
  .description('Decrypt the credential files.')
50
- .argument('<secret-name>', 'The name of secret in the environment')
61
+ .argument('[secret-name]', 'The name of secret in the environment', undefined)
51
62
  .action(async (secretName) => {
52
63
  await updateConfig((config) => {
64
+ secretName = checkSecretName(secretName, config.default_secret_name);
53
65
  const target = getTargetFromConfig(config, secretName);
54
66
  const secret = getSecret(target.secret_name);
55
67
  const srcFilePaths = target.files;
@@ -58,8 +70,64 @@ program
58
70
  return false;
59
71
  });
60
72
  });
73
+ program
74
+ .command('set-default')
75
+ .description('Set the default secret name.')
76
+ .argument('<secret-name>', 'The name of secret in the environment')
77
+ .action(async (secretName) => {
78
+ await updateConfig((config) => {
79
+ config.default_secret_name = secretName;
80
+ return false;
81
+ });
82
+ });
83
+ program
84
+ .command('get-default')
85
+ .description('Get the default secret name.')
86
+ .action(async () => {
87
+ await updateConfig((config) => {
88
+ console.log(config.default_secret_name);
89
+ return false;
90
+ });
91
+ });
61
92
  program.parse();
62
93
  process.on('uncaughtException', (err) => {
63
94
  resolve_error(err);
64
95
  process.exit(1);
65
96
  });
97
+ export function resolve_error(err) {
98
+ if (!(err instanceof Error)) {
99
+ console.error(`Unknown error: ${err}`);
100
+ process.exit(2);
101
+ }
102
+ if (err instanceof WriteConfigError) {
103
+ console.error(chalk.red('Fail to write Trick config file'));
104
+ }
105
+ else if (err instanceof ReadConfigError) {
106
+ console.error(chalk.red('Fail to read Trick config file'));
107
+ }
108
+ else if (err instanceof SecretNotFoundError) {
109
+ console.error(chalk.red(err.message));
110
+ }
111
+ else if (err instanceof TargetNotFoundError) {
112
+ console.error(chalk.red(err.message));
113
+ }
114
+ else if (err instanceof FailToEncryptFileError) {
115
+ console.error(chalk.red(err.message));
116
+ if (err.opensslErrMessage) {
117
+ console.error(chalk.red(err.opensslErrMessage));
118
+ }
119
+ else {
120
+ console.error(chalk.yellow('Make sure the file exists and you have enough permission to access it'));
121
+ }
122
+ }
123
+ else if (err instanceof FailToDecryptFileError) {
124
+ console.error(chalk.red(err.message));
125
+ if (err.opensslErrMessage) {
126
+ console.error(chalk.red(err.opensslErrMessage));
127
+ }
128
+ else {
129
+ console.error(chalk.yellow('Make sure the file exists and you have enough permission to access it'));
130
+ }
131
+ }
132
+ process.exit(1);
133
+ }
package/dist/config.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export declare const CONFIG_FILE_NAME: string;
2
2
  export interface Config {
3
3
  iteration_count: number;
4
+ default_secret_name?: string;
4
5
  targets: Target[];
5
6
  }
6
7
  export interface Target {
package/dist/encrypt.d.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  export declare class FailToEncryptFileError extends Error {
2
2
  readonly srcFilePath: string;
3
- constructor(srcFilePath: string);
3
+ readonly opensslErrMessage?: string | undefined;
4
+ constructor(srcFilePath: string, opensslErrMessage?: string | undefined);
4
5
  }
5
6
  export declare class FailToDecryptFileError extends Error {
6
7
  readonly destFilePath: string;
7
- constructor(destFilePath: string);
8
+ readonly opensslErrMessage?: string | undefined;
9
+ constructor(destFilePath: string, opensslErrMessage?: string | undefined);
8
10
  }
9
11
  export declare function encryptFile(srcFilePath: string, destFilePath: string, secret: string, iteration_count: number): Promise<void>;
10
12
  export declare function decryptFile(srcFilePath: string, destFilePath: string, secret: string, iteration_count: number): Promise<void>;
package/dist/encrypt.js CHANGED
@@ -3,16 +3,20 @@ import * as path from 'node:path';
3
3
  import fsExtra from 'fs-extra';
4
4
  export class FailToEncryptFileError extends Error {
5
5
  srcFilePath;
6
- constructor(srcFilePath) {
6
+ opensslErrMessage;
7
+ constructor(srcFilePath, opensslErrMessage) {
7
8
  super(`Fail to encrypt source file: ${srcFilePath}`);
8
9
  this.srcFilePath = srcFilePath;
10
+ this.opensslErrMessage = opensslErrMessage;
9
11
  }
10
12
  }
11
13
  export class FailToDecryptFileError extends Error {
12
14
  destFilePath;
13
- constructor(destFilePath) {
15
+ opensslErrMessage;
16
+ constructor(destFilePath, opensslErrMessage) {
14
17
  super(`Fail to decrypt destination file: ${destFilePath}`);
15
18
  this.destFilePath = destFilePath;
19
+ this.opensslErrMessage = opensslErrMessage;
16
20
  }
17
21
  }
18
22
  export async function encryptFile(srcFilePath, destFilePath, secret, iteration_count) {
@@ -39,7 +43,12 @@ export async function encryptFile(srcFilePath, destFilePath, secret, iteration_c
39
43
  await execa(`${command}`, { shell: true });
40
44
  }
41
45
  catch (err) {
42
- throw new FailToEncryptFileError(srcFilePath);
46
+ if (typeof err == 'object' && err && Object.hasOwn(err, 'stderr')) {
47
+ throw new FailToEncryptFileError(srcFilePath, err.stderr);
48
+ }
49
+ else {
50
+ throw new FailToDecryptFileError(srcFilePath, "Unknown error when encrypting the file.");
51
+ }
43
52
  }
44
53
  }
45
54
  export async function decryptFile(srcFilePath, destFilePath, secret, iteration_count) {
@@ -67,7 +76,12 @@ export async function decryptFile(srcFilePath, destFilePath, secret, iteration_c
67
76
  await execa(`${command}`, { shell: true });
68
77
  }
69
78
  catch (err) {
70
- throw new FailToDecryptFileError(destFilePath);
79
+ if (typeof err == 'object' && err && Object.hasOwn(err, 'stderr')) {
80
+ throw new FailToDecryptFileError(srcFilePath, err['stderr']);
81
+ }
82
+ else {
83
+ throw new FailToDecryptFileError(srcFilePath, "Unknown error when decrypting the file.");
84
+ }
71
85
  }
72
86
  }
73
87
  export async function encryptFiles(srcFilePaths, destDir, secret, iteration_count) {
package/dist/index.d.ts CHANGED
@@ -2,5 +2,4 @@ export * from './app.js';
2
2
  export * from './config.js';
3
3
  export * from './constant.js';
4
4
  export * from './encrypt.js';
5
- export * from './utility.js';
6
5
  export * from './secret.js';
package/dist/index.js CHANGED
@@ -2,5 +2,4 @@ export * from './app.js';
2
2
  export * from './config.js';
3
3
  export * from './constant.js';
4
4
  export * from './encrypt.js';
5
- export * from './utility.js';
6
5
  export * from './secret.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@typinghare/trick",
3
3
  "description": "Save credential files to remote safely.",
4
- "version": "1.0.1",
4
+ "version": "1.0.2",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "type": "module",
@@ -24,6 +24,7 @@
24
24
  "@types/fs-extra": "^11.0.4",
25
25
  "prettier": "^3.3.3"
26
26
  },
27
+ "packageManager": "pnpm@8.15.1+sha1.8adba2d20330c02d3856e18c4eb3819d1d3ca6aa",
27
28
  "scripts": {
28
29
  "build": "tsc"
29
30
  }
package/src/app.ts CHANGED
@@ -1,10 +1,22 @@
1
1
  import { Command } from 'commander'
2
- import { getTargetFromConfig, Target, updateConfig } from './config.js'
3
- import { decryptFiles, encryptFiles } from './encrypt.js'
4
- import { getSecret } from './secret.js'
2
+ import {
3
+ getTargetFromConfig,
4
+ ReadConfigError,
5
+ Target,
6
+ TargetNotFoundError,
7
+ updateConfig,
8
+ WriteConfigError,
9
+ } from './config.js'
10
+ import {
11
+ decryptFiles,
12
+ encryptFiles,
13
+ FailToDecryptFileError,
14
+ FailToEncryptFileError,
15
+ } from './encrypt.js'
16
+ import { getSecret, SecretNotFoundError } from './secret.js'
5
17
  import { TRICK_ENCRYPTED_DIR } from './constant.js'
6
18
  import fsExtra from 'fs-extra'
7
- import { resolve_error } from './utility.js'
19
+ import chalk from 'chalk'
8
20
 
9
21
  const program = new Command()
10
22
 
@@ -21,6 +33,7 @@ program
21
33
  try {
22
34
  getTargetFromConfig(config, secretName)
23
35
  } catch (err) {
36
+ config.default_secret_name = secretName
24
37
  config.targets.push({
25
38
  secret_name: secretName,
26
39
  files,
@@ -36,12 +49,34 @@ program
36
49
  })
37
50
  })
38
51
 
52
+ function checkSecretName(
53
+ secretName?: string,
54
+ defaultSecretName?: string
55
+ ): string {
56
+ if (!secretName) {
57
+ secretName = defaultSecretName
58
+ }
59
+
60
+ if (!secretName) {
61
+ throw new Error(
62
+ 'No secret name given, and the default secret name is not set.'
63
+ )
64
+ }
65
+
66
+ return secretName
67
+ }
68
+
39
69
  program
40
70
  .command('encrypt')
41
71
  .description('Encrypt the credential files.')
42
- .argument('<secret-name>', 'The name of secret in the environment')
43
- .action(async (secretName: string): Promise<void> => {
72
+ .argument(
73
+ '[secret-name]',
74
+ 'The name of secret in the environment',
75
+ undefined
76
+ )
77
+ .action(async (secretName?: string): Promise<void> => {
44
78
  await updateConfig((config) => {
79
+ secretName = checkSecretName(secretName, config.default_secret_name)
45
80
  const target: Target = getTargetFromConfig(config, secretName)
46
81
  const secret: string = getSecret(target.secret_name)
47
82
  const srcFilePaths: string[] = target.files
@@ -59,9 +94,14 @@ program
59
94
  program
60
95
  .command('decrypt')
61
96
  .description('Decrypt the credential files.')
62
- .argument('<secret-name>', 'The name of secret in the environment')
63
- .action(async (secretName: string): Promise<void> => {
97
+ .argument(
98
+ '[secret-name]',
99
+ 'The name of secret in the environment',
100
+ undefined
101
+ )
102
+ .action(async (secretName?: string): Promise<void> => {
64
103
  await updateConfig((config) => {
104
+ secretName = checkSecretName(secretName, config.default_secret_name)
65
105
  const target: Target = getTargetFromConfig(config, secretName)
66
106
  const secret: string = getSecret(target.secret_name)
67
107
  const srcFilePaths: string[] = target.files
@@ -76,9 +116,72 @@ program
76
116
  })
77
117
  })
78
118
 
119
+ program
120
+ .command('set-default')
121
+ .description('Set the default secret name.')
122
+ .argument('<secret-name>', 'The name of secret in the environment')
123
+ .action(async (secretName: string): Promise<void> => {
124
+ await updateConfig((config) => {
125
+ config.default_secret_name = secretName
126
+
127
+ return false
128
+ })
129
+ })
130
+
131
+ program
132
+ .command('get-default')
133
+ .description('Get the default secret name.')
134
+ .action(async (): Promise<void> => {
135
+ await updateConfig((config) => {
136
+ console.log(config.default_secret_name)
137
+ return false
138
+ })
139
+ })
140
+
79
141
  program.parse()
80
142
 
81
143
  process.on('uncaughtException', (err) => {
82
144
  resolve_error(err)
83
145
  process.exit(1)
84
146
  })
147
+
148
+ export function resolve_error(err: any): void {
149
+ if (!(err instanceof Error)) {
150
+ console.error(`Unknown error: ${err}`)
151
+ process.exit(2)
152
+ }
153
+
154
+ if (err instanceof WriteConfigError) {
155
+ console.error(chalk.red('Fail to write Trick config file'))
156
+ } else if (err instanceof ReadConfigError) {
157
+ console.error(chalk.red('Fail to read Trick config file'))
158
+ } else if (err instanceof SecretNotFoundError) {
159
+ console.error(chalk.red(err.message))
160
+ } else if (err instanceof TargetNotFoundError) {
161
+ console.error(chalk.red(err.message))
162
+ } else if (err instanceof FailToEncryptFileError) {
163
+ console.error(chalk.red(err.message))
164
+ if (err.opensslErrMessage) {
165
+ console.error(chalk.red(err.opensslErrMessage))
166
+ } else {
167
+ console.error(
168
+ chalk.yellow(
169
+ 'Make sure the file exists and you have enough permission to access it'
170
+ )
171
+ )
172
+ }
173
+ } else if (err instanceof FailToDecryptFileError) {
174
+ console.error(chalk.red(err.message))
175
+ if (err.opensslErrMessage) {
176
+ console.error(chalk.red(err.opensslErrMessage))
177
+ } else {
178
+ console.error(
179
+ chalk.yellow(
180
+ 'Make sure the file exists and you have enough permission to access it'
181
+ )
182
+ )
183
+ }
184
+ }
185
+
186
+ process.exit(1)
187
+ }
package/src/config.ts CHANGED
@@ -4,6 +4,7 @@ export const CONFIG_FILE_NAME: string = 'trick.config.json'
4
4
 
5
5
  export interface Config {
6
6
  iteration_count: number
7
+ default_secret_name?: string
7
8
  targets: Target[]
8
9
  }
9
10
 
@@ -17,9 +18,11 @@ const defaultConfig: Config = {
17
18
  targets: [],
18
19
  }
19
20
 
20
- export class WriteConfigError extends Error {}
21
+ export class WriteConfigError extends Error {
22
+ }
21
23
 
22
- export class ReadConfigError extends Error {}
24
+ export class ReadConfigError extends Error {
25
+ }
23
26
 
24
27
  export async function writeConfig(config: Config): Promise<void> {
25
28
  try {
package/src/encrypt.ts CHANGED
@@ -1,15 +1,15 @@
1
- import { execa } from 'execa'
1
+ import {execa} from 'execa'
2
2
  import * as path from 'node:path'
3
3
  import fsExtra from 'fs-extra'
4
4
 
5
5
  export class FailToEncryptFileError extends Error {
6
- public constructor(public readonly srcFilePath: string) {
6
+ public constructor(public readonly srcFilePath: string, public readonly opensslErrMessage?: string) {
7
7
  super(`Fail to encrypt source file: ${srcFilePath}`)
8
8
  }
9
9
  }
10
10
 
11
11
  export class FailToDecryptFileError extends Error {
12
- public constructor(public readonly destFilePath: string) {
12
+ public constructor(public readonly destFilePath: string, public readonly opensslErrMessage?: string) {
13
13
  super(`Fail to decrypt destination file: ${destFilePath}`)
14
14
  }
15
15
  }
@@ -43,9 +43,13 @@ export async function encryptFile(
43
43
  await fsExtra.ensureDir(path.dirname(destFilePath))
44
44
 
45
45
  try {
46
- await execa(`${command}`, { shell: true })
46
+ await execa(`${command}`, {shell: true})
47
47
  } catch (err) {
48
- throw new FailToEncryptFileError(srcFilePath)
48
+ if (typeof err == 'object' && err && Object.hasOwn(err, 'stderr')) {
49
+ throw new FailToEncryptFileError(srcFilePath, (err as { stderr: string }).stderr)
50
+ } else {
51
+ throw new FailToDecryptFileError(srcFilePath, "Unknown error when encrypting the file.")
52
+ }
49
53
  }
50
54
  }
51
55
 
@@ -79,9 +83,13 @@ export async function decryptFile(
79
83
  await fsExtra.ensureDir(path.dirname(srcFilePath))
80
84
 
81
85
  try {
82
- await execa(`${command}`, { shell: true })
86
+ await execa(`${command}`, {shell: true})
83
87
  } catch (err) {
84
- throw new FailToDecryptFileError(destFilePath)
88
+ if (typeof err == 'object' && err && Object.hasOwn(err, 'stderr')) {
89
+ throw new FailToDecryptFileError(srcFilePath, (err as { stderr: string })['stderr'])
90
+ } else {
91
+ throw new FailToDecryptFileError(srcFilePath, "Unknown error when decrypting the file.")
92
+ }
85
93
  }
86
94
  }
87
95
 
package/src/index.ts CHANGED
@@ -2,5 +2,4 @@ export * from './app.js'
2
2
  export * from './config.js'
3
3
  export * from './constant.js'
4
4
  export * from './encrypt.js'
5
- export * from './utility.js'
6
5
  export * from './secret.js'
package/trick.config.json CHANGED
@@ -2,11 +2,12 @@
2
2
  "iteration_count": 114514,
3
3
  "targets": [
4
4
  {
5
- "secret_name": "TEST_SECRET",
5
+ "secret_name": "ROBLOX_PROJECT_TRICK_SECRET",
6
6
  "files": [
7
7
  "test/resources/really.json",
8
8
  "test/resources/task.yml"
9
9
  ]
10
10
  }
11
- ]
11
+ ],
12
+ "default_secret_name": "ROBLOX_PROJECT_TRICK_SECRET"
12
13
  }
package/dist/utility.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare function resolve_error(err: any): void;
package/dist/utility.js DELETED
@@ -1,27 +0,0 @@
1
- import { ReadConfigError, TargetNotFoundError, WriteConfigError, } from './config.js';
2
- import { FailToDecryptFileError, FailToEncryptFileError } from './encrypt.js';
3
- import chalk from 'chalk';
4
- export function resolve_error(err) {
5
- if (!(err instanceof Error)) {
6
- console.error(`Unknown error: ${err}`);
7
- process.exit(2);
8
- }
9
- if (err instanceof WriteConfigError) {
10
- console.error(chalk.red('Fail to write Trick config file'));
11
- }
12
- else if (err instanceof ReadConfigError) {
13
- console.error(chalk.red('Fail to read Trick config file'));
14
- }
15
- else if (err instanceof TargetNotFoundError) {
16
- console.error(chalk.red(err.message));
17
- }
18
- else if (err instanceof FailToEncryptFileError) {
19
- console.error(chalk.red(err.message));
20
- console.error(chalk.yellow('Make sure the file exists and you have enough permission to access'));
21
- }
22
- else if (err instanceof FailToDecryptFileError) {
23
- console.error(chalk.red(err.message));
24
- console.error(chalk.yellow('Make sure the file exists and you have enough permission to access'));
25
- }
26
- process.exit(1);
27
- }
package/src/utility.ts DELETED
@@ -1,38 +0,0 @@
1
- import {
2
- ReadConfigError,
3
- TargetNotFoundError,
4
- WriteConfigError,
5
- } from './config.js'
6
- import { FailToDecryptFileError, FailToEncryptFileError } from './encrypt.js'
7
- import chalk from 'chalk'
8
-
9
- export function resolve_error(err: any): void {
10
- if (!(err instanceof Error)) {
11
- console.error(`Unknown error: ${err}`)
12
- process.exit(2)
13
- }
14
-
15
- if (err instanceof WriteConfigError) {
16
- console.error(chalk.red('Fail to write Trick config file'))
17
- } else if (err instanceof ReadConfigError) {
18
- console.error(chalk.red('Fail to read Trick config file'))
19
- } else if (err instanceof TargetNotFoundError) {
20
- console.error(chalk.red(err.message))
21
- } else if (err instanceof FailToEncryptFileError) {
22
- console.error(chalk.red(err.message))
23
- console.error(
24
- chalk.yellow(
25
- 'Make sure the file exists and you have enough permission to access'
26
- )
27
- )
28
- } else if (err instanceof FailToDecryptFileError) {
29
- console.error(chalk.red(err.message))
30
- console.error(
31
- chalk.yellow(
32
- 'Make sure the file exists and you have enough permission to access'
33
- )
34
- )
35
- }
36
-
37
- process.exit(1)
38
- }
File without changes