carlin 2.4.0 → 2.6.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 (2) hide show
  1. package/dist/index.mjs +78 -14
  2. package/package.json +3 -3
package/dist/index.mjs CHANGED
@@ -557,9 +557,15 @@ const uploadFileToS3 = async ({ bucket, contentType, file, filePath, key }) => {
557
557
  };
558
558
  /**
559
559
  * Get all files inside $directory.
560
+ *
561
+ * `dot: true` because a static site's dot directories are content: RFC 8615
562
+ * puts the files agents and clients fetch by convention under `.well-known/`,
563
+ * and glob skips a segment starting with a dot by default. Without it those
564
+ * files are absent from the deployed site and nothing reports it — the deploy
565
+ * succeeds, the path 404s.
560
566
  */
561
567
  const getAllFilesInsideADirectory = async ({ directory }) => {
562
- return (await glob(`${directory}/**/*`)).filter((item) => {
568
+ return (await glob(`${directory}/**/*`, { dot: true })).filter((item) => {
563
569
  return fs.lstatSync(item).isFile();
564
570
  });
565
571
  };
@@ -589,7 +595,7 @@ const copyRoot404To404Index = async ({ bucket }) => {
589
595
  throw error;
590
596
  }
591
597
  };
592
- const uploadDirectoryToS3 = async ({ bucket, bucketKey = "", directory, uploadSourceMaps = false }) => {
598
+ const uploadDirectoryToS3 = async ({ bucket, bucketKey = "", contentTypes = {}, directory, uploadSourceMaps = false }) => {
593
599
  log.info(logPrefix$18, `Uploading directory ${directory}/ to ${bucket}/${bucketKey}...`);
594
600
  const allFiles = await getAllFilesInsideADirectory({ directory });
595
601
  /**
@@ -597,6 +603,15 @@ const uploadDirectoryToS3 = async ({ bucket, bucketKey = "", directory, uploadSo
597
603
  * error. Discovered at #16 https://github.com/ttoss/carlin/issues/16.
598
604
  */
599
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.`);
600
615
  /**
601
616
  * Source maps expose the application's original source, and the bucket this
602
617
  * uploads to is public, so they are excluded unless explicitly requested.
@@ -625,6 +640,7 @@ const uploadDirectoryToS3 = async ({ bucket, bucketKey = "", directory, uploadSo
625
640
  await Promise.all(groupOfFiles.map((file) => {
626
641
  return uploadFileToS3({
627
642
  bucket,
643
+ contentType: contentTypes[relativePath(file)],
628
644
  key: path.join(bucketKey, path.relative(directory, file)),
629
645
  filePath: file
630
646
  });
@@ -3320,6 +3336,43 @@ const reportToGitHubPR = async () => {
3320
3336
  log.info(logPrefix$6, existingComment ? "PR comment updated." : "PR comment created.");
3321
3337
  };
3322
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
3323
3376
  //#region src/deploy/staticApp/getStaticAppBucket.ts
3324
3377
  const STATIC_APP_BUCKET_LOGICAL_ID$1 = "StaticBucket";
3325
3378
  const getStaticAppBucket = async ({ stackName }) => {
@@ -4088,7 +4141,7 @@ const findDefaultBuildFolder = async () => {
4088
4141
  };
4089
4142
  //#endregion
4090
4143
  //#region src/deploy/staticApp/uploadBuiltAppToS3.ts
4091
- const uploadBuiltAppToS3 = async ({ buildFolder: directory, bucket, uploadSourceMaps }) => {
4144
+ const uploadBuiltAppToS3 = async ({ buildFolder: directory, bucket, contentTypes, uploadSourceMaps }) => {
4092
4145
  /**
4093
4146
  * Only empty directory if the number of the files inside $directory.
4094
4147
  * If the number of files is zero, uploadDirectoryToS3 will thrown.
@@ -4100,6 +4153,7 @@ const uploadBuiltAppToS3 = async ({ buildFolder: directory, bucket, uploadSource
4100
4153
  });
4101
4154
  await uploadDirectoryToS3({
4102
4155
  bucket,
4156
+ contentTypes,
4103
4157
  directory,
4104
4158
  uploadSourceMaps
4105
4159
  });
@@ -4113,6 +4167,7 @@ const uploadBuiltAppToS3 = async ({ buildFolder: directory, bucket, uploadSource
4113
4167
  });
4114
4168
  await uploadDirectoryToS3({
4115
4169
  bucket,
4170
+ contentTypes,
4116
4171
  directory: defaultDirectory,
4117
4172
  uploadSourceMaps
4118
4173
  });
@@ -4132,7 +4187,7 @@ const logPrefix$4 = "static-app";
4132
4187
  * 1. Upload static files to the host bucket S3.
4133
4188
  * 1. Remove old deployment versions. Keep only the 3 most recent ones.
4134
4189
  */
4135
- 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 }) => {
4136
4191
  try {
4137
4192
  const { stackName } = await handleDeployInitialization({ logPrefix: logPrefix$4 });
4138
4193
  const params = { StackName: stackName };
@@ -4151,16 +4206,25 @@ const deployStaticApp = async ({ acm, aliases, appendIndexHtml, buildFolder, clo
4151
4206
  });
4152
4207
  const bucket = await getStaticAppBucket({ stackName });
4153
4208
  /**
4154
- * Stack already exists. Upload files first after changing the files routes
4155
- * 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.
4156
4212
  */
4157
- if (bucket) {
4158
- if (!skipUpload) await uploadBuiltAppToS3({
4213
+ const upload = ({ bucket }) => {
4214
+ return uploadBuiltAppToS3({
4159
4215
  buildFolder,
4160
4216
  bucket,
4161
4217
  cloudfront,
4218
+ contentTypes,
4162
4219
  uploadSourceMaps
4163
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 });
4164
4228
  const { Outputs } = await deploy({
4165
4229
  params,
4166
4230
  template
@@ -4177,12 +4241,7 @@ const deployStaticApp = async ({ acm, aliases, appendIndexHtml, buildFolder, clo
4177
4241
  });
4178
4242
  const newBucket = await getStaticAppBucket({ stackName });
4179
4243
  if (!newBucket) throw new Error(`Cannot find bucket at ${stackName}.`);
4180
- await uploadBuiltAppToS3({
4181
- buildFolder,
4182
- bucket: newBucket,
4183
- cloudfront,
4184
- uploadSourceMaps
4185
- });
4244
+ await upload({ bucket: newBucket });
4186
4245
  }
4187
4246
  } catch (error) {
4188
4247
  handleDeployError({
@@ -4218,6 +4277,11 @@ const options$4 = {
4218
4277
  require: false,
4219
4278
  type: "boolean"
4220
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
+ },
4221
4285
  "hosted-zone-name": {
4222
4286
  required: false,
4223
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.4.0",
3
+ "version": "2.6.0",
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/cloudformation": "^0.14.0",
60
+ "@ttoss/config": "^1.38.0",
61
61
  "@ttoss/read-config-file": "^2.3.0",
62
- "@ttoss/config": "^1.38.0"
62
+ "@ttoss/cloudformation": "^0.14.0"
63
63
  },
64
64
  "devDependencies": {
65
65
  "@types/adm-zip": "^0.5.6",