resend-cli 1.0.2 → 1.1.0

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.
Files changed (180) hide show
  1. package/.claude/settings.local.json +14 -0
  2. package/.github/scripts/pr-title-check.js +34 -0
  3. package/.github/workflows/ci.yml +32 -0
  4. package/.github/workflows/pr-title-check.yml +13 -0
  5. package/.github/workflows/release.yml +93 -0
  6. package/CHANGELOG.md +31 -0
  7. package/LICENSE +21 -21
  8. package/README.md +416 -19
  9. package/biome.json +36 -0
  10. package/bun.lock +76 -0
  11. package/bunfig.toml +2 -0
  12. package/install.ps1 +140 -0
  13. package/install.sh +294 -0
  14. package/package.json +43 -22
  15. package/src/cli.ts +65 -0
  16. package/src/commands/api-keys/create.ts +114 -0
  17. package/src/commands/api-keys/delete.ts +47 -0
  18. package/src/commands/api-keys/index.ts +26 -0
  19. package/src/commands/api-keys/list.ts +35 -0
  20. package/src/commands/api-keys/utils.ts +8 -0
  21. package/src/commands/auth/index.ts +20 -0
  22. package/src/commands/auth/login.ts +211 -0
  23. package/src/commands/auth/logout.ts +105 -0
  24. package/src/commands/broadcasts/create.ts +196 -0
  25. package/src/commands/broadcasts/delete.ts +46 -0
  26. package/src/commands/broadcasts/get.ts +59 -0
  27. package/src/commands/broadcasts/index.ts +43 -0
  28. package/src/commands/broadcasts/list.ts +60 -0
  29. package/src/commands/broadcasts/send.ts +56 -0
  30. package/src/commands/broadcasts/update.ts +95 -0
  31. package/src/commands/broadcasts/utils.ts +35 -0
  32. package/src/commands/contact-properties/create.ts +118 -0
  33. package/src/commands/contact-properties/delete.ts +48 -0
  34. package/src/commands/contact-properties/get.ts +46 -0
  35. package/src/commands/contact-properties/index.ts +48 -0
  36. package/src/commands/contact-properties/list.ts +68 -0
  37. package/src/commands/contact-properties/update.ts +88 -0
  38. package/src/commands/contact-properties/utils.ts +17 -0
  39. package/src/commands/contacts/add-segment.ts +78 -0
  40. package/src/commands/contacts/create.ts +122 -0
  41. package/src/commands/contacts/delete.ts +49 -0
  42. package/src/commands/contacts/get.ts +53 -0
  43. package/src/commands/contacts/index.ts +58 -0
  44. package/src/commands/contacts/list.ts +57 -0
  45. package/src/commands/contacts/remove-segment.ts +48 -0
  46. package/src/commands/contacts/segments.ts +39 -0
  47. package/src/commands/contacts/topics.ts +45 -0
  48. package/src/commands/contacts/update-topics.ts +90 -0
  49. package/src/commands/contacts/update.ts +77 -0
  50. package/src/commands/contacts/utils.ts +119 -0
  51. package/src/commands/doctor.ts +298 -0
  52. package/src/commands/domains/create.ts +83 -0
  53. package/src/commands/domains/delete.ts +42 -0
  54. package/src/commands/domains/get.ts +47 -0
  55. package/src/commands/domains/index.ts +35 -0
  56. package/src/commands/domains/list.ts +53 -0
  57. package/src/commands/domains/update.ts +75 -0
  58. package/src/commands/domains/utils.ts +44 -0
  59. package/src/commands/domains/verify.ts +38 -0
  60. package/src/commands/emails/batch.ts +140 -0
  61. package/src/commands/emails/index.ts +24 -0
  62. package/src/commands/emails/receiving/attachment.ts +55 -0
  63. package/src/commands/emails/receiving/attachments.ts +68 -0
  64. package/src/commands/emails/receiving/get.ts +58 -0
  65. package/src/commands/emails/receiving/index.ts +28 -0
  66. package/src/commands/emails/receiving/list.ts +59 -0
  67. package/src/commands/emails/receiving/utils.ts +38 -0
  68. package/src/commands/emails/send.ts +189 -0
  69. package/src/commands/segments/create.ts +50 -0
  70. package/src/commands/segments/delete.ts +47 -0
  71. package/src/commands/segments/get.ts +38 -0
  72. package/src/commands/segments/index.ts +36 -0
  73. package/src/commands/segments/list.ts +58 -0
  74. package/src/commands/segments/utils.ts +7 -0
  75. package/src/commands/teams/index.ts +10 -0
  76. package/src/commands/teams/list.ts +35 -0
  77. package/src/commands/teams/remove.ts +83 -0
  78. package/src/commands/teams/switch.ts +73 -0
  79. package/src/commands/topics/create.ts +73 -0
  80. package/src/commands/topics/delete.ts +47 -0
  81. package/src/commands/topics/get.ts +42 -0
  82. package/src/commands/topics/index.ts +42 -0
  83. package/src/commands/topics/list.ts +34 -0
  84. package/src/commands/topics/update.ts +59 -0
  85. package/src/commands/topics/utils.ts +16 -0
  86. package/src/commands/webhooks/create.ts +128 -0
  87. package/src/commands/webhooks/delete.ts +49 -0
  88. package/src/commands/webhooks/get.ts +42 -0
  89. package/src/commands/webhooks/index.ts +44 -0
  90. package/src/commands/webhooks/list.ts +55 -0
  91. package/src/commands/webhooks/update.ts +83 -0
  92. package/src/commands/webhooks/utils.ts +36 -0
  93. package/src/commands/whoami.ts +71 -0
  94. package/src/lib/actions.ts +157 -0
  95. package/src/lib/client.ts +29 -0
  96. package/src/lib/config.ts +211 -0
  97. package/src/lib/files.ts +15 -0
  98. package/src/lib/help-text.ts +36 -0
  99. package/src/lib/output.ts +54 -0
  100. package/src/lib/pagination.ts +36 -0
  101. package/src/lib/prompts.ts +149 -0
  102. package/src/lib/spinner.ts +89 -0
  103. package/src/lib/table.ts +57 -0
  104. package/src/lib/tty.ts +28 -0
  105. package/src/lib/version.ts +4 -0
  106. package/tests/commands/api-keys/create.test.ts +195 -0
  107. package/tests/commands/api-keys/delete.test.ts +156 -0
  108. package/tests/commands/api-keys/list.test.ts +133 -0
  109. package/tests/commands/auth/login.test.ts +119 -0
  110. package/tests/commands/auth/logout.test.ts +146 -0
  111. package/tests/commands/broadcasts/create.test.ts +447 -0
  112. package/tests/commands/broadcasts/delete.test.ts +182 -0
  113. package/tests/commands/broadcasts/get.test.ts +146 -0
  114. package/tests/commands/broadcasts/list.test.ts +196 -0
  115. package/tests/commands/broadcasts/send.test.ts +161 -0
  116. package/tests/commands/broadcasts/update.test.ts +283 -0
  117. package/tests/commands/contact-properties/create.test.ts +250 -0
  118. package/tests/commands/contact-properties/delete.test.ts +183 -0
  119. package/tests/commands/contact-properties/get.test.ts +144 -0
  120. package/tests/commands/contact-properties/list.test.ts +180 -0
  121. package/tests/commands/contact-properties/update.test.ts +216 -0
  122. package/tests/commands/contacts/add-segment.test.ts +188 -0
  123. package/tests/commands/contacts/create.test.ts +270 -0
  124. package/tests/commands/contacts/delete.test.ts +192 -0
  125. package/tests/commands/contacts/get.test.ts +148 -0
  126. package/tests/commands/contacts/list.test.ts +175 -0
  127. package/tests/commands/contacts/remove-segment.test.ts +166 -0
  128. package/tests/commands/contacts/segments.test.ts +167 -0
  129. package/tests/commands/contacts/topics.test.ts +163 -0
  130. package/tests/commands/contacts/update-topics.test.ts +247 -0
  131. package/tests/commands/contacts/update.test.ts +205 -0
  132. package/tests/commands/doctor.test.ts +165 -0
  133. package/tests/commands/domains/create.test.ts +192 -0
  134. package/tests/commands/domains/delete.test.ts +156 -0
  135. package/tests/commands/domains/get.test.ts +137 -0
  136. package/tests/commands/domains/list.test.ts +164 -0
  137. package/tests/commands/domains/update.test.ts +223 -0
  138. package/tests/commands/domains/verify.test.ts +117 -0
  139. package/tests/commands/emails/batch.test.ts +313 -0
  140. package/tests/commands/emails/receiving/attachment.test.ts +140 -0
  141. package/tests/commands/emails/receiving/attachments.test.ts +168 -0
  142. package/tests/commands/emails/receiving/get.test.ts +140 -0
  143. package/tests/commands/emails/receiving/list.test.ts +181 -0
  144. package/tests/commands/emails/send.test.ts +309 -0
  145. package/tests/commands/segments/create.test.ts +163 -0
  146. package/tests/commands/segments/delete.test.ts +182 -0
  147. package/tests/commands/segments/get.test.ts +137 -0
  148. package/tests/commands/segments/list.test.ts +173 -0
  149. package/tests/commands/teams/list.test.ts +63 -0
  150. package/tests/commands/teams/remove.test.ts +103 -0
  151. package/tests/commands/teams/switch.test.ts +96 -0
  152. package/tests/commands/topics/create.test.ts +191 -0
  153. package/tests/commands/topics/delete.test.ts +156 -0
  154. package/tests/commands/topics/get.test.ts +125 -0
  155. package/tests/commands/topics/list.test.ts +124 -0
  156. package/tests/commands/topics/update.test.ts +177 -0
  157. package/tests/commands/webhooks/create.test.ts +224 -0
  158. package/tests/commands/webhooks/delete.test.ts +156 -0
  159. package/tests/commands/webhooks/get.test.ts +125 -0
  160. package/tests/commands/webhooks/list.test.ts +177 -0
  161. package/tests/commands/webhooks/update.test.ts +206 -0
  162. package/tests/commands/whoami.test.ts +99 -0
  163. package/tests/helpers.ts +93 -0
  164. package/tests/lib/client.test.ts +71 -0
  165. package/tests/lib/config.test.ts +414 -0
  166. package/tests/lib/files.test.ts +65 -0
  167. package/tests/lib/help-text.test.ts +96 -0
  168. package/tests/lib/output.test.ts +127 -0
  169. package/tests/lib/prompts.test.ts +178 -0
  170. package/tests/lib/spinner.test.ts +146 -0
  171. package/tests/lib/table.test.ts +63 -0
  172. package/tests/lib/tty.test.ts +85 -0
  173. package/tsconfig.json +14 -0
  174. package/src/index.js +0 -72
  175. package/src/routes.js +0 -37
  176. package/src/sections/apikeys.js +0 -99
  177. package/src/sections/audiences.js +0 -84
  178. package/src/sections/contacts.js +0 -177
  179. package/src/sections/domain.js +0 -195
  180. package/src/sections/email.js +0 -132
@@ -0,0 +1,178 @@
1
+ import { afterEach, describe, expect, spyOn, test } from 'bun:test';
2
+ import { expectExit1, mockExitThrow } from '../helpers';
3
+
4
+ describe('promptForMissing', () => {
5
+ const originalStdinIsTTY = process.stdin.isTTY;
6
+ const originalStdoutIsTTY = process.stdout.isTTY;
7
+ let errorSpy: ReturnType<typeof spyOn> | undefined;
8
+ let exitSpy: ReturnType<typeof spyOn> | undefined;
9
+
10
+ afterEach(() => {
11
+ errorSpy?.mockRestore();
12
+ exitSpy?.mockRestore();
13
+ errorSpy = undefined;
14
+ exitSpy = undefined;
15
+ Object.defineProperty(process.stdin, 'isTTY', {
16
+ value: originalStdinIsTTY,
17
+ writable: true,
18
+ });
19
+ Object.defineProperty(process.stdout, 'isTTY', {
20
+ value: originalStdoutIsTTY,
21
+ writable: true,
22
+ });
23
+ });
24
+
25
+ test('returns options unchanged when nothing is missing', async () => {
26
+ const { promptForMissing } = require('../../src/lib/prompts');
27
+ const opts = { from: 'a@b.com', to: 'c@d.com', subject: 'Hi' };
28
+ const result = await promptForMissing(
29
+ opts,
30
+ [
31
+ { flag: 'from', message: 'From' },
32
+ { flag: 'to', message: 'To' },
33
+ { flag: 'subject', message: 'Subject' },
34
+ ],
35
+ {},
36
+ );
37
+ expect(result).toEqual(opts);
38
+ });
39
+
40
+ test('exits with missing_flags error in non-interactive mode', async () => {
41
+ Object.defineProperty(process.stdin, 'isTTY', {
42
+ value: undefined,
43
+ writable: true,
44
+ });
45
+ Object.defineProperty(process.stdout, 'isTTY', {
46
+ value: undefined,
47
+ writable: true,
48
+ });
49
+ errorSpy = spyOn(console, 'error').mockImplementation(() => {});
50
+ exitSpy = mockExitThrow();
51
+
52
+ const { promptForMissing } = require('../../src/lib/prompts');
53
+
54
+ await expectExit1(() =>
55
+ promptForMissing(
56
+ { from: undefined, to: 'c@d.com', subject: undefined },
57
+ [
58
+ { flag: 'from', message: 'From' },
59
+ { flag: 'to', message: 'To' },
60
+ { flag: 'subject', message: 'Subject' },
61
+ ],
62
+ {},
63
+ ),
64
+ );
65
+
66
+ const allErrors = errorSpy?.mock.calls.map((c) => c[0]).join(' ');
67
+ expect(allErrors).toContain('--from');
68
+ expect(allErrors).toContain('--subject');
69
+ // --to should NOT be listed since it has a value
70
+ expect(allErrors).not.toContain('--to,');
71
+ });
72
+
73
+ test('errors output includes missing_flags code', async () => {
74
+ Object.defineProperty(process.stdin, 'isTTY', {
75
+ value: undefined,
76
+ writable: true,
77
+ });
78
+ Object.defineProperty(process.stdout, 'isTTY', {
79
+ value: undefined,
80
+ writable: true,
81
+ });
82
+ errorSpy = spyOn(console, 'error').mockImplementation(() => {});
83
+ exitSpy = mockExitThrow();
84
+
85
+ const { promptForMissing } = require('../../src/lib/prompts');
86
+
87
+ await expectExit1(() =>
88
+ promptForMissing(
89
+ { from: undefined },
90
+ [{ flag: 'from', message: 'From' }],
91
+ {},
92
+ ),
93
+ );
94
+
95
+ const allErrors = errorSpy?.mock.calls.map((c) => c[0]).join(' ');
96
+ expect(allErrors).toContain('missing_flags');
97
+ });
98
+
99
+ test('skips fields marked as required=false', async () => {
100
+ const { promptForMissing } = require('../../src/lib/prompts');
101
+ const opts = { from: 'a@b.com', to: undefined };
102
+ const result = await promptForMissing(
103
+ opts,
104
+ [
105
+ { flag: 'from', message: 'From' },
106
+ { flag: 'to', message: 'To', required: false },
107
+ ],
108
+ {},
109
+ );
110
+ expect(result).toEqual(opts);
111
+ });
112
+ });
113
+
114
+ describe('confirmDelete', () => {
115
+ const originalStdinIsTTY = process.stdin.isTTY;
116
+ const originalStdoutIsTTY = process.stdout.isTTY;
117
+ let errorSpy: ReturnType<typeof spyOn> | undefined;
118
+ let exitSpy: ReturnType<typeof spyOn> | undefined;
119
+
120
+ afterEach(() => {
121
+ errorSpy?.mockRestore();
122
+ exitSpy?.mockRestore();
123
+ errorSpy = undefined;
124
+ exitSpy = undefined;
125
+ Object.defineProperty(process.stdin, 'isTTY', {
126
+ value: originalStdinIsTTY,
127
+ writable: true,
128
+ });
129
+ Object.defineProperty(process.stdout, 'isTTY', {
130
+ value: originalStdoutIsTTY,
131
+ writable: true,
132
+ });
133
+ });
134
+
135
+ test('exits with confirmation_required when non-interactive', async () => {
136
+ Object.defineProperty(process.stdin, 'isTTY', {
137
+ value: undefined,
138
+ writable: true,
139
+ });
140
+ Object.defineProperty(process.stdout, 'isTTY', {
141
+ value: undefined,
142
+ writable: true,
143
+ });
144
+ errorSpy = spyOn(console, 'error').mockImplementation(() => {});
145
+ exitSpy = mockExitThrow();
146
+
147
+ const { confirmDelete } = require('../../src/lib/prompts');
148
+
149
+ await expectExit1(() =>
150
+ confirmDelete('res_123', 'Delete resource res_123?', { json: false }),
151
+ );
152
+
153
+ const output = errorSpy?.mock.calls.map((c) => c[0]).join(' ');
154
+ expect(output).toContain('confirmation_required');
155
+ });
156
+
157
+ test('outputs JSON confirmation_required error when json option is true', async () => {
158
+ Object.defineProperty(process.stdin, 'isTTY', {
159
+ value: undefined,
160
+ writable: true,
161
+ });
162
+ Object.defineProperty(process.stdout, 'isTTY', {
163
+ value: undefined,
164
+ writable: true,
165
+ });
166
+ errorSpy = spyOn(console, 'error').mockImplementation(() => {});
167
+ exitSpy = mockExitThrow();
168
+
169
+ const { confirmDelete } = require('../../src/lib/prompts');
170
+ await expectExit1(() =>
171
+ confirmDelete('res_123', 'Delete?', { json: true }),
172
+ );
173
+
174
+ const raw = errorSpy?.mock.calls.map((c) => c[0]).join(' ');
175
+ const parsed = JSON.parse(raw);
176
+ expect(parsed.error.code).toBe('confirmation_required');
177
+ });
178
+ });
@@ -0,0 +1,146 @@
1
+ import { afterEach, describe, expect, spyOn, test } from 'bun:test';
2
+
3
+ describe('createSpinner', () => {
4
+ const originalStdinIsTTY = process.stdin.isTTY;
5
+ const originalStdoutIsTTY = process.stdout.isTTY;
6
+ let stderrSpy: ReturnType<typeof spyOn>;
7
+
8
+ afterEach(() => {
9
+ stderrSpy?.mockRestore();
10
+ Object.defineProperty(process.stdin, 'isTTY', {
11
+ value: originalStdinIsTTY,
12
+ writable: true,
13
+ });
14
+ Object.defineProperty(process.stdout, 'isTTY', {
15
+ value: originalStdoutIsTTY,
16
+ writable: true,
17
+ });
18
+ delete process.env.CI;
19
+ });
20
+
21
+ test('returns no-op spinner in non-interactive mode', () => {
22
+ Object.defineProperty(process.stdin, 'isTTY', {
23
+ value: undefined,
24
+ writable: true,
25
+ });
26
+ Object.defineProperty(process.stdout, 'isTTY', {
27
+ value: undefined,
28
+ writable: true,
29
+ });
30
+
31
+ const { createSpinner } = require('../../src/lib/spinner');
32
+ const spinner = createSpinner('test message');
33
+
34
+ // Should not throw when calling any method
35
+ expect(() => spinner.stop('done')).not.toThrow();
36
+ expect(() => spinner.fail('error')).not.toThrow();
37
+ expect(() => spinner.warn('warning')).not.toThrow();
38
+ expect(() => spinner.update('updating')).not.toThrow();
39
+ });
40
+
41
+ test('returns functional spinner in interactive mode', () => {
42
+ Object.defineProperty(process.stdin, 'isTTY', {
43
+ value: true,
44
+ writable: true,
45
+ });
46
+ Object.defineProperty(process.stdout, 'isTTY', {
47
+ value: true,
48
+ writable: true,
49
+ });
50
+ delete process.env.CI;
51
+ delete process.env.GITHUB_ACTIONS;
52
+ delete process.env.TERM;
53
+
54
+ stderrSpy = spyOn(process.stderr, 'write').mockImplementation(() => true);
55
+
56
+ const { createSpinner } = require('../../src/lib/spinner');
57
+ const spinner = createSpinner('loading...');
58
+
59
+ expect(spinner).toHaveProperty('stop');
60
+ expect(spinner).toHaveProperty('fail');
61
+ expect(spinner).toHaveProperty('warn');
62
+ expect(spinner).toHaveProperty('update');
63
+
64
+ // Stop to clean up the interval
65
+ spinner.stop('done');
66
+ expect(stderrSpy).toHaveBeenCalled();
67
+ });
68
+
69
+ test('stop writes checkmark to stderr', () => {
70
+ Object.defineProperty(process.stdin, 'isTTY', {
71
+ value: true,
72
+ writable: true,
73
+ });
74
+ Object.defineProperty(process.stdout, 'isTTY', {
75
+ value: true,
76
+ writable: true,
77
+ });
78
+ delete process.env.CI;
79
+ delete process.env.GITHUB_ACTIONS;
80
+ delete process.env.TERM;
81
+
82
+ stderrSpy = spyOn(process.stderr, 'write').mockImplementation(() => true);
83
+
84
+ const { createSpinner } = require('../../src/lib/spinner');
85
+ const spinner = createSpinner('loading...');
86
+ spinner.stop('completed');
87
+
88
+ const lastCall = stderrSpy.mock.calls[
89
+ stderrSpy.mock.calls.length - 1
90
+ ][0] as string;
91
+ expect(lastCall).toContain('✔');
92
+ expect(lastCall).toContain('completed');
93
+ });
94
+
95
+ test('fail writes cross mark to stderr', () => {
96
+ Object.defineProperty(process.stdin, 'isTTY', {
97
+ value: true,
98
+ writable: true,
99
+ });
100
+ Object.defineProperty(process.stdout, 'isTTY', {
101
+ value: true,
102
+ writable: true,
103
+ });
104
+ delete process.env.CI;
105
+ delete process.env.GITHUB_ACTIONS;
106
+ delete process.env.TERM;
107
+
108
+ stderrSpy = spyOn(process.stderr, 'write').mockImplementation(() => true);
109
+
110
+ const { createSpinner } = require('../../src/lib/spinner');
111
+ const spinner = createSpinner('loading...');
112
+ spinner.fail('error occurred');
113
+
114
+ const lastCall = stderrSpy.mock.calls[
115
+ stderrSpy.mock.calls.length - 1
116
+ ][0] as string;
117
+ expect(lastCall).toContain('✗');
118
+ expect(lastCall).toContain('error occurred');
119
+ });
120
+
121
+ test('warn writes warning icon to stderr', () => {
122
+ Object.defineProperty(process.stdin, 'isTTY', {
123
+ value: true,
124
+ writable: true,
125
+ });
126
+ Object.defineProperty(process.stdout, 'isTTY', {
127
+ value: true,
128
+ writable: true,
129
+ });
130
+ delete process.env.CI;
131
+ delete process.env.GITHUB_ACTIONS;
132
+ delete process.env.TERM;
133
+
134
+ stderrSpy = spyOn(process.stderr, 'write').mockImplementation(() => true);
135
+
136
+ const { createSpinner } = require('../../src/lib/spinner');
137
+ const spinner = createSpinner('loading...');
138
+ spinner.warn('watch out');
139
+
140
+ const lastCall = stderrSpy.mock.calls[
141
+ stderrSpy.mock.calls.length - 1
142
+ ][0] as string;
143
+ expect(lastCall).toContain('⚠');
144
+ expect(lastCall).toContain('watch out');
145
+ });
146
+ });
@@ -0,0 +1,63 @@
1
+ import { describe, expect, test } from 'bun:test';
2
+ import { renderTable } from '../../src/lib/table';
3
+
4
+ describe('renderTable', () => {
5
+ test('renders a table with correct border characters', () => {
6
+ const output = renderTable(['Name', 'ID'], [['Alice', 'abc-123']]);
7
+ expect(output).toContain('┌');
8
+ expect(output).toContain('┐');
9
+ expect(output).toContain('└');
10
+ expect(output).toContain('┘');
11
+ expect(output).toContain('│');
12
+ });
13
+
14
+ test('includes headers in output', () => {
15
+ const output = renderTable(
16
+ ['Name', 'Status'],
17
+ [['my-domain.com', 'verified']],
18
+ );
19
+ expect(output).toContain('Name');
20
+ expect(output).toContain('Status');
21
+ });
22
+
23
+ test('includes row data in output', () => {
24
+ const output = renderTable(
25
+ ['Name', 'Status'],
26
+ [['my-domain.com', 'verified']],
27
+ );
28
+ expect(output).toContain('my-domain.com');
29
+ expect(output).toContain('verified');
30
+ });
31
+
32
+ test('pads columns to the widest cell in each column', () => {
33
+ const output = renderTable(
34
+ ['Key', 'Value'],
35
+ [
36
+ ['short', 'a very long value here'],
37
+ ['much longer key', 'v'],
38
+ ],
39
+ );
40
+ // All rows should have the same line length
41
+ const lines = output.split('\n');
42
+ const lengths = lines.map((l) => l.length);
43
+ expect(new Set(lengths).size).toBe(1);
44
+ });
45
+
46
+ test('renders multiple rows', () => {
47
+ const output = renderTable(
48
+ ['A', 'B'],
49
+ [
50
+ ['row1a', 'row1b'],
51
+ ['row2a', 'row2b'],
52
+ ],
53
+ );
54
+ expect(output).toContain('row1a');
55
+ expect(output).toContain('row2a');
56
+ });
57
+
58
+ test('contains a separator row between header and data', () => {
59
+ const output = renderTable(['Col'], [['val']]);
60
+ expect(output).toContain('├');
61
+ expect(output).toContain('┤');
62
+ });
63
+ });
@@ -0,0 +1,85 @@
1
+ import { afterEach, describe, expect, test } from 'bun:test';
2
+ import { captureTestEnv } from '../helpers';
3
+
4
+ // We need to import the module fresh per test to pick up env changes,
5
+ // so we use dynamic imports.
6
+
7
+ describe('isInteractive', () => {
8
+ const restoreEnv = captureTestEnv();
9
+
10
+ afterEach(() => {
11
+ restoreEnv();
12
+ });
13
+
14
+ function setTTY(stdin: boolean, stdout: boolean) {
15
+ Object.defineProperty(process.stdin, 'isTTY', {
16
+ value: stdin ? true : undefined,
17
+ writable: true,
18
+ });
19
+ Object.defineProperty(process.stdout, 'isTTY', {
20
+ value: stdout ? true : undefined,
21
+ writable: true,
22
+ });
23
+ }
24
+
25
+ test('returns true when stdin and stdout are TTY and no CI env', () => {
26
+ setTTY(true, true);
27
+ delete process.env.CI;
28
+ delete process.env.GITHUB_ACTIONS;
29
+ delete process.env.TERM;
30
+
31
+ const { isInteractive } = require('../../src/lib/tty');
32
+ expect(isInteractive()).toBe(true);
33
+ });
34
+
35
+ test('returns false when stdin is not TTY', () => {
36
+ setTTY(false, true);
37
+ delete process.env.CI;
38
+
39
+ const { isInteractive } = require('../../src/lib/tty');
40
+ expect(isInteractive()).toBe(false);
41
+ });
42
+
43
+ test('returns false when stdout is not TTY', () => {
44
+ setTTY(true, false);
45
+ delete process.env.CI;
46
+
47
+ const { isInteractive } = require('../../src/lib/tty');
48
+ expect(isInteractive()).toBe(false);
49
+ });
50
+
51
+ test('returns false when CI=true', () => {
52
+ setTTY(true, true);
53
+ process.env.CI = 'true';
54
+
55
+ const { isInteractive } = require('../../src/lib/tty');
56
+ expect(isInteractive()).toBe(false);
57
+ });
58
+
59
+ test('returns false when CI=1', () => {
60
+ setTTY(true, true);
61
+ process.env.CI = '1';
62
+
63
+ const { isInteractive } = require('../../src/lib/tty');
64
+ expect(isInteractive()).toBe(false);
65
+ });
66
+
67
+ test('returns false when GITHUB_ACTIONS is set', () => {
68
+ setTTY(true, true);
69
+ delete process.env.CI;
70
+ process.env.GITHUB_ACTIONS = 'true';
71
+
72
+ const { isInteractive } = require('../../src/lib/tty');
73
+ expect(isInteractive()).toBe(false);
74
+ });
75
+
76
+ test('returns false when TERM=dumb', () => {
77
+ setTTY(true, true);
78
+ delete process.env.CI;
79
+ delete process.env.GITHUB_ACTIONS;
80
+ process.env.TERM = 'dumb';
81
+
82
+ const { isInteractive } = require('../../src/lib/tty');
83
+ expect(isInteractive()).toBe(false);
84
+ });
85
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "esModuleInterop": true,
7
+ "strict": true,
8
+ "skipLibCheck": true,
9
+ "outDir": "./dist",
10
+ "rootDir": "./src",
11
+ "types": ["bun"]
12
+ },
13
+ "include": ["src/**/*.ts"]
14
+ }
package/src/index.js DELETED
@@ -1,72 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- import enquirer from "enquirer"
4
- import * as fs from "fs/promises"
5
- import {homedir} from "os"
6
- import figlet from "figlet"
7
- import {promisify} from "util"
8
- const fig = promisify(figlet)
9
- import ora from "ora"
10
- import { Resend } from "resend"
11
- import routes from "./routes.js"
12
- import readline from "readline"
13
-
14
- if (process.platform === "win32") {
15
- var rl = readline.createInterface({
16
- input: process.stdin,
17
- output: process.stdout
18
- });
19
-
20
- rl.on("SIGINT", function () {
21
- process.emit("SIGINT");
22
- });
23
- }
24
-
25
- process.on("SIGINT", function () {
26
- //graceful shutdown
27
- console.log("\nExiting...");
28
- process.exit();
29
- });
30
-
31
- const text = await fig("resend-cli")
32
- console.log(text)
33
- const configPath = `${homedir()}/.resend_config.json`
34
- const fileExists = await fs.access(configPath).then(() => true).catch(() => false);
35
- if (fileExists) {
36
- let config, instance
37
- try {
38
- let fileContents = await fs.readFile(configPath)
39
- fileContents = fileContents + ''
40
- config = JSON.parse(fileContents)
41
- //console.log(config.apiKey)
42
- instance = new Resend(config.apiKey)
43
- } catch (e) {
44
- console.error("Error reading config file")
45
- console.error(e)
46
- process.exit(1)
47
- }
48
-
49
- try {
50
- await routes({resend: instance, apiKey: config.apiKey, config})
51
- } catch {} // this is needed so that Ctrl+C doesn't throw an exception
52
- } else {
53
- const apiKeyResponse = await enquirer.password({
54
- message: "Enter an API key with full access: "
55
- })
56
- const spinner = ora({text: "Authenticating...", spinner: "toggle9"}).start()
57
- const resend = new Resend(apiKeyResponse)
58
- const apiKeys = await resend.apiKeys.list()
59
- spinner.stop()
60
- if (apiKeys?.error?.statusCode === 400) {
61
- console.error("Invalid API key")
62
- process.exit(1)
63
- } else {
64
- const config = {
65
- apiKey: apiKeyResponse
66
- }
67
- await fs.writeFile(configPath, JSON.stringify(config))
68
- console.log("API key saved in plain text to ~/.resend_config.json.")
69
- console.log("Run `resend-cli` again to use the CLI.")
70
- process.exit(0)
71
- }
72
- }
package/src/routes.js DELETED
@@ -1,37 +0,0 @@
1
- import enquirer from "enquirer";
2
- import email from "./sections/email.js";
3
- import domain from "./sections/domain.js";
4
- import apikeys from "./sections/apikeys.js";
5
- import audiences from "./sections/audiences.js";
6
- import contacts from "./sections/contacts.js";
7
-
8
- export default async ({resend, apiKey, config}) => {
9
- const prompt = new enquirer.Select({
10
- message: "Choose a section",
11
- choices: [
12
- {name: "email", message: "Emails"},
13
- {name: "domain", message: "Domains"},
14
- {name: "apiKeys", message: "API Keys"},
15
- {name: "audiences", message: "Audiences"},
16
- {name: "contacts", message: "Contacts"},
17
- ]
18
- })
19
- const answer = await prompt.run()
20
- switch (answer) {
21
- case "email":
22
- await email({resend, apiKey, config})
23
- break
24
- case "domain":
25
- await domain({resend, apiKey, config})
26
- break
27
- case "apiKeys":
28
- await apikeys({resend, apiKey, config})
29
- break
30
- case "audiences":
31
- await audiences({resend, apiKey, config})
32
- break
33
- case "contacts":
34
- await contacts({resend, apiKey, config})
35
- break
36
- }
37
- }
@@ -1,99 +0,0 @@
1
- import ora from "ora"
2
- import enquirer from "enquirer";
3
-
4
- const logAPIKey = (apiKey) => {
5
- console.log("- ID: " + apiKey.id)
6
- if (apiKey.token) {
7
- console.log("- API Key: " + apiKey.token)
8
- }
9
- if (apiKey.name) {
10
- console.log("- Name: " + apiKey.name)
11
- }
12
- if (apiKey.created_at) {
13
- console.log("- Created At: " + apiKey.created_at)
14
- }
15
- }
16
-
17
- export default async function({resend}, apiKey) {
18
- const prompt = new enquirer.Select({
19
- message: "Choose an action",
20
- choices: [
21
- {name: "create", message: "Create API Key"},
22
- {name: "list", message: "List API Keys"},
23
- {name: "delete", message: "Delete API Key"},
24
- ]
25
- })
26
- const answer = await prompt.run()
27
- switch (answer) {
28
- case "create":
29
- {
30
- let form = new enquirer.Form({
31
- name: "createApiKey",
32
- message: "Create an API Key.\nPossible values for permission: full, sending.",
33
- choices: [
34
- {name: "name", message: "Name", initial: "My API Key"},
35
- {name: "permission", message: "Permission", initial: "full"}
36
- ]
37
- })
38
- form = await form.run()
39
- const name = form.name
40
- const permission = form.permission === "sending" ? "sending_access" : "full_access"
41
- const spinner = ora({text: "Creating API Key...", spinner: "toggle9"}).start()
42
- const apiKey = await resend.apiKeys.create({name, permission})
43
- spinner.stop()
44
- console.log("IMPORTANT! Save this API key in a safe place. It will not be shown again.")
45
- logAPIKey(apiKey.data)
46
- }
47
- break;
48
- case "list":
49
- {
50
- const spinner2 = ora({text: "Fetching API Keys...", spinner: "toggle9"}).start()
51
- const listApiKeysResponse = await resend.apiKeys.list()
52
- spinner2.stop()
53
- const choices = listApiKeysResponse.data.data.map((apiKey) => {
54
- return {message: apiKey.name, name: apiKey.id}
55
- })
56
- //console.log(choices)
57
- let answer = new enquirer.Select({
58
- name: "listApiKeys",
59
- message: "Choose an API Key to view info on",
60
- choices
61
- })
62
- answer = await answer.run()
63
- for (let apiKey of listApiKeysResponse.data.data) {
64
- if (apiKey.id === answer) {
65
- logAPIKey(apiKey)
66
- }
67
- }
68
- }
69
- break
70
- case "delete":
71
- {
72
- const spinner3 = ora({text: "Fetching API Keys...", spinner: "toggle9"}).start()
73
- const listApiKeysResponse2 = await resend.apiKeys.list()
74
- spinner3.stop()
75
- const choices2 = listApiKeysResponse2.data.data.map((apiKey) => {return {message: apiKey.name, name: apiKey.id}})
76
- let answer2 = new enquirer.Select({
77
- name: "deleteApiKey",
78
- message: "Choose an API Key to delete",
79
- choices: choices2
80
- })
81
- answer2 = await answer2.run()
82
- console.log(answer2)
83
- let confirmDelete = new enquirer.Confirm({
84
- message: "If this API Key is the one you make resend-cli with, you will not be able to use resend-cli anymore with the same key. This action is IRREVERSIBLE!!"
85
- })
86
- confirmDelete = await confirmDelete.run()
87
- if (!confirmDelete) {
88
- console.log("Aborting...")
89
- process.exit(0)
90
- }
91
- const spinner4 = ora({text: "Deleting API Key...", spinner: "toggle9"}).start()
92
- await resend.apiKeys.remove(answer2)
93
- spinner4.stop()
94
- console.log("API Key deleted.")
95
- }
96
- break
97
- }
98
- process.exit(0)
99
- }