@fastly/compute-js-static-publish 8.0.1 → 8.1.0-beta.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.
Files changed (247) hide show
  1. package/DOCUMENTATION.md +792 -35
  2. package/README.md +13 -3
  3. package/README.short.md +13 -3
  4. package/build/cli/cli/commands/manage/clean.d.ts.map +1 -1
  5. package/build/cli/cli/commands/manage/clean.js +14 -8
  6. package/build/cli/cli/commands/manage/collections/delete.d.ts.map +1 -1
  7. package/build/cli/cli/commands/manage/collections/delete.js +32 -15
  8. package/build/cli/cli/commands/manage/collections/index.d.ts.map +1 -1
  9. package/build/cli/cli/commands/manage/collections/index.js +10 -4
  10. package/build/cli/cli/commands/manage/collections/list.d.ts.map +1 -1
  11. package/build/cli/cli/commands/manage/collections/list.js +49 -8
  12. package/build/cli/cli/commands/manage/collections/promote.d.ts.map +1 -1
  13. package/build/cli/cli/commands/manage/collections/promote.js +69 -21
  14. package/build/cli/cli/commands/manage/collections/update-expiration.d.ts.map +1 -1
  15. package/build/cli/cli/commands/manage/collections/update-expiration.js +12 -12
  16. package/build/cli/cli/commands/manage/deploy.d.ts +2 -0
  17. package/build/cli/cli/commands/manage/deploy.d.ts.map +1 -0
  18. package/build/cli/cli/commands/manage/deploy.js +194 -0
  19. package/build/cli/cli/commands/manage/dev.d.ts +2 -0
  20. package/build/cli/cli/commands/manage/dev.d.ts.map +1 -0
  21. package/build/cli/cli/commands/manage/dev.js +78 -0
  22. package/build/cli/cli/commands/manage/eject.d.ts +2 -0
  23. package/build/cli/cli/commands/manage/eject.d.ts.map +1 -0
  24. package/build/cli/cli/commands/manage/eject.js +155 -0
  25. package/build/cli/cli/commands/manage/index.d.ts.map +1 -1
  26. package/build/cli/cli/commands/manage/index.js +42 -5
  27. package/build/cli/cli/commands/manage/publish-content.d.ts.map +1 -1
  28. package/build/cli/cli/commands/manage/publish-content.js +83 -37
  29. package/build/cli/cli/commands/manage/status.d.ts +2 -0
  30. package/build/cli/cli/commands/manage/status.d.ts.map +1 -0
  31. package/build/cli/cli/commands/manage/status.js +162 -0
  32. package/build/cli/cli/commands/manage/template/index.d.ts +2 -0
  33. package/build/cli/cli/commands/manage/template/index.d.ts.map +1 -0
  34. package/build/cli/cli/commands/manage/template/index.js +43 -0
  35. package/build/cli/cli/commands/manage/template/init.d.ts +13 -0
  36. package/build/cli/cli/commands/manage/template/init.d.ts.map +1 -0
  37. package/build/cli/cli/commands/manage/template/init.js +172 -0
  38. package/build/cli/cli/commands/manage/upgrade.d.ts +2 -0
  39. package/build/cli/cli/commands/manage/upgrade.d.ts.map +1 -0
  40. package/build/cli/cli/commands/manage/upgrade.js +59 -0
  41. package/build/cli/cli/commands/run-dev-watch.d.ts +8 -0
  42. package/build/cli/cli/commands/run-dev-watch.d.ts.map +1 -0
  43. package/build/cli/cli/commands/run-dev-watch.js +41 -0
  44. package/build/cli/cli/commands/run-template-hook.d.ts +2 -0
  45. package/build/cli/cli/commands/run-template-hook.d.ts.map +1 -0
  46. package/build/cli/cli/commands/run-template-hook.js +28 -0
  47. package/build/cli/cli/commands/scaffold/app-template.d.ts +17 -0
  48. package/build/cli/cli/commands/scaffold/app-template.d.ts.map +1 -0
  49. package/build/cli/cli/commands/scaffold/app-template.js +54 -0
  50. package/build/cli/cli/commands/scaffold/fastly-toml-sections.d.ts +5 -0
  51. package/build/cli/cli/commands/scaffold/fastly-toml-sections.d.ts.map +1 -0
  52. package/build/cli/cli/commands/scaffold/fastly-toml-sections.js +154 -0
  53. package/build/cli/cli/commands/scaffold/generate-app.d.ts +47 -0
  54. package/build/cli/cli/commands/scaffold/generate-app.d.ts.map +1 -0
  55. package/build/cli/cli/commands/scaffold/generate-app.js +164 -0
  56. package/build/cli/cli/commands/scaffold/index.d.ts +6 -0
  57. package/build/cli/cli/commands/scaffold/index.d.ts.map +1 -1
  58. package/build/cli/cli/commands/scaffold/index.js +208 -290
  59. package/build/cli/cli/commands/scaffold/render.d.ts +7 -0
  60. package/build/cli/cli/commands/scaffold/render.d.ts.map +1 -0
  61. package/build/cli/cli/commands/scaffold/render.js +72 -0
  62. package/build/cli/cli/hono/build.d.ts +43 -0
  63. package/build/cli/cli/hono/build.d.ts.map +1 -0
  64. package/build/cli/cli/hono/build.js +298 -0
  65. package/build/cli/cli/index.js +98 -5
  66. package/build/cli/cli/managed/app.d.ts +21 -0
  67. package/build/cli/cli/managed/app.d.ts.map +1 -0
  68. package/build/cli/cli/managed/app.js +162 -0
  69. package/build/cli/cli/managed/command-helpers.d.ts +6 -0
  70. package/build/cli/cli/managed/command-helpers.d.ts.map +1 -0
  71. package/build/cli/cli/managed/command-helpers.js +81 -0
  72. package/build/cli/cli/managed/eject.d.ts +6 -0
  73. package/build/cli/cli/managed/eject.d.ts.map +1 -0
  74. package/build/cli/cli/managed/eject.js +68 -0
  75. package/build/cli/cli/managed/fastly-service.d.ts +8 -0
  76. package/build/cli/cli/managed/fastly-service.d.ts.map +1 -0
  77. package/build/cli/cli/managed/fastly-service.js +21 -0
  78. package/build/cli/cli/managed/fingerprint.d.ts +13 -0
  79. package/build/cli/cli/managed/fingerprint.d.ts.map +1 -0
  80. package/build/cli/cli/managed/fingerprint.js +60 -0
  81. package/build/cli/cli/managed/lockfile.d.ts +31 -0
  82. package/build/cli/cli/managed/lockfile.d.ts.map +1 -0
  83. package/build/cli/cli/managed/lockfile.js +62 -0
  84. package/build/cli/cli/managed/project-edit.d.ts +3 -0
  85. package/build/cli/cli/managed/project-edit.d.ts.map +1 -0
  86. package/build/cli/cli/managed/project-edit.js +35 -0
  87. package/build/cli/cli/managed/project-file.d.ts +16 -0
  88. package/build/cli/cli/managed/project-file.d.ts.map +1 -0
  89. package/build/cli/cli/managed/project-file.js +119 -0
  90. package/build/cli/cli/managed/template-init.d.ts +12 -0
  91. package/build/cli/cli/managed/template-init.d.ts.map +1 -0
  92. package/build/cli/cli/managed/template-init.js +120 -0
  93. package/build/cli/cli/managed/version-check.d.ts +25 -0
  94. package/build/cli/cli/managed/version-check.d.ts.map +1 -0
  95. package/build/cli/cli/managed/version-check.js +84 -0
  96. package/build/cli/cli/storage/kv-store-local-provider.d.ts +1 -0
  97. package/build/cli/cli/storage/kv-store-local-provider.d.ts.map +1 -1
  98. package/build/cli/cli/storage/kv-store-local-provider.js +6 -3
  99. package/build/cli/cli/storage/kv-store-provider.d.ts +1 -0
  100. package/build/cli/cli/storage/kv-store-provider.d.ts.map +1 -1
  101. package/build/cli/cli/storage/kv-store-provider.js +9 -1
  102. package/build/cli/cli/storage/s3-storage-provider.d.ts +1 -0
  103. package/build/cli/cli/storage/s3-storage-provider.d.ts.map +1 -1
  104. package/build/cli/cli/storage/s3-storage-provider.js +4 -0
  105. package/build/cli/cli/storage/storage-provider.d.ts +3 -0
  106. package/build/cli/cli/storage/storage-provider.d.ts.map +1 -1
  107. package/build/cli/cli/storage/storage-provider.js +32 -0
  108. package/build/cli/cli/util/api-token.d.ts.map +1 -1
  109. package/build/cli/cli/util/collection-name.d.ts +3 -0
  110. package/build/cli/cli/util/collection-name.d.ts.map +1 -0
  111. package/build/cli/cli/util/collection-name.js +24 -0
  112. package/build/cli/cli/util/collection-selection-flags.d.ts +13 -0
  113. package/build/cli/cli/util/collection-selection-flags.d.ts.map +1 -0
  114. package/build/cli/cli/util/collection-selection-flags.js +131 -0
  115. package/build/cli/cli/util/config.d.ts +13 -0
  116. package/build/cli/cli/util/config.d.ts.map +1 -1
  117. package/build/cli/cli/util/config.js +172 -38
  118. package/build/cli/cli/util/data.d.ts.map +1 -1
  119. package/build/cli/cli/util/global-options.d.ts +13 -0
  120. package/build/cli/cli/util/global-options.d.ts.map +1 -0
  121. package/build/cli/cli/util/global-options.js +53 -0
  122. package/build/cli/cli/util/kv-store-items.d.ts +1 -0
  123. package/build/cli/cli/util/kv-store-items.d.ts.map +1 -1
  124. package/build/cli/cli/util/kv-store-items.js +16 -40
  125. package/build/cli/cli/util/kv-store-local-server.d.ts +1 -1
  126. package/build/cli/cli/util/kv-store-local-server.d.ts.map +1 -1
  127. package/build/cli/cli/util/kv-store-local-server.js +4 -2
  128. package/build/cli/cli/util/output.d.ts +31 -0
  129. package/build/cli/cli/util/output.d.ts.map +1 -0
  130. package/build/cli/cli/util/output.js +139 -0
  131. package/build/cli/cli/util/package.d.ts +6 -0
  132. package/build/cli/cli/util/package.d.ts.map +1 -1
  133. package/build/cli/cli/util/package.js +23 -0
  134. package/build/cli/cli/util/progress.d.ts.map +1 -1
  135. package/build/cli/cli/util/purge.d.ts +6 -2
  136. package/build/cli/cli/util/purge.d.ts.map +1 -1
  137. package/build/cli/cli/util/purge.js +7 -2
  138. package/build/cli/cli/util/resources.d.ts +2 -0
  139. package/build/cli/cli/util/resources.d.ts.map +1 -0
  140. package/build/cli/cli/util/resources.js +8 -0
  141. package/build/cli/cli/util/service-id.d.ts +1 -0
  142. package/build/cli/cli/util/service-id.d.ts.map +1 -1
  143. package/build/cli/cli/util/service-id.js +5 -1
  144. package/build/cli/cli/util/variants.d.ts.map +1 -1
  145. package/build/cli/models/assets/index.d.ts +6 -0
  146. package/build/cli/models/assets/index.d.ts.map +1 -1
  147. package/build/cli/models/assets/index.js +24 -0
  148. package/build/cli/models/collections/index.d.ts +3 -0
  149. package/build/cli/models/collections/index.d.ts.map +1 -0
  150. package/build/cli/models/collections/index.js +15 -0
  151. package/build/cli/models/compression/index.d.ts +1 -1
  152. package/build/cli/models/compression/index.d.ts.map +1 -1
  153. package/build/cli/models/config/collection-selection.d.ts +55 -0
  154. package/build/cli/models/config/collection-selection.d.ts.map +1 -0
  155. package/build/cli/models/config/collection-selection.js +238 -0
  156. package/build/cli/models/config/item-patterns.d.ts +4 -0
  157. package/build/cli/models/config/item-patterns.d.ts.map +1 -0
  158. package/build/cli/models/config/item-patterns.js +43 -0
  159. package/build/cli/models/config/publish-content-config.d.ts +6 -1
  160. package/build/cli/models/config/publish-content-config.d.ts.map +1 -1
  161. package/build/cli/models/config/static-publish-project.d.ts +23 -0
  162. package/build/cli/models/config/static-publish-project.d.ts.map +1 -0
  163. package/build/cli/models/config/static-publish-project.js +11 -0
  164. package/build/cli/models/config/static-publish-rc.d.ts +25 -0
  165. package/build/cli/models/config/static-publish-rc.d.ts.map +1 -1
  166. package/build/cli/models/config/static-publish-rc.js +31 -0
  167. package/build/cli/models/config/template-settings.d.ts +10 -0
  168. package/build/cli/models/config/template-settings.d.ts.map +1 -0
  169. package/build/cli/models/config/template-settings.js +72 -0
  170. package/build/cli/models/server/index.d.ts.map +1 -1
  171. package/build/cli/models/server/index.js +6 -2
  172. package/build/server/hono/extensions.d.ts +48 -0
  173. package/build/server/hono/extensions.d.ts.map +1 -0
  174. package/build/server/hono/extensions.js +65 -0
  175. package/build/server/hono/index.d.ts +30 -0
  176. package/build/server/hono/index.d.ts.map +1 -0
  177. package/build/server/hono/index.js +72 -0
  178. package/build/server/hono/path.d.ts +7 -0
  179. package/build/server/hono/path.d.ts.map +1 -0
  180. package/build/server/hono/path.js +32 -0
  181. package/build/server/models/assets/index.d.ts +6 -0
  182. package/build/server/models/assets/index.d.ts.map +1 -1
  183. package/build/server/models/assets/index.js +24 -0
  184. package/build/server/models/collections/index.d.ts +3 -0
  185. package/build/server/models/collections/index.d.ts.map +1 -0
  186. package/build/server/models/collections/index.js +15 -0
  187. package/build/server/models/compression/index.d.ts +1 -1
  188. package/build/server/models/compression/index.d.ts.map +1 -1
  189. package/build/server/models/config/collection-selection.d.ts +55 -0
  190. package/build/server/models/config/collection-selection.d.ts.map +1 -0
  191. package/build/server/models/config/collection-selection.js +238 -0
  192. package/build/server/models/config/item-patterns.d.ts +4 -0
  193. package/build/server/models/config/item-patterns.d.ts.map +1 -0
  194. package/build/server/models/config/item-patterns.js +43 -0
  195. package/build/server/models/config/publish-content-config.d.ts +6 -1
  196. package/build/server/models/config/publish-content-config.d.ts.map +1 -1
  197. package/build/server/models/config/static-publish-project.d.ts +23 -0
  198. package/build/server/models/config/static-publish-project.d.ts.map +1 -0
  199. package/build/server/models/config/static-publish-project.js +11 -0
  200. package/build/server/models/config/static-publish-rc.d.ts +25 -0
  201. package/build/server/models/config/static-publish-rc.d.ts.map +1 -1
  202. package/build/server/models/config/static-publish-rc.js +31 -0
  203. package/build/server/models/server/index.d.ts.map +1 -1
  204. package/build/server/models/server/index.js +6 -2
  205. package/build/server/server/collection-selection/index.d.ts +11 -0
  206. package/build/server/server/collection-selection/index.d.ts.map +1 -0
  207. package/build/server/server/collection-selection/index.js +192 -0
  208. package/build/server/server/collection-selector/from-cookie.d.ts.map +1 -1
  209. package/build/server/server/collection-selector/from-cookie.js +6 -8
  210. package/build/server/server/collection-selector/from-request.d.ts.map +1 -1
  211. package/build/server/server/index.d.ts +4 -1
  212. package/build/server/server/index.d.ts.map +1 -1
  213. package/build/server/server/index.js +3 -1
  214. package/build/server/server/publisher-server/accept-html.d.ts +2 -0
  215. package/build/server/server/publisher-server/accept-html.d.ts.map +1 -0
  216. package/build/server/server/publisher-server/accept-html.js +50 -0
  217. package/build/server/server/publisher-server/index.d.ts +59 -7
  218. package/build/server/server/publisher-server/index.d.ts.map +1 -1
  219. package/build/server/server/publisher-server/index.js +362 -145
  220. package/build/server/server/publisher-server/response-cache.d.ts +3 -0
  221. package/build/server/server/publisher-server/response-cache.d.ts.map +1 -1
  222. package/build/server/server/publisher-server/response-cache.js +24 -1
  223. package/build/server/server/storage/kv-store-provider.d.ts.map +1 -1
  224. package/build/server/server/storage/s3-storage-provider.d.ts.map +1 -1
  225. package/build/server/server/storage/storage-provider.d.ts.map +1 -1
  226. package/build/server/server/util/body-length.d.ts.map +1 -1
  227. package/build/server/server/util/server-timing.d.ts.map +1 -1
  228. package/package.json +21 -3
  229. package/resources/app/fastly.toml.njk +58 -0
  230. package/resources/app/gitignore.njk +9 -0
  231. package/resources/app/publish-content.config.from-project.js.njk +9 -0
  232. package/resources/app/publish-content.config.js.njk +35 -0
  233. package/resources/app/static-publish.project.js.njk +83 -0
  234. package/resources/app/static-publish.rc.js.njk +43 -0
  235. package/resources/template-init/hono/_example.ts +22 -0
  236. package/resources/template-init/hono/tsconfig.json +17 -0
  237. package/resources/templates/hono/package-lock.json +4123 -0
  238. package/resources/templates/hono/package.json +15 -0
  239. package/resources/templates/hono/scripts/template_postinstall.js +10 -0
  240. package/resources/templates/hono/scripts/template_prebuild.js +8 -0
  241. package/resources/templates/hono/scripts/template_watch.js +15 -0
  242. package/resources/templates/hono/src/hono-extensions.generated.d.ts +5 -0
  243. package/resources/templates/hono/src/index.ts +53 -0
  244. package/resources/templates/hono/tsconfig.json +17 -0
  245. package/resources/templates/plain/package-lock.json +4088 -0
  246. package/resources/templates/plain/package.json +10 -0
  247. package/resources/templates/plain/src/index.js +34 -0
@@ -6,14 +6,18 @@ import fs from 'node:fs';
6
6
  import path from 'node:path';
7
7
  import { decodeIndexMetadata, encodeIndexMetadata } from '../../../../models/server/index.js';
8
8
  import { calcExpirationTime } from '../../../../models/time/index.js';
9
- import { LoadConfigError, loadStaticPublisherRcFile } from '../../../util/config.js';
9
+ import { LoadConfigError, configFileName, getComputeAppDir, loadStaticPublisherRcFile, } from '../../../util/config.js';
10
10
  import { parseCommandLine } from '../../../util/args.js';
11
+ import { errorCodeOf, fail, recordFailure, setJsonResult } from '../../../util/output.js';
12
+ import { checkCollectionNameForWrite } from '../../../util/collection-name.js';
11
13
  import { loadStorageProviderFromStaticPublishRc } from '../../../storage/storage-provider.js';
14
+ import { cliCommand } from '../../../util/package.js';
15
+ import { loadPurgeTarget, purgeCollection } from '../../../util/purge.js';
12
16
  function help() {
13
17
  console.log(`\
14
18
 
15
19
  Usage:
16
- npx @fastly/compute-js-static-publish collections promote
20
+ ${cliCommand()} collections promote
17
21
  --collection-name=<name> \\
18
22
  --to=name> \\
19
23
  [options]
@@ -21,6 +25,9 @@ Usage:
21
25
  Description:
22
26
  Copies an existing collection (content + config) to a new collection name.
23
27
 
28
+ If a Service ID is found, the tool then purges the cached responses of the target
29
+ collection from that service.
30
+
24
31
  Required:
25
32
  --collection-name=<name> The name of the collection to promote
26
33
  --to=<name> The name of the new (target) collection to create or overwrite
@@ -45,10 +52,21 @@ KV Store Options:
45
52
  with the local development environment.
46
53
  KV Store mode only. In S3 mode, --local is an error.
47
54
 
48
- --fastly-api-token=<token> Fastly API token for KV Store access.
55
+ --fastly-api-token=<token> Fastly API token for KV Store or cache access.
49
56
  If not set, the tool uses the FASTLY_API_TOKEN
50
57
  environment variable.
51
58
 
59
+ Purge Options:
60
+ --fastly-service-id=<id> Fastly Service ID to purge after promoting (not with --local).
61
+ If not set, the tool will check:
62
+ 1. FASTLY_SERVICE_ID environment variable
63
+ 2. service_id in fastly.toml
64
+ If none is found, the purge is skipped.
65
+
66
+ --purge-environment=<env> Environment to purge: production, staging, or a
67
+ comma-separated list of both. Can be repeated.
68
+ Default: production
69
+
52
70
  S3 Storage Options (BETA):
53
71
  --s3-access-key-id=<id> Access Key ID and Secret Access Key used to
54
72
  --s3-secret-access-key=<key> interface with S3 or compatible storage.
@@ -69,6 +87,8 @@ export async function action(actionArgs) {
69
87
  { name: 'expires-never', type: Boolean },
70
88
  { name: 'local', type: Boolean },
71
89
  { name: 'fastly-api-token', type: String, },
90
+ { name: 'fastly-service-id', type: String, },
91
+ { name: 'purge-environment', type: String, multiple: true, },
72
92
  { name: 's3-access-key-id', type: String, },
73
93
  { name: 's3-secret-access-key', type: String, },
74
94
  ];
@@ -77,23 +97,21 @@ export async function action(actionArgs) {
77
97
  if (parsed.error != null) {
78
98
  console.error(String(parsed.error));
79
99
  console.error();
80
- process.exitCode = 1;
100
+ recordFailure('usage', String(parsed.error));
81
101
  }
82
102
  help();
83
103
  return;
84
104
  }
85
- const { verbose, ['collection-name']: collectionNameValue, to: toCollectionNameValue, ['expires-in']: expiresIn, ['expires-at']: expiresAt, ['expires-never']: expiresNever, local: localMode, ['fastly-api-token']: fastlyApiToken, ['s3-access-key-id']: s3AccessKeyId, ['s3-secret-access-key']: s3SecretAccessKey, } = parsed.commandLineOptions;
105
+ const { verbose, ['collection-name']: collectionNameValue, to: toCollectionNameValue, ['expires-in']: expiresIn, ['expires-at']: expiresAt, ['expires-never']: expiresNever, local: localMode, ['fastly-api-token']: fastlyApiToken, ['fastly-service-id']: fastlyServiceId, ['purge-environment']: purgeEnvironmentValues, ['s3-access-key-id']: s3AccessKeyId, ['s3-secret-access-key']: s3SecretAccessKey, } = parsed.commandLineOptions;
86
106
  // compute-js-static-publisher cli is always run from the Compute application directory
87
107
  // in other words, the directory that contains `fastly.toml`.
88
- const computeAppDir = path.resolve();
108
+ const computeAppDir = getComputeAppDir();
89
109
  if (collectionNameValue == null) {
90
- console.error("❌ Required argument '--collection-name' not specified.");
91
- process.exitCode = 1;
110
+ fail('usage', "Required argument '--collection-name' not specified.");
92
111
  return;
93
112
  }
94
113
  if (toCollectionNameValue == null) {
95
- console.error("❌ Required argument '--to' not specified.");
96
- process.exitCode = 1;
114
+ fail('usage', "Required argument '--to' not specified.");
97
115
  return;
98
116
  }
99
117
  let expirationTime;
@@ -103,7 +121,7 @@ export async function action(actionArgs) {
103
121
  catch (err) {
104
122
  console.error(`❌ Cannot process expiration time`);
105
123
  console.error(String(err));
106
- process.exitCode = 1;
124
+ recordFailure('usage', `Cannot process expiration time: ${String(err)}`);
107
125
  return;
108
126
  }
109
127
  console.log(`📃 Promoting collection...`);
@@ -113,14 +131,14 @@ export async function action(actionArgs) {
113
131
  staticPublisherRc = await loadStaticPublisherRcFile();
114
132
  }
115
133
  catch (err) {
116
- console.error("❌ Can't load static-publish.rc.js");
134
+ console.error(`❌ Can't load ${configFileName()}`);
117
135
  console.error("Run this from a compute-js-static-publish compute-js directory.");
118
136
  if (err instanceof LoadConfigError) {
119
137
  for (const error of err.errors) {
120
138
  console.error(error);
121
139
  }
122
140
  }
123
- process.exitCode = 1;
141
+ recordFailure('config-invalid', `Can't load ${configFileName()}.`);
124
142
  return;
125
143
  }
126
144
  const publishId = staticPublisherRc.publishId;
@@ -143,13 +161,32 @@ export async function action(actionArgs) {
143
161
  catch (err) {
144
162
  console.error(`❌ Could not instantiate store provider`);
145
163
  console.error(String(err));
146
- process.exitCode = 1;
164
+ recordFailure(errorCodeOf(err), String(err));
147
165
  return;
148
166
  }
149
167
  const sourceCollectionName = collectionNameValue;
150
168
  console.log(`✔️ Collection to copy: ${sourceCollectionName}`);
169
+ // Purge target: checked now, so that a missing API token fails before anything is written.
170
+ let purgeTarget;
171
+ try {
172
+ purgeTarget = loadPurgeTarget({
173
+ localMode: localMode ?? false,
174
+ computeAppDir,
175
+ fastlyServiceId,
176
+ fastlyApiToken,
177
+ purgeEnvironments: purgeEnvironmentValues,
178
+ });
179
+ }
180
+ catch (err) {
181
+ console.error(String(err));
182
+ process.exitCode = 1;
183
+ return;
184
+ }
151
185
  const targetCollectionName = toCollectionNameValue;
152
186
  console.log(`✔️ Collection to promote to: ${targetCollectionName}`);
187
+ if (!await checkCollectionNameForWrite(storageProvider, publishId, targetCollectionName)) {
188
+ return;
189
+ }
153
190
  if (expirationTime === undefined) {
154
191
  console.log(`✔️ Not updating expiration timestamp.`);
155
192
  }
@@ -170,11 +207,9 @@ export async function action(actionArgs) {
170
207
  storageProvider.getStorageEntry(sourceCollectionIndexKey),
171
208
  storageProvider.getStorageEntry(sourceCollectionSettingsKey),
172
209
  ]);
173
- if (!indexEntryInfo) {
174
- throw new Error(`Error querying index for '${collectionNameValue}' in storage`);
175
- }
176
- if (!settingsEntryInfo) {
177
- throw new Error(`Error querying settings for '${collectionNameValue}' in storage`);
210
+ if (!indexEntryInfo || !settingsEntryInfo) {
211
+ fail('collection-not-found', `Collection '${collectionNameValue}' does not exist.`);
212
+ return;
178
213
  }
179
214
  let indexMetadata = decodeIndexMetadata(indexEntryInfo.metadata) ?? {};
180
215
  if (indexMetadata.publishedTime == null) {
@@ -191,11 +226,24 @@ export async function action(actionArgs) {
191
226
  console.log(`Uploading to storage: '${targetCollectionName}'`);
192
227
  const storageContentDir = `${staticPublisherWorkingDir}/storage-content`;
193
228
  fs.mkdirSync(storageContentDir, { recursive: true });
194
- const indexFileName = `index_${collectionNameValue}.json`;
229
+ // The files have the name of the target collection, as in publish-content. In
230
+ // --local mode, kvstore.json refers to them, so the target must not share a
231
+ // file with the source.
232
+ const indexFileName = `index_${targetCollectionName}.json`;
195
233
  const indexFilePath = path.resolve(storageContentDir, indexFileName);
196
234
  await storageProvider.submitStorageEntry(targetCollectionIndexKey, indexFilePath, indexEntryInfo.data, encodeIndexMetadata(indexMetadata));
197
- const settingsFileName = `settings_${collectionNameValue}.json`;
235
+ const settingsFileName = `settings_${targetCollectionName}.json`;
198
236
  const settingsFilePath = path.resolve(storageContentDir, settingsFileName);
199
237
  await storageProvider.submitStorageEntry(targetCollectionSettingsKey, settingsFilePath, settingsEntryInfo.data);
238
+ if (purgeTarget != null) {
239
+ await purgeCollection(purgeTarget, publishId, targetCollectionName);
240
+ }
200
241
  console.log("✅ Completed.");
242
+ setJsonResult({
243
+ from: sourceCollectionName,
244
+ to: targetCollectionName,
245
+ publishId,
246
+ // The expiration time of the target, after the promote. null: it does not expire.
247
+ expiresAt: indexMetadata.expirationTime != null ? new Date(indexMetadata.expirationTime * 1000).toISOString() : null,
248
+ });
201
249
  }
@@ -1 +1 @@
1
- {"version":3,"file":"update-expiration.d.ts","sourceRoot":"","sources":["../../../../../../src/cli/commands/manage/collections/update-expiration.ts"],"names":[],"mappings":"AA+DA,wBAAsB,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,iBA0JhD"}
1
+ {"version":3,"file":"update-expiration.d.ts","sourceRoot":"","sources":["../../../../../../src/cli/commands/manage/collections/update-expiration.ts"],"names":[],"mappings":"AAsEA,wBAAsB,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,iBAwJhD"}
@@ -6,14 +6,16 @@ import fs from 'node:fs';
6
6
  import path from 'node:path';
7
7
  import { decodeIndexMetadata, encodeIndexMetadata } from '../../../../models/server/index.js';
8
8
  import { calcExpirationTime } from '../../../../models/time/index.js';
9
- import { LoadConfigError, loadStaticPublisherRcFile } from '../../../util/config.js';
9
+ import { LoadConfigError, configFileName, getComputeAppDir, loadStaticPublisherRcFile, } from '../../../util/config.js';
10
10
  import { parseCommandLine } from '../../../util/args.js';
11
+ import { errorCodeOf, fail, recordFailure } from '../../../util/output.js';
11
12
  import { loadStorageProviderFromStaticPublishRc } from '../../../storage/storage-provider.js';
13
+ import { cliCommand } from '../../../util/package.js';
12
14
  function help() {
13
15
  console.log(`\
14
16
 
15
17
  Usage:
16
- npx @fastly/compute-js-static-publish collections update-expiration \\
18
+ ${cliCommand()} collections update-expiration \\
17
19
  --collection-name=<name> \\
18
20
  [options]
19
21
 
@@ -72,7 +74,7 @@ export async function action(actionArgs) {
72
74
  if (parsed.error != null) {
73
75
  console.error(String(parsed.error));
74
76
  console.error();
75
- process.exitCode = 1;
77
+ recordFailure('usage', String(parsed.error));
76
78
  }
77
79
  help();
78
80
  return;
@@ -80,10 +82,9 @@ export async function action(actionArgs) {
80
82
  const { verbose, ['collection-name']: collectionNameValue, ['expires-in']: expiresIn, ['expires-at']: expiresAt, ['expires-never']: expiresNever, local: localMode, ['fastly-api-token']: fastlyApiToken, ['s3-access-key-id']: s3AccessKeyId, ['s3-secret-access-key']: s3SecretAccessKey, } = parsed.commandLineOptions;
81
83
  // compute-js-static-publisher cli is always run from the Compute application directory
82
84
  // in other words, the directory that contains `fastly.toml`.
83
- const computeAppDir = path.resolve();
85
+ const computeAppDir = getComputeAppDir();
84
86
  if (collectionNameValue == null) {
85
- console.error("❌ Required argument '--collection-name' not specified.");
86
- process.exitCode = 1;
87
+ fail('usage', "Required argument '--collection-name' not specified.");
87
88
  return;
88
89
  }
89
90
  let expirationTime;
@@ -93,12 +94,11 @@ export async function action(actionArgs) {
93
94
  catch (err) {
94
95
  console.error(`❌ Cannot process expiration time`);
95
96
  console.error(String(err));
96
- process.exitCode = 1;
97
+ recordFailure('usage', `Cannot process expiration time: ${String(err)}`);
97
98
  return;
98
99
  }
99
100
  if (expirationTime === undefined) {
100
- console.error("❌ Exactly one of '--expires-in', '--expires-at', or '--expires-never' is required.");
101
- process.exitCode = 1;
101
+ fail('usage', "Exactly one of '--expires-in', '--expires-at', or '--expires-never' is required.");
102
102
  return;
103
103
  }
104
104
  console.log(`📃 Promoting collection...`);
@@ -108,14 +108,14 @@ export async function action(actionArgs) {
108
108
  staticPublisherRc = await loadStaticPublisherRcFile();
109
109
  }
110
110
  catch (err) {
111
- console.error("❌ Can't load static-publish.rc.js");
111
+ console.error(`❌ Can't load ${configFileName()}`);
112
112
  console.error("Run this from a compute-js-static-publish compute-js directory.");
113
113
  if (err instanceof LoadConfigError) {
114
114
  for (const error of err.errors) {
115
115
  console.error(error);
116
116
  }
117
117
  }
118
- process.exitCode = 1;
118
+ recordFailure('config-invalid', `Can't load ${configFileName()}.`);
119
119
  return;
120
120
  }
121
121
  const publishId = staticPublisherRc.publishId;
@@ -138,7 +138,7 @@ export async function action(actionArgs) {
138
138
  catch (err) {
139
139
  console.error(`❌ Could not instantiate store provider`);
140
140
  console.error(String(err));
141
- process.exitCode = 1;
141
+ recordFailure(errorCodeOf(err), String(err));
142
142
  return;
143
143
  }
144
144
  const collectionName = collectionNameValue;
@@ -0,0 +1,2 @@
1
+ export declare function action(actionArgs: string[]): Promise<void>;
2
+ //# sourceMappingURL=deploy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deploy.d.ts","sourceRoot":"","sources":["../../../../../src/cli/commands/manage/deploy.ts"],"names":[],"mappings":"AAuEA,wBAAsB,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,iBAqJhD"}
@@ -0,0 +1,194 @@
1
+ /*
2
+ * Copyright Fastly, Inc.
3
+ * Licensed under the MIT license. See LICENSE file for details.
4
+ */
5
+ import child_process from 'node:child_process';
6
+ import fs from 'node:fs';
7
+ import path from 'node:path';
8
+ import { managedAppState, prepareManagedApp } from '../../managed/app.js';
9
+ import { checkVersionForCommand, loadManagedProject, showTemplateFolderHint } from '../../managed/command-helpers.js';
10
+ import { getActiveVersion, getVersionDomains } from '../../managed/fastly-service.js';
11
+ import { formatVersionComment, parseVersionComment } from '../../managed/fingerprint.js';
12
+ import { writeBackServiceId } from '../../managed/project-edit.js';
13
+ import { loadApiToken } from '../../util/api-token.js';
14
+ import { parseCommandLine } from '../../util/args.js';
15
+ import { detectProjectMode } from '../../util/config.js';
16
+ import { readServiceId } from '../../util/fastly-toml.js';
17
+ import { cliCommand, getOwnPackageDir, getOwnVersion } from '../../util/package.js';
18
+ import { fail, isJsonMode, isNonInteractive, recordFailure, setJsonResult } from '../../util/output.js';
19
+ import { loadServiceId } from '../../util/service-id.js';
20
+ import { isS3StorageConfigRc } from '../../../models/config/static-publish-rc.js';
21
+ import { PROJECT_FILE_NAME } from '../../../models/config/static-publish-project.js';
22
+ function help() {
23
+ console.log(`\
24
+
25
+ Usage:
26
+ ${cliCommand()} deploy [options]
27
+
28
+ Description:
29
+ Deploys the app of a managed project, only if it changed: generates the app,
30
+ installs its dependencies if necessary, and runs 'fastly compute build' and
31
+ 'fastly compute deploy'. The comment of the new service version has the
32
+ fingerprint of the app, so that 'status' and the next deploy can compare it.
33
+
34
+ After the first deploy, the Service ID is written into managed.serviceId in
35
+ ${PROJECT_FILE_NAME}.
36
+
37
+ In an ejected app, this runs 'fastly compute publish'.
38
+
39
+ Options:
40
+ --force Deploy also when the fingerprint did not change.
41
+ --auto-upgrade If the CLI is newer than managed.staticPublishVersion,
42
+ set it to the version that runs, and continue.
43
+ --comment=<text> Added to the comment of the service version,
44
+ for example a commit SHA.
45
+ --fastly-service-id=<id> The service. Default: FASTLY_SERVICE_ID, then
46
+ managed.serviceId. Without one, the Fastly CLI
47
+ makes a new service.
48
+ --fastly-api-token=<token> Default: FASTLY_API_TOKEN.
49
+
50
+ Global Options:
51
+ -h, --help Show this help message and exit.
52
+ `);
53
+ }
54
+ function runFastly(appDir, args, apiToken) {
55
+ const appFastly = path.resolve(appDir, 'node_modules/.bin/fastly');
56
+ const fastly = fs.existsSync(appFastly) ? appFastly : 'fastly';
57
+ console.log(`▶️ fastly ${args.join(' ')}`);
58
+ return child_process.spawnSync(fastly, args, {
59
+ cwd: appDir,
60
+ env: { ...process.env, FASTLY_API_TOKEN: apiToken },
61
+ // With --json, stdout is for the JSON object only.
62
+ stdio: [isNonInteractive() ? 'ignore' : 'inherit', isJsonMode() ? process.stderr : 'inherit', 'inherit'],
63
+ });
64
+ }
65
+ export async function action(actionArgs) {
66
+ const optionDefinitions = [
67
+ { name: 'force', type: Boolean },
68
+ { name: 'auto-upgrade', type: Boolean },
69
+ { name: 'comment', type: String },
70
+ { name: 'fastly-service-id', type: String },
71
+ { name: 'fastly-api-token', type: String },
72
+ ];
73
+ const parsed = parseCommandLine(actionArgs, optionDefinitions);
74
+ if (parsed.needHelp) {
75
+ if (parsed.error != null) {
76
+ console.error(String(parsed.error));
77
+ console.error();
78
+ recordFailure('usage', String(parsed.error));
79
+ }
80
+ help();
81
+ return;
82
+ }
83
+ const { force, ['auto-upgrade']: autoUpgrade, comment, ['fastly-service-id']: fastlyServiceId, ['fastly-api-token']: fastlyApiToken, } = parsed.commandLineOptions;
84
+ if (detectProjectMode() === 'ejected') {
85
+ if (isJsonMode()) {
86
+ fail('json-not-supported', `--json is not supported for 'deploy' in an ejected app.`);
87
+ return;
88
+ }
89
+ const result = child_process.spawnSync('fastly', ['compute', 'publish'], { stdio: 'inherit' });
90
+ if (result.error != null || result.status !== 0) {
91
+ fail('deploy-failed', `'fastly compute publish' failed.`);
92
+ }
93
+ return;
94
+ }
95
+ const project = await loadManagedProject('deploy', '');
96
+ if (project == null) {
97
+ return;
98
+ }
99
+ showTemplateFolderHint(project);
100
+ if (!checkVersionForCommand(project, autoUpgrade === true)) {
101
+ return;
102
+ }
103
+ const apiToken = loadApiToken({ commandLine: fastlyApiToken });
104
+ if (apiToken == null) {
105
+ fail('credentials-missing', 'Fastly API Token not provided. Set the FASTLY_API_TOKEN environment variable to an API token that can deploy the service.');
106
+ return;
107
+ }
108
+ const context = { apiToken: apiToken.apiToken };
109
+ const serviceIdResult = loadServiceId({
110
+ commandLine: fastlyServiceId,
111
+ fastlyTomlPath: path.resolve('.static-publish/app/fastly.toml'),
112
+ projectServiceId: project.managed.serviceId,
113
+ });
114
+ let serviceId = serviceIdResult?.serviceId ?? null;
115
+ if (serviceId != null) {
116
+ console.log(`✔️ Service ID from ${serviceIdResult.source}: ${serviceId}`);
117
+ }
118
+ // The Fastly CLI cannot make the Secret Store of an S3 app without prompts.
119
+ if (serviceId == null && isS3StorageConfigRc(project.rc) && isNonInteractive()) {
120
+ fail('first-deploy-manual', 'The first deploy of an app with S3 storage must be done by hand: run deploy on your computer (the Fastly CLI asks for the S3 credentials), ' +
121
+ 'or give the ID of a service that is already set up (--fastly-service-id, FASTLY_SERVICE_ID, or managed.serviceId).');
122
+ return;
123
+ }
124
+ // A CLI run from the npx cache puts paths of the home directory into the Wasm binary.
125
+ if (getOwnPackageDir().includes(`${path.sep}_npx${path.sep}`)) {
126
+ console.warn(`⚠️ This CLI runs from the npx cache. The paths of the cache (in your home directory) go into the deployed Wasm binary.`);
127
+ console.warn(` * Add @fastly/compute-js-static-publish as a dev dependency of the project, and run it from there.`);
128
+ }
129
+ const { fingerprint } = await managedAppState(path.resolve(), project);
130
+ const staticPublishVersion = getOwnVersion();
131
+ if (serviceId != null && !force) {
132
+ const active = await getActiveVersion(context, serviceId);
133
+ const deployed = parseVersionComment(active?.comment);
134
+ if (active != null && deployed?.fingerprint === fingerprint) {
135
+ console.log(`✅ The active version ${active.number} already has this app (fingerprint ${fingerprint}). Nothing to deploy. Use --force to deploy anyway.`);
136
+ setJsonResult({
137
+ deployed: false,
138
+ reason: 'up-to-date',
139
+ serviceId,
140
+ version: active.number,
141
+ domain: (await getVersionDomains(context, serviceId, active.number))[0] ?? null,
142
+ fingerprint,
143
+ staticPublishVersion,
144
+ });
145
+ return;
146
+ }
147
+ }
148
+ const { appDir } = await prepareManagedApp(path.resolve(), project);
149
+ const build = runFastly(appDir, ['compute', 'build'], apiToken.apiToken);
150
+ if (build.error != null || build.status !== 0) {
151
+ fail('deploy-failed', `'fastly compute build' failed.`);
152
+ return;
153
+ }
154
+ const deployArgs = ['compute', 'deploy', '--comment', formatVersionComment(staticPublishVersion, fingerprint, comment)];
155
+ if (serviceId != null) {
156
+ deployArgs.push('--service-id', serviceId);
157
+ }
158
+ if (isNonInteractive()) {
159
+ deployArgs.push('--non-interactive');
160
+ }
161
+ const deploy = runFastly(appDir, deployArgs, apiToken.apiToken);
162
+ if (deploy.error != null || deploy.status !== 0) {
163
+ fail('deploy-failed', `'fastly compute deploy' failed.`);
164
+ return;
165
+ }
166
+ // After the first deploy, the Fastly CLI has written service_id into fastly.toml.
167
+ serviceId ??= readServiceId(path.resolve(appDir, 'fastly.toml')) ?? null;
168
+ if (serviceId == null) {
169
+ fail('deploy-failed', 'The deploy finished, but the Service ID is not known.');
170
+ return;
171
+ }
172
+ if (project.managed.serviceId == null) {
173
+ const projectFilePath = path.resolve(PROJECT_FILE_NAME);
174
+ const updated = writeBackServiceId(fs.readFileSync(projectFilePath, 'utf-8'), serviceId);
175
+ if (updated != null) {
176
+ fs.writeFileSync(projectFilePath, updated, 'utf-8');
177
+ console.log(`✔️ Wrote managed.serviceId "${serviceId}" into ${PROJECT_FILE_NAME}. Commit it.`);
178
+ }
179
+ else {
180
+ console.log(`⚠️ Set managed.serviceId to "${serviceId}" in ${PROJECT_FILE_NAME}. (The line 'serviceId: undefined,' was not found, so the CLI did not change the file.)`);
181
+ }
182
+ }
183
+ const active = await getActiveVersion(context, serviceId);
184
+ const domain = active != null ? (await getVersionDomains(context, serviceId, active.number))[0] ?? null : null;
185
+ console.log(`🎉 Deployed ${serviceId}${active != null ? ` version ${active.number}` : ''}${domain != null ? ` (https://${domain}/)` : ''}.`);
186
+ setJsonResult({
187
+ deployed: true,
188
+ serviceId,
189
+ version: active?.number ?? null,
190
+ domain,
191
+ fingerprint,
192
+ staticPublishVersion,
193
+ });
194
+ }
@@ -0,0 +1,2 @@
1
+ export declare function action(actionArgs: string[]): Promise<void>;
2
+ //# sourceMappingURL=dev.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dev.d.ts","sourceRoot":"","sources":["../../../../../src/cli/commands/manage/dev.ts"],"names":[],"mappings":"AAgDA,wBAAsB,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,iBAwChD"}
@@ -0,0 +1,78 @@
1
+ /*
2
+ * Copyright Fastly, Inc.
3
+ * Licensed under the MIT license. See LICENSE file for details.
4
+ */
5
+ import path from 'node:path';
6
+ import { prepareManagedApp } from '../../managed/app.js';
7
+ import { parseCommandLine } from '../../util/args.js';
8
+ import { fail, recordFailure } from '../../util/output.js';
9
+ import { checkVersionForCommand, loadManagedProject, showTemplateFolderHint } from '../../managed/command-helpers.js';
10
+ import { PROJECT_FILE_NAME } from '../../../models/config/static-publish-project.js';
11
+ import { serveApp } from '../run-dev-watch.js';
12
+ import { cliCommand } from '../../util/package.js';
13
+ function help() {
14
+ console.log(`\
15
+
16
+ Usage:
17
+ ${cliCommand()} dev [-- <options of fastly compute serve>]
18
+
19
+ Description:
20
+ Runs the app of a managed project on your computer. Generates the app in
21
+ .static-publish/app from ${PROJECT_FILE_NAME}, installs its dependencies
22
+ if they changed, and runs 'fastly compute serve' in it.
23
+
24
+ The app serves the content of the local KV Store. To publish content to it,
25
+ run 'publish-content --local' first.
26
+
27
+ Only in a managed project. In an ejected app, run 'npm run dev:start', or
28
+ 'npm run dev:watch' (fastly compute serve --watch).
29
+
30
+ Options:
31
+ --auto-upgrade If the CLI is newer than managed.staticPublishVersion,
32
+ set it to the version that runs, and continue.
33
+ -- <options> Options after -- go to 'fastly compute serve',
34
+ for example: dev -- --addr=127.0.0.1:8080
35
+ With -- --watch, a change in the app builds and
36
+ restarts it. If the template has a watch script,
37
+ it runs next to 'fastly compute serve'.
38
+
39
+ Global Options:
40
+ -h, --help Show this help message and exit.
41
+ `);
42
+ }
43
+ export async function action(actionArgs) {
44
+ // Options after -- go to fastly compute serve.
45
+ const separator = actionArgs.indexOf('--');
46
+ const serveArgs = separator === -1 ? [] : actionArgs.slice(separator + 1);
47
+ const ownArgs = separator === -1 ? actionArgs : actionArgs.slice(0, separator);
48
+ const optionDefinitions = [
49
+ { name: 'auto-upgrade', type: Boolean },
50
+ ];
51
+ const parsed = parseCommandLine(ownArgs, optionDefinitions);
52
+ if (parsed.needHelp) {
53
+ if (parsed.error != null) {
54
+ console.error(String(parsed.error));
55
+ console.error();
56
+ recordFailure('usage', String(parsed.error));
57
+ }
58
+ help();
59
+ return;
60
+ }
61
+ const project = await loadManagedProject('dev', `In an ejected app, run 'npm run dev:start'.`);
62
+ if (project == null) {
63
+ return;
64
+ }
65
+ showTemplateFolderHint(project);
66
+ if (!checkVersionForCommand(project, parsed.commandLineOptions['auto-upgrade'] === true)) {
67
+ return;
68
+ }
69
+ const { appDir } = await prepareManagedApp(path.resolve(), project);
70
+ const result = await serveApp(appDir, serveArgs);
71
+ if (result.error != null) {
72
+ fail('failed', `Could not run 'fastly compute serve': ${String(result.error)}`);
73
+ return;
74
+ }
75
+ if (result.status !== 0 && result.signal == null) {
76
+ fail('failed', `'fastly compute serve' exited with ${result.status}.`);
77
+ }
78
+ }
@@ -0,0 +1,2 @@
1
+ export declare function action(actionArgs: string[]): Promise<void>;
2
+ //# sourceMappingURL=eject.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eject.d.ts","sourceRoot":"","sources":["../../../../../src/cli/commands/manage/eject.ts"],"names":[],"mappings":"AA+DA,wBAAsB,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,iBAoHhD"}