carlin 2.5.0 → 2.6.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 (2) hide show
  1. package/dist/index.mjs +71 -13
  2. package/package.json +5 -5
package/dist/index.mjs CHANGED
@@ -595,7 +595,7 @@ const copyRoot404To404Index = async ({ bucket }) => {
595
595
  throw error;
596
596
  }
597
597
  };
598
- const uploadDirectoryToS3 = async ({ bucket, bucketKey = "", directory, uploadSourceMaps = false }) => {
598
+ const uploadDirectoryToS3 = async ({ bucket, bucketKey = "", contentTypes = {}, directory, uploadSourceMaps = false }) => {
599
599
  log.info(logPrefix$18, `Uploading directory ${directory}/ to ${bucket}/${bucketKey}...`);
600
600
  const allFiles = await getAllFilesInsideADirectory({ directory });
601
601
  /**
@@ -603,6 +603,15 @@ const uploadDirectoryToS3 = async ({ bucket, bucketKey = "", directory, uploadSo
603
603
  * error. Discovered at #16 https://github.com/ttoss/carlin/issues/16.
604
604
  */
605
605
  if (allFiles.length === 0) throw new Error(`Directory ${directory}/ has no files.`);
606
+ const relativePath = (file) => {
607
+ return path.relative(directory, file).split(path.sep).join("/");
608
+ };
609
+ /**
610
+ * A mapped path that matches no file is a typo or a stale entry, and it would
611
+ * otherwise fail silently: the file it meant keeps its extension's type.
612
+ */
613
+ const builtPaths = new Set(allFiles.map(relativePath));
614
+ for (const mappedPath of Object.keys(contentTypes)) if (!builtPaths.has(mappedPath)) throw new Error(`The content-types option maps ${mappedPath}, which is not a file in the build folder.`);
606
615
  /**
607
616
  * Source maps expose the application's original source, and the bucket this
608
617
  * uploads to is public, so they are excluded unless explicitly requested.
@@ -631,6 +640,7 @@ const uploadDirectoryToS3 = async ({ bucket, bucketKey = "", directory, uploadSo
631
640
  await Promise.all(groupOfFiles.map((file) => {
632
641
  return uploadFileToS3({
633
642
  bucket,
643
+ contentType: contentTypes[relativePath(file)],
634
644
  key: path.join(bucketKey, path.relative(directory, file)),
635
645
  filePath: file
636
646
  });
@@ -3326,6 +3336,43 @@ const reportToGitHubPR = async () => {
3326
3336
  log.info(logPrefix$6, existingComment ? "PR comment updated." : "PR comment created.");
3327
3337
  };
3328
3338
  //#endregion
3339
+ //#region src/deploy/staticApp/contentTypes.ts
3340
+ const isRecord = (value) => {
3341
+ return typeof value === "object" && value !== null && !Array.isArray(value);
3342
+ };
3343
+ /**
3344
+ * yargs reads every dot in an option key as nesting, from the command line and
3345
+ * from config files alike, so `.well-known/api-catalog` arrives as
3346
+ * `{ '': { 'well-known/api-catalog': … } }`. A content type is a string, so a
3347
+ * nested object can only be a split path, and joining its keys back on the dot
3348
+ * restores the path as written.
3349
+ */
3350
+ const flatten = ({ prefix, record }) => {
3351
+ return Object.entries(record).flatMap(([key, value]) => {
3352
+ const path = prefix === void 0 ? key : `${prefix}.${key}`;
3353
+ return isRecord(value) ? flatten({
3354
+ prefix: path,
3355
+ record: value
3356
+ }) : [[path, value]];
3357
+ });
3358
+ };
3359
+ const parseContentTypes = (arg) => {
3360
+ if (arg === void 0 || arg === null) return {};
3361
+ if (!isRecord(arg)) throw new Error("The content-types option must be an object whose keys are file paths relative to the build folder and values are content types.");
3362
+ const contentTypes = {};
3363
+ for (const [key, value] of flatten({ record: arg })) {
3364
+ /**
3365
+ * A path is written the way its URL reads, so a leading slash names the
3366
+ * same object as none.
3367
+ */
3368
+ const path = key.replace(/^\/+/, "");
3369
+ if (!path) throw new Error("The content-types option has an empty path.");
3370
+ if (typeof value !== "string" || !value.trim()) throw new Error(`The content type of "${path}" must be a non-empty string.`);
3371
+ contentTypes[path] = value;
3372
+ }
3373
+ return contentTypes;
3374
+ };
3375
+ //#endregion
3329
3376
  //#region src/deploy/staticApp/getStaticAppBucket.ts
3330
3377
  const STATIC_APP_BUCKET_LOGICAL_ID$1 = "StaticBucket";
3331
3378
  const getStaticAppBucket = async ({ stackName }) => {
@@ -4094,7 +4141,7 @@ const findDefaultBuildFolder = async () => {
4094
4141
  };
4095
4142
  //#endregion
4096
4143
  //#region src/deploy/staticApp/uploadBuiltAppToS3.ts
4097
- const uploadBuiltAppToS3 = async ({ buildFolder: directory, bucket, uploadSourceMaps }) => {
4144
+ const uploadBuiltAppToS3 = async ({ buildFolder: directory, bucket, contentTypes, uploadSourceMaps }) => {
4098
4145
  /**
4099
4146
  * Only empty directory if the number of the files inside $directory.
4100
4147
  * If the number of files is zero, uploadDirectoryToS3 will thrown.
@@ -4106,6 +4153,7 @@ const uploadBuiltAppToS3 = async ({ buildFolder: directory, bucket, uploadSource
4106
4153
  });
4107
4154
  await uploadDirectoryToS3({
4108
4155
  bucket,
4156
+ contentTypes,
4109
4157
  directory,
4110
4158
  uploadSourceMaps
4111
4159
  });
@@ -4119,6 +4167,7 @@ const uploadBuiltAppToS3 = async ({ buildFolder: directory, bucket, uploadSource
4119
4167
  });
4120
4168
  await uploadDirectoryToS3({
4121
4169
  bucket,
4170
+ contentTypes,
4122
4171
  directory: defaultDirectory,
4123
4172
  uploadSourceMaps
4124
4173
  });
@@ -4138,7 +4187,7 @@ const logPrefix$4 = "static-app";
4138
4187
  * 1. Upload static files to the host bucket S3.
4139
4188
  * 1. Remove old deployment versions. Keep only the 3 most recent ones.
4140
4189
  */
4141
- const deployStaticApp = async ({ acm, aliases, appendIndexHtml, buildFolder, cloudfront, redirectToTrailingSlash, responseHeaders, responseHeadersPolicy, spa, hostedZoneName, region, skipUpload, uploadSourceMaps, viewerRequestFunctionCode }) => {
4190
+ const deployStaticApp = async ({ acm, aliases, appendIndexHtml, buildFolder, cloudfront, contentTypes, redirectToTrailingSlash, responseHeaders, responseHeadersPolicy, spa, hostedZoneName, region, skipUpload, uploadSourceMaps, viewerRequestFunctionCode }) => {
4142
4191
  try {
4143
4192
  const { stackName } = await handleDeployInitialization({ logPrefix: logPrefix$4 });
4144
4193
  const params = { StackName: stackName };
@@ -4157,16 +4206,25 @@ const deployStaticApp = async ({ acm, aliases, appendIndexHtml, buildFolder, clo
4157
4206
  });
4158
4207
  const bucket = await getStaticAppBucket({ stackName });
4159
4208
  /**
4160
- * Stack already exists. Upload files first after changing the files routes
4161
- * because of the version changing.
4209
+ * Both paths below upload, and every upload option must reach both: an
4210
+ * option forwarded through only one would apply on some deploys and not
4211
+ * others.
4162
4212
  */
4163
- if (bucket) {
4164
- if (!skipUpload) await uploadBuiltAppToS3({
4213
+ const upload = ({ bucket }) => {
4214
+ return uploadBuiltAppToS3({
4165
4215
  buildFolder,
4166
4216
  bucket,
4167
4217
  cloudfront,
4218
+ contentTypes,
4168
4219
  uploadSourceMaps
4169
4220
  });
4221
+ };
4222
+ /**
4223
+ * Stack already exists. Upload files first after changing the files routes
4224
+ * because of the version changing.
4225
+ */
4226
+ if (bucket) {
4227
+ if (!skipUpload) await upload({ bucket });
4170
4228
  const { Outputs } = await deploy({
4171
4229
  params,
4172
4230
  template
@@ -4183,12 +4241,7 @@ const deployStaticApp = async ({ acm, aliases, appendIndexHtml, buildFolder, clo
4183
4241
  });
4184
4242
  const newBucket = await getStaticAppBucket({ stackName });
4185
4243
  if (!newBucket) throw new Error(`Cannot find bucket at ${stackName}.`);
4186
- await uploadBuiltAppToS3({
4187
- buildFolder,
4188
- bucket: newBucket,
4189
- cloudfront,
4190
- uploadSourceMaps
4191
- });
4244
+ await upload({ bucket: newBucket });
4192
4245
  }
4193
4246
  } catch (error) {
4194
4247
  handleDeployError({
@@ -4224,6 +4277,11 @@ const options$4 = {
4224
4277
  require: false,
4225
4278
  type: "boolean"
4226
4279
  },
4280
+ "content-types": {
4281
+ coerce: parseContentTypes,
4282
+ default: {},
4283
+ describe: "Content types set on uploaded files instead of the one their extension implies. Pass an object whose keys are file paths relative to the build folder and values are content types."
4284
+ },
4227
4285
  "hosted-zone-name": {
4228
4286
  required: false,
4229
4287
  describe: `Is the name of a Route 53 hosted zone. If this value is provided, ${NAME} creates the subdomains defined on \`--aliases\` option. E.g. if you have a hosted zone named "sub.domain.com", the value provided may be "sub.domain.com".`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "carlin",
3
- "version": "2.5.0",
3
+ "version": "2.6.1",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "homepage": "https://ttoss.dev/docs/carlin/",
@@ -57,9 +57,9 @@
57
57
  "ts-node": "^10.9.2",
58
58
  "uglify-js": "^3.19.3",
59
59
  "yargs": "^17.7.2",
60
- "@ttoss/read-config-file": "^2.3.0",
61
- "@ttoss/cloudformation": "^0.14.0",
62
- "@ttoss/config": "^1.38.0"
60
+ "@ttoss/cloudformation": "^0.15.0",
61
+ "@ttoss/config": "^1.39.0",
62
+ "@ttoss/read-config-file": "^2.4.0"
63
63
  },
64
64
  "devDependencies": {
65
65
  "@types/adm-zip": "^0.5.6",
@@ -79,7 +79,7 @@
79
79
  "jest": "^30.4.2",
80
80
  "tsdown": "^0.22.2",
81
81
  "typescript": "~6.0.3",
82
- "@ttoss/test-utils": "^4.2.19"
82
+ "@ttoss/test-utils": "^4.3.0"
83
83
  },
84
84
  "publishConfig": {
85
85
  "access": "public",