Skip to content

Commit fbc4d4f

Browse files
authored
1287 library assets url (#1479)
related to issue: [1287](RaspberryPiFoundation/digital-editor-issues#1287)
1 parent 2069391 commit fbc4d4f

17 files changed

Lines changed: 346 additions & 161 deletions

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ HTML_RENDERER_URL='http://localhost:3011'
77
REACT_APP_GOOGLE_TAG_MANAGER_ID=''
88
REACT_APP_API_ENDPOINT='http://localhost:3009'
99
REACT_APP_ALLOWED_IFRAME_ORIGINS='http://localhost:3011,http://localhost:3012,http://classroom.localhost:3012'
10+
11+

.github/workflows/ci-cd.yml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@ on:
1010
branches:
1111
- "**"
1212

13+
permissions:
14+
contents: read
15+
packages: read
16+
1317
jobs:
1418
lint:
1519
runs-on: ubuntu-latest
20+
env:
21+
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
22+
NPM_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1623
steps:
1724
- name: Checkout
1825
uses: actions/checkout@v4
@@ -39,8 +46,6 @@ jobs:
3946

4047
- name: Install code
4148
run: yarn install --immutable
42-
env:
43-
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4449

4550
- name: eslint
4651
run: yarn lint
@@ -51,6 +56,9 @@ jobs:
5156

5257
test:
5358
runs-on: ubuntu-latest
59+
env:
60+
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
61+
NPM_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5462
steps:
5563
- name: Checkout
5664
uses: actions/checkout@v1
@@ -66,8 +74,6 @@ jobs:
6674

6775
- name: Install code
6876
run: yarn install --immutable
69-
env:
70-
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7177

7278
- name: Run tests
7379
run: yarn run test --coverage --maxWorkers=4 --workerThreads=true --reporters=default --reporters=jest-junit --reporters=jest-github-actions-reporter
@@ -81,6 +87,9 @@ jobs:
8187

8288
test-cypress:
8389
runs-on: ubuntu-latest
90+
env:
91+
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
92+
NPM_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
8493
steps:
8594
- name: Checkout
8695
uses: actions/checkout@v1
@@ -96,8 +105,6 @@ jobs:
96105

97106
- name: Install code
98107
run: yarn install --immutable
99-
env:
100-
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
101108

102109
- name: Cypress run
103110
uses: cypress-io/github-action@v7

.github/workflows/deploy.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ jobs:
114114
url: ${{ needs.setup-environment.outputs.public_url }}
115115
env:
116116
HAS_CLOUDFLARE_SECRETS: ${{ secrets.CLOUDFLARE_ZONE_ID != '' && secrets.CLOUDFLARE_API_TOKEN != '' }}
117+
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
118+
NPM_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
117119
steps:
118120
- name: Check deployment path
119121
run: |
@@ -134,8 +136,6 @@ jobs:
134136

135137
- name: Install code
136138
run: yarn install --immutable
137-
env:
138-
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
139139

140140
- name: Build WC and HTML renderer bundles
141141
run: yarn build

.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@RaspberryPiFoundation:registry=https://npm.pkg.github.com
2+
//npm.pkg.github.com/:_authToken=${NPM_AUTH_TOKEN}

.yarnrc.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ nodeLinker: node-modules
88

99
yarnPath: .yarn/releases/yarn-4.12.0.cjs
1010

11+
npmScopes:
12+
RaspberryPiFoundation:
13+
npmRegistryServer: "https://npm.pkg.github.com"
14+
npmAuthToken: "${NPM_AUTH_TOKEN:-}"
15+
1116
npmMinimalAgeGate: 7d
1217

1318
npmPreapprovedPackages:
1419
- "@raspberrypifoundation/design-system-react"
15-
- "@raspberrypifoundation/design-system-core"
20+
- "@raspberrypifoundation/design-system-core"
21+
- "@RaspberryPiFoundation/scratch-gui"

README.md

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,47 @@ This project provides a web component containing the Raspberry Pi Code Editor fo
44

55
## Install dependencies
66

7-
This repository uses Yarn (see `package.json``packageManager`). Please install dependencies with Yarn:
7+
This repository uses Yarn (see `package.json``packageManager`).
8+
9+
`@RaspberryPiFoundation/scratch-gui` is installed from [GitHub Packages](https://github.com/RaspberryPiFoundation/scratch-editor/pkgs/npm/scratch-gui). Complete the steps below, then run `yarn install`.
10+
11+
### Set a personal access token
12+
13+
If you don't already have this set up you will need it to access deps in the RPF private registry
14+
15+
1. On GitHub, create a **classic** personal access token: [Settings → Developer settings → Personal access tokens](https://github.com/settings/tokens). Enable **`read:packages`** and **`repo`**. For packages tied to private repositories, `read:packages` alone can cause `yarn install` to fail with `401`/`403`.
16+
2. If your organisation uses SAML SSO, open the token on GitHub and **Authorize** it for **RaspberryPiFoundation** (Configure SSO).
17+
3. Add the token to your shell profile (for example `~/.zshrc` on macOS):
18+
19+
```bash
20+
export GITHUB_TOKEN=ghp_your_token_here
21+
export NPM_AUTH_TOKEN=$GITHUB_TOKEN
22+
```
23+
24+
Replace `ghp_your_token_here` with your token.
25+
26+
4. Load the profile in any terminal you are currently using for this project:
27+
28+
```bash
29+
source ~/.zshrc
30+
```
31+
32+
5. Confirm GitHub Packages is reachable from this directory:
33+
34+
```bash
35+
yarn npm info @RaspberryPiFoundation/scratch-gui version
36+
```
37+
38+
You should see the version pinned in `package.json` (for example `13.7.3-code-classroom.20260522151700`), not an authentication error. If you see `unauthenticated` or `401`, run `source ~/.zshrc` again or check the token scopes and SSO authorisation.
39+
40+
6. When you use Docker, run `docker compose up` from a shell where `NPM_AUTH_TOKEN` is set. Compose passes it from the host into the container so `yarn install` can run there too.
41+
42+
Then install dependencies:
843

944
```
1045
yarn install
1146
```
1247

13-
Using `npm install` can fail due to strict peer-dependency resolution in npm for some legacy packages in this project.
14-
1548
## Environment variables
1649

1750
The app uses the `dotenv` package to provide access to environment variables. Copy the example file into `.env` and use this file for any other environment variables the web component may require:
@@ -227,6 +260,10 @@ Python code snippets are styled and syntax-highlighted using the `language-pytho
227260
<code class="language-python">print('hello world')</code>
228261
```
229262

263+
### Linking a local scratch-editor (Scratch GUI)
264+
265+
See [docs/linking-scratch-editor.md](./docs/linking-scratch-editor.md) for local scratch-editor linking instructions.
266+
230267
## Deployment
231268

232269
Deployment is managed through Github actions. The UI is deployed to staging and production environments through an S3 bucket, managed via Cloudflare. This requires the following environment variables to be set

docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
x-app: &x-app
22
build:
33
context: .
4+
environment:
5+
NPM_AUTH_TOKEN: ${NPM_AUTH_TOKEN}
46
volumes:
57
- .:/app
68
- node_modules:/app/node_modules

docs/linking-scratch-editor.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Linking a local scratch-editor (Scratch GUI)
2+
3+
When you are working on the [Raspberry Pi Foundation scratch-editor](https://github.com/RaspberryPiFoundation/scratch-editor) fork (for example changes to `scratch-gui`), you may want editor-ui to load that build instead of the `@RaspberryPiFoundation/scratch-gui` version from GitHub Packages.
4+
5+
**Use the `code-classroom` branch** in scratch-editor as your base for editor-ui work. That branch is the integration line for this editor (RPF packaging, publish, and GUI props such as `libraryAssetUrlTemplate`). Check it out before you link or build:
6+
7+
```bash
8+
cd ../scratch-editor
9+
git fetch origin
10+
git checkout code-classroom
11+
```
12+
13+
Feature work should branch from `code-classroom`, not from upstream `main` or `code-classroom-base`.
14+
15+
editor-ui does not bundle Scratch GUI into the main webpack app. It copies a prebuilt `dist/scratch-gui.js` (and static assets) from `node_modules` into the dev server output. Pointing the dependency at your local clone is enough to test GUI changes in the Scratch iframe.
16+
17+
**Do not commit these linking changes.** They are temporary for local development only. These changes to `package.json`, `yarn.lock`, and `docker-compose.yml` are for local development only.
18+
19+
## Repository layout
20+
21+
Clone both repositories as siblings:
22+
23+
```text
24+
Development/
25+
editor-ui/
26+
scratch-editor/ ← github.com/RaspberryPiFoundation/scratch-editor
27+
```
28+
29+
## Temporary changes in editor-ui
30+
31+
**1. `package.json`** — replace the GitHub Packages dependency with a file link (the name must match the package in scratch-editor):
32+
33+
```json
34+
"@RaspberryPiFoundation/scratch-gui": "file:../scratch-editor/packages/scratch-gui"
35+
```
36+
37+
**2. `docker-compose.yml`** — mount the scratch-editor repo into the container (read-only):
38+
39+
```yaml
40+
volumes:
41+
- .:/app
42+
- ../scratch-editor:/scratch-editor:ro
43+
- node_modules:/app/node_modules
44+
```
45+
46+
From `/app` in the container, `file:../scratch-editor/packages/scratch-gui` resolves to `/scratch-editor/packages/scratch-gui` on the mounted volume.
47+
48+
**3. `yarn.lock`** — run `yarn install` inside Docker (see below) and commit nothing; the lockfile will change while the link is active. Revert it when you restore the GitHub Packages version in `package.json`.
49+
50+
## Build scratch-gui
51+
52+
Build on your machine (the `scratch-editor` mount is read-only inside the editor-ui container):
53+
54+
```bash
55+
cd ../scratch-editor
56+
nvm install
57+
nvm use
58+
NODE_ENV=development npm ci
59+
npm run build
60+
```
61+
62+
After every scratch-gui code change, run the build again, then restart the editor-ui container so webpack copies the new `dist/scratch-gui.js`.
63+
64+
## Run with Docker
65+
66+
**editor-api** (Scratch projects and assets API):
67+
68+
```bash
69+
cd ../editor-api
70+
docker compose up
71+
```
72+
73+
**editor-ui**:
74+
75+
```bash
76+
cd ../editor-ui
77+
docker compose up
78+
```
79+
80+
The container runs `yarn install` then `yarn start` on each start. The first start after switching to the file link may take longer while dependencies are linked.
81+
82+
## Verify in the browser
83+
84+
Scratch runs in an iframe served from editor-ui (port **3011**), not from the main web-component bundle alone.
85+
86+
- Web component test page: open a **Scratch** sample, e.g.
87+
`http://localhost:3011/web-component.html`
88+
then choose **cool-scratch**.
89+
- **editor-standalone** (port **3012**): also loads the web component from `http://localhost:3011`; open a Scratch project under `/en-US/projects/…` with editor-ui and editor-api running.
90+
91+
## Revert when finished
92+
93+
1. Restore the GitHub Packages pin in `package.json`, for example:
94+
95+
```json
96+
"@RaspberryPiFoundation/scratch-gui": "13.7.3-code-classroom.20260522151700"
97+
```
98+
99+
Use the version your branch pins (see [scratch-gui on GitHub Packages](https://github.com/RaspberryPiFoundation/scratch-editor/pkgs/npm/scratch-gui)).
100+
2. Remove the `../scratch-editor:/scratch-editor:ro` volume from `docker-compose.yml`.
101+
3. Revert `yarn.lock` (e.g. `git checkout -- yarn.lock`) or run `yarn install` again after restoring `package.json`.
102+
4. Restart `docker compose up` in editor-ui.
103+

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.34.20",
44
"private": true,
55
"dependencies": {
6+
"@RaspberryPiFoundation/scratch-gui": "13.7.3-code-classroom.20260522151700",
67
"@apollo/client": "^3.7.8",
78
"@babel/core": "^7.17.10",
89
"@codemirror/commands": "^6.1.1",
@@ -20,7 +21,6 @@
2021
"@react-three/fiber": "^8.0.13",
2122
"@reduxjs/toolkit": "^1.6.2",
2223
"@replit/codemirror-indentation-markers": "^6.1.0",
23-
"@scratch/scratch-gui": "^13.0.0",
2424
"@sentry/browser": "^7.17.3",
2525
"@sentry/react": "7.16.0",
2626
"@sentry/tracing": "7.16.0",

src/components/ScratchEditor/ScratchEditor.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { useCallback, useRef, useEffect, useState } from "react";
44
import WrapperdScratchGui from "./WrappedScratchGui.jsx";
55
import { postScratchGuiEvent, allowedParentOrigin } from "./events.js";
66

7+
/** Scratch library picker assets (not project save/load — those use editor-api). */
8+
export const SCRATCH_LIBRARY_ASSET_URL_TEMPLATE =
9+
"https://editor-assets.raspberrypi.org/internalapi/asset/{assetPath}/get/";
10+
711
const handleUpdateProjectId = (updatedProjectId) => {
812
postScratchGuiEvent("scratch-gui-project-id-updated", {
913
projectId: updatedProjectId,
@@ -79,6 +83,7 @@ const ScratchEditor = ({
7983
menuBarHidden={true}
8084
projectHost={`${apiUrl}/api/scratch/projects`}
8185
assetHost={`${apiUrl}/api/scratch/assets`}
86+
libraryAssetUrlTemplate={SCRATCH_LIBRARY_ASSET_URL_TEMPLATE}
8287
basePath={`${process.env.ASSETS_URL}/scratch-gui/`}
8388
onStorageInit={(storage) => {
8489
scratchFetchApiRef.current = storage.scratchFetch;

0 commit comments

Comments
 (0)