@stacksjs/ts-cloud 0.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.
Files changed (117) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +321 -0
  3. package/bin/cli.ts +133 -0
  4. package/bin/commands/analytics.ts +328 -0
  5. package/bin/commands/api.ts +379 -0
  6. package/bin/commands/assets.ts +221 -0
  7. package/bin/commands/audit.ts +501 -0
  8. package/bin/commands/backup.ts +682 -0
  9. package/bin/commands/cache.ts +294 -0
  10. package/bin/commands/cdn.ts +281 -0
  11. package/bin/commands/config.ts +202 -0
  12. package/bin/commands/container.ts +105 -0
  13. package/bin/commands/cost.ts +208 -0
  14. package/bin/commands/database.ts +401 -0
  15. package/bin/commands/deploy.ts +674 -0
  16. package/bin/commands/domain.ts +397 -0
  17. package/bin/commands/email.ts +423 -0
  18. package/bin/commands/environment.ts +285 -0
  19. package/bin/commands/events.ts +424 -0
  20. package/bin/commands/firewall.ts +145 -0
  21. package/bin/commands/function.ts +116 -0
  22. package/bin/commands/generate.ts +280 -0
  23. package/bin/commands/git.ts +139 -0
  24. package/bin/commands/iam.ts +464 -0
  25. package/bin/commands/index.ts +48 -0
  26. package/bin/commands/init.ts +120 -0
  27. package/bin/commands/logs.ts +148 -0
  28. package/bin/commands/network.ts +579 -0
  29. package/bin/commands/notify.ts +489 -0
  30. package/bin/commands/queue.ts +407 -0
  31. package/bin/commands/scheduler.ts +370 -0
  32. package/bin/commands/secrets.ts +54 -0
  33. package/bin/commands/server.ts +629 -0
  34. package/bin/commands/shared.ts +97 -0
  35. package/bin/commands/ssl.ts +138 -0
  36. package/bin/commands/stack.ts +325 -0
  37. package/bin/commands/status.ts +385 -0
  38. package/bin/commands/storage.ts +450 -0
  39. package/bin/commands/team.ts +96 -0
  40. package/bin/commands/tunnel.ts +489 -0
  41. package/bin/commands/utils.ts +202 -0
  42. package/build.ts +15 -0
  43. package/cloud +2 -0
  44. package/package.json +99 -0
  45. package/src/aws/acm.ts +768 -0
  46. package/src/aws/application-autoscaling.ts +845 -0
  47. package/src/aws/bedrock.ts +4074 -0
  48. package/src/aws/client.ts +878 -0
  49. package/src/aws/cloudformation.ts +896 -0
  50. package/src/aws/cloudfront.ts +1531 -0
  51. package/src/aws/cloudwatch-logs.ts +154 -0
  52. package/src/aws/comprehend.ts +839 -0
  53. package/src/aws/connect.ts +1056 -0
  54. package/src/aws/deploy-imap.ts +384 -0
  55. package/src/aws/dynamodb.ts +340 -0
  56. package/src/aws/ec2.ts +1385 -0
  57. package/src/aws/ecr.ts +621 -0
  58. package/src/aws/ecs.ts +615 -0
  59. package/src/aws/elasticache.ts +301 -0
  60. package/src/aws/elbv2.ts +942 -0
  61. package/src/aws/email.ts +928 -0
  62. package/src/aws/eventbridge.ts +248 -0
  63. package/src/aws/iam.ts +1689 -0
  64. package/src/aws/imap-server.ts +2100 -0
  65. package/src/aws/index.ts +213 -0
  66. package/src/aws/kendra.ts +1097 -0
  67. package/src/aws/lambda.ts +786 -0
  68. package/src/aws/opensearch.ts +158 -0
  69. package/src/aws/personalize.ts +977 -0
  70. package/src/aws/polly.ts +559 -0
  71. package/src/aws/rds.ts +888 -0
  72. package/src/aws/rekognition.ts +846 -0
  73. package/src/aws/route53-domains.ts +359 -0
  74. package/src/aws/route53.ts +1046 -0
  75. package/src/aws/s3.ts +2318 -0
  76. package/src/aws/scheduler.ts +571 -0
  77. package/src/aws/secrets-manager.ts +769 -0
  78. package/src/aws/ses.ts +1081 -0
  79. package/src/aws/setup-phone.ts +104 -0
  80. package/src/aws/setup-sms.ts +580 -0
  81. package/src/aws/sms.ts +1735 -0
  82. package/src/aws/smtp-server.ts +531 -0
  83. package/src/aws/sns.ts +758 -0
  84. package/src/aws/sqs.ts +382 -0
  85. package/src/aws/ssm.ts +807 -0
  86. package/src/aws/sts.ts +92 -0
  87. package/src/aws/support.ts +391 -0
  88. package/src/aws/test-imap.ts +86 -0
  89. package/src/aws/textract.ts +780 -0
  90. package/src/aws/transcribe.ts +108 -0
  91. package/src/aws/translate.ts +641 -0
  92. package/src/aws/voice.ts +1379 -0
  93. package/src/config.ts +35 -0
  94. package/src/deploy/index.ts +7 -0
  95. package/src/deploy/static-site-external-dns.ts +906 -0
  96. package/src/deploy/static-site.ts +1125 -0
  97. package/src/dns/godaddy.ts +412 -0
  98. package/src/dns/index.ts +183 -0
  99. package/src/dns/porkbun.ts +362 -0
  100. package/src/dns/route53-adapter.ts +414 -0
  101. package/src/dns/types.ts +114 -0
  102. package/src/dns/validator.ts +369 -0
  103. package/src/generators/index.ts +5 -0
  104. package/src/generators/infrastructure.ts +1660 -0
  105. package/src/index.ts +163 -0
  106. package/src/push/apns.ts +452 -0
  107. package/src/push/fcm.ts +506 -0
  108. package/src/push/index.ts +58 -0
  109. package/src/ssl/acme-client.ts +478 -0
  110. package/src/ssl/index.ts +7 -0
  111. package/src/ssl/letsencrypt.ts +747 -0
  112. package/src/types.ts +2 -0
  113. package/src/utils/cli.ts +398 -0
  114. package/src/validation/index.ts +5 -0
  115. package/src/validation/template.ts +405 -0
  116. package/test/index.test.ts +128 -0
  117. package/tsconfig.json +18 -0
@@ -0,0 +1,294 @@
1
+ import type { CLI } from '@stacksjs/clapp'
2
+ import * as cli from '../../src/utils/cli'
3
+ import { ElastiCacheClient } from '../../src/aws/elasticache'
4
+ import { loadValidatedConfig } from './shared'
5
+
6
+ export function registerCacheCommands(app: CLI): void {
7
+ app
8
+ .command('cache:list', 'List all ElastiCache clusters')
9
+ .option('--region <region>', 'AWS region')
10
+ .action(async (options: { region?: string }) => {
11
+ cli.header('ElastiCache Clusters')
12
+
13
+ try {
14
+ const config = await loadValidatedConfig()
15
+ const region = options.region || config.project.region || 'us-east-1'
16
+ const elasticache = new ElastiCacheClient(region)
17
+
18
+ const spinner = new cli.Spinner('Fetching clusters...')
19
+ spinner.start()
20
+
21
+ const result = await elasticache.describeCacheClusters()
22
+ const clusters = result.CacheClusters || []
23
+
24
+ spinner.succeed(`Found ${clusters.length} cluster(s)`)
25
+
26
+ if (clusters.length === 0) {
27
+ cli.info('No ElastiCache clusters found')
28
+ cli.info('Use `cloud cache:create` to create a new cluster')
29
+ return
30
+ }
31
+
32
+ cli.table(
33
+ ['Cluster ID', 'Engine', 'Node Type', 'Nodes', 'Status'],
34
+ clusters.map(cluster => [
35
+ cluster.CacheClusterId || 'N/A',
36
+ `${cluster.Engine || 'N/A'} ${cluster.EngineVersion || ''}`,
37
+ cluster.CacheNodeType || 'N/A',
38
+ (cluster.NumCacheNodes || 0).toString(),
39
+ cluster.CacheClusterStatus || 'N/A',
40
+ ]),
41
+ )
42
+ }
43
+ catch (error: any) {
44
+ cli.error(`Failed to list clusters: ${error.message}`)
45
+ process.exit(1)
46
+ }
47
+ })
48
+
49
+ app
50
+ .command('cache:create <name>', 'Create a new ElastiCache cluster')
51
+ .option('--engine <engine>', 'Cache engine (redis or memcached)', { default: 'redis' })
52
+ .option('--node-type <type>', 'Node instance type', { default: 'cache.t3.micro' })
53
+ .option('--nodes <number>', 'Number of cache nodes', { default: '1' })
54
+ .option('--region <region>', 'AWS region', { default: 'us-east-1' })
55
+ .option('--version <version>', 'Engine version')
56
+ .action(async (name: string, options: { engine: string; nodeType: string; nodes: string; region: string; version?: string }) => {
57
+ cli.header('Create ElastiCache Cluster')
58
+
59
+ try {
60
+ const elasticache = new ElastiCacheClient(options.region)
61
+
62
+ cli.info(`Cluster ID: ${name}`)
63
+ cli.info(`Engine: ${options.engine}`)
64
+ cli.info(`Node Type: ${options.nodeType}`)
65
+ cli.info(`Number of Nodes: ${options.nodes}`)
66
+ cli.info(`Region: ${options.region}`)
67
+
68
+ const confirmed = await cli.confirm('\nCreate this cluster?', true)
69
+ if (!confirmed) {
70
+ cli.info('Operation cancelled')
71
+ return
72
+ }
73
+
74
+ const spinner = new cli.Spinner('Creating cluster...')
75
+ spinner.start()
76
+
77
+ await elasticache.createCacheCluster({
78
+ cacheClusterId: name,
79
+ engine: options.engine as 'redis' | 'memcached',
80
+ cacheNodeType: options.nodeType,
81
+ numCacheNodes: Number.parseInt(options.nodes),
82
+ engineVersion: options.version,
83
+ })
84
+
85
+ spinner.succeed('Cluster creation initiated')
86
+
87
+ cli.success(`\nCluster: ${name}`)
88
+ cli.info('Status: creating')
89
+ cli.info('\nNote: Cluster creation may take several minutes.')
90
+ cli.info('Use `cloud cache:list` to check status.')
91
+ }
92
+ catch (error: any) {
93
+ cli.error(`Failed to create cluster: ${error.message}`)
94
+ process.exit(1)
95
+ }
96
+ })
97
+
98
+ app
99
+ .command('cache:delete <clusterId>', 'Delete an ElastiCache cluster')
100
+ .option('--region <region>', 'AWS region', { default: 'us-east-1' })
101
+ .action(async (clusterId: string, options: { region: string }) => {
102
+ cli.header('Delete ElastiCache Cluster')
103
+
104
+ try {
105
+ const elasticache = new ElastiCacheClient(options.region)
106
+
107
+ cli.warn(`This will permanently delete cluster: ${clusterId}`)
108
+ cli.warn('All data in the cluster will be lost!')
109
+
110
+ const confirmed = await cli.confirm('\nDelete this cluster?', false)
111
+ if (!confirmed) {
112
+ cli.info('Operation cancelled')
113
+ return
114
+ }
115
+
116
+ const spinner = new cli.Spinner('Deleting cluster...')
117
+ spinner.start()
118
+
119
+ await elasticache.deleteCacheCluster(clusterId)
120
+
121
+ spinner.succeed('Cluster deletion initiated')
122
+
123
+ cli.info('\nNote: Cluster deletion may take several minutes.')
124
+ }
125
+ catch (error: any) {
126
+ cli.error(`Failed to delete cluster: ${error.message}`)
127
+ process.exit(1)
128
+ }
129
+ })
130
+
131
+ app
132
+ .command('cache:stats <clusterId>', 'Show ElastiCache cluster statistics')
133
+ .option('--region <region>', 'AWS region', { default: 'us-east-1' })
134
+ .action(async (clusterId: string, options: { region: string }) => {
135
+ cli.header(`ElastiCache Stats: ${clusterId}`)
136
+
137
+ try {
138
+ const elasticache = new ElastiCacheClient(options.region)
139
+
140
+ const spinner = new cli.Spinner('Fetching cluster details...')
141
+ spinner.start()
142
+
143
+ const result = await elasticache.describeCacheClusters(clusterId)
144
+
145
+ const cluster = result.CacheClusters?.[0]
146
+
147
+ if (!cluster) {
148
+ spinner.fail('Cluster not found')
149
+ return
150
+ }
151
+
152
+ spinner.succeed('Cluster details loaded')
153
+
154
+ cli.info('\nCluster Information:')
155
+ cli.info(` Cluster ID: ${cluster.CacheClusterId}`)
156
+ cli.info(` Engine: ${cluster.Engine} ${cluster.EngineVersion}`)
157
+ cli.info(` Node Type: ${cluster.CacheNodeType}`)
158
+ cli.info(` Status: ${cluster.CacheClusterStatus}`)
159
+ cli.info(` Nodes: ${cluster.NumCacheNodes}`)
160
+ cli.info(` Availability Zone: ${cluster.PreferredAvailabilityZone || 'Not set'}`)
161
+
162
+ if (cluster.CacheNodes && cluster.CacheNodes.length > 0) {
163
+ cli.info('\nCache Nodes:')
164
+ for (const node of cluster.CacheNodes) {
165
+ cli.info(` - ${node.CacheNodeId}: ${node.CacheNodeStatus}`)
166
+ if (node.Endpoint) {
167
+ cli.info(` Endpoint: ${node.Endpoint.Address}:${node.Endpoint.Port}`)
168
+ }
169
+ }
170
+ }
171
+ }
172
+ catch (error: any) {
173
+ cli.error(`Failed to get cluster stats: ${error.message}`)
174
+ process.exit(1)
175
+ }
176
+ })
177
+
178
+ app
179
+ .command('cache:flush <clusterId>', 'Flush all data from a Redis cluster')
180
+ .option('--region <region>', 'AWS region', { default: 'us-east-1' })
181
+ .action(async (clusterId: string, options: { region: string }) => {
182
+ cli.header('Flush ElastiCache Cluster')
183
+
184
+ try {
185
+ const elasticache = new ElastiCacheClient(options.region)
186
+
187
+ // First get cluster info to get the endpoint
188
+ const spinner = new cli.Spinner('Getting cluster endpoint...')
189
+ spinner.start()
190
+
191
+ const result = await elasticache.describeCacheClusters(clusterId)
192
+
193
+ const cluster = result.CacheClusters?.[0]
194
+
195
+ if (!cluster) {
196
+ spinner.fail('Cluster not found')
197
+ return
198
+ }
199
+
200
+ if (cluster.Engine !== 'redis') {
201
+ spinner.fail('Flush is only supported for Redis clusters')
202
+ cli.info('For Memcached, items expire automatically based on TTL.')
203
+ return
204
+ }
205
+
206
+ spinner.stop()
207
+
208
+ const endpoint = cluster.CacheNodes?.[0]?.Endpoint
209
+
210
+ if (!endpoint) {
211
+ cli.error('Could not determine cluster endpoint')
212
+ return
213
+ }
214
+
215
+ cli.warn(`This will delete ALL data in cluster: ${clusterId}`)
216
+ cli.warn(`Endpoint: ${endpoint.Address}:${endpoint.Port}`)
217
+ cli.warn('This action cannot be undone!')
218
+
219
+ const confirmed = await cli.confirm('\nFlush all data?', false)
220
+ if (!confirmed) {
221
+ cli.info('Operation cancelled')
222
+ return
223
+ }
224
+
225
+ cli.info('\nTo flush the Redis cluster, connect and run FLUSHALL:')
226
+ cli.info(` redis-cli -h ${endpoint.Address} -p ${endpoint.Port} FLUSHALL`)
227
+ cli.info('\nNote: Direct FLUSHALL via AWS API is not supported.')
228
+ cli.info('You must connect to the cluster directly to execute this command.')
229
+ }
230
+ catch (error: any) {
231
+ cli.error(`Failed to get cluster info: ${error.message}`)
232
+ process.exit(1)
233
+ }
234
+ })
235
+
236
+ app
237
+ .command('cache:reboot <clusterId>', 'Reboot cache cluster nodes')
238
+ .option('--region <region>', 'AWS region', { default: 'us-east-1' })
239
+ .option('--node <nodeId>', 'Specific node ID to reboot')
240
+ .action(async (clusterId: string, options: { region: string; node?: string }) => {
241
+ cli.header('Reboot ElastiCache Cluster')
242
+
243
+ try {
244
+ const elasticache = new ElastiCacheClient(options.region)
245
+
246
+ // Get cluster info first
247
+ const spinner = new cli.Spinner('Getting cluster info...')
248
+ spinner.start()
249
+
250
+ const result = await elasticache.describeCacheClusters(clusterId)
251
+
252
+ const cluster = result.CacheClusters?.[0]
253
+
254
+ if (!cluster) {
255
+ spinner.fail('Cluster not found')
256
+ return
257
+ }
258
+
259
+ spinner.stop()
260
+
261
+ const nodeIds = options.node
262
+ ? [options.node]
263
+ : cluster.CacheNodes?.map(n => n.CacheNodeId).filter(Boolean) || []
264
+
265
+ if (nodeIds.length === 0) {
266
+ cli.error('No nodes found to reboot')
267
+ return
268
+ }
269
+
270
+ cli.warn(`This will reboot the following nodes: ${nodeIds.join(', ')}`)
271
+ cli.warn('The cluster may be temporarily unavailable during reboot.')
272
+
273
+ const confirmed = await cli.confirm('\nReboot these nodes?', false)
274
+ if (!confirmed) {
275
+ cli.info('Operation cancelled')
276
+ return
277
+ }
278
+
279
+ const rebootSpinner = new cli.Spinner('Rebooting nodes...')
280
+ rebootSpinner.start()
281
+
282
+ await elasticache.rebootCacheCluster(clusterId, nodeIds)
283
+
284
+ rebootSpinner.succeed('Reboot initiated')
285
+
286
+ cli.info('\nNote: Reboot may take several minutes to complete.')
287
+ cli.info('Use `cloud cache:stats` to check status.')
288
+ }
289
+ catch (error: any) {
290
+ cli.error(`Failed to reboot cluster: ${error.message}`)
291
+ process.exit(1)
292
+ }
293
+ })
294
+ }
@@ -0,0 +1,281 @@
1
+ import type { CLI } from '@stacksjs/clapp'
2
+ import * as cli from '../../src/utils/cli'
3
+ import type { Distribution } from '../../src/aws/cloudfront'
4
+ import { CloudFrontClient } from '../../src/aws/cloudfront'
5
+ import { loadValidatedConfig } from './shared'
6
+
7
+ export function registerCdnCommands(app: CLI): void {
8
+ app
9
+ .command('cdn:list', 'List all CloudFront distributions')
10
+ .action(async () => {
11
+ cli.header('CloudFront Distributions')
12
+
13
+ try {
14
+ const config = await loadValidatedConfig()
15
+ const cloudfront = new CloudFrontClient()
16
+
17
+ const spinner = new cli.Spinner('Fetching distributions...')
18
+ spinner.start()
19
+
20
+ const distributions = await cloudfront.listDistributions()
21
+
22
+ spinner.succeed(`Found ${distributions.length} distribution(s)`)
23
+
24
+ if (distributions.length === 0) {
25
+ cli.info('No CloudFront distributions found')
26
+ cli.info('Use `cloud cdn:create` to create a new distribution')
27
+ return
28
+ }
29
+
30
+ cli.table(
31
+ ['ID', 'Domain', 'Status', 'Enabled', 'Aliases'],
32
+ distributions.map((dist: Distribution) => [
33
+ dist.Id || 'N/A',
34
+ dist.DomainName || 'N/A',
35
+ dist.Status || 'N/A',
36
+ dist.Enabled ? 'Yes' : 'No',
37
+ (dist.Aliases?.Items?.length || 0).toString(),
38
+ ]),
39
+ )
40
+ }
41
+ catch (error: any) {
42
+ cli.error(`Failed to list distributions: ${error.message}`)
43
+ process.exit(1)
44
+ }
45
+ })
46
+
47
+ app
48
+ .command('cdn:status <distributionId>', 'Get CloudFront distribution status')
49
+ .action(async (distributionId: string) => {
50
+ cli.header(`CloudFront Distribution: ${distributionId}`)
51
+
52
+ try {
53
+ const cloudfront = new CloudFrontClient()
54
+
55
+ const spinner = new cli.Spinner('Fetching distribution details...')
56
+ spinner.start()
57
+
58
+ const dist = await cloudfront.getDistribution(distributionId)
59
+
60
+ spinner.succeed('Distribution details loaded')
61
+
62
+ cli.info('\nDistribution Information:')
63
+ cli.info(` ID: ${dist.Id}`)
64
+ cli.info(` Domain: ${dist.DomainName}`)
65
+ cli.info(` Status: ${dist.Status}`)
66
+ cli.info(` ARN: ${dist.ARN}`)
67
+ cli.info(` Enabled: ${dist.Enabled ? 'Yes' : 'No'}`)
68
+
69
+ if (dist.Aliases?.Items?.length) {
70
+ cli.info(` Aliases: ${dist.Aliases.Items.join(', ')}`)
71
+ }
72
+
73
+ // Fetch full config for origin and cache behavior details
74
+ const config = await cloudfront.getDistributionConfig(distributionId)
75
+
76
+ cli.info('\nOrigins:')
77
+ const origins = config.DistributionConfig?.Origins?.Items || []
78
+ if (Array.isArray(origins)) {
79
+ for (const origin of origins) {
80
+ cli.info(` - ${origin.Id}: ${origin.DomainName}`)
81
+ }
82
+ }
83
+
84
+ if (config.DistributionConfig?.DefaultCacheBehavior) {
85
+ const behavior = config.DistributionConfig.DefaultCacheBehavior
86
+ cli.info('\nDefault Cache Behavior:')
87
+ cli.info(` Target Origin: ${behavior.TargetOriginId}`)
88
+ cli.info(` Viewer Protocol: ${behavior.ViewerProtocolPolicy}`)
89
+ }
90
+ }
91
+ catch (error: any) {
92
+ cli.error(`Failed to get distribution: ${error.message}`)
93
+ process.exit(1)
94
+ }
95
+ })
96
+
97
+ app
98
+ .command('cdn:create', 'Create a new CloudFront distribution for an S3 bucket')
99
+ .option('--bucket <name>', 'S3 bucket name')
100
+ .option('--region <region>', 'S3 bucket region', { default: 'us-east-1' })
101
+ .option('--alias <domain>', 'Custom domain alias (CNAME)')
102
+ .option('--certificate <arn>', 'ACM certificate ARN for custom domain')
103
+ .option('--comment <text>', 'Distribution comment/description')
104
+ .action(async (options: { bucket?: string; region: string; alias?: string; certificate?: string; comment?: string }) => {
105
+ cli.header('Create CloudFront Distribution')
106
+
107
+ try {
108
+ if (!options.bucket) {
109
+ cli.error('--bucket is required')
110
+ cli.info('Example: cloud cdn:create --bucket my-bucket --region us-east-1')
111
+ process.exit(1)
112
+ }
113
+
114
+ const cloudfront = new CloudFrontClient()
115
+
116
+ cli.info(`Bucket: ${options.bucket}`)
117
+ cli.info(`Region: ${options.region}`)
118
+ if (options.alias) {
119
+ cli.info(`Alias: ${options.alias}`)
120
+ }
121
+
122
+ const confirmed = await cli.confirm('\nCreate this distribution?', true)
123
+ if (!confirmed) {
124
+ cli.info('Operation cancelled')
125
+ return
126
+ }
127
+
128
+ const spinner = new cli.Spinner('Setting up Origin Access Control...')
129
+ spinner.start()
130
+
131
+ // Find or create an OAC for S3
132
+ const oac = await cloudfront.findOrCreateOriginAccessControl(`OAC-${options.bucket}`)
133
+ if (oac.isNew) {
134
+ spinner.text = 'Created new Origin Access Control'
135
+ }
136
+
137
+ spinner.text = 'Creating distribution...'
138
+
139
+ const aliases = options.alias ? [options.alias] : []
140
+
141
+ const result = await cloudfront.createDistributionForS3({
142
+ bucketName: options.bucket,
143
+ bucketRegion: options.region,
144
+ originAccessControlId: oac.Id,
145
+ aliases,
146
+ certificateArn: options.certificate,
147
+ comment: options.comment || `Created by ts-cloud CLI`,
148
+ })
149
+
150
+ spinner.succeed('Distribution created')
151
+
152
+ cli.success(`\nDistribution ID: ${result.Id}`)
153
+ cli.info(`Domain: ${result.DomainName}`)
154
+ cli.info(`Status: ${result.Status}`)
155
+ cli.info('\nNote: Distribution deployment may take 15-30 minutes.')
156
+
157
+ // Show the S3 bucket policy that needs to be applied
158
+ cli.info('\nIMPORTANT: Update your S3 bucket policy to allow CloudFront access:')
159
+ const policy = CloudFrontClient.getS3BucketPolicyForCloudFront(options.bucket, result.ARN)
160
+ cli.info(JSON.stringify(policy, null, 2))
161
+ }
162
+ catch (error: any) {
163
+ cli.error(`Failed to create distribution: ${error.message}`)
164
+ process.exit(1)
165
+ }
166
+ })
167
+
168
+ app
169
+ .command('cdn:invalidate <distributionId>', 'Invalidate CloudFront cache')
170
+ .option('--paths <paths>', 'Paths to invalidate (comma-separated)', { default: '/*' })
171
+ .action(async (distributionId: string, options: { paths: string }) => {
172
+ cli.header('Invalidate CloudFront Cache')
173
+
174
+ try {
175
+ const cloudfront = new CloudFrontClient()
176
+
177
+ const paths = options.paths.split(',').map(p => p.trim())
178
+
179
+ cli.info(`Distribution: ${distributionId}`)
180
+ cli.info(`Paths: ${paths.join(', ')}`)
181
+
182
+ const confirmed = await cli.confirm('\nCreate invalidation?', true)
183
+ if (!confirmed) {
184
+ cli.info('Operation cancelled')
185
+ return
186
+ }
187
+
188
+ const spinner = new cli.Spinner('Creating invalidation...')
189
+ spinner.start()
190
+
191
+ const result = await cloudfront.createInvalidation({
192
+ distributionId,
193
+ paths,
194
+ callerReference: `cli-${Date.now()}`,
195
+ })
196
+
197
+ spinner.succeed('Invalidation created')
198
+
199
+ cli.success(`\nInvalidation ID: ${result.Id}`)
200
+ cli.info(`Status: ${result.Status}`)
201
+ cli.info('\nNote: Invalidation typically completes in 5-10 minutes.')
202
+ }
203
+ catch (error: any) {
204
+ cli.error(`Failed to create invalidation: ${error.message}`)
205
+ process.exit(1)
206
+ }
207
+ })
208
+
209
+ app
210
+ .command('cdn:disable <distributionId>', 'Disable a CloudFront distribution')
211
+ .action(async (distributionId: string) => {
212
+ cli.header('Disable CloudFront Distribution')
213
+
214
+ try {
215
+ const cloudfront = new CloudFrontClient()
216
+
217
+ cli.warn(`This will disable distribution: ${distributionId}`)
218
+ cli.info('The distribution will stop serving content.')
219
+
220
+ const confirmed = await cli.confirm('\nDisable this distribution?', false)
221
+ if (!confirmed) {
222
+ cli.info('Operation cancelled')
223
+ return
224
+ }
225
+
226
+ const spinner = new cli.Spinner('Disabling distribution...')
227
+ spinner.start()
228
+
229
+ await cloudfront.disableDistribution(distributionId)
230
+
231
+ spinner.succeed('Distribution disabled')
232
+
233
+ cli.info('\nNote: Changes may take 15-30 minutes to propagate.')
234
+ cli.info('To delete the distribution, wait for it to be fully disabled first.')
235
+ }
236
+ catch (error: any) {
237
+ cli.error(`Failed to disable distribution: ${error.message}`)
238
+ process.exit(1)
239
+ }
240
+ })
241
+
242
+ app
243
+ .command('cdn:delete <distributionId>', 'Delete a CloudFront distribution')
244
+ .action(async (distributionId: string) => {
245
+ cli.header('Delete CloudFront Distribution')
246
+
247
+ try {
248
+ const cloudfront = new CloudFrontClient()
249
+
250
+ cli.warn(`This will permanently delete distribution: ${distributionId}`)
251
+ cli.warn('The distribution must be disabled first.')
252
+
253
+ const confirmed = await cli.confirm('\nDelete this distribution?', false)
254
+ if (!confirmed) {
255
+ cli.info('Operation cancelled')
256
+ return
257
+ }
258
+
259
+ const spinner = new cli.Spinner('Checking distribution status...')
260
+ spinner.start()
261
+
262
+ // Get distribution to check status
263
+ const dist = await cloudfront.getDistribution(distributionId)
264
+ if (dist.Status !== 'Deployed' || dist.Enabled) {
265
+ spinner.fail('Distribution must be disabled and deployed before deletion')
266
+ cli.info('\nRun `cloud cdn:disable` first and wait for status to be "Deployed"')
267
+ return
268
+ }
269
+
270
+ spinner.text = 'Deleting distribution...'
271
+
272
+ await cloudfront.deleteDistribution(distributionId)
273
+
274
+ spinner.succeed('Distribution deleted')
275
+ }
276
+ catch (error: any) {
277
+ cli.error(`Failed to delete distribution: ${error.message}`)
278
+ process.exit(1)
279
+ }
280
+ })
281
+ }