@sbuilder/cli 0.6.2 → 0.8.0
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/README.md +210 -20
- package/README.vi.md +205 -19
- package/dist/bin/sb.js +320 -22
- package/dist/bin/sb.js.map +1 -1
- package/dist/src/appblocks.js +67 -0
- package/dist/src/appblocks.js.map +1 -0
- package/dist/src/code.js +1 -1
- package/dist/src/code.js.map +1 -1
- package/dist/src/deploy.js +5 -2
- package/dist/src/deploy.js.map +1 -1
- package/dist/src/dev.js.map +1 -1
- package/dist/src/export.js +167 -0
- package/dist/src/export.js.map +1 -0
- package/dist/src/host.js +162 -0
- package/dist/src/host.js.map +1 -0
- package/dist/src/login.js +62 -8
- package/dist/src/login.js.map +1 -1
- package/dist/src/new.js +133 -0
- package/dist/src/new.js.map +1 -0
- package/dist/src/next.js +122 -0
- package/dist/src/next.js.map +1 -0
- package/dist/src/repo.js +118 -0
- package/dist/src/repo.js.map +1 -0
- package/dist/src/site.js +448 -0
- package/dist/src/site.js.map +1 -0
- package/dist/src/siteinit.js +326 -0
- package/dist/src/siteinit.js.map +1 -0
- package/dist/src/source/parse.js +470 -0
- package/dist/src/source/parse.js.map +1 -0
- package/dist/src/source/print.js +270 -0
- package/dist/src/source/print.js.map +1 -0
- package/package.json +6 -2
package/dist/src/login.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { authedFetch, credentialsPath, readPair, savePair } from './auth.js';
|
|
2
3
|
import { DevError, explain } from './dev.js';
|
|
3
4
|
/**
|
|
4
5
|
* `sb login` / `sb logout` — a browser device-authorization sign-in.
|
|
@@ -14,7 +15,7 @@ import { DevError, explain } from './dev.js';
|
|
|
14
15
|
* in POST bodies and is never printed, and neither are the tokens it buys.
|
|
15
16
|
*/
|
|
16
17
|
/** Sent as `clientVersion`; a test holds it equal to package.json. */
|
|
17
|
-
export const VERSION = '0.
|
|
18
|
+
export const VERSION = '0.8.0';
|
|
18
19
|
/**
|
|
19
20
|
* The platform's API, when SB_API is not set. PRODUCTION, because this CLI is
|
|
20
21
|
* published: `npx @sbuilder/cli agent` on an author's machine must reach the
|
|
@@ -49,8 +50,44 @@ async function postJSON(url, body) {
|
|
|
49
50
|
}
|
|
50
51
|
return { status: res.status, data, retryAfter: Number(res.headers.get('Retry-After')) || 0 };
|
|
51
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* The system browser, when a person is plausibly at this terminal: a TTY, not
|
|
55
|
+
* CI, not SB_NO_BROWSER. It opens the PAGE only — the code is still typed there
|
|
56
|
+
* (see the header), so an auto-opened tab cannot approve anything by itself.
|
|
57
|
+
*/
|
|
58
|
+
function defaultOpener() {
|
|
59
|
+
if (!process.stdout.isTTY || process.env.CI || process.env.SB_NO_BROWSER)
|
|
60
|
+
return null;
|
|
61
|
+
const [cmd, args] = process.platform === 'darwin'
|
|
62
|
+
? ['open', []]
|
|
63
|
+
: process.platform === 'win32'
|
|
64
|
+
? ['cmd', ['/c', 'start', '""']]
|
|
65
|
+
: ['xdg-open', []];
|
|
66
|
+
return (url) => {
|
|
67
|
+
try {
|
|
68
|
+
spawn(cmd, [...args, url], { stdio: 'ignore', detached: true })
|
|
69
|
+
.on('error', () => { })
|
|
70
|
+
.unref();
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
// No browser is not a failure: the address is printed above.
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/** The signed-in account's email, or undefined when the server will not say. */
|
|
78
|
+
export async function whoIs(api, cred) {
|
|
79
|
+
try {
|
|
80
|
+
const res = await authedFetch(`${api}/api/auth/me`, { method: 'GET' }, cred);
|
|
81
|
+
if (!res.ok)
|
|
82
|
+
return undefined;
|
|
83
|
+
return (await res.json()).user?.email || undefined;
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
52
89
|
/** Runs the device flow and stores the pair. Throws DevError on any refusal. */
|
|
53
|
-
export async function login({ api, log = console.log, sleep = (ms) => new Promise((r) => setTimeout(r, ms)), path = credentialsPath(), }) {
|
|
90
|
+
export async function login({ api, log = console.log, sleep = (ms) => new Promise((r) => setTimeout(r, ms)), path = credentialsPath(), open = defaultOpener(), }) {
|
|
54
91
|
const start = await postJSON(`${api}/api/auth/device/start`, {
|
|
55
92
|
client: 'sb-cli',
|
|
56
93
|
clientVersion: VERSION,
|
|
@@ -58,12 +95,25 @@ export async function login({ api, log = console.log, sleep = (ms) => new Promis
|
|
|
58
95
|
if (start.status !== 200)
|
|
59
96
|
throw new DevError(explain(start.status, start.data));
|
|
60
97
|
const s = start.data;
|
|
61
|
-
|
|
98
|
+
const bar = '─'.repeat(s.userCode.length + 8);
|
|
99
|
+
log('Sign in to Store Builder');
|
|
100
|
+
log('');
|
|
101
|
+
log(` 1. Open ${s.verificationUri}`);
|
|
102
|
+
log(' 2. Type this code there:');
|
|
62
103
|
log('');
|
|
63
|
-
log(`
|
|
104
|
+
log(` ┌${bar}┐`);
|
|
105
|
+
log(` │ ${s.userCode} │`);
|
|
106
|
+
log(` └${bar}┘`);
|
|
64
107
|
log('');
|
|
65
|
-
log(`Check
|
|
66
|
-
log('
|
|
108
|
+
log(` 3. Check the page shows the same code, then Approve. It expires in ${Math.round(s.expiresIn / 60)} minutes.`);
|
|
109
|
+
log('');
|
|
110
|
+
if (open) {
|
|
111
|
+
open(s.verificationUri);
|
|
112
|
+
log('Opened the page in your browser. Waiting for approval… (Ctrl+C to stop)');
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
log('Waiting for approval… (Ctrl+C to stop)');
|
|
116
|
+
}
|
|
67
117
|
let interval = s.interval * 1000;
|
|
68
118
|
const deadline = Date.now() + s.expiresIn * 1000;
|
|
69
119
|
for (;;) {
|
|
@@ -82,7 +132,11 @@ export async function login({ api, log = console.log, sleep = (ms) => new Promis
|
|
|
82
132
|
if (r.status === 200) {
|
|
83
133
|
const { tokens } = r.data;
|
|
84
134
|
await savePair(api, { accessToken: tokens.accessToken, refreshToken: tokens.refreshToken }, path);
|
|
85
|
-
|
|
135
|
+
const email = await whoIs(api, tokens.accessToken);
|
|
136
|
+
log('');
|
|
137
|
+
log(email ? `✓ Signed in as ${email}.` : '✓ Signed in.');
|
|
138
|
+
log(` Credentials: ${path} (readable only by you). sb logout forgets them.`);
|
|
139
|
+
log(' Next: sb site init my-shop · sb new "<what to build>" · sb whoami');
|
|
86
140
|
return;
|
|
87
141
|
}
|
|
88
142
|
const code = r.data.code;
|
package/dist/src/login.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"login.js","sourceRoot":"","sources":["../../src/login.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"login.js","sourceRoot":"","sources":["../../src/login.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAE3C,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAG7E,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAG7C;;;;;;;;;;;;GAYG;AAEH,sEAAsE;AACtE,MAAM,CAAC,MAAM,OAAO,GAAG,OAAO,CAAC;AAE/B;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,4BAA4B,CAAC;AAExD,gEAAgE;AAChE,MAAM,UAAU,OAAO;IACrB,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,IAAI,WAAW,CAAC;AAC3C,CAAC;AAUD,KAAK,UAAU,QAAQ,CACrB,GAAW,EACX,IAAa;IAEb,IAAI,GAAa,CAAC;IAClB,IAAI,CAAC;QACH,GAAG,GAAG,MAAM,KAAK,CA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,CAAC,YAAY,EAAE,CAAC,CAAC;QACxF,IAAI,CAAC,CAAC,MAAM,IAAI,GAAG;YACjB,GAAG,CAAC,4BAA4B,CAAC,CAAC,MAAM,sCAAsC,CAAC,CAAC;IACpF,CAAC;IAAC,MAAM,CAAC;QACP,GAAG,CAAC,gEAAgE,CAAC,CAAC;IACxE,CAAC;IACD,MAAM,QAAQ,CAAC,GAAG,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC;IACrC,GAAG,CAAC,iBAAiB,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC;AAC/C,CAAC"}
|
package/dist/src/new.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
2
|
+
import { agent } from './agent.js';
|
|
3
|
+
import { authedFetch } from './auth.js';
|
|
4
|
+
import { DevError } from './dev.js';
|
|
5
|
+
/**
|
|
6
|
+
* `sb new "a shop selling candles"` — describe a site in the terminal, and watch
|
|
7
|
+
* it get built in the editor.
|
|
8
|
+
*
|
|
9
|
+
* The server seals the sentence into a link only this account can open, and the
|
|
10
|
+
* dashboard behind that link creates the site and starts the assistant. So this
|
|
11
|
+
* file creates nothing: it makes sure the assistant has a machine to run on, then
|
|
12
|
+
* opens the link.
|
|
13
|
+
*
|
|
14
|
+
* With no `sb agent` running it serves as one itself, and has to be connected
|
|
15
|
+
* BEFORE the browser opens, because the dashboard asks once whether an agent is
|
|
16
|
+
* there and a site created in that gap would go on the platform's key.
|
|
17
|
+
*/
|
|
18
|
+
/** How long a just-started agent gets to connect. */
|
|
19
|
+
export const CONNECT_MS = 10_000;
|
|
20
|
+
const POLL_MS = 500;
|
|
21
|
+
export const KEEP_RUNNING = 'Keep this running: Claude Code on this machine is building your site. Ctrl-C stops it.';
|
|
22
|
+
export const ON_THIS_MACHINE = 'Your running sb agent will build it.';
|
|
23
|
+
export const ON_THE_PLATFORM = "The platform's assistant will build it, using your plan's AI allowance.";
|
|
24
|
+
export function openBrowser(url) {
|
|
25
|
+
const bin = process.platform === 'darwin' ? 'open' : process.platform === 'linux' ? 'xdg-open' : '';
|
|
26
|
+
if (!bin)
|
|
27
|
+
return;
|
|
28
|
+
// A machine with no browser is fine: the caller has already printed the link.
|
|
29
|
+
const child = spawn(bin, [url], { stdio: 'ignore', detached: true });
|
|
30
|
+
child.on('error', () => { });
|
|
31
|
+
child.unref();
|
|
32
|
+
}
|
|
33
|
+
function claudeInstalled() {
|
|
34
|
+
// sb agent refuses every job on Windows, even with a working claude.exe.
|
|
35
|
+
if (process.platform === 'win32')
|
|
36
|
+
return false;
|
|
37
|
+
return spawnSync('claude', ['--version'], { stdio: 'ignore' }).status === 0;
|
|
38
|
+
}
|
|
39
|
+
async function bridgeStatus(o) {
|
|
40
|
+
const res = await authedFetch(`${o.api}/api/agent-bridge/status`, {}, o.token);
|
|
41
|
+
// A server without this route is older than this command; the start request
|
|
42
|
+
// below is the one that says so.
|
|
43
|
+
if (!res.ok)
|
|
44
|
+
return {};
|
|
45
|
+
return (await res.json()).bridge ?? {};
|
|
46
|
+
}
|
|
47
|
+
export async function newSite(o) {
|
|
48
|
+
const prompt = o.prompt.trim();
|
|
49
|
+
if (!prompt) {
|
|
50
|
+
throw new DevError('Say what to build: sb new "a shop selling handmade candles"');
|
|
51
|
+
}
|
|
52
|
+
const open = o.open ?? openBrowser;
|
|
53
|
+
const hasClaude = o.hasClaude ?? claudeInstalled;
|
|
54
|
+
// The link first: a refused sentence must not leave an agent running for nothing.
|
|
55
|
+
const res = await authedFetch(`${o.api}/api/agent-bridge/start`, {
|
|
56
|
+
method: 'POST',
|
|
57
|
+
headers: { 'Content-Type': 'application/json' },
|
|
58
|
+
body: JSON.stringify({ prompt }),
|
|
59
|
+
}, o.token);
|
|
60
|
+
if (!res.ok) {
|
|
61
|
+
const why = (await res.json().catch(() => ({})));
|
|
62
|
+
throw new DevError(res.status === 404
|
|
63
|
+
? `${o.api} does not support sb new yet.`
|
|
64
|
+
: (why.error ?? `could not start (HTTP ${res.status})`));
|
|
65
|
+
}
|
|
66
|
+
const { start } = (await res.json());
|
|
67
|
+
const url = start?.url ?? '';
|
|
68
|
+
// Handed to the OS's URL opener, so only ever a web address.
|
|
69
|
+
if (!/^https?:\/\//.test(url))
|
|
70
|
+
throw new DevError('the server did not return a link to open.');
|
|
71
|
+
const status = await bridgeStatus(o);
|
|
72
|
+
let serving;
|
|
73
|
+
if (status.online) {
|
|
74
|
+
o.log(ON_THIS_MACHINE);
|
|
75
|
+
}
|
|
76
|
+
else if (status.enabled && hasClaude()) {
|
|
77
|
+
serving = await serveUntilConnected(o);
|
|
78
|
+
}
|
|
79
|
+
if (!status.online && !serving)
|
|
80
|
+
o.log(ON_THE_PLATFORM);
|
|
81
|
+
// Ctrl-C during the connect wait: nothing has been created yet, so keep it so.
|
|
82
|
+
if (o.signal?.aborted)
|
|
83
|
+
return;
|
|
84
|
+
o.log(`Opening ${url}`);
|
|
85
|
+
open(url);
|
|
86
|
+
if (serving) {
|
|
87
|
+
o.log(KEEP_RUNNING);
|
|
88
|
+
await serving.done;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Starts an agent in this process. Undefined if it never connected in time.
|
|
93
|
+
* Wrapped because an async function returning a promise would wait for it, and
|
|
94
|
+
* this one runs until Ctrl-C.
|
|
95
|
+
*/
|
|
96
|
+
async function serveUntilConnected(o) {
|
|
97
|
+
const stop = new AbortController();
|
|
98
|
+
o.signal?.addEventListener('abort', () => stop.abort());
|
|
99
|
+
// ponytail: a missed probe (the agent mid-reconnect, a replica on an older
|
|
100
|
+
// build) starts a second agent, and two agents both run every job. Job claiming
|
|
101
|
+
// in bridge.go is the fix if that shows up.
|
|
102
|
+
let failed;
|
|
103
|
+
const serving = agent({
|
|
104
|
+
api: o.api,
|
|
105
|
+
token: o.token,
|
|
106
|
+
log: o.log,
|
|
107
|
+
signal: stop.signal,
|
|
108
|
+
allowCodex: false,
|
|
109
|
+
}).catch((err) => {
|
|
110
|
+
failed = err;
|
|
111
|
+
});
|
|
112
|
+
const deadline = Date.now() + (o.connectMs ?? CONNECT_MS);
|
|
113
|
+
while (Date.now() < deadline && !failed && !o.signal?.aborted) {
|
|
114
|
+
if ((await bridgeStatus(o)).online) {
|
|
115
|
+
return {
|
|
116
|
+
// The agent can still be refused later (a signed-out reconnect). The site
|
|
117
|
+
// is pinned to it by then, so leaving quietly would look like a finished build.
|
|
118
|
+
done: serving.then(() => {
|
|
119
|
+
if (failed && !o.signal?.aborted) {
|
|
120
|
+
throw new DevError(`sb agent stopped: ${failed.message}`);
|
|
121
|
+
}
|
|
122
|
+
}),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
await new Promise((r) => setTimeout(r, POLL_MS));
|
|
126
|
+
}
|
|
127
|
+
stop.abort();
|
|
128
|
+
await serving;
|
|
129
|
+
if (failed)
|
|
130
|
+
o.log(`sb agent could not start: ${failed.message}`);
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
//# sourceMappingURL=new.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"new.js","sourceRoot":"","sources":["../../src/new.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAEtD,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAExC,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEpC;;;;;;;;;;;;GAYG;AAEH,qDAAqD;AACrD,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAAC;AACjC,MAAM,OAAO,GAAG,GAAG,CAAC;AAEpB,MAAM,CAAC,MAAM,YAAY,GACvB,wFAAwF,CAAC;AAC3F,MAAM,CAAC,MAAM,eAAe,GAAG,sCAAsC,CAAC;AACtE,MAAM,CAAC,MAAM,eAAe,GAC1B,yEAAy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G,GAAG,CAAC;IACf,CAAC,CAAC,CAAC;IACH,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,SAAS,IAAI,UAAU,CAAC,CAAC;IAC1D,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC,CAAC,MAAM,EAAE,OAAO,EAAE,CAAC;QAC9D,IAAI,CAAC,MAAM,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;YACnC,OAAO;gBACL,0EAA0E;gBAC1E,gFAAgF;gBAChF,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE;oBACtB,IAAI,MAAM,IAAI,CAAC,CAAC,CAAC,MAAM,EAAE,OAAO,EAAE,CAAC;wBACjC,MAAM,IAAI,QAAQ,CAAC,qBAAsB,MAAgB,CAAC,OAAO,EAAE,CAAC,CAAC;oBACvE,CAAC;gBACH,CAAC,CAAC;aACH,CAAC;QACJ,CAAC;QACD,MAAM,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC;IACnD,CAAC;IACD,IAAI,CAAC,KAAK,EAAE,CAAC;IACb,MAAM,OAAO,CAAC;IACd,IAAI,MAAM;QAAE,CAAC,CAAC,GAAG,CAAC,6BAA8B,MAAgB,CAAC,OAAO,EAAE,CAAC,CAAC;IAC5E,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
package/dist/src/next.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { mkdir, readdir, readFile, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { SiteRefused } from './site.js';
|
|
4
|
+
/**
|
|
5
|
+
* `sb site next` — make a site repo a Next.js app that serves its
|
|
6
|
+
* `design/pages/*.page.tsx` pages, rendered by the platform's own renderer
|
|
7
|
+
* (`@sbuilder/render`, the Go code as WebAssembly), next to whatever routes the
|
|
8
|
+
* developer writes by hand.
|
|
9
|
+
*
|
|
10
|
+
* What it writes, and nothing else — an existing file is never overwritten:
|
|
11
|
+
* - `app/[[...slug]]/route.js`: a page file's path → its rendered HTML; any
|
|
12
|
+
* other path → the live storefront (product pages, search, …).
|
|
13
|
+
* - `next.config.mjs`: `/_wb/*` (cart, checkout, forms, the islands runtime)
|
|
14
|
+
* forwarded to the storefront, and the renderer kept out of the bundler.
|
|
15
|
+
* - `package.json` scripts + dependencies.
|
|
16
|
+
*
|
|
17
|
+
* A route the developer adds under `app/` wins over the catch-all, which is how
|
|
18
|
+
* hand-written React pages live beside the builder's.
|
|
19
|
+
*/
|
|
20
|
+
export const NEXT_RANGE = '^16.0.0';
|
|
21
|
+
export const REACT_RANGE = '^19.0.0';
|
|
22
|
+
export const ROUTE_JS = `// Serves design/pages/*.page.tsx through @sbuilder/cli/host — rendered by the
|
|
23
|
+
// platform's own renderer, so a page looks here exactly as it does on the store.
|
|
24
|
+
// A route you add under app/ wins over this one. Written by \`sb site next\`.
|
|
25
|
+
import { renderRequest, errorPage, forwardToStore } from '@sbuilder/cli/host';
|
|
26
|
+
|
|
27
|
+
export const runtime = 'nodejs';
|
|
28
|
+
export const dynamic = 'force-dynamic';
|
|
29
|
+
|
|
30
|
+
export async function GET(request) {
|
|
31
|
+
try {
|
|
32
|
+
// No page file for this path: the live storefront answers (product pages, search, …).
|
|
33
|
+
return (await renderRequest(request)) ?? (await forwardToStore(request, process.env.SB_STOREFRONT));
|
|
34
|
+
} catch (err) {
|
|
35
|
+
return errorPage(err);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
`;
|
|
39
|
+
/** /_wb/* arrives here (next.config.mjs rewrites it), so only the store's cookies travel. */
|
|
40
|
+
export const PROXY_JS = `// /_wb/* — cart, checkout, forms, the islands runtime — belongs to the live
|
|
41
|
+
// storefront. next.config.mjs routes it here so that only the store's own cookies
|
|
42
|
+
// (wb_*) are forwarded, never this app's. Written by \`sb site next\`.
|
|
43
|
+
import { forwardToStore } from '@sbuilder/cli/host';
|
|
44
|
+
|
|
45
|
+
export const runtime = 'nodejs';
|
|
46
|
+
export const dynamic = 'force-dynamic';
|
|
47
|
+
|
|
48
|
+
const forward = (request) => forwardToStore(request, process.env.SB_STOREFRONT);
|
|
49
|
+
export { forward as GET, forward as POST, forward as PUT, forward as PATCH, forward as DELETE };
|
|
50
|
+
`;
|
|
51
|
+
export const NEXT_CONFIG = `// Written by \`sb site next\`. Pages come from design/pages/*.page.tsx (see
|
|
52
|
+
// app/[[...slug]]/route.js). Everything under /_wb/ — cart, checkout, forms,
|
|
53
|
+
// the islands runtime — belongs to the live storefront, so it is forwarded there.
|
|
54
|
+
/** @type {import('next').NextConfig} */
|
|
55
|
+
export default {
|
|
56
|
+
// Loaded from node_modules at run time: the renderer is a 17 MB .wasm it reads
|
|
57
|
+
// from its own folder, which a bundler would break.
|
|
58
|
+
serverExternalPackages: ['@sbuilder/render', '@sbuilder/cli'],
|
|
59
|
+
async rewrites() {
|
|
60
|
+
// Through a route handler, not straight to the store: it forwards only the
|
|
61
|
+
// store's wb_* cookies (app/sb-proxy/[...path]/route.js).
|
|
62
|
+
return { beforeFiles: [{ source: '/_wb/:path*', destination: '/sb-proxy/_wb/:path*' }] };
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
`;
|
|
66
|
+
export function packageJson(name, cliRange, renderRange) {
|
|
67
|
+
return (JSON.stringify({
|
|
68
|
+
name,
|
|
69
|
+
private: true,
|
|
70
|
+
type: 'module',
|
|
71
|
+
scripts: {
|
|
72
|
+
dev: 'next dev',
|
|
73
|
+
build: 'next build',
|
|
74
|
+
start: 'next start',
|
|
75
|
+
pull: 'sb site pull',
|
|
76
|
+
push: 'sb site push',
|
|
77
|
+
},
|
|
78
|
+
dependencies: {
|
|
79
|
+
'@sbuilder/cli': cliRange,
|
|
80
|
+
'@sbuilder/render': renderRange,
|
|
81
|
+
next: NEXT_RANGE,
|
|
82
|
+
react: REACT_RANGE,
|
|
83
|
+
'react-dom': REACT_RANGE,
|
|
84
|
+
},
|
|
85
|
+
}, null, 2) + '\n');
|
|
86
|
+
}
|
|
87
|
+
export async function initNext(o) {
|
|
88
|
+
const log = o.log ?? console.log;
|
|
89
|
+
const have = await readdir(o.dir).catch(() => []);
|
|
90
|
+
if (!have.includes('sb.json'))
|
|
91
|
+
throw new SiteRefused(`${o.dir} is not a site repo (no sb.json) — run sb site init first.`);
|
|
92
|
+
const files = {
|
|
93
|
+
'app/[[...slug]]/route.js': ROUTE_JS,
|
|
94
|
+
'app/sb-proxy/[...path]/route.js': PROXY_JS,
|
|
95
|
+
'next.config.mjs': NEXT_CONFIG,
|
|
96
|
+
'package.json': packageJson((o.dir.split(/[\\/]/).pop() || 'site').toLowerCase().replace(/[^a-z0-9-]+/g, '-'), o.cliRange, o.renderRange),
|
|
97
|
+
};
|
|
98
|
+
const exists = async (f) => readFile(join(o.dir, f)).then(() => true, () => false);
|
|
99
|
+
const clash = [];
|
|
100
|
+
for (const f of Object.keys(files))
|
|
101
|
+
if (await exists(f))
|
|
102
|
+
clash.push(f);
|
|
103
|
+
if (clash.length)
|
|
104
|
+
throw new SiteRefused(`${clash.join(', ')} already exist — refusing to overwrite. Move them aside, or add the pieces by hand (README: "Run it as a Next.js app").`);
|
|
105
|
+
for (const [f, text] of Object.entries(files)) {
|
|
106
|
+
await mkdir(dirname(join(o.dir, f)), { recursive: true });
|
|
107
|
+
await writeFile(join(o.dir, f), text, 'utf8');
|
|
108
|
+
}
|
|
109
|
+
// Next and its build output never belong in git.
|
|
110
|
+
const gi = (await readFile(join(o.dir, '.gitignore'), 'utf8').catch(() => '')).split('\n');
|
|
111
|
+
const add = ['node_modules', '.next'].filter((l) => !gi.includes(l));
|
|
112
|
+
if (add.length)
|
|
113
|
+
await writeFile(join(o.dir, '.gitignore'), [...gi.filter(Boolean), ...add].join('\n') + '\n');
|
|
114
|
+
const env = await readFile(join(o.dir, '.env'), 'utf8').catch(() => '');
|
|
115
|
+
log('A Next.js app now serves this repo’s pages:');
|
|
116
|
+
log(' npm install');
|
|
117
|
+
log(' npm run dev # http://localhost:3000 — save a .page.tsx, refresh, see it');
|
|
118
|
+
if (!/^SB_STOREFRONT=/m.test(env))
|
|
119
|
+
log(' (set SB_STOREFRONT=https://your-store… in .env so cart, checkout and other pages work)');
|
|
120
|
+
return Object.keys(files);
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=next.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"next.js","sourceRoot":"","sources":["../../src/next.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACvE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAE1C,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAExC;;;;;;;;;;;;;;;GAeG;AAEH,MAAM,CAAC,MAAM,UAAU,GAAG,SAAS,CAAC;AACpC,MAAM,CAAC,MAAM,WAAW,GAAG,SAAS,CAAC;AAErC,MAAM,CAAC,MAAM,QAAQ,GAAG;;;;;;;;;;;;;;;;CAgBvB,CAAC;AAEF,6FAA6F;AAC7F,MAAM,CAAC,MAAM,QAAQ,GAAG;;;;;;;;;;CAUvB,CAAC;AAEF,MAAM,CAAC,MAAM,WAAW,GAAG;;;;;;;;;;;;;;CAc1B,CAAC;AAEF,MAAM,UAAU,WAAW,CAAC,IAAY,EAAE,QAAgB,EAAE,WAAmB;IAC7E,OAAO,CACL,IAAI,CAAC,SAAS,CACZ;QACE,IAAI;QACJ,OAAO,EAAE,IAAI;QACb,IAAI,EAAE,QAAQ;QACd,OAAO,EAAE;YACP,GAAG,EAAE,UAAU;YACf,KAAK,EAAE,YAAY;YACnB,KAAK,EAAE,YAAY;YACnB,IAAI,EAAE,cAAc;YACpB,IAAI,EA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AEhG,MAAM,GAAG,GAAG,MAAM,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC;IACxE,GAAG,CAAC,6CAA6C,CAAC,CAAC;IACnD,GAAG,CAAC,eAAe,CAAC,CAAC;IACrB,GAAG,CAAC,sFAAsF,CAAC,CAAC;IAC5F,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,GAAG,CAAC;QAC/B,GAAG,CAAC,0FAA0F,CAAC,CAAC;IAClG,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC5B,CAAC"}
|
package/dist/src/repo.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { dirname, join, resolve } from 'node:path';
|
|
4
|
+
/**
|
|
5
|
+
* A SITE REPO's own settings: `sb.json` marks its root, and `.env` beside it
|
|
6
|
+
* (never committed) holds the API key `sb site init` minted for it.
|
|
7
|
+
*
|
|
8
|
+
* Read so a repo works the moment it is cloned or opened by an agent: the site
|
|
9
|
+
* commands and `sb mcp` find the key without anyone exporting it. A variable
|
|
10
|
+
* already in the environment always wins — the repo fills only what is unset.
|
|
11
|
+
*/
|
|
12
|
+
/** The nearest folder at or above `from` that holds sb.json. */
|
|
13
|
+
export function repoRoot(from = process.cwd()) {
|
|
14
|
+
for (let dir = resolve(from);; dir = dirname(dir)) {
|
|
15
|
+
if (existsSync(join(dir, 'sb.json')))
|
|
16
|
+
return dir;
|
|
17
|
+
if (dirname(dir) === dir)
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** KEY=VALUE lines; `#` comments, blank lines and optional quotes. Nothing else. */
|
|
22
|
+
export function parseEnv(text) {
|
|
23
|
+
const out = {};
|
|
24
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
25
|
+
const line = raw.trim();
|
|
26
|
+
if (!line || line.startsWith('#'))
|
|
27
|
+
continue;
|
|
28
|
+
const m = line.match(/^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/);
|
|
29
|
+
if (!m)
|
|
30
|
+
continue;
|
|
31
|
+
out[m[1]] = m[2].replace(/^(['"])(.*)\1$/, '$2');
|
|
32
|
+
}
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
/** The repo's .env, or {} outside a repo / without one. */
|
|
36
|
+
export function repoEnv(from) {
|
|
37
|
+
const root = repoRoot(from);
|
|
38
|
+
if (!root)
|
|
39
|
+
return {};
|
|
40
|
+
try {
|
|
41
|
+
return parseEnv(readFileSync(join(root, '.env'), 'utf8'));
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return {};
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** The only names a repo's .env may supply. Anything else (NODE_OPTIONS, PATH, …) is ignored. */
|
|
48
|
+
export const REPO_KEYS = [
|
|
49
|
+
'SB_TOKEN',
|
|
50
|
+
'SB_API',
|
|
51
|
+
'SB_SITE',
|
|
52
|
+
'SB_SITE_NAME',
|
|
53
|
+
'SB_STOREFRONT',
|
|
54
|
+
];
|
|
55
|
+
/**
|
|
56
|
+
* What a site command acts with: the shell's values over the repo's .env —
|
|
57
|
+
* restricted to REPO_KEYS, because a cloned repo is somebody else's file.
|
|
58
|
+
*
|
|
59
|
+
* SB_TOKEN and SB_API travel as a PAIR. The repo's SB_API is honoured only with
|
|
60
|
+
* the repo's own SB_TOKEN; when the token comes from the shell, so does the
|
|
61
|
+
* address. Otherwise a cloned .env naming a foreign SB_API would collect the
|
|
62
|
+
* key you exported for your own site.
|
|
63
|
+
*/
|
|
64
|
+
export function siteEnv(from) {
|
|
65
|
+
const repo = repoEnv(from);
|
|
66
|
+
const shell = (k) => process.env[k] || undefined;
|
|
67
|
+
const out = {};
|
|
68
|
+
if (shell('SB_TOKEN')) {
|
|
69
|
+
out.SB_TOKEN = shell('SB_TOKEN');
|
|
70
|
+
out.SB_API = shell('SB_API');
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
out.SB_TOKEN = repo.SB_TOKEN;
|
|
74
|
+
out.SB_API = repo.SB_TOKEN ? (shell('SB_API') ?? repo.SB_API) : shell('SB_API');
|
|
75
|
+
}
|
|
76
|
+
out.SB_SITE = shell('SB_SITE') ?? repo.SB_SITE;
|
|
77
|
+
out.SB_SITE_NAME = shell('SB_SITE_NAME') ?? repo.SB_SITE_NAME;
|
|
78
|
+
out.SB_STOREFRONT = shell('SB_STOREFRONT') ?? repo.SB_STOREFRONT;
|
|
79
|
+
for (const k of REPO_KEYS)
|
|
80
|
+
if (!out[k])
|
|
81
|
+
delete out[k];
|
|
82
|
+
return out;
|
|
83
|
+
}
|
|
84
|
+
export const MCP_PACKAGE = 'sbuilder-mcp';
|
|
85
|
+
export const MCP_NO_KEY = 'sb mcp: no API key for this site. Run `sb site init` (it writes one into .env), or set SB_TOKEN ' +
|
|
86
|
+
'to an API key (wbk_…) from Settings -> API keys.';
|
|
87
|
+
/**
|
|
88
|
+
* `sb mcp` — the Store Builder MCP server, started with THIS repo's key.
|
|
89
|
+
*
|
|
90
|
+
* The repo's .mcp.json names this command rather than sbuilder-mcp directly
|
|
91
|
+
* because an agent client starts servers with its own environment, which never
|
|
92
|
+
* has the key: the key lives in .env, and only something that reads .env can
|
|
93
|
+
* hand it over. stdout belongs to the MCP protocol, so every word here goes to
|
|
94
|
+
* stderr.
|
|
95
|
+
*/
|
|
96
|
+
export function mcp(from) {
|
|
97
|
+
const env = siteEnv(from);
|
|
98
|
+
if (!env.SB_TOKEN) {
|
|
99
|
+
console.error(MCP_NO_KEY);
|
|
100
|
+
return Promise.resolve(1);
|
|
101
|
+
}
|
|
102
|
+
return new Promise((done) => {
|
|
103
|
+
const child = spawn('npx', ['-y', MCP_PACKAGE], {
|
|
104
|
+
stdio: 'inherit',
|
|
105
|
+
// Only the four allow-listed names reach the child — never a repo's NODE_OPTIONS or PATH.
|
|
106
|
+
env: { ...process.env, ...env },
|
|
107
|
+
shell: process.platform === 'win32',
|
|
108
|
+
});
|
|
109
|
+
child.on('error', (err) => {
|
|
110
|
+
console.error(`sb mcp: could not start ${MCP_PACKAGE}: ${err.message}`);
|
|
111
|
+
done(1);
|
|
112
|
+
});
|
|
113
|
+
child.on('exit', (code, signal) => done(code ?? (signal ? 1 : 0)));
|
|
114
|
+
for (const sig of ['SIGINT', 'SIGTERM'])
|
|
115
|
+
process.on(sig, () => child.kill(sig));
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=repo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repo.js","sourceRoot":"","sources":["../../src/repo.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEnD;;;;;;;GAOG;AAEH,gEAAgE;AAChE,MAAM,UAAU,QAAQ,CAAC,OAAe,OAAO,CAAC,GAAG,EAAE;IACnD,KAAK,IAAI,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC,GAAI,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;QACnD,IAAI,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;YAAE,OAAO,GAAG,CAAC;QACjD,IAAI,OAAO,CAAC,GAAG,CAAC,KAAK,GAAG;YAAE,OAAO,SAAS,CAAC;IAC7C,CAAC;AACH,CAAC;AAED,oFAAoF;AACpF,MAAM,UAAU,QAAQ,CAAC,IAAY;IACnC,MAAM,GAAG,GAA2B,EAAE,CAAC;IACvC,KAAK,MAAM,GAAG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