@40rty/ams-cli 0.5.18 → 0.5.20
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/bin/commands/__tests__/connect.test.mjs +57 -0
- package/bin/commands/connect.mjs +86 -107
- package/bin/commands/doctor.mjs +6 -16
- package/bin/forty.mjs +5 -4
- package/bin/lib/context.mjs +3 -22
- package/dist/wire.js +514 -485
- package/package.json +1 -1
- package/templates/package/README.md +1 -1
- package/bin/lib/__tests__/store-page.test.mjs +0 -26
- package/bin/lib/store-page.d.mts +0 -14
- package/bin/lib/store-page.mjs +0 -82
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@ tells it which blocks answer which question.
|
|
|
8
8
|
```bash
|
|
9
9
|
npm install
|
|
10
10
|
npx forty init # sign in, pick (or --create) your space
|
|
11
|
-
npx forty connect --
|
|
11
|
+
npx forty connect shopify --domain your-store.myshopify.com --storefront-access-token <token> # `forty connect --list` for the rest
|
|
12
12
|
npm run dev # sign in, pick your space, then every save updates your preview
|
|
13
13
|
npm run release # publish a new release — visitors see it
|
|
14
14
|
```
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
// Where a storefront ships its public Storefront token — what `forty connect --from` reads.
|
|
2
|
-
import { describe, expect, it } from 'vitest';
|
|
3
|
-
import { findShopDomains, findTokenCandidates } from '../store-page.mjs';
|
|
4
|
-
|
|
5
|
-
describe('findTokenCandidates', () => {
|
|
6
|
-
it('reads the token Hydrogen serializes into its consent config (acme.example shape)', () => {
|
|
7
|
-
const html = String.raw`\"checkoutDomain\",\"checkout.acme.example\",\"storefrontAccessToken\",\"2efed5e20dc72ac998fdbbc55ae0649e\",\"withPrivacyBanner\"`;
|
|
8
|
-
expect(findTokenCandidates(html)).toEqual(['2efed5e20dc72ac998fdbbc55ae0649e']);
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
it('reads it from JSON and from an Online Store theme meta tag', () => {
|
|
12
|
-
expect(findTokenCandidates('{"storefrontAccessToken":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}')).toEqual(['a'.repeat(32)]);
|
|
13
|
-
expect(findTokenCandidates('<meta name="shopify-checkout-api-token" content="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb">')).toEqual(['b'.repeat(32)]);
|
|
14
|
-
expect(findTokenCandidates('<script id="shopify-features" type="application/json">{"accessToken":"cccccccccccccccccccccccccccccccc","betas":[]}</script>')).toEqual(['c'.repeat(32)]);
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it('finds nothing on a page that ships no token', () => {
|
|
18
|
-
expect(findTokenCandidates('<html>469e4518231869be4ba940a1dcefec4b</html>')).toEqual([]);
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
describe('findShopDomains', () => {
|
|
23
|
-
it('ranks the store domain the page mentions most', () => {
|
|
24
|
-
expect(findShopDomains('x.myshopify.com acmebody.myshopify.com acmebody.myshopify.com')).toEqual(['acmebody.myshopify.com', 'x.myshopify.com']);
|
|
25
|
-
});
|
|
26
|
-
});
|
package/bin/lib/store-page.d.mts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/** Types for `store-page.mjs`, for the TypeScript tooling that reads a storefront the way `forty connect --from` does (scripts/create-space/resolve.ts). */
|
|
2
|
-
export const STOREFRONT_API_VERSION: string;
|
|
3
|
-
|
|
4
|
-
/** Every myshopify domain the page names, most-mentioned first. */
|
|
5
|
-
export function findShopDomains(html: string): string[];
|
|
6
|
-
|
|
7
|
-
/** Public Storefront token candidates the page ships, most trusted first. */
|
|
8
|
-
export function findTokenCandidates(html: string): string[];
|
|
9
|
-
|
|
10
|
-
/** Does the Storefront API at `domain` accept `token`? */
|
|
11
|
-
export function acceptsToken(domain: string, token: string): Promise<boolean>;
|
|
12
|
-
|
|
13
|
-
/** The store behind a storefront URL (or its page saved from a browser), or why there is none. */
|
|
14
|
-
export function resolveStorePage(source: string): Promise<{ domain: string; token: string } | { error: string }>;
|
package/bin/lib/store-page.mjs
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
// Which Shopify store is behind a storefront URL, and a Storefront token that
|
|
2
|
-
// answers for it — read from the page every visitor's browser already gets.
|
|
3
|
-
//
|
|
4
|
-
// A storefront's Storefront token is PUBLIC by design: it ships to every
|
|
5
|
-
// browser so the page can query products. Where it ships depends on the stack:
|
|
6
|
-
// - Hydrogen renders it into its consent config (`storefrontAccessToken`
|
|
7
|
-
// beside `checkoutDomain`);
|
|
8
|
-
// - Online Store themes publish it as `<meta name="shopify-checkout-api-token">`
|
|
9
|
-
// or as the `shopify-features` script's `accessToken`.
|
|
10
|
-
// A candidate only counts once the Storefront API accepts it for the store's
|
|
11
|
-
// own domain. `forty connect --from <url>` and create-space's resolve stage
|
|
12
|
-
// both read the page through this module.
|
|
13
|
-
|
|
14
|
-
import { readFile } from 'node:fs/promises';
|
|
15
|
-
|
|
16
|
-
export const STOREFRONT_API_VERSION = '2025-07';
|
|
17
|
-
|
|
18
|
-
/** Every myshopify domain the page names, most-mentioned first. */
|
|
19
|
-
export function findShopDomains(html) {
|
|
20
|
-
const counts = new Map();
|
|
21
|
-
for (const m of html.matchAll(/\b([a-z0-9][a-z0-9-]*)\.myshopify\.com\b/g)) {
|
|
22
|
-
const domain = `${m[1]}.myshopify.com`;
|
|
23
|
-
counts.set(domain, (counts.has(domain) ? counts.get(domain) : 0) + 1);
|
|
24
|
-
}
|
|
25
|
-
return [...counts].sort((a, b) => b[1] - a[1]).map(([d]) => d);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** Token candidates in the order the places above are trusted. */
|
|
29
|
-
export function findTokenCandidates(html) {
|
|
30
|
-
const found = [];
|
|
31
|
-
const patterns = [
|
|
32
|
-
// Hydrogen, serialized either as JSON or as a flat turbo-stream array (\"key\",\"value\").
|
|
33
|
-
/storefrontAccessToken\\?"\s*[:,]\s*\\?"([a-f0-9]{32})/g,
|
|
34
|
-
/<meta[^>]+name="shopify-checkout-api-token"[^>]+content="([a-f0-9]{32})"/g,
|
|
35
|
-
/<meta[^>]+content="([a-f0-9]{32})"[^>]+name="shopify-checkout-api-token"/g,
|
|
36
|
-
/<script[^>]+id="shopify-features"[^>]*>\s*\{"accessToken":"([a-f0-9]{32})"/g,
|
|
37
|
-
];
|
|
38
|
-
for (const pattern of patterns) for (const m of html.matchAll(pattern)) if (!found.includes(m[1])) found.push(m[1]);
|
|
39
|
-
return found;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** Does the Storefront API at `domain` accept `token`? */
|
|
43
|
-
export async function acceptsToken(domain, token) {
|
|
44
|
-
const res = await fetch(`https://${domain}/api/${STOREFRONT_API_VERSION}/graphql.json`, {
|
|
45
|
-
method: 'POST',
|
|
46
|
-
headers: { 'content-type': 'application/json', 'x-shopify-storefront-access-token': token },
|
|
47
|
-
body: JSON.stringify({ query: '{ shop { id } }' }),
|
|
48
|
-
});
|
|
49
|
-
if (!res.ok) return false;
|
|
50
|
-
const body = await res.json().catch((err) => {
|
|
51
|
-
console.warn(`${domain} answered ${res.status} with no JSON (${err.message}) — treating the token as refused`);
|
|
52
|
-
return null;
|
|
53
|
-
});
|
|
54
|
-
return body !== null && body.data !== undefined && body.data !== null && body.data.shop !== null;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** The page's HTML: fetched for a URL, read from disk for a page a real browser saved (a bot wall lets only browsers through). */
|
|
58
|
-
async function pageHtml(source) {
|
|
59
|
-
if (!/^https?:\/\//.test(source)) {
|
|
60
|
-
return readFile(source, 'utf8').then((html) => ({ html }), (err) => ({ error: `cannot read ${source} (${err.message}) — pass the store's URL, or a page saved from it` }));
|
|
61
|
-
}
|
|
62
|
-
const res = await fetch(source, { headers: { 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130 Safari/537.36' } });
|
|
63
|
-
if (!res.ok) return { error: `${source} answered ${res.status} — a bot wall lets only a real browser through: open the store there, save the page (or copy its HTML into a file) and pass that file to --from` };
|
|
64
|
-
return { html: await res.text() };
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* `{ domain, token }` for the store behind `source` — a storefront URL, or its
|
|
69
|
-
* page saved from a browser — or `{ error }` saying why there is none.
|
|
70
|
-
*/
|
|
71
|
-
export async function resolveStorePage(source) {
|
|
72
|
-
const page = await pageHtml(source);
|
|
73
|
-
if ('error' in page) return page;
|
|
74
|
-
const domains = findShopDomains(page.html);
|
|
75
|
-
if (domains.length === 0) return { error: `${source} names no *.myshopify.com domain — not a Shopify storefront page` };
|
|
76
|
-
const tokens = findTokenCandidates(page.html);
|
|
77
|
-
if (tokens.length === 0) return { error: `${source} ships no public Storefront token in its page` };
|
|
78
|
-
for (const domain of domains) {
|
|
79
|
-
for (const token of tokens) if (await acceptsToken(domain, token)) return { domain, token };
|
|
80
|
-
}
|
|
81
|
-
return { error: `none of the ${tokens.length} token(s) on ${source} is accepted by ${domains.join(', ')}` };
|
|
82
|
-
}
|