Skip to content

Commit 0292581

Browse files
authored
Merge pull request #364 from processing/p5-version-script
add script to pull in latest p5 version
2 parents a82f00d + 9a54560 commit 0292581

9 files changed

Lines changed: 87 additions & 44 deletions

File tree

docs/scripts.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ To update the content on the p5.js website following a new release of p5.js, you
1818

1919
## Contributors List Build Script
2020

21-
`npm run build:contribtuors` parses the list of contributors from the all contributors [config file](https://github.com/processing/p5.js/blob/main/.all-contributorsrc) in the p5.js repo and adds entries in the [people collection](src/content/people/en) for anyone who is missing. These are used to render the list of contributors on the [People page](https://p5js.org/people/). This script will **not** remove or update existing entries, that must be done manually.
21+
`npm run build:contributors` parses the list of contributors from the all contributors [config file](https://github.com/processing/p5.js/blob/main/.all-contributorsrc) in the p5.js repo and adds entries in the [people collection](src/content/people/en) for anyone who is missing. These are used to render the list of contributors on the [People page](https://p5js.org/people/). This script will **not** remove or update existing entries, that must be done manually.
22+
23+
## Update p5 version Build Script
24+
25+
`npm run build:p5-version` reads the latest version of the p5.js library (for example, "2.19.3") and updates the website config to use that version of the library for embeds and download links across the site.
2226

2327
## Search Indices Build Script
2428

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"build:contributor-docs": "tsx ./src/scripts/builders/contribute.ts",
1515
"build:contributors": "tsx ./src/scripts/builders/people.ts",
1616
"build:reference": "tsx ./src/scripts/builders/reference.ts",
17-
"build:search": "tsx ./src/scripts/builders/search.ts"
17+
"build:search": "tsx ./src/scripts/builders/search.ts",
18+
"build:p5-version": "tsx ./src/scripts/p5-version.ts"
1819
},
1920
"dependencies": {
2021
"@astrojs/check": "^0.5.5",

src/components/CodeEmbed/frame.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { useRef, useLayoutEffect, useEffect } from "preact/hooks";
2-
import { p5VersionForEmbeds } from "@/src/globals/globals";
3-
4-
/*
5-
* Url to fetch the p5.js library from
6-
*/
7-
const p5LibraryUrl = `https://cdnjs.cloudflare.com/ajax/libs/p5.js/${p5VersionForEmbeds}/p5.min.js`;
2+
import { cdnLibraryUrl } from "@/src/globals/globals";
83

94
interface CodeBundle {
105
css?: string;
@@ -53,7 +48,7 @@ ${code.css || ""}
5348
window.addEventListener("message", event => {
5449
// Include check to prevent p5.min.js from being loaded twice
5550
const scriptExists = !!document.getElementById("p5ScriptTagInIframe");
56-
if (!scriptExists && event.data?.sender === '${p5LibraryUrl}') {
51+
if (!scriptExists && event.data?.sender === '${cdnLibraryUrl}') {
5752
const p5ScriptElement = document.createElement('script');
5853
p5ScriptElement.id = "p5ScriptTagInIframe";
5954
p5ScriptElement.type = 'text/javascript';
@@ -136,7 +131,7 @@ export const CodeFrame = (props: CodeFrameProps) => {
136131
);
137132
iframeRef.current.contentWindow?.postMessage(
138133
{
139-
sender: p5LibraryUrl,
134+
sender: cdnLibraryUrl,
140135
message: p5ScriptText,
141136
},
142137
"*",

src/components/CodeEmbed/frameForServer.tsx

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { useRef, useLayoutEffect } from "preact/hooks";
2-
import { p5VersionForEmbeds } from "@/src/globals/globals";
3-
4-
/*
5-
* Url to fetch the p5.js library from
6-
*/
7-
const p5LibraryUrl = `https://cdnjs.cloudflare.com/ajax/libs/p5.js/${p5VersionForEmbeds}/p5.min.js`;
2+
import { cdnLibraryUrl } from "@/src/globals/globals";
83

94
interface CodeBundle {
105
css?: string;
@@ -33,7 +28,7 @@ ${code.css || ""}
3328
</style>
3429
<body>${code.htmlBody || ""}</body>
3530
<script id="code" type="text/javascript">${code.js || ""}</script>
36-
<script src="${p5LibraryUrl}"></script>
31+
<script src="${cdnLibraryUrl}"></script>
3732
`.replace(/\u00A0/g, " ");
3833

3934
export interface CodeFrameProps {
@@ -63,23 +58,29 @@ export const CodeFrameForServer = (props: CodeFrameProps) => {
6358
const { IntersectionObserver } = window;
6459
if (!IntersectionObserver) return;
6560

66-
const observer = new IntersectionObserver((entries) => {
67-
entries.forEach(entry => {
68-
if (!iframeRef.current) return;
69-
if (entry.isIntersecting) {
70-
iframeRef.current.style.removeProperty('display');
71-
} else {
72-
iframeRef.current.style.display = 'none';
73-
}
74-
});
75-
}, { rootMargin: '20px' });
76-
observer.observe(containerRef.current)
61+
const observer = new IntersectionObserver(
62+
(entries) => {
63+
entries.forEach((entry) => {
64+
if (!iframeRef.current) return;
65+
if (entry.isIntersecting) {
66+
iframeRef.current.style.removeProperty("display");
67+
} else {
68+
iframeRef.current.style.display = "none";
69+
}
70+
});
71+
},
72+
{ rootMargin: "20px" },
73+
);
74+
observer.observe(containerRef.current);
7775

7876
return () => observer.disconnect();
7977
}, []);
8078

8179
return (
82-
<div ref={containerRef} style={{ width: props.width, height: props.height }}>
80+
<div
81+
ref={containerRef}
82+
style={{ width: props.width, height: props.height }}
83+
>
8384
<iframe
8485
ref={iframeRef}
8586
srcDoc={wrapInMarkup({
@@ -96,4 +97,4 @@ export const CodeFrameForServer = (props: CodeFrameProps) => {
9697
/>
9798
</div>
9899
);
99-
}
100+
};

src/components/CodeEmbed/index.jsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useState, useEffect, useRef } from "preact/hooks";
22
import CodeMirror, { EditorView } from "@uiw/react-codemirror";
33
import { javascript } from "@codemirror/lang-javascript";
4-
import { p5VersionForEmbeds } from "@/src/globals/globals";
4+
import { cdnLibraryUrl } from "@/src/globals/globals";
55

66
import { CodeFrame } from "./frame";
77
import { CopyCodeButton } from "../CopyCodeButton";
@@ -47,18 +47,13 @@ export const CodeEmbed = (props) => {
4747

4848
const [previewCodeString, setPreviewCodeString] = useState(codeString);
4949

50-
/*
51-
* Url to fetch the p5.js library from
52-
*/
53-
const p5LibraryUrl = `https://cdnjs.cloudflare.com/ajax/libs/p5.js/${p5VersionForEmbeds}/p5.min.js`;
54-
5550
useEffect(() => {
5651
setRendered(true);
5752

5853
// Includes p5.min.js script to be used by `CodeFrame` iframe(s)
5954
const p5ScriptElement = document.createElement("script");
6055
p5ScriptElement.id = "p5ScriptTag";
61-
p5ScriptElement.src = p5LibraryUrl;
56+
p5ScriptElement.src = cdnLibraryUrl;
6257
document.head.appendChild(p5ScriptElement);
6358
}, []);
6459

@@ -102,7 +97,7 @@ export const CodeEmbed = (props) => {
10297
</div>
10398
</div>
10499
) : null}
105-
<div className="relative w-full code-editor-container">
100+
<div className="code-editor-container relative w-full">
106101
<CodeMirror
107102
value={codeString}
108103
theme="light"

src/content/text-detail/en/download.mdx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ title: "Download"
33
---
44
import LinkButton from '../../../components/LinkButton/index.astro'
55
import CodeContainerWithCopy from "../../../components/CodeContainer/CodeContainerWithCopy.astro";
6+
import {
7+
cdnLibraryUrl,
8+
fullDownloadUrl,
9+
libraryDownloadUrl,
10+
minifiedLibraryDownloadUrl
11+
} from '@src/globals/globals'
612

713
Welcome! This page contains the links to start using p5.js in the way that suits you best. Open the p5.js Editor in your web browser, or download the library to your own computer. We’ve tried to order the links to reflect what a beginner might want first, then what a more experienced programmer may be looking for.
814

@@ -14,20 +20,20 @@ This link redirects you to the p5.js Editor online so you can begin using p5.js
1420
### Download the Complete Library
1521
This is a download containing the p5.js library file, the p5.sound addon, and an example project. It does not contain an editor. Visit [Get Started](/tutorials/get-started) to learn how to setup a p5.js project.
1622

17-
<LinkButton variant='download' url='https://github.com/processing/p5.js/releases/download/v1.9.2/p5.zip'> Complete Library </LinkButton>
23+
<LinkButton variant='download' url={fullDownloadUrl}> Complete Library </LinkButton>
1824

1925
### Download Single Files
2026
These are downloads or links to the p5.js library file. No additional contents are included.
2127

2228
<div class="flex gap-sm md:gap-lg justify-between lg:justify-normal">
23-
<LinkButton variant='download' url='https://github.com/processing/p5.js/releases/download/v1.9.2/p5.js'> p5.js </LinkButton>
24-
<LinkButton variant='download' url='https://github.com/processing/p5.js/releases/download/v1.9.2/p5.min.js'> p5.min.js </LinkButton>
29+
<LinkButton variant='download' url={libraryDownloadUrl}> p5.js </LinkButton>
30+
<LinkButton variant='download' url={minifiedLibraryDownloadUrl}> p5.min.js </LinkButton>
2531
</div>
2632

2733
### Use p5.js from CDN
2834
p5.js is a client-side JS platform that empowers artists, designers, students, and anyone to learn to code and express themselves creatively on the web. It is based on the core principles of Processing.
2935

30-
<CodeContainerWithCopy>https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.2/p5.min.js</CodeContainerWithCopy>
36+
<CodeContainerWithCopy>{cdnLibraryUrl}</CodeContainerWithCopy>
3137

3238
### Older versions
3339
Looking for older versions?

src/globals/globals.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { p5Version } from "./p5-version";
2+
13
export const contentTypes = [
24
"contributor-docs",
35
"examples",
@@ -8,7 +10,14 @@ export const contentTypes = [
810
"tutorials",
911
] as const;
1012

11-
export const p5VersionForEmbeds = "1.9.1" as const;
12-
1313
export const sketchesPerPage = 12 as const;
1414
export const eventsPerPage = 12 as const;
15+
16+
export const cdnLibraryUrl =
17+
`https://cdnjs.cloudflare.com/ajax/libs/p5.js/${p5Version}/p5.min.js` as const;
18+
export const fullDownloadUrl =
19+
`https://github.com/processing/p5.js/releases/download/v${p5Version}/p5.zip` as const;
20+
export const libraryDownloadUrl =
21+
`https://github.com/processing/p5.js/releases/download/v${p5Version}/p5.js` as const;
22+
export const minifiedLibraryDownloadUrl =
23+
`https://github.com/processing/p5.js/releases/download/v${p5Version}/p5.min.js` as const;

src/globals/p5-version.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const p5Version = "1.9.4" as const;

src/scripts/p5-version.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import path from "path";
2+
import { cloneLibraryRepo, repoRootPath, writeFile } from "./utils";
3+
import { readFile } from "fs/promises";
4+
5+
/* Where to clone the repo to */
6+
const clonedRepoPath = path.join(repoRootPath, "in", "p5.js");
7+
8+
const outputFile = path.join(repoRootPath, "src", "globals", "p5-version.ts");
9+
10+
const outputString = (version: string) =>
11+
`export const p5Version = "${version}" as const;\n`;
12+
13+
const run = async () => {
14+
console.log("Reading latest p5 version to update config...");
15+
16+
await cloneLibraryRepo(clonedRepoPath);
17+
18+
// read version from package.json
19+
const packageConfigContents = await readFile(
20+
path.join(clonedRepoPath, "package.json"),
21+
"utf8",
22+
);
23+
const newP5Version = JSON.parse(packageConfigContents)["version"] as string;
24+
25+
// write to ts file
26+
await writeFile(outputFile, outputString(newP5Version));
27+
28+
console.log("Done!");
29+
};
30+
31+
run();

0 commit comments

Comments
 (0)