@anthusai/papyrus 1.18.0-next.3 → 1.18.0-next.5

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();
@@ -134,20 +143,24 @@ if (command === "deploy") {
134
143
  const outputDirectory = path.resolve(opt("out", process.env.CDK_OUTDIR ?? "cdk.out"));
135
144
  const explicitAccount = opt("account");
136
145
  const explicitRegion = opt("region");
137
- const synthCommand = [
146
+ const synthesis = spawnSync(
138
147
  process.execPath,
139
- path.resolve(process.argv[1]),
140
- "synth",
141
- "--site",
142
- path.resolve(opt("site", "infra/site.json")),
143
- "--out",
144
- outputDirectory,
145
- ...(explicitAccount ? ["--account", explicitAccount] : []),
146
- ...(explicitRegion ? ["--region", explicitRegion] : []),
147
- ].map((part) => JSON.stringify(part)).join(" ");
148
+ [
149
+ path.resolve(process.argv[1]),
150
+ "synth",
151
+ "--site",
152
+ path.resolve(opt("site", "infra/site.json")),
153
+ "--out",
154
+ outputDirectory,
155
+ ...(explicitAccount ? ["--account", explicitAccount] : []),
156
+ ...(explicitRegion ? ["--region", explicitRegion] : []),
157
+ ],
158
+ { stdio: "inherit" },
159
+ );
160
+ if (synthesis.status !== 0) process.exit(synthesis.status ?? 1);
148
161
  const deployment = spawnSync(
149
162
  "npx",
150
- ["--yes", "aws-cdk@2", "deploy", "--app", synthCommand, "--require-approval", "never", "--output", outputDirectory, `AmplifyAppShell-${config.siteId}`],
163
+ ["--yes", "aws-cdk@2", "deploy", "--app", outputDirectory, "--require-approval", "never", `AmplifyAppShell-${config.siteId}`],
151
164
  { stdio: "inherit" },
152
165
  );
153
166
  process.exit(deployment.status ?? 1);
@@ -164,7 +177,7 @@ try {
164
177
  const { App } = createRequire(path.join(process.cwd(), "noop.js"))("aws-cdk-lib");
165
178
  const app = new App({ outdir: path.resolve(opt("out", "cdk.out")) });
166
179
  new AmplifyAppShellStack(app, `AmplifyAppShell-${config.siteId}`, config, {
167
- env: { account: opt("account", process.env.CDK_DEFAULT_ACCOUNT ?? "335163751677"), region: opt("region", process.env.CDK_DEFAULT_REGION ?? "us-east-1") },
180
+ env: resolveEnvironmentOrExit(),
168
181
  stackName: resolveStackName(config),
169
182
  });
170
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.3",
382
+ "version": "1.18.0-next.5",
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.3",
2
+ "version": "1.18.0-next.5",
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
+ }
@@ -25,15 +25,22 @@ function logicalIdsInStack(stackName, region) {
25
25
  return new Set(JSON.parse(completed.stdout).StackResourceSummaries.map((summary) => summary.LogicalResourceId));
26
26
  }
27
27
 
28
- function templateParameterFlags(parameterValues, templateFile) {
28
+ function parameterNamesInStack(stackName, region) {
29
+ const completed = runAws(["cloudformation", "describe-stacks", "--stack-name", stackName, "--region", region, "--output", "json"], { allowFailure: true });
30
+ if (completed.status !== 0) return new Set();
31
+ const stack = JSON.parse(completed.stdout).Stacks?.[0];
32
+ return new Set((stack?.Parameters ?? []).map((parameter) => parameter.ParameterKey));
33
+ }
34
+
35
+ function templateParameterFlags(parameterValues, templateFile, parameterNamesInStackNow) {
29
36
  const templateText = fs.readFileSync(templateFile, "utf8");
30
37
  const parametersSection = templateText.split("\nResources:")[0].split("\nParameters:\n")[1] ?? "";
31
38
  const parameterNames = [...parametersSection.matchAll(/^ ([A-Za-z0-9]+):\s*$/gm)].map((match) => match[1]);
32
- return parameterNames.map((name) =>
33
- name in parameterValues
34
- ? `ParameterKey=${name},ParameterValue=${parameterValues[name]}`
35
- : `ParameterKey=${name},UsePreviousValue=true`,
36
- );
39
+ return parameterNames.flatMap((name) => {
40
+ if (name in parameterValues) return [`ParameterKey=${name},ParameterValue=${parameterValues[name]}`];
41
+ if (parameterNamesInStackNow.has(name)) return [`ParameterKey=${name},UsePreviousValue=true`];
42
+ return [];
43
+ });
37
44
  }
38
45
 
39
46
  export function applyChangeSet({ stackName, region, templateFile, parameterValues, adoptions }) {
@@ -47,7 +54,7 @@ export function applyChangeSet({ stackName, region, templateFile, parameterValue
47
54
  "--template-body", `file://${path.resolve(templateFile)}`,
48
55
  "--capabilities", "CAPABILITY_IAM",
49
56
  "--region", region,
50
- "--parameters", ...templateParameterFlags(parameterValues, templateFile),
57
+ "--parameters", ...templateParameterFlags(parameterValues, templateFile, parameterNamesInStack(stackName, region)),
51
58
  ];
52
59
  if (adoptions) createArguments.push("--resources-to-import", JSON.stringify(adoptions));
53
60
  runAws(createArguments);