@sbuilder/cli 0.3.0 → 0.4.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 CHANGED
@@ -15,6 +15,11 @@ sb dev upload an island's code to your DRAFT version
15
15
  sb login (or SB_TOKEN).
16
16
  sb deploy [--submit] push the version's payload in dependency order.
17
17
  --submit then sends it for review.
18
+ sb code pull [--site <id>] [--dir <path>] [--force]
19
+ mirror a site's Code Dock files into ./code
20
+ sb code push [--site <id>] [--dir <path>] [--force] [--dry-run] [--delete]
21
+ send local edits back; refuses a file changed
22
+ in the Code Dock since your pull unless --force
18
23
  ```
19
24
 
20
25
  Node 20 or newer. Written in TypeScript, published compiled — an installed copy
@@ -32,6 +37,7 @@ has no dependencies and nothing to build.
32
37
  - [`sb login` / `sb logout`](#sb-login--sb-logout--sign-in-through-the-browser)
33
38
  - [`sb dev`](#sb-dev--save-upload-refresh)
34
39
  - [`sb deploy`](#sb-deploy--the-whole-payload-in-dependency-order)
40
+ - [`sb code`](#sb-code--your-code-dock-files-as-a-folder)
35
41
  - [Environment variables](#environment-variables)
36
42
  - [CI](#ci)
37
43
  - [Refusals, and what they mean](#refusals-and-what-they-mean)
@@ -197,7 +203,7 @@ has not happened yet at `init` time. Write it by hand once you have them:
197
203
  {
198
204
  "orgId": "org_yourorg",
199
205
  "appId": "app_1a2b3c4d",
200
- "versionId": "ver_5e6f7a8b",
206
+ "versionId": "apv_5e6f7a8b",
201
207
  "island": "countdown",
202
208
  "entry": "islands/countdown.js"
203
209
  }
@@ -229,13 +235,16 @@ them in even if you are only deploying blocks.
229
235
  An `sb.json` with only the five required keys is valid: `sb dev` works, and
230
236
  `sb deploy` finds no steps to run.
231
237
 
238
+ `sb code` reads one more optional key from the same file, `siteId`, and nothing
239
+ else: it does not need the five required keys.
240
+
232
241
  A fuller one:
233
242
 
234
243
  ```json
235
244
  {
236
245
  "orgId": "org_yourorg",
237
246
  "appId": "app_1a2b3c4d",
238
- "versionId": "ver_5e6f7a8b",
247
+ "versionId": "apv_5e6f7a8b",
239
248
  "island": "countdown",
240
249
  "entry": "islands/countdown.js",
241
250
  "version": {
@@ -282,8 +291,8 @@ local and a production sign-in coexist), in
282
291
  only (mode `0600`). `sb login` is a separate signed-in device on your account,
283
292
  listed with your other sessions; signing out everywhere ends it too.
284
293
 
285
- **It renews itself.** When the server refuses the access token, `sb dev` and
286
- `sb deploy` rotate the pair once, save it, and retry the request. Two `sb`
294
+ **It renews itself.** When the server refuses the access token, `sb dev`,
295
+ `sb deploy` and `sb code` rotate the pair once, save it, and retry the request. Two `sb`
287
296
  processes renewing at the same moment take turns through a lock file beside
288
297
  the credentials, and the second one adopts the pair the first one saved — a
289
298
  refresh token presented twice would end the session. If the renewal itself is
@@ -308,7 +317,7 @@ every time you save. It uploads once immediately on start rather than waiting fo
308
317
  your first save, then runs until you interrupt it.
309
318
 
310
319
  ```
311
- watching islands/countdown.js — save it and this uploads to version ver_5e6f7a8b.
320
+ watching islands/countdown.js — save it and this uploads to version apv_5e6f7a8b.
312
321
  This is not hot reload: after each upload, refresh the page.
313
322
  uploaded countdown — refresh the page to see it
314
323
  ```
@@ -398,6 +407,142 @@ earlier, with more room to explain than a status code has.
398
407
 
399
408
  ---
400
409
 
410
+ ## `sb code` — your Code Dock files as a folder
411
+
412
+ ```bash
413
+ sb code pull --site <site-id>
414
+ # edit code/site/css/main.css in your own editor, then
415
+ sb code push
416
+ ```
417
+
418
+ Mirrors a site's **Code Dock** files — the site-wide and per-page CSS, JavaScript
419
+ and HTML — into a folder, so you can edit them in your own editor, keep them in
420
+ git and push them back.
421
+
422
+ ### Layout, and the manifest
423
+
424
+ ```
425
+ code/
426
+ .sb-code.json the manifest — commit it
427
+ site/css/main.css the site-wide file named "css/main.css"
428
+ pages/about/track.js the file "track.js" on the page whose slug is "about"
429
+ ```
430
+
431
+ A `/` in a file's name becomes a folder. A page's folder is its slug **at the
432
+ first pull** (the homepage is `home`; an empty or clashing slug becomes
433
+ `page-<id>`) and is then recorded in the manifest, so renaming the slug later does
434
+ not move your files. Renaming a file in the Code Dock does not move it either: it
435
+ keeps its local path, because it is tracked by its id, and its content still syncs.
436
+ A Code Dock file with no name pulls as `unnamed-<id>.<ext>` and is
437
+ tracked by its id.
438
+
439
+ Symbolic links never lead `sb code` outside the root: a file that is a link, or whose
440
+ folder leads outside the root through one, is named and skipped — pull writes nothing
441
+ through it, push sends nothing from it — while the other files still sync and the run
442
+ exits `1`. Two links are refused before anything is sent: a `.sb-code.json` that is a
443
+ link, and, when `--dir` is omitted, a `code` folder that is a link — pass `--dir <path>`
444
+ to sync through it deliberately.
445
+
446
+ `.sb-code.json` records, per file, the server's id and metadata, its `updatedAt`
447
+ and a sha256 of the content **as last pulled or pushed**. That is the base both
448
+ directions compare against, which is how `sb code` tells your edit from an edit
449
+ made in the Code Dock. **Commit it**: a CI push then judges conflicts against the
450
+ last pull you committed.
451
+
452
+ ### Pull
453
+
454
+ `sb code pull` writes every file the server has. It **never overwrites a local
455
+ edit**:
456
+
457
+ - Your file changed and the server's did not: it is left alone and printed as
458
+ `pending` — `sb code push` sends it. Exit `0`.
459
+ - Both changed: the file is kept, named, and the run exits `1`.
460
+ - `sb code pull --force` overwrites every local edit with the server's version,
461
+ in both cases.
462
+ - A file deleted on the server is removed locally only if you never edited it.
463
+
464
+ It ends with a summary line: written, pending, kept, removed.
465
+
466
+ ### Push
467
+
468
+ `sb code push` builds a plan against the manifest:
469
+
470
+ | Plan | When | Needs |
471
+ | --- | --- | --- |
472
+ | update | a tracked file whose content differs from its base | nothing |
473
+ | create | an untracked file under `site/` or `pages/<page>/` | `.css`, `.js` or `.html`; any other extension is refused by name |
474
+ | delete | a tracked file that is missing locally | `--delete`; without it the file is printed as `missing` and kept on the server |
475
+
476
+ Creates run first, then updates, then deletes. Each update and delete carries the
477
+ `updatedAt` from your last pull, and **the server compares it inside the write**:
478
+ a file changed in the Code Dock since then answers `409 code_file_changed`, that
479
+ one file is refused ("changed in the Code Dock since your pull; run sb code pull,
480
+ or push --force"), the rest of the run continues, and the exit code is `1`.
481
+
482
+ **The manifest is written after every operation**, so a run that stops halfway —
483
+ a server error or a lost connection stops it — is resumed by running it again;
484
+ nothing is created twice.
485
+
486
+ Only the **content** syncs. Placement, enabled, position and the language of an
487
+ existing file stay in the Code Dock, where their effect is visible. A new file gets
488
+ the server's defaults — placement `head` for `.css` and `body_end` for `.js` and
489
+ `.html`, the same as in the Code Dock — and the line printed for it says which, so
490
+ a `.js` that lands in `body_end` is never a surprise. A moved or renamed local
491
+ file is a create plus a `--delete`able delete, and `--dry-run` shows it as exactly that.
492
+
493
+ ### Flags
494
+
495
+ | Flag | Applies to | Effect |
496
+ | --- | --- | --- |
497
+ | `--site <id>` | pull, push | The site to sync. Needed with `sb login` or a user token unless `sb.json` or the manifest names it; a `wbk_` key implies its site |
498
+ | `--dir <path>` | pull, push | The root folder, `code` by default |
499
+ | `--force` | pull, push | Pull overwrites every local edit with the server's version. Push sends no `expectedUpdatedAt`, so the **content** you have wins; it still sends the server's current metadata, so it never reverts what the Code Dock changed |
500
+ | `--dry-run` | push | Print the plan and send nothing |
501
+ | `--delete` | push | Also delete the server files whose local copy is gone |
502
+
503
+ Deleting something you still depend on is a decision, never a default — which is
504
+ why `--delete` exists at all rather than push inferring it.
505
+
506
+ ### Which API it talks to
507
+
508
+ - **`SB_TOKEN` starting `wbk_`** — a merchant API key — uses `/api/v1/code-files`.
509
+ The key implies its site, so `--site` is not needed.
510
+ - **Anything else** — the credential stored by `sb login`, or a pasted user token
511
+ — uses `/api/sites/{siteId}/code-files`. The site comes from `--site`, else
512
+ the optional `siteId` in `sb.json`, else the manifest's. Two sources that name
513
+ different sites are refused before any request.
514
+ - **`SB_TOKEN` starting `sbdk_`** is refused before any request. A deploy key
515
+ belongs to a developer organization and only reaches its apps' version routes;
516
+ site code is a merchant's content.
517
+
518
+ A `wbk_` key whose manifest shares **no** file with the server's list is refused
519
+ as belonging to a different site, so a CI key cannot write site A's files onto
520
+ site B. `--force` overrides it.
521
+
522
+ ### CI for `sb code`
523
+
524
+ Create an API key in the merchant's **Settings → API keys** with `codefiles.write`
525
+ (add `pages.read` if CI creates files in a page folder the manifest does not
526
+ know yet), and store it as the CI secret `SB_TOKEN`:
527
+
528
+ ```bash
529
+ SB_TOKEN="$SB_API_KEY" sb code push
530
+ ```
531
+
532
+ A key without `codefiles.write` is refused by the server and nothing is written.
533
+ `codefiles.write` means "may add a script to every published page", so give it
534
+ to a key only when a pipeline needs it. Commit `code/`, manifest included.
535
+
536
+ ### Exit codes
537
+
538
+ `0` when every planned operation succeeded. `1` when anything was refused or skipped
539
+ against the server's will: a `409`, a validation error, an unknown extension, a
540
+ pull that kept a file you and the server both changed, an `sbdk_` key, a site
541
+ mismatch or a refused token. A pending local edit seen by pull, or a tracked
542
+ file missing locally without `--delete`, is printed and is **not** a failure.
543
+
544
+ ---
545
+
401
546
  ## CI
402
547
 
403
548
  A user access token expires, so it is the wrong credential for a pipeline. Use a
@@ -407,7 +552,7 @@ A user access token expires, so it is the wrong credential for a pipeline. Use a
407
552
  (an owner or admin can do this), then create one. The secret starts with
408
553
  `sbdk_` and is shown **once**.
409
554
  2. Store it as the CI secret `SB_TOKEN`. `sb dev` and `sb deploy` accept it
410
- exactly like a pasted token.
555
+ exactly like a pasted token. (`sb code` does not: see its own CI recipe.)
411
556
 
412
557
  ```bash
413
558
  SB_TOKEN="$SB_DEPLOY_KEY" sb deploy
@@ -430,18 +575,23 @@ Two are read by the CLI itself:
430
575
 
431
576
  | Variable | Required by | Default |
432
577
  | --- | --- | --- |
433
- | `SB_TOKEN` | `sb dev`, `sb deploy` — optional | none — the `sb login` credential is used; with neither, both stop and name `sb login` |
578
+ | `SB_TOKEN` | `sb dev`, `sb deploy`, `sb code` — optional | none — the `sb login` credential is used; with neither, each stops and names `sb login` |
434
579
  | `SB_API` | every command but `sb init` | `http://localhost:8080` |
435
580
 
436
- `SB_TOKEN` **overrides** the stored `sb login` credential when set. It is a
437
- token for the **organization that owns the app** — the same user access token
438
- you would put in an `Authorization: Bearer` header against the org-scoped
439
- routes. It is **not** a `wba_` app token; those belong to an install, and an
440
- install cannot edit the app it is an install of.
441
-
442
- A pasted token cannot renew itself. When the server refuses it, the CLI says
443
- `SB_TOKEN was refused: it has expired or been revoked` and points you at
444
- `sb login` — it never tries to refresh it.
581
+ `SB_TOKEN` **overrides** the stored `sb login` credential when set. For `sb dev`
582
+ and `sb deploy` it is a token for the **organization that owns the app** — the
583
+ same user access token you would put in an `Authorization: Bearer` header against
584
+ the org-scoped routes. It is **not** a `wba_` app token; those belong to an
585
+ install, and an install cannot edit the app it is an install of. For `sb code` it
586
+ is a user token with access to the site, or a merchant `wbk_` API key (see
587
+ [`sb code`](#sb-code--your-code-dock-files-as-a-folder)).
588
+
589
+ A pasted token cannot renew itself, and the CLI never tries to refresh it. When
590
+ the server refuses a pasted token, `sb dev` and `sb deploy` say
591
+ `SB_TOKEN was refused: it has expired or been revoked` and point you at
592
+ `sb login`. `sb code` says
593
+ ``Not signed in, or the token was refused (expired or revoked). Run `sb login`, or set SB_TOKEN to an API key (wbk_…) with codefiles.read / codefiles.write from Settings -> API keys.``
594
+ instead, and never suggests a deploy key, because `sb code` refuses one.
445
595
 
446
596
  Three more are read by the app `sb init` writes, from its own `.env`:
447
597
 
@@ -461,7 +611,10 @@ Every command exits `0` on success and `1` on a refusal it can explain. Anything
461
611
  else is a bug in the tool and gets a stack trace, because a scaffolder that
462
612
  swallows its own failures is one nobody can report.
463
613
 
464
- The server codes the CLI translates, rather than passing through verbatim:
614
+ `sb dev` and `sb deploy` translate the server codes below rather than passing
615
+ them through verbatim. `sb code` words its own refusals: the refused-token one
616
+ is above, and the [`sb code`](#sb-code--your-code-dock-files-as-a-folder)
617
+ section describes the command.
465
618
 
466
619
  | Server code | What `sb` says instead |
467
620
  | --- | --- |
@@ -491,7 +644,6 @@ Stated so you can plan around it rather than discover it.
491
644
  - **`sb dev` is not hot reload**, and no future version of it can be while an
492
645
  island's code is served from the platform's object store.
493
646
  - **`sb deploy` is not atomic**, deliberately.
494
- - **It is not on npm yet.**
495
647
 
496
648
  ---
497
649
 
@@ -502,6 +654,7 @@ Stated so you can plan around it rather than discover it.
502
654
  | `Not signed in. Run sb login …` | `dev`/`deploy` have no credential | `sb login`, or set `SB_TOKEN` |
503
655
  | `Your sb login has expired or was revoked.` | The stored session ended (signed out everywhere, or it lapsed) | `sb login` again |
504
656
  | `SB_TOKEN was refused: …` | The pasted token expired or was revoked | `sb login`, or a fresh token |
657
+ | `Not signed in, or the token was refused …` | `sb code` has no credential, the pasted `SB_TOKEN` was refused, or the server still answered 401 after renewing your `sb login` | `sb login`, or set `SB_TOKEN` to a `wbk_` API key with `codefiles.read` / `codefiles.write` from Settings → API keys |
505
658
  | `No sb.json here.` | Run from the wrong directory | `cd` to the folder holding `sb.json` |
506
659
  | `sb.json is missing "versionId".` | A required key is absent or empty | Add all five required keys |
507
660
  | `unknown command: …` | Typo, or a stale `dist/` | `npm run build --workspace cli` |
package/README.vi.md CHANGED
@@ -14,6 +14,11 @@ sb dev tải code của một island lên bản DRAFT m
14
14
  lưu file. Đọc sb.json; cần sb login (hoặc SB_TOKEN).
15
15
  sb deploy [--submit] đẩy toàn bộ payload của version theo đúng thứ tự
16
16
  phụ thuộc. --submit thì gửi luôn đi duyệt.
17
+ sb code pull [--site <id>] [--dir <path>] [--force]
18
+ kéo các file Code Dock của một site về ./code
19
+ sb code push [--site <id>] [--dir <path>] [--force] [--dry-run] [--delete]
20
+ gửi các chỗ sửa ở máy ngược lại; từ chối file đã
21
+ đổi trong Code Dock kể từ lần pull, trừ khi --force
17
22
  ```
18
23
 
19
24
  Cần Node 20 trở lên. Viết bằng TypeScript, **publish ở dạng đã biên dịch** — bản
@@ -31,6 +36,7 @@ cài về không có dependency nào và không phải build gì cả.
31
36
  - [`sb login` / `sb logout`](#sb-login--sb-logout--đăng-nhập-qua-trình-duyệt)
32
37
  - [`sb dev`](#sb-dev--lưu-tải-lên-refresh)
33
38
  - [`sb deploy`](#sb-deploy--toàn-bộ-payload-theo-thứ-tự-phụ-thuộc)
39
+ - [`sb code`](#sb-code--file-code-dock-của-bạn-dưới-dạng-một-thư-mục)
34
40
  - [Biến môi trường](#biến-môi-trường)
35
41
  - [CI](#ci)
36
42
  - [Các từ chối, và ý nghĩa của chúng](#các-từ-chối-và-ý-nghĩa-của-chúng)
@@ -196,7 +202,7 @@ việc chưa xảy ra ở thời điểm `init`. Hãy tự viết một lần kh
196
202
  {
197
203
  "orgId": "org_yourorg",
198
204
  "appId": "app_1a2b3c4d",
199
- "versionId": "ver_5e6f7a8b",
205
+ "versionId": "apv_5e6f7a8b",
200
206
  "island": "countdown",
201
207
  "entry": "islands/countdown.js"
202
208
  }
@@ -228,13 +234,16 @@ hãy điền đủ ngay cả khi bạn chỉ deploy blocks.
228
234
  Một `sb.json` chỉ có năm khoá bắt buộc vẫn hợp lệ: `sb dev` chạy được, còn
229
235
  `sb deploy` sẽ không tìm thấy bước nào để chạy.
230
236
 
237
+ `sb code` đọc thêm đúng một khoá tuỳ chọn từ cùng file này, `siteId`, và không đọc
238
+ gì khác: nó không cần năm khoá bắt buộc.
239
+
231
240
  Một file đầy đủ hơn:
232
241
 
233
242
  ```json
234
243
  {
235
244
  "orgId": "org_yourorg",
236
245
  "appId": "app_1a2b3c4d",
237
- "versionId": "ver_5e6f7a8b",
246
+ "versionId": "apv_5e6f7a8b",
238
247
  "island": "countdown",
239
248
  "entry": "islands/countdown.js",
240
249
  "version": {
@@ -281,8 +290,8 @@ bạn đọc được (mode `0600`). Mỗi lần `sb login` là một thiết b
281
290
  trên tài khoản, nằm cùng danh sách phiên với các thiết bị khác; đăng xuất mọi
282
291
  nơi cũng kết thúc nó.
283
292
 
284
- **Nó tự gia hạn.** Khi server từ chối access token, `sb dev` và `sb deploy` xoay
285
- cặp token một lần, lưu lại, rồi gửi lại request. Hai tiến trình `sb` cùng gia
293
+ **Nó tự gia hạn.** Khi server từ chối access token, `sb dev`, `sb deploy` và
294
+ `sb code` xoay cặp token một lần, lưu lại, rồi gửi lại request. Hai tiến trình `sb` cùng gia
286
295
  hạn một lúc sẽ lần lượt đi qua một file khoá đặt cạnh file credential, và tiến
287
296
  trình thứ hai dùng luôn cặp mà tiến trình đầu đã lưu — một refresh token bị dùng
288
297
  hai lần sẽ kết thúc phiên. Nếu chính việc gia hạn bị từ chối, mục đó bị xoá và
@@ -307,7 +316,7 @@ lần bạn lưu. Nó tải lên ngay một lần lúc khởi động chứ khô
307
316
  tiên, rồi chạy cho tới khi bạn ngắt.
308
317
 
309
318
  ```
310
- watching islands/countdown.js — save it and this uploads to version ver_5e6f7a8b.
319
+ watching islands/countdown.js — save it and this uploads to version apv_5e6f7a8b.
311
320
  This is not hot reload: after each upload, refresh the page.
312
321
  uploaded countdown — refresh the page to see it
313
322
  ```
@@ -398,6 +407,144 @@ kiểm tra thật, và nếu hai bên có mâu thuẫn thì server thắng; cái
398
407
 
399
408
  ---
400
409
 
410
+ ## `sb code` — file Code Dock của bạn dưới dạng một thư mục
411
+
412
+ ```bash
413
+ sb code pull --site <site-id>
414
+ # sửa code/site/css/main.css bằng trình soạn thảo của bạn, rồi
415
+ sb code push
416
+ ```
417
+
418
+ Đồng bộ các file **Code Dock** của một site — CSS, JavaScript và HTML dùng chung
419
+ toàn site hoặc riêng từng trang — về một thư mục, để bạn sửa bằng trình soạn
420
+ thảo của mình, lưu trong git và đẩy ngược lại.
421
+
422
+ ### Bố cục, và manifest
423
+
424
+ ```
425
+ code/
426
+ .sb-code.json manifest — hãy commit nó
427
+ site/css/main.css file toàn site tên "css/main.css"
428
+ pages/about/track.js file "track.js" của trang có slug là "about"
429
+ ```
430
+
431
+ Dấu `/` trong tên file trở thành thư mục. Thư mục của một trang là slug của nó
432
+ **tại lần pull đầu tiên** (trang chủ là `home`; slug rỗng hoặc trùng thì thành
433
+ `page-<id>`) rồi được ghi vào manifest, nên đổi slug về sau không làm file của
434
+ bạn đổi chỗ. Đổi tên một file trong Code Dock cũng không làm nó đổi chỗ: nó giữ
435
+ nguyên đường dẫn ở máy, vì được theo dõi theo id, và nội dung của nó vẫn được đồng
436
+ bộ. Một file Code Dock không có tên sẽ được pull về thành
437
+ `unnamed-<id>.<ext>` và được theo dõi theo id.
438
+
439
+ Symlink không bao giờ dẫn `sb code` ra ngoài thư mục gốc: một file là symlink, hoặc nằm
440
+ trong thư mục dẫn ra ngoài thư mục gốc qua một symlink, được nêu tên và bỏ qua — pull
441
+ không ghi gì qua nó, push không gửi gì từ nó — các file khác vẫn đồng bộ và lần chạy
442
+ thoát `1`. Hai symlink bị từ chối trước khi gửi bất kỳ thứ gì: một `.sb-code.json` là
443
+ symlink, và, khi không có `--dir`, một thư mục `code` là symlink — truyền `--dir <path>`
444
+ để cố ý đồng bộ qua nó.
445
+
446
+ `.sb-code.json` ghi, cho mỗi file, id và metadata phía server, `updatedAt` của nó
447
+ và sha256 của nội dung **như lần pull hoặc push gần nhất**. Đó là mốc gốc mà cả
448
+ hai chiều so sánh với, nhờ vậy `sb code` phân biệt được chỗ bạn sửa với chỗ sửa
449
+ trong Code Dock. **Hãy commit nó**: push từ CI khi đó xét xung đột theo lần pull
450
+ gần nhất mà bạn đã commit.
451
+
452
+ ### Pull
453
+
454
+ `sb code pull` ghi mọi file mà server có. Nó **không bao giờ ghi đè chỗ bạn đã
455
+ sửa**:
456
+
457
+ - File của bạn đã đổi còn server thì không: để nguyên và in là `pending` —
458
+ `sb code push` sẽ gửi nó. Thoát `0`.
459
+ - Cả hai cùng đổi: file được giữ lại, được nêu tên, và lần chạy thoát `1`.
460
+ - `sb code pull --force` ghi đè mọi chỗ bạn đã sửa bằng bản của server, trong
461
+ cả hai trường hợp.
462
+ - File bị xoá trên server chỉ bị xoá ở máy bạn nếu bạn chưa từng sửa nó.
463
+
464
+ Cuối cùng là một dòng tổng kết: đã ghi, pending, giữ lại, đã xoá.
465
+
466
+ ### Push
467
+
468
+ `sb code push` lập một kế hoạch dựa trên manifest:
469
+
470
+ | Kế hoạch | Khi nào | Điều kiện |
471
+ | --- | --- | --- |
472
+ | update | file đã theo dõi mà nội dung khác mốc gốc | không |
473
+ | create | file chưa theo dõi nằm trong `site/` hoặc `pages/<page>/` | đuôi `.css`, `.js` hoặc `.html`; đuôi khác bị từ chối và nêu tên |
474
+ | delete | file đã theo dõi nhưng không còn ở máy | `--delete`; không có nó thì file được in là `missing` và vẫn giữ trên server |
475
+
476
+ Create chạy trước, rồi update, rồi delete. Mỗi update và delete mang theo
477
+ `updatedAt` từ lần pull gần nhất của bạn, và **server so sánh nó ngay bên trong
478
+ lệnh ghi**: một file đã bị đổi trong Code Dock kể từ đó sẽ trả `409
479
+ code_file_changed`, riêng file đó bị từ chối ("changed in the Code Dock since your
480
+ pull; run sb code pull, or push --force"), phần còn lại vẫn chạy tiếp, và mã
481
+ thoát là `1`.
482
+
483
+ **Manifest được ghi sau mỗi thao tác**, nên một lần chạy dừng giữa chừng — lỗi
484
+ server hoặc mất kết nối sẽ làm nó dừng — được tiếp tục bằng cách chạy lại;
485
+ không có gì bị tạo hai lần.
486
+
487
+ Chỉ **nội dung** được đồng bộ. Vị trí chèn (placement), bật/tắt, thứ tự và ngôn
488
+ ngữ của một file đã có vẫn nằm trong Code Dock, nơi hiệu ứng của chúng nhìn thấy
489
+ được. File mới nhận giá trị mặc định của server — vị trí chèn `head` cho `.css`
490
+ và `body_end` cho `.js` và `.html`, giống như trong Code Dock — và dòng in ra cho
491
+ nó nói rõ là giá trị nào, nên một file `.js` rơi vào `body_end` không bao giờ là bất ngờ.
492
+ File bị di chuyển hay đổi tên ở máy là một create cộng với một delete (cần
493
+ `--delete`), và `--dry-run` hiển thị đúng như vậy.
494
+
495
+ ### Flags
496
+
497
+ | Flag | Áp dụng cho | Tác dụng |
498
+ | --- | --- | --- |
499
+ | `--site <id>` | pull, push | Site cần đồng bộ. Cần khi dùng `sb login` hoặc user token, trừ khi `sb.json` hay manifest đã nêu; key `wbk_` ngầm chỉ định site của nó |
500
+ | `--dir <path>` | pull, push | Thư mục gốc, mặc định `code` |
501
+ | `--force` | pull, push | Pull ghi đè mọi chỗ bạn đã sửa bằng bản của server. Push không gửi `expectedUpdatedAt`, nên **nội dung** của bạn thắng; nó vẫn gửi metadata hiện tại của server, nên không bao giờ hoàn tác những gì Code Dock đã đổi |
502
+ | `--dry-run` | push | In kế hoạch và không gửi gì |
503
+ | `--delete` | push | Xoá luôn các file trên server mà bản ở máy đã mất |
504
+
505
+ Xoá thứ mà bạn còn đang dựa vào là một quyết định, không bao giờ là mặc định —
506
+ vì thế `--delete` tồn tại, thay vì để push tự suy ra.
507
+
508
+ ### Nó nói chuyện với API nào
509
+
510
+ - **`SB_TOKEN` bắt đầu bằng `wbk_`** — một API key của merchant — dùng
511
+ `/api/v1/code-files`. Key ngầm chỉ định site của nó, nên không cần `--site`.
512
+ - **Mọi trường hợp khác** — credential do `sb login` lưu, hoặc một user token dán
513
+ tay — dùng `/api/sites/{siteId}/code-files`. Site lấy từ `--site`, nếu không
514
+ thì khoá `siteId` tuỳ chọn trong `sb.json`, nếu không nữa thì từ manifest. Hai
515
+ nguồn nêu hai site khác nhau thì bị từ chối trước khi gửi bất kỳ request nào.
516
+ - **`SB_TOKEN` bắt đầu bằng `sbdk_`** bị từ chối trước khi gửi bất kỳ request nào.
517
+ Deploy key thuộc về một tổ chức developer và chỉ chạm tới các route version của
518
+ app nó; code của site là nội dung của merchant.
519
+
520
+ Một key `wbk_` mà manifest **không** có file nào trùng với danh sách của server bị
521
+ từ chối vì thuộc về site khác, nên một key CI không thể ghi file của site A lên
522
+ site B. `--force` bỏ qua điều đó.
523
+
524
+ ### CI cho `sb code`
525
+
526
+ Tạo một API key trong **Settings → API keys** của merchant với `codefiles.write`
527
+ (thêm `pages.read` nếu CI tạo file trong một thư mục trang mà manifest chưa
528
+ biết), rồi lưu nó thành secret `SB_TOKEN` của CI:
529
+
530
+ ```bash
531
+ SB_TOKEN="$SB_API_KEY" sb code push
532
+ ```
533
+
534
+ Key không có `codefiles.write` bị server từ chối và không có gì được ghi.
535
+ `codefiles.write` nghĩa là "được thêm script vào mọi trang đã publish", nên chỉ
536
+ cấp cho key khi pipeline thật sự cần. Hãy commit `code/`, kể cả manifest.
537
+
538
+ ### Mã thoát
539
+
540
+ `0` khi mọi thao tác đã lên kế hoạch đều thành công. `1` khi có gì đó bị từ chối
541
+ hoặc bỏ qua trái ý server: một `409`, lỗi validate, đuôi file lạ, một lần pull
542
+ đã giữ lại file mà cả bạn và server cùng sửa, key `sbdk_`, site không khớp, hoặc
543
+ token bị từ chối. Một chỗ sửa ở máy đang chờ mà pull nhìn thấy, hoặc một file đã
544
+ theo dõi bị mất ở máy khi không có `--delete`, được in ra và **không** phải lỗi.
545
+
546
+ ---
547
+
401
548
  ## CI
402
549
 
403
550
  User access token sẽ hết hạn, nên không hợp cho pipeline. Hãy dùng **deploy key**:
@@ -406,7 +553,7 @@ User access token sẽ hết hạn, nên không hợp cho pipeline. Hãy dùng *
406
553
  hoặc admin mới làm được), rồi tạo một key. Secret bắt đầu bằng `sbdk_` và chỉ
407
554
  hiện **một lần**.
408
555
  2. Lưu nó thành secret `SB_TOKEN` của CI. `sb dev` và `sb deploy` nhận nó y như
409
- một token dán tay.
556
+ một token dán tay. (`sb code` thì không: xem công thức CI riêng của nó.)
410
557
 
411
558
  ```bash
412
559
  SB_TOKEN="$SB_DEPLOY_KEY" sb deploy
@@ -429,18 +576,23 @@ Hai biến do chính CLI đọc:
429
576
 
430
577
  | Biến | Lệnh cần nó | Mặc định |
431
578
  | --- | --- | --- |
432
- | `SB_TOKEN` | `sb dev`, `sb deploy` — tuỳ chọn | không có — dùng credential của `sb login`; thiếu cả hai thì cả hai lệnh dừng và nhắc `sb login` |
579
+ | `SB_TOKEN` | `sb dev`, `sb deploy`, `sb code` — tuỳ chọn | không có — dùng credential của `sb login`; thiếu cả hai thì mỗi lệnh đều dừng và nhắc `sb login` |
433
580
  | `SB_API` | mọi lệnh trừ `sb init` | `http://localhost:8080` |
434
581
 
435
- `SB_TOKEN`, khi được đặt, **ghi đè** credential đã lưu của `sb login`. Nó là
436
- token của **tổ chức sở hữu app** — đúng cái user access token bạn sẽ đặt vào
437
- header `Authorization: Bearer` khi gọi các route theo tổ chức. Nó **không phải**
438
- app token dạng `wba_`; loại đó thuộc về một install, và một install không thể
439
- sửa chính cái app mà nó là install của.
582
+ `SB_TOKEN`, khi được đặt, **ghi đè** credential đã lưu của `sb login`. Với
583
+ `sb dev` và `sb deploy`, nó là token của **tổ chức sở hữu app** — đúng cái user
584
+ access token bạn sẽ đặt vào header `Authorization: Bearer` khi gọi các route theo
585
+ tổ chức. Nó **không phải** app token dạng `wba_`; loại đó thuộc về một install,
586
+ và một install không thể sửa chính cái app mà nó là install của. Với `sb code`,
587
+ nó là user token có quyền truy cập site, hoặc một API key `wbk_` của merchant
588
+ (xem [`sb code`](#sb-code--file-code-dock-của-bạn-dưới-dạng-một-thư-mục)).
440
589
 
441
- Token dán tay không tự gia hạn được. Khi server từ chối nó, CLI báo
590
+ Token dán tay không tự gia hạn được, và CLI không bao giờ thử refresh nó. Khi
591
+ server từ chối một token dán tay, `sb dev` và `sb deploy` báo
442
592
  `SB_TOKEN was refused: it has expired or been revoked` và chỉ bạn sang
443
- `sb login` — nó không bao giờ thử refresh token đó.
593
+ `sb login`. `sb code` thì báo
594
+ ``Not signed in, or the token was refused (expired or revoked). Run `sb login`, or set SB_TOKEN to an API key (wbk_…) with codefiles.read / codefiles.write from Settings -> API keys.``
595
+ và không bao giờ gợi ý deploy key, vì `sb code` từ chối deploy key.
444
596
 
445
597
  Ba biến còn lại do app mà `sb init` sinh ra tự đọc, từ `.env` của chính nó:
446
598
 
@@ -461,7 +613,11 @@ Ngoài hai giá trị đó là bug của chính công cụ, và khi đó nó in
461
613
  một công cụ tạo khung mà nuốt lỗi của chính mình là công cụ không ai báo lỗi
462
614
  được.
463
615
 
464
- Các mã lỗi từ server mà CLI dịch lại thay vì bê nguyên văn:
616
+ `sb dev` và `sb deploy` dịch lại các mã lỗi từ server trong bảng dưới đây thay vì
617
+ bê nguyên văn. `sb code` có những câu từ chối riêng: câu về token bị từ chối nằm
618
+ ở phía trên, và mục
619
+ [`sb code`](#sb-code--file-code-dock-của-bạn-dưới-dạng-một-thư-mục) mô tả lệnh
620
+ này.
465
621
 
466
622
  | Mã từ server | `sb` nói gì thay vào đó |
467
623
  | --- | --- |
@@ -492,7 +648,6 @@ Nói ra để bạn tính đường trước, thay vì tự phát hiện giữa
492
648
  thể là hot reload chừng nào code island còn được phục vụ từ object store của
493
649
  nền tảng.
494
650
  - **`sb deploy` không nguyên tử**, một cách có chủ đích.
495
- - **Chưa có trên npm.**
496
651
 
497
652
  ---
498
653
 
@@ -503,6 +658,7 @@ Nói ra để bạn tính đường trước, thay vì tự phát hiện giữa
503
658
  | `Not signed in. Run sb login …` | `dev`/`deploy` không có credential nào | `sb login`, hoặc đặt `SB_TOKEN` |
504
659
  | `Your sb login has expired or was revoked.` | Phiên đã lưu đã kết thúc (đăng xuất mọi nơi, hoặc hết hạn) | Chạy lại `sb login` |
505
660
  | `SB_TOKEN was refused: …` | Token dán tay đã hết hạn hoặc bị thu hồi | `sb login`, hoặc lấy token mới |
661
+ | `Not signed in, or the token was refused …` | `sb code` không có credential nào, `SB_TOKEN` dán tay bị từ chối, hoặc server vẫn trả 401 sau khi đã gia hạn `sb login` của bạn | `sb login`, hoặc đặt `SB_TOKEN` là một API key `wbk_` có `codefiles.read` / `codefiles.write` từ Settings → API keys |
506
662
  | `No sb.json here.` | Chạy sai thư mục | `cd` về đúng thư mục chứa `sb.json` |
507
663
  | `sb.json is missing "versionId".` | Thiếu hoặc để rỗng một khoá bắt buộc | Điền đủ cả năm khoá bắt buộc |
508
664
  | `unknown command: …` | Gõ nhầm, hoặc `dist/` đã cũ | `npm run build --workspace cli` |
package/dist/bin/sb.js CHANGED
@@ -6,11 +6,14 @@
6
6
  * without spawning a process. This file's whole job is to turn argv into a call
7
7
  * and an exit code.
8
8
  */
9
+ import { lstat } from 'node:fs/promises';
10
+ import { resolve } from 'node:path';
9
11
  import { init, nextSteps, InitError } from '../src/init.js';
10
12
  import { readConfig, watchAndUpload, DevError } from '../src/dev.js';
11
13
  import { deploy, submit, DeployStopped } from '../src/deploy.js';
12
14
  import { readPair, storedAuth } from '../src/auth.js';
13
15
  import { login, logout } from '../src/login.js';
16
+ import { pull, push, ROOT_LINKED } from '../src/code.js';
14
17
  const USAGE = `sb — build an app for Store Builder
15
18
 
16
19
  sb init [name] [--port 3000] scaffold an app that runs, registers, and opens
@@ -26,6 +29,11 @@ const USAGE = `sb — build an app for Store Builder
26
29
  Not one write: if it stops it says where, and
27
30
  running it again is safe. --submit then sends
28
31
  it for review.
32
+ sb code pull [--site <id>] [--dir <path>] [--force]
33
+ mirror a site's Code Dock files into ./code
34
+ sb code push [--site <id>] [--dir <path>] [--force] [--dry-run] [--delete]
35
+ send local edits back; refuses a file changed
36
+ in the Code Dock since your pull unless --force
29
37
 
30
38
  Docs: /cli-docs (this tool) · /app-docs (building an app) · /block-docs (contributing
31
39
  to the editor)`;
@@ -39,6 +47,21 @@ function parse(argv) {
39
47
  else if (a === '--submit') {
40
48
  args.submit = true;
41
49
  }
50
+ else if (a === '--site') {
51
+ args.site = argv[++i];
52
+ }
53
+ else if (a === '--dir') {
54
+ args.dir = argv[++i];
55
+ }
56
+ else if (a === '--force') {
57
+ args.force = true;
58
+ }
59
+ else if (a === '--dry-run') {
60
+ args.dryRun = true;
61
+ }
62
+ else if (a === '--delete') {
63
+ args.delete = true;
64
+ }
42
65
  else if (a === '-h' || a === '--help') {
43
66
  args.help = true;
44
67
  }
@@ -117,6 +140,41 @@ async function main(argv) {
117
140
  throw err;
118
141
  }
119
142
  }
143
+ if (cmd === 'code') {
144
+ const sub = args._[1];
145
+ if (sub !== 'pull' && sub !== 'push') {
146
+ console.error('usage: sb code pull|push [--site <id>] [--dir <path>] [--force] [--dry-run] [--delete]');
147
+ return 1;
148
+ }
149
+ // A committed `code` link would re-root the sync; typing --dir is the deliberate act.
150
+ // lstat checks only the final component, and an absent ./code (a first pull) is not a link.
151
+ if (args.dir === undefined &&
152
+ (await lstat(resolve('code')).catch(() => undefined))?.isSymbolicLink()) {
153
+ console.error(ROOT_LINKED);
154
+ return 1;
155
+ }
156
+ const api = process.env.SB_API ?? 'http://localhost:8080';
157
+ try {
158
+ return await (sub === 'pull' ? pull : push)({
159
+ api,
160
+ token: await credential(api),
161
+ dir: resolve(args.dir ?? 'code'),
162
+ site: args.site,
163
+ force: args.force,
164
+ dryRun: args.dryRun,
165
+ delete: args.delete,
166
+ log: (l) => console.log(l),
167
+ warn: (l) => console.error(l),
168
+ });
169
+ }
170
+ catch (err) {
171
+ if (err instanceof DevError) {
172
+ console.error(err.message);
173
+ return 1;
174
+ }
175
+ throw err;
176
+ }
177
+ }
120
178
  if (cmd === 'login' || cmd === 'logout') {
121
179
  const api = process.env.SB_API ?? 'http://localhost:8080';
122
180
  try {