@40rty/ams-cli 0.3.1 → 0.4.2

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.
@@ -1,16 +1,18 @@
1
- // `forty deploy`'s build: a Hydrogen app as Vercel's Build Output (v3) — the
2
- // client as static files, the Oxygen worker as one Edge Function — plus the
3
- // env the hosted site runs with.
1
+ // `forty deploy`'s build, as Vercel's Build Output (v3), plus the env the
2
+ // hosted site runs with. A Hydrogen app: the client as static files, the
3
+ // Oxygen worker as one Edge Function. A Next.js app: whatever `vercel build`
4
+ // makes of it — the same output a Vercel deploy of the app would serve.
4
5
  import { spawnSync } from 'node:child_process';
5
6
  import { createHash } from 'node:crypto';
6
7
  import { cp, mkdir, readdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
7
8
  import path from 'node:path';
8
9
  import { fail } from './context.mjs';
9
- import { ASSET_BASE_ENV } from './base-path-build.mjs';
10
10
  import { findCredential } from './credentials.mjs';
11
11
 
12
12
  /** Inside `dist/`, which a Hydrogen app already ignores — never the developer's own `.vercel/`. */
13
13
  const OUTPUT_DIR = path.join('dist', 'forty-output');
14
+ /** Inside `.next/`, which a Next.js app already ignores. */
15
+ const NEXT_OUTPUT_DIR = path.join('.next', 'forty-output');
14
16
 
15
17
  // The Oxygen worker (`export default { fetch(request, env, ctx) }`) as a Vercel
16
18
  // Edge Function. Vercel Edge has no Cache API, so Hydrogen's cache is a no-op
@@ -23,25 +25,75 @@ export default function handler(request, context) {
23
25
  }
24
26
  `;
25
27
 
26
- /** `shopify hydrogen build` in `cwd` (the app's base-path copy), its assets served from `assetBase`, with its output on the terminal. */
27
- export function buildHydrogen(cwd, assetBase) {
28
- const env = { ...process.env, [ASSET_BASE_ENV]: assetBase };
28
+ /** The build env that makes `/` the agentic webpage (the contract in `deploy.mjs`). */
29
+ const AGENTIC = { FORTY_AGENTIC: '1' };
30
+
31
+ /** `shopify hydrogen build` in `cwd` (the app's build copy), agentic, with its output on the terminal. */
32
+ export function buildHydrogen(cwd) {
33
+ const env = { ...process.env, ...AGENTIC };
29
34
  const run = spawnSync('npx', ['--no-install', 'shopify', 'hydrogen', 'build', '--no-lockfile-check'], { cwd, stdio: 'inherit', env });
30
35
  if (run.error !== undefined) fail(`could not run \`shopify hydrogen build\` (${run.error.message})`);
31
36
  if (run.status !== 0) fail('`shopify hydrogen build` failed — fix the build, then run `forty deploy` again');
32
37
  }
33
38
 
39
+ /**
40
+ * `vercel build` in `dir` (the app's build copy), agentic, offline: a local project
41
+ * file stands in for `vercel pull`, so no Vercel login or project is needed;
42
+ * its no-op install keeps the copy's flat node_modules (`build-copy.mjs`).
43
+ * Each function names the project files it needs in its `filePathMap`
44
+ * (node_modules, mostly) instead of holding them. Those files are copied ONCE
45
+ * into the output's `_deps/` and the maps pointed there, so the upload — which
46
+ * sends only the output, deduplicated by content — carries them. (`vercel
47
+ * build --standalone` inlines them per function instead: a GB for 76 functions.)
48
+ * Then its `.vercel/output` is copied under `cwd`'s NEXT_OUTPUT_DIR with
49
+ * symlinks resolved — the build links functions that share files, and the
50
+ * upload sends files, not links. Returns the output directory.
51
+ */
52
+ export async function buildNext(cwd, dir) {
53
+ await mkdir(path.join(dir, '.vercel'), { recursive: true });
54
+ await writeFile(path.join(dir, '.vercel', 'project.json'), JSON.stringify({ projectId: 'forty-deploy', orgId: 'forty-deploy', settings: { framework: 'nextjs', installCommand: 'true' } }));
55
+ const run = spawnSync('npx', ['--yes', 'vercel@latest', 'build', '--prod', '--yes'], { cwd: dir, stdio: 'inherit', env: { ...process.env, ...AGENTIC, VERCEL_TELEMETRY_DISABLED: '1' } });
56
+ if (run.error !== undefined) fail(`could not run \`vercel build\` (${run.error.message})`);
57
+ if (run.status !== 0) fail('`vercel build` failed — fix the build, then run `forty deploy` again');
58
+ const out = path.join(cwd, NEXT_OUTPUT_DIR);
59
+ await rm(out, { recursive: true, force: true });
60
+ await cp(path.join(dir, '.vercel', 'output'), out, { recursive: true, dereference: true });
61
+ const copied = new Set();
62
+ for (const entry of await readdir(path.join(out, 'functions'), { recursive: true, withFileTypes: true })) {
63
+ if (!entry.isFile() || entry.name !== '.vc-config.json') continue;
64
+ const file = path.join(entry.parentPath, entry.name);
65
+ const config = JSON.parse(await readFile(file, 'utf8'));
66
+ if (config.filePathMap === undefined) continue;
67
+ const map = {};
68
+ for (const [key, from] of Object.entries(config.filePathMap)) {
69
+ const source = path.join(dir, from);
70
+ if (!copied.has(from)) {
71
+ await mkdir(path.dirname(path.join(out, '_deps', from)), { recursive: true });
72
+ await cp(source, path.join(out, '_deps', from), { dereference: true, recursive: true });
73
+ copied.add(from);
74
+ }
75
+ map[key] = `${DEPLOYED_OUTPUT}/_deps/${from}`;
76
+ }
77
+ config.filePathMap = map;
78
+ await writeFile(file, JSON.stringify(config));
79
+ }
80
+ return out;
81
+ }
82
+
83
+ /** Where the api places the output inside the deployment (`spacefront-hosting.ts`); `filePathMap` is relative to the deployment root. */
84
+ const DEPLOYED_OUTPUT = '.vercel/output';
85
+
34
86
  /**
35
87
  * Lay a build's `dist/client` + `dist/server` out as a Build Output under
36
- * `cwd`'s OUTPUT_DIR. The app answers under `/<slug>/`, so its client files are
37
- * served from there too (`static/<slug>/assets/…`).
88
+ * `cwd`'s OUTPUT_DIR: its client files served as they are (`static/assets/…`),
89
+ * the worker the one function every other path falls through to.
38
90
  */
39
- export async function assembleBuildOutput(cwd, buildDir, slug) {
91
+ export async function assembleBuildOutput(cwd, buildDir) {
40
92
  const out = path.join(cwd, OUTPUT_DIR);
41
93
  const func = path.join(out, 'functions', 'index.func');
42
94
  await rm(out, { recursive: true, force: true });
43
95
  await mkdir(func, { recursive: true });
44
- await cp(path.join(buildDir, 'dist', 'client'), path.join(out, 'static', slug), { recursive: true });
96
+ await cp(path.join(buildDir, 'dist', 'client'), path.join(out, 'static'), { recursive: true });
45
97
  // The worker and any chunk or wasm it imports — not its source map, metafile or bundle report. Its entry becomes server.js.
46
98
  const runtime = (src) => !/\.[a-z]+$/.test(src) || /\.(m?js|wasm)$/.test(src);
47
99
  await cp(path.join(buildDir, 'dist', 'server'), func, { recursive: true, filter: runtime });
@@ -66,25 +118,28 @@ export async function listOutputFiles(dir) {
66
118
  }
67
119
 
68
120
  /** What a Hydrogen app cannot start without. */
69
- const REQUIRED = ['SESSION_SECRET', 'PUBLIC_STORE_DOMAIN', 'PUBLIC_STOREFRONT_API_TOKEN'];
70
- /** Read by Hydrogen when present; public by Shopify's own naming. */
71
- const KNOWN = ['PUBLIC_CHECKOUT_DOMAIN', 'SHOP_ID', 'PUBLIC_STOREFRONT_ID'];
121
+ export const HYDROGEN_REQUIRED = ['SESSION_SECRET', 'PUBLIC_STORE_DOMAIN', 'PUBLIC_STOREFRONT_API_TOKEN'];
122
+ /**
123
+ * Deployed even though the name looks secret: Shopify's own public values
124
+ * (Hydrogen's, and Next.js Commerce's names for the same Storefront token).
125
+ */
126
+ const KNOWN = ['PUBLIC_CHECKOUT_DOMAIN', 'SHOP_ID', 'PUBLIC_STOREFRONT_ID', 'SHOPIFY_STORE_DOMAIN', 'SHOPIFY_STOREFRONT_ACCESS_TOKEN'];
72
127
  const LOOKS_SECRET = /SECRET|TOKEN|PRIVATE|PASSWORD|PASSWD|CREDENTIAL|(^|_)KEY$|(^|_)AUTH/;
73
128
 
74
129
  /**
75
- * The env the hosted site runs with, from the app's `.env`: what Hydrogen
76
- * needs, anything `PUBLIC_`, anything that does not look like a secret, and
130
+ * The env the hosted site runs with, from the app's `.env`: what the app
131
+ * needs (`required`), anything `PUBLIC_`, anything that does not look like a secret, and
77
132
  * whatever the developer opted in with `--env KEY`. `FORTY_*` never — the site
78
133
  * is the space's published face, and `FORTY_DEV_TOKEN` would turn it into a
79
134
  * developer's preview.
80
135
  */
81
- export function pickDeployEnv(dotenv, optIn) {
82
- for (const key of REQUIRED) if (typeof dotenv[key] !== 'string' || dotenv[key] === '') fail(`.env must set ${key} — the app cannot start without it`);
136
+ export function pickDeployEnv(dotenv, optIn, required = HYDROGEN_REQUIRED) {
137
+ for (const key of required) if (typeof dotenv[key] !== 'string' || dotenv[key] === '') fail(`.env must set ${key} — the app cannot start without it`);
83
138
  const env = {};
84
139
  const withheld = [];
85
140
  for (const [key, value] of Object.entries(dotenv)) {
86
141
  if (key.startsWith('FORTY_')) continue;
87
- if (REQUIRED.includes(key) || KNOWN.includes(key) || key.startsWith('PUBLIC_') || optIn.includes(key) || !LOOKS_SECRET.test(key)) env[key] = value;
142
+ if (required.includes(key) || KNOWN.includes(key) || key.startsWith('PUBLIC_') || optIn.includes(key) || !LOOKS_SECRET.test(key)) env[key] = value;
88
143
  else withheld.push(key);
89
144
  }
90
145
  for (const key of optIn) {