@anthusai/papyrus 1.18.0-next.4 → 1.18.0-next.6

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.
@@ -12,6 +12,7 @@ import fs from "node:fs";
12
12
  import { spawnSync } from "node:child_process";
13
13
  import path from "node:path";
14
14
  import { createRequire } from "node:module";
15
+ import { resolveCdkEnvironment } from "../src/cdk-environment.mjs";
15
16
 
16
17
  const USAGE = [
17
18
  "Usage: papyrus-infra synth --site infra/site.json [--out cdk.out] [--account A] [--region R]",
@@ -26,6 +27,14 @@ const opt = (name, fallback) => {
26
27
  const i = args.indexOf(`--${name}`);
27
28
  return i >= 0 ? args[i + 1] : fallback;
28
29
  };
30
+ const resolveEnvironmentOrExit = () => {
31
+ try {
32
+ return resolveCdkEnvironment({ explicitAccount: opt("account"), explicitRegion: opt("region") });
33
+ } catch (error) {
34
+ console.error(`papyrus-infra: ${error instanceof Error ? error.message : error}`);
35
+ process.exit(1);
36
+ }
37
+ };
29
38
  const command = args[0];
30
39
  if (command !== "synth" && command !== "print-buildspec" && command !== "deploy" && command !== "deploy-edge") {
31
40
  console.error(USAGE);
@@ -85,7 +94,7 @@ if (accountStack !== undefined) {
85
94
  const { App } = createRequire(path.join(process.cwd(), "noop.js"))("aws-cdk-lib");
86
95
  const app = new App({ outdir: path.resolve(opt("out", "cdk.out")) });
87
96
  new GithubOidcProviderStack(app, "GithubOidcProviderStack", {
88
- env: { account: opt("account", process.env.CDK_DEFAULT_ACCOUNT ?? "335163751677"), region: opt("region", process.env.CDK_DEFAULT_REGION ?? "us-east-1") },
97
+ env: resolveEnvironmentOrExit(),
89
98
  stackName: "github-oidc-provider",
90
99
  });
91
100
  app.synth();
@@ -168,7 +177,7 @@ try {
168
177
  const { App } = createRequire(path.join(process.cwd(), "noop.js"))("aws-cdk-lib");
169
178
  const app = new App({ outdir: path.resolve(opt("out", "cdk.out")) });
170
179
  new AmplifyAppShellStack(app, `AmplifyAppShell-${config.siteId}`, config, {
171
- env: { account: opt("account", process.env.CDK_DEFAULT_ACCOUNT ?? "335163751677"), region: opt("region", process.env.CDK_DEFAULT_REGION ?? "us-east-1") },
180
+ env: resolveEnvironmentOrExit(),
172
181
  stackName: resolveStackName(config),
173
182
  });
174
183
  app.synth();
package/package.json CHANGED
@@ -379,7 +379,7 @@
379
379
  "optional": true
380
380
  }
381
381
  },
382
- "version": "1.18.0-next.4",
382
+ "version": "1.18.0-next.6",
383
383
  "dependencies": {
384
384
  "@aws-amplify/adapter-nextjs": "^1.7.3",
385
385
  "@aws-crypto/sha256-js": "5.2.0",
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.18.0-next.4",
2
+ "version": "1.18.0-next.6",
3
3
  "routes": [
4
4
  {
5
5
  "file": "app/%5F_preview/[[...path]]/route.ts",
@@ -0,0 +1,44 @@
1
+ import { spawnSync } from "node:child_process";
2
+
3
+ const DEFAULT_REGION = "us-east-1";
4
+
5
+ export function resolveCdkRegion({ explicitRegion, environment = process.env }) {
6
+ return (
7
+ explicitRegion ||
8
+ environment.AWS_REGION ||
9
+ environment.AWS_DEFAULT_REGION ||
10
+ environment.CDK_DEFAULT_REGION ||
11
+ DEFAULT_REGION
12
+ );
13
+ }
14
+
15
+ function callerAccountFromAwsCli() {
16
+ const completed = spawnSync("aws", ["sts", "get-caller-identity", "--output", "json"], { encoding: "utf8" });
17
+ if (completed.error) throw new Error(`could not run the aws CLI: ${completed.error.message}`);
18
+ if (completed.status !== 0) throw new Error(String(completed.stderr || completed.stdout).trim() || `aws exited with status ${completed.status}`);
19
+ return JSON.parse(completed.stdout).Account;
20
+ }
21
+
22
+ export function resolveCdkAccount({ explicitAccount, environment = process.env, lookupCallerAccount = callerAccountFromAwsCli }) {
23
+ if (explicitAccount) return explicitAccount;
24
+ if (environment.CDK_DEFAULT_ACCOUNT) return environment.CDK_DEFAULT_ACCOUNT;
25
+ let account;
26
+ try {
27
+ account = lookupCallerAccount();
28
+ } catch (error) {
29
+ throw new Error(
30
+ `cannot determine the AWS account: pass --account, set CDK_DEFAULT_ACCOUNT, or provide credentials for "aws sts get-caller-identity" (${error instanceof Error ? error.message : error})`,
31
+ );
32
+ }
33
+ if (!/^\d{12}$/.test(String(account))) {
34
+ throw new Error(`cannot determine the AWS account: "aws sts get-caller-identity" returned ${JSON.stringify(account)}; pass --account or set CDK_DEFAULT_ACCOUNT`);
35
+ }
36
+ return String(account);
37
+ }
38
+
39
+ export function resolveCdkEnvironment({ explicitAccount, explicitRegion, environment = process.env, lookupCallerAccount }) {
40
+ return {
41
+ account: resolveCdkAccount({ explicitAccount, environment, lookupCallerAccount }),
42
+ region: resolveCdkRegion({ explicitRegion, environment }),
43
+ };
44
+ }