Skip to content

Commit b8408d4

Browse files
authored
Merge pull request #605 from processing/dev-2.0-docs
Add script to preview a custom branch
2 parents 94f263b + 7874ed3 commit b8408d4

File tree

11 files changed

+137
-21
lines changed

11 files changed

+137
-21
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ pnpm-debug.log*
2323
temp/*
2424
in/
2525
out/
26-
translator/
26+
translator/
27+
28+
# Don't commit custom dev builds
29+
public/p5.min.js

docs/scripts.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@ This repo includes a few scripts to pull content from external sources, primaril
44

55
They are all runnable via npm scripts.
66

7+
## Testing the docs of your fork
8+
9+
If you are contributing a change to the main p5.js repo that affects documentation, you will want to preview how your changes will look on the website. To do so, make sure you have committed your changes to a branch of your fork of p5.js.
10+
11+
Then, in the p5.js-website repo, run the following command, using the URL of your fork of p5 before the `#`, and the name of your branch after the `#`:
12+
13+
```sh
14+
npm run custom:dev https://github.com/yourUsername/p5.js.git#yourBranch
15+
```
16+
17+
This will build the reference from your branch and start a development preview of the website. A URL will be logged in the console that you can go to in your browser to test out your changes.
18+
19+
When you're done, you can run this command to reset your changes:
20+
```sh
21+
npm run custom:cleanup
22+
```
23+
724
## After a p5.js release
825

926
To update the content on the p5.js website following a new release of p5.js, you should run all of these commands to pull in the latest content. **Be sure to run the search indices script last as it depends on the results of the other scripts to be accurate.**

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"build:contributors": "tsx ./src/scripts/builders/people.ts",
1616
"build:reference": "tsx ./src/scripts/builders/reference.ts",
1717
"build:search": "tsx ./src/scripts/builders/search.ts",
18-
"build:p5-version": "tsx ./src/scripts/p5-version.ts"
18+
"build:p5-version": "tsx ./src/scripts/p5-version.ts",
19+
"custom:dev": "tsx ./src/scripts/branchTest.ts",
20+
"custom:cleanup": "tsx ./src/scripts/resetBranchTest.ts"
1921
},
2022
"dependencies": {
2123
"@astrojs/check": "^0.5.5",

src/content/reference/config.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ export const categories = [
2626
const paramSchema = z.object({
2727
name: z.string(),
2828
description: z.string(),
29-
type: z.string(),
30-
optional: z.boolean().optional(),
29+
type: z.string().optional(),
30+
optional: z.coerce.boolean().optional(),
3131
});
3232

3333
const returnSchema = z.object({
@@ -41,15 +41,15 @@ const exampleSchema = z.string();
4141
* Method schema for methods associated with a class in the Reference collection.
4242
*/
4343
const methodSchema = z.object({
44-
description: z.string(),
44+
description: z.string().optional(),
4545
path: z.string(),
4646
});
4747

4848
/**
4949
* Property schema for properties associated with a class in the Reference collection.
5050
*/
5151
const propertySchema = z.object({
52-
description: z.string(),
52+
description: z.string().optional(),
5353
path: z.string(),
5454
});
5555

@@ -78,6 +78,7 @@ export const referenceSchema = z.object({
7878
properties: z.record(propertySchema).optional(),
7979
isConstructor: z
8080
.boolean()
81+
.or(z.number().transform((n: number) => !!n))
8182
.or(z.literal("true").transform(() => true))
8283
.or(z.literal("false").transform(() => false))
8384
.optional(),

src/globals/globals.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export const sketchesPerPage = 12 as const;
1414
export const eventsPerPage = 12 as const;
1515

1616
export const cdnLibraryUrl =
17-
`https://cdn.jsdelivr.net/npm/p5@${p5Version}/lib/p5.min.js` as const;
17+
`/p5.min.js` as const;
18+
// `https://cdn.jsdelivr.net/npm/p5@${p5Version}/lib/p5.min.js` as const;
1819
export const fullDownloadUrl =
1920
`https://github.com/processing/p5.js/releases/download/v${p5Version}/p5.zip` as const;
2021
export const libraryDownloadUrl =

src/scripts/branchTest.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { execSync } from "child_process";
2+
import { existsSync, rmSync } from "fs";
3+
import path from "path";
4+
import { fileURLToPath } from "url";
5+
6+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
7+
8+
const url = process.argv[2];
9+
const match = /^(.+)#(.+)$/.exec(url);
10+
if (!match) {
11+
console.error(
12+
`Please pass a git URL followed by a # and then a branch name, tag, or commit as the parameter to this script, e.g. https://github.com/processing/p5.js.git#main`,
13+
);
14+
process.exit(1);
15+
}
16+
17+
const repoUrl = match[1];
18+
const branch = match[2];
19+
20+
const env = `P5_LIBRARY_PATH='/p5.min.js' P5_REPO_URL='${repoUrl}' P5_BRANCH='${branch}'`;
21+
22+
// First delete the existing cloned p5 to make sure we clone fresh
23+
const parsedP5Path = path.join(__dirname, "./parsers/in/p5.js/");
24+
if (existsSync(parsedP5Path)) {
25+
rmSync(parsedP5Path, { recursive: true });
26+
}
27+
28+
// Build the reference using the specified environment
29+
execSync(`${env} npm run build:reference`, { stdio: "inherit" });
30+
31+
// Run a dev server
32+
execSync(`${env} npm run dev`, { stdio: "inherit" });

src/scripts/builders/contribute.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import matter from "gray-matter";
1919
import { compile } from "@mdx-js/mdx";
2020
import isAbsoluteUrl from "is-absolute-url";
2121
import { nonDefaultSupportedLocales } from "@/src/i18n/const";
22+
import { p5Version } from "@/src/globals/p5-version";
2223

2324
/* Repo to pull the contributor documentation from */
2425
const docsRepoUrl = "https://github.com/processing/p5.js.git";
@@ -258,7 +259,7 @@ const moveContentDirectory = async (
258259
const buildContributorDocs = async () => {
259260
console.log("Building contributor docs...");
260261

261-
await cloneLibraryRepo(clonedRepoPath, docsRepoUrl);
262+
await cloneLibraryRepo(clonedRepoPath, docsRepoUrl, p5Version);
262263

263264
// Clean out previous files
264265
console.log("Cleaning out current content collection...");

src/scripts/builders/people.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from "../utils";
88
import { readFile } from "fs/promises";
99
import yaml from "js-yaml";
10+
import { p5Version } from "@/src/globals/p5-version";
1011

1112
/* Repo to pull the contributor documentation from */
1213
const sourceRepoUrl = "https://github.com/processing/p5.js.git";
@@ -24,7 +25,7 @@ const outputDirectory = path.join(
2425
const run = async () => {
2526
console.log("Copying people from p5.js website into people collection...");
2627

27-
await cloneLibraryRepo(clonedRepoPath, sourceRepoUrl);
28+
await cloneLibraryRepo(clonedRepoPath, sourceRepoUrl, p5Version);
2829

2930
const contents = await readFile(
3031
path.join(clonedRepoPath, ".all-contributorsrc"),

src/scripts/parsers/reference.ts

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { cloneLibraryRepo, readFile } from "../utils";
1+
import { cloneLibraryRepo, p5RepoUrl, readFile } from "../utils";
22
import fs from "fs/promises";
3-
import { exec } from "child_process";
3+
import { exec, execSync } from "child_process";
44
import path from "path";
55
import { fileURLToPath } from "url";
66
import type { ParsedLibraryReference } from "../../../types/parsers.interface";
7+
import { p5Version } from "@/src/globals/p5-version";
78

89
// Derive the directory name (__dirname equivalent) in ES Module scope
910
const __dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -18,13 +19,23 @@ const yuidocOutputPath = path.join(__dirname, "out")
1819
export const parseLibraryReference =
1920
async (): Promise<ParsedLibraryReference | null> => {
2021
// Clone p5.js
21-
await cloneLibraryRepo(localPath);
22-
// TODO(dave): let this happen via `npm run docs` in the p5 repo once we
23-
// merge the 2.0 branch
24-
await saveYuidocOutput('p5.js', 'data-p5', {
25-
flags: `--config ${path.join(__dirname, '../../../yuidoc.json')}`,
26-
inputPath: './src',
27-
});
22+
await cloneLibraryRepo(
23+
localPath,
24+
process.env.P5_REPO_URL || p5RepoUrl,
25+
process.env.P5_BRANCH || p5Version,
26+
{ shouldFixAbsolutePathInPreprocessor: false },
27+
);
28+
29+
// Install dependencies and build docs in the p5 repo
30+
await createP5Docs('p5.js', 'data-p5')
31+
32+
// If we're using a custom build of p5 instead of a public release, create
33+
// a build and copy it to the specified path
34+
if (process.env.P5_LIBRARY_PATH) {
35+
await createP5Build('p5.js', '../../../public' + process.env.P5_LIBRARY_PATH)
36+
}
37+
38+
// Copy the reference output so we can process it
2839
const p5Data = await getYuidocOutput('data-p5');
2940
if (!p5Data) throw new Error('Error generating p5 reference data!');
3041

@@ -128,6 +139,31 @@ export const saveYuidocOutput = async (
128139
}
129140
};
130141

142+
export const createP5Docs = async (inDirName: string, outDirName: string) => {
143+
execSync('npm install', {
144+
cwd: path.join(__dirname, 'in', inDirName),
145+
})
146+
execSync('npm run docs', {
147+
cwd: path.join(__dirname, 'in', inDirName),
148+
})
149+
const outputFilePath = path.join(yuidocOutputPath, outDirName);
150+
await fs.mkdir(outputFilePath, { recursive: true })
151+
await fs.cp(
152+
path.join(__dirname, 'in', inDirName, 'docs', 'reference', 'data.json'),
153+
path.join(outputFilePath, 'data.json'),
154+
)
155+
}
156+
157+
export const createP5Build = async (inDirName: string, outPath: string) => {
158+
execSync('npm run build', {
159+
cwd: path.join(__dirname, 'in', inDirName)
160+
})
161+
await fs.cp(
162+
path.join(__dirname, 'in', inDirName, 'lib', 'p5.min.js'),
163+
path.join(__dirname, outPath),
164+
)
165+
}
166+
131167
export async function combineYuidocData(
132168
inputData: ParsedLibraryReference[],
133169
outDirName: string

src/scripts/resetBranchTest.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { fileURLToPath } from "url";
2+
import path from "path";
3+
import { existsSync, rmSync } from "fs";
4+
import simpleGit from "simple-git";
5+
6+
async function main() {
7+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
8+
9+
const referencePath = path.join(__dirname, '../content/reference/');
10+
const dataPath = path.join(__dirname, '../../public/reference/data.json')
11+
rmSync(referencePath, { recursive: true });
12+
13+
const git = simpleGit();
14+
await git.checkout('HEAD', [referencePath, dataPath]);
15+
16+
const p5BuildPath = path.join(__dirname, '../../public/p5.min.js');
17+
if (existsSync(p5BuildPath)) {
18+
rmSync(p5BuildPath);
19+
}
20+
}
21+
22+
main().then(() => process.exit(0))

0 commit comments

Comments
 (0)