@anthusai/papyrus 1.0.0-next.3 → 1.0.0-next.4

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.
package/app/layout.tsx CHANGED
@@ -6,6 +6,8 @@ import { PapyrusConsoleShell } from "../components/papyrus-console-shell";
6
6
  import { TooltipProvider } from "../components/ui/tooltip";
7
7
  import { getPresentationChoices, getSiteBrand } from "../lib/site-brand";
8
8
  import { getSiteStack } from "../lib/site-stack";
9
+ import { isIndexable, showStagingBanner } from "../lib/site-env";
10
+ import { StagingBanner } from "../components/staging-banner";
9
11
  import "./tailwind.css";
10
12
  import "./globals.css";
11
13
  import "papyrus-site-theme";
@@ -27,6 +29,7 @@ export async function generateMetadata(): Promise<Metadata> {
27
29
  return {
28
30
  title: siteBrand.appTitle,
29
31
  description: siteBrand.appDescription,
32
+ ...(isIndexable() ? {} : { robots: { index: false, follow: false } }),
30
33
  icons: {
31
34
  icon: [
32
35
  { url: `/icon-light.png?v=${faviconVersion}`, type: "image/png" },
@@ -127,6 +130,7 @@ export default async function RootLayout({ children }: Readonly<{ children: Reac
127
130
  window.addEventListener("papyrus:settings-changed", apply);
128
131
  })();`}
129
132
  </Script>
133
+ {showStagingBanner() ? <StagingBanner /> : null}
130
134
  <AmplifyClientProvider>
131
135
  <TooltipProvider>
132
136
  <PapyrusConsoleShell>{children}</PapyrusConsoleShell>
package/app/robots.ts ADDED
@@ -0,0 +1,11 @@
1
+ import type { MetadataRoute } from "next";
2
+ import { isIndexable } from "../lib/site-env";
3
+
4
+ export const dynamic = "force-dynamic";
5
+
6
+ export default function robots(): MetadataRoute.Robots {
7
+ if (isIndexable()) {
8
+ return { rules: { userAgent: "*", allow: "/" } };
9
+ }
10
+ return { rules: { userAgent: "*", disallow: "/" } };
11
+ }
@@ -1,35 +1,65 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
- * papyrus-infra synth --site infra/site.json [--out cdk.out]
4
- * Synthesizes the app-shell stack for a publication from its own site.json
5
- * (SPIKE, PPY-82be6c). Synth only: deploying is a separate, reviewed step.
3
+ * papyrus-infra synth --site infra/site.json [--out cdk.out] [--account A] [--region R]
4
+ * papyrus-infra print-buildspec --site infra/site.json --app cms-production|cms-staging|reader
5
+ * Synthesizes the app-shell stack for a publication from its own site.json, or
6
+ * prints one of the generated Amplify build specs. Synth only: deploying is a
7
+ * separate, reviewed step.
6
8
  */
7
9
  import fs from "node:fs";
8
10
  import path from "node:path";
9
11
  import { createRequire } from "node:module";
12
+ import { buildSpecFor } from "../infra/build-specs.js";
13
+ import { parseSiteConfig } from "../infra/site-config.js";
14
+
15
+ const USAGE = [
16
+ "Usage: papyrus-infra synth --site infra/site.json [--out cdk.out] [--account A] [--region R]",
17
+ " papyrus-infra print-buildspec --site infra/site.json --app cms-production|cms-staging|reader",
18
+ ].join("\n");
19
+
20
+ const args = process.argv.slice(2);
21
+ const opt = (name, fallback) => {
22
+ const i = args.indexOf(`--${name}`);
23
+ return i >= 0 ? args[i + 1] : fallback;
24
+ };
25
+ const command = args[0];
26
+ if (command !== "synth" && command !== "print-buildspec") {
27
+ console.error(USAGE);
28
+ process.exit(command ? 1 : 0);
29
+ }
30
+
31
+ let config;
32
+ try {
33
+ config = parseSiteConfig(JSON.parse(fs.readFileSync(path.resolve(opt("site", "infra/site.json")), "utf8")));
34
+ } catch (error) {
35
+ console.error(`papyrus-infra: ${error instanceof Error ? error.message : error}`);
36
+ process.exit(1);
37
+ }
38
+
39
+ if (command === "print-buildspec") {
40
+ const app = opt("app");
41
+ if (!["cms-production", "cms-staging", "reader"].includes(app)) {
42
+ console.error(USAGE);
43
+ process.exit(1);
44
+ }
45
+ try {
46
+ process.stdout.write(buildSpecFor(config, app));
47
+ } catch (error) {
48
+ console.error(`papyrus-infra: ${error instanceof Error ? error.message : error}`);
49
+ process.exit(1);
50
+ }
51
+ process.exit(0);
52
+ }
10
53
 
11
54
  let AmplifyAppShellStack;
12
55
  try {
13
56
  ({ AmplifyAppShellStack } = await import("../infra/index.js"));
14
57
  } catch (error) {
15
- // infra/index.js explains the missing optional peers; print it without a stack trace.
16
58
  console.error(`papyrus-infra: ${error instanceof Error ? error.message : error}`);
17
59
  process.exit(1);
18
60
  }
19
61
 
20
- // aws-cdk-lib is an optional peer: resolve it from the publication's infra/ package (cwd).
21
62
  const { App } = createRequire(path.join(process.cwd(), "noop.js"))("aws-cdk-lib");
22
-
23
- const args = process.argv.slice(2);
24
- const opt = (name, fallback) => {
25
- const i = args.indexOf(`--${name}`);
26
- return i >= 0 ? args[i + 1] : fallback;
27
- };
28
- if (args[0] !== "synth") {
29
- console.error("Usage: papyrus-infra synth --site infra/site.json [--out cdk.out]");
30
- process.exit(args[0] ? 1 : 0);
31
- }
32
- const config = JSON.parse(fs.readFileSync(path.resolve(opt("site", "infra/site.json")), "utf8"));
33
63
  const app = new App({ outdir: path.resolve(opt("out", "cdk.out")) });
34
64
  new AmplifyAppShellStack(app, `AmplifyAppShell-${config.siteId}`, config, {
35
65
  env: { account: opt("account", "335163751677"), region: opt("region", "us-east-1") },
@@ -0,0 +1,11 @@
1
+ export function StagingBanner() {
2
+ return (
3
+ <div
4
+ data-staging-banner
5
+ role="status"
6
+ className="sticky top-0 z-50 bg-amber-300 px-3 py-1 text-center text-xs font-semibold uppercase tracking-wide text-black"
7
+ >
8
+ STAGING: drafts shown as published
9
+ </div>
10
+ );
11
+ }
@@ -1,109 +1,152 @@
1
- import { Stack, SecretValue } from "aws-cdk-lib";
1
+ import { ArnFormat, CfnOutput, Stack } from "aws-cdk-lib";
2
2
  import * as iam from "aws-cdk-lib/aws-iam";
3
3
  import * as amplify from "aws-cdk-lib/aws-amplify";
4
+ import { cmsProductionBuildSpec, cmsStagingBuildSpec, readerBuildSpec } from "./build-specs.js";
5
+ import { resolveStagingDomainName } from "./site-config.js";
6
+ function environmentVariables(variables) {
7
+ return Object.entries(variables).map(([name, value]) => ({ name, value }));
8
+ }
4
9
  /**
5
- * Provisions the Amplify *app shell* for a Papyrus publication CMS:
6
- *
7
- * - AWS::Amplify::App (WEB_COMPUTE, GitHub repo connection via PAT in Secrets Manager)
8
- * - AWS::Amplify::Branch (main, with PAPYRUS_SITE_BRAND env vars)
9
- * - AWS::Amplify::Domain (newsroom.<site>.us on the site's Route 53 zone)
10
- * - IAM service role (backend deploy) + compute role (SSR rendering)
10
+ * Provisions the Amplify *app shell* of a Papyrus publication from its
11
+ * site.json:
11
12
  *
12
- * The Papyrus *backend* (AppSync, Storage, Lambda) is NOT created here. It
13
- * deploys via `ampx pipeline-deploy` from the repo `amplify.yml` once Amplify
14
- * CI runs a build on the branch. This stack only creates the container the
15
- * backend deploys into, so the app is never "created manually" in the console.
13
+ * - CMS app (WEB_COMPUTE) with branches `main` (production, owns the backend)
14
+ * and `staging` (frontend only, reads the production backend)
15
+ * - for `markus-static` sites, a reader app (WEB) with branch `main`
16
+ * - one domain association per branch/app, IAM service and compute roles
16
17
  *
17
- * The GitHub access token is read from Secrets Manager by dynamic reference,
18
- * so the token value never appears in the synthesized template.
18
+ * No repository and no access token are configured: the apps are manual-deploy
19
+ * until Amplify's GitHub App is connected once in the console
20
+ * (docs/site-hosting.md). The Papyrus backend itself is created inside the CMS
21
+ * app by `ampx pipeline-deploy` from the production build.
19
22
  */
20
23
  export class AmplifyAppShellStack extends Stack {
21
- appId;
22
- defaultDomain;
24
+ cmsAppId;
25
+ readerAppId;
23
26
  constructor(scope, id, config, props) {
24
27
  super(scope, id, props);
25
- const githubToken = SecretValue.secretsManager(config.githubTokenSecretName).unsafeUnwrap();
26
- // Service role Amplify assumes to deploy the backend (ampx pipeline-deploy
27
- // creates AppSync, Storage, Lambda, etc., so it needs broad permissions).
28
- const serviceRole = new iam.Role(this, "AmplifyServiceRole", {
28
+ const cmsAppName = config.cms.appName ?? `${config.siteId}-cms`;
29
+ const stagingDomainName = resolveStagingDomainName(config);
30
+ const cmsServiceRole = new iam.Role(this, "AmplifyServiceRole", {
29
31
  assumedBy: new iam.ServicePrincipal("amplify.amazonaws.com"),
30
- description: `Backend deploy service role for Amplify app ${config.appName}`,
32
+ description: `Backend deploy and staging build service role for Amplify app ${cmsAppName} (AdministratorAccess: documented risk)`,
31
33
  managedPolicies: [iam.ManagedPolicy.fromAwsManagedPolicyName("AdministratorAccess")],
32
34
  });
33
- // WEB_COMPUTE (Next.js SSR) requires a compute role for the rendering server.
34
35
  const computeRole = new iam.Role(this, "AmplifyComputeRole", {
35
36
  assumedBy: new iam.ServicePrincipal("amplify.amazonaws.com"),
36
- description: `SSR compute role for Amplify app ${config.appName}`,
37
+ description: `SSR compute role for Amplify app ${cmsAppName}`,
37
38
  managedPolicies: [
38
39
  iam.ManagedPolicy.fromAwsManagedPolicyName("service-role/AWSLambdaBasicExecutionRole"),
39
40
  iam.ManagedPolicy.fromAwsManagedPolicyName("CloudWatchLogsFullAccess"),
40
41
  ],
41
42
  });
42
- const app = new amplify.CfnApp(this, "App", {
43
- name: config.appName,
44
- description: config.description,
45
- platform: config.platform,
46
- repository: config.repository,
47
- accessToken: githubToken,
48
- iamServiceRole: serviceRole.roleArn,
43
+ const cmsApp = new amplify.CfnApp(this, "App", {
44
+ name: cmsAppName,
45
+ description: `Papyrus newsroom CMS for ${config.siteId} (WEB_COMPUTE)`,
46
+ platform: "WEB_COMPUTE",
47
+ iamServiceRole: cmsServiceRole.roleArn,
49
48
  computeRoleArn: computeRole.roleArn,
50
- buildSpec: this.buildSpec(),
49
+ buildSpec: cmsProductionBuildSpec(config),
50
+ jobConfig: config.cms.buildComputeType ? { buildComputeType: config.cms.buildComputeType } : undefined,
51
51
  customRules: [{ source: "/<*>", target: "/index.html", status: "404-200" }],
52
52
  });
53
- this.appId = app.attrAppId;
54
- this.defaultDomain = `${app.attrAppId}.amplifyapp.com`;
55
- const branch = new amplify.CfnBranch(this, "Branch", {
56
- appId: app.attrAppId,
57
- branchName: config.branchName,
53
+ const stagingMode = config.frontend === "markus-static"
54
+ ? { PAPYRUS_STAGING_PREVIEW: "static" }
55
+ : { PAPYRUS_CONTENT_SOURCE: "drafts" };
56
+ const productionBranch = new amplify.CfnBranch(this, "Branch", {
57
+ appId: cmsApp.attrAppId,
58
+ branchName: "main",
58
59
  stage: "PRODUCTION",
59
- enableAutoBuild: config.enableAutoBuild ?? true,
60
+ enableAutoBuild: true,
61
+ enablePerformanceMode: false,
62
+ environmentVariables: environmentVariables({
63
+ ...config.cms.environment,
64
+ PAPYRUS_SITE_BRAND: config.brand,
65
+ SITE_ENV: "production",
66
+ PAPYRUS_CONTENT_SOURCE: "published",
67
+ }),
68
+ });
69
+ const stagingBranch = new amplify.CfnBranch(this, "StagingBranch", {
70
+ appId: cmsApp.attrAppId,
71
+ branchName: "staging",
72
+ stage: "BETA",
73
+ enableAutoBuild: true,
60
74
  enablePerformanceMode: false,
61
- environmentVariables: Object.entries(config.environment).map(([key, value]) => ({
62
- name: key,
63
- value,
64
- })),
75
+ buildSpec: cmsStagingBuildSpec(config),
76
+ environmentVariables: environmentVariables({
77
+ ...config.cms.environment,
78
+ PAPYRUS_SITE_BRAND: config.brand,
79
+ SITE_ENV: "staging",
80
+ ...stagingMode,
81
+ NEXT_PUBLIC_PAPYRUS_STAGING_URL: `https://${stagingDomainName}`,
82
+ }),
83
+ });
84
+ const productionDomain = new amplify.CfnDomain(this, "Domain", {
85
+ appId: cmsApp.attrAppId,
86
+ domainName: config.cms.domainName,
87
+ subDomainSettings: [{ branchName: "main", prefix: "" }],
65
88
  });
66
- branch.addResourceDependency(app);
67
- const domain = new amplify.CfnDomain(this, "Domain", {
68
- appId: app.attrAppId,
69
- domainName: config.domainName,
70
- subDomainSettings: [{ branchName: config.branchName, prefix: "" }],
89
+ productionDomain.addResourceDependency(productionBranch);
90
+ const stagingDomain = new amplify.CfnDomain(this, "StagingDomain", {
91
+ appId: cmsApp.attrAppId,
92
+ domainName: stagingDomainName,
93
+ subDomainSettings: [{ branchName: "staging", prefix: "" }],
71
94
  });
72
- domain.addResourceDependency(branch);
73
- this.exportValue(app.attrAppId, { description: `Amplify app id for ${config.appName}` });
95
+ stagingDomain.addResourceDependency(stagingBranch);
96
+ this.cmsAppId = cmsApp.attrAppId;
97
+ new CfnOutput(this, "CmsAppId", { value: cmsApp.attrAppId, description: `Amplify app id for ${cmsAppName}` });
98
+ new CfnOutput(this, "CmsOrigin", { value: `https://${config.cms.domainName}/` });
99
+ new CfnOutput(this, "StagingOrigin", { value: `https://${stagingDomainName}/` });
100
+ if (config.reader) {
101
+ this.readerAppId = this.addReaderApp(config, config.reader, cmsApp.attrAppId);
102
+ }
74
103
  }
75
- /**
76
- * Build spec mirrors the repo root amplify.yml so the app deploys the full
77
- * Papyrus backend + Next.js frontend on every build. Inlined because Amplify
78
- * needs a buildSpec on the app for the first deploy before the repo is cloned.
79
- */
80
- buildSpec() {
81
- return [
82
- "version: 1",
83
- "backend:",
84
- " phases:",
85
- " build:",
86
- " commands:",
87
- " - npm install --cache .npm --prefer-offline",
88
- " - |",
89
- " if [ -z \"${PAPYRUS_CONSOLE_RESPONDER_IMAGE_URI:-}\" ]; then",
90
- " export PAPYRUS_CONSOLE_RESPONDER_ALLOW_LOCAL_BUILD=true",
91
- " fi",
92
- " - npx ampx pipeline-deploy --debug --branch $AWS_BRANCH --app-id $AWS_APP_ID",
93
- "frontend:",
94
- " phases:",
95
- " build:",
96
- " commands:",
97
- " - env | grep -e '^PAPYRUS_CONTENT_SOURCE=' -e '^PAPYRUS_EDITION_SLUG=' -e '^PAPYRUS_REVALIDATE_SECRET=' >> .env.production",
98
- " - npm run build",
99
- " artifacts:",
100
- " baseDirectory: .next",
101
- " files:",
102
- " - '**/*'",
103
- " cache:",
104
- " paths:",
105
- " - .next/cache/**/*",
106
- " - .npm/**/*",
107
- ].join("\n");
104
+ addReaderApp(config, reader, cmsAppId) {
105
+ const readerAppName = reader.appName ?? `${config.siteId}-reader`;
106
+ const readerBranchName = reader.branchName ?? "main";
107
+ const readerServiceRole = new iam.Role(this, "ReaderServiceRole", {
108
+ assumedBy: new iam.ServicePrincipal("amplify.amazonaws.com"),
109
+ description: `Static reader build role for Amplify app ${readerAppName} (reads the CMS app's SSM secrets and media only)`,
110
+ });
111
+ readerServiceRole.addToPolicy(new iam.PolicyStatement({
112
+ sid: "ReadSiteSecretsForJwtMinting",
113
+ actions: ["ssm:GetParameter", "ssm:GetParameters"],
114
+ resources: [
115
+ this.formatArn({ service: "ssm", resource: "parameter", resourceName: `amplify/${cmsAppId}/*`, arnFormat: ArnFormat.SLASH_RESOURCE_NAME }),
116
+ this.formatArn({ service: "ssm", resource: "parameter", resourceName: `amplify/shared/${cmsAppId}/*`, arnFormat: ArnFormat.SLASH_RESOURCE_NAME }),
117
+ ],
118
+ }));
119
+ readerServiceRole.addToPolicy(new iam.PolicyStatement({
120
+ sid: "ReadMediaForExport",
121
+ actions: ["s3:GetObject"],
122
+ resources: ["arn:aws:s3:::amplify-*/media/*"],
123
+ }));
124
+ const readerApp = new amplify.CfnApp(this, "ReaderApp", {
125
+ name: readerAppName,
126
+ description: `Papyrus static reader for ${config.siteId} (WEB)`,
127
+ platform: "WEB",
128
+ iamServiceRole: readerServiceRole.roleArn,
129
+ buildSpec: readerBuildSpec(config),
130
+ });
131
+ const readerBranch = new amplify.CfnBranch(this, "ReaderBranch", {
132
+ appId: readerApp.attrAppId,
133
+ branchName: readerBranchName,
134
+ stage: "PRODUCTION",
135
+ enableAutoBuild: true,
136
+ environmentVariables: environmentVariables({
137
+ ...(reader.environment ?? {}),
138
+ PAPYRUS_SITE_BRAND: config.brand,
139
+ SITE_ENV: "production",
140
+ }),
141
+ });
142
+ const readerDomain = new amplify.CfnDomain(this, "ReaderDomain", {
143
+ appId: readerApp.attrAppId,
144
+ domainName: reader.domainName,
145
+ subDomainSettings: [{ branchName: readerBranchName, prefix: "" }],
146
+ });
147
+ readerDomain.addResourceDependency(readerBranch);
148
+ new CfnOutput(this, "ReaderAppId", { value: readerApp.attrAppId, description: `Amplify app id for ${readerAppName}` });
149
+ new CfnOutput(this, "ReaderOrigin", { value: `https://${reader.domainName}/` });
150
+ return readerApp.attrAppId;
108
151
  }
109
152
  }