Skip to content

Commit a5f85a7

Browse files
committed
Fix TypeScript typing for filterColor shader hook
1 parent 618e4c8 commit a5f85a7

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

utils/patch.mjs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ import fs from 'fs';
33
export function applyPatches() {
44
const cache = {};
55
const patched = {};
6-
6+
77
const replace = (path, src, dest) => {
88
if (Array.isArray(path)) {
99
path.forEach(path => replace(path, src, dest));
1010
return;
1111
}
1212
try {
13-
if (!path.startsWith("types/"))
14-
path = "types/" + path;
13+
if (!path.startsWith('types/'))
14+
path = 'types/' + path;
1515

1616
const before = patched[path] ??
17-
(cache[path] ??= fs.readFileSync("./" + path, { encoding: 'utf-8' }));
17+
(cache[path] ??= fs.readFileSync('./' + path, { encoding: 'utf-8' }));
1818
const after = before.replaceAll(src, dest);
1919

2020
if (after !== before)
@@ -28,8 +28,8 @@ export function applyPatches() {
2828

2929
// TODO: Handle this better in the docs instead of patching
3030
replace(
31-
"p5.d.ts",
32-
"constructor(detailX?: number, detailY?: number, callback?: Function);",
31+
'p5.d.ts',
32+
'constructor(detailX?: number, detailY?: number, callback?: Function);',
3333
`constructor(
3434
detailX?: number,
3535
detailY?: number,
@@ -39,9 +39,9 @@ export function applyPatches() {
3939
// https://github.com/p5-types/p5.ts/issues/31
4040
// #todo: add readonly to appropriate array params, either here or in doc comments
4141
replace(
42-
["p5.d.ts", "global.d.ts"],
43-
"random(choices: any[]): any;",
44-
"random<T>(choices: readonly T[]): T;"
42+
['p5.d.ts', 'global.d.ts'],
43+
'random(choices: any[]): any;',
44+
'random<T>(choices: readonly T[]): T;'
4545
);
4646

4747
replace(
@@ -53,7 +53,7 @@ export function applyPatches() {
5353
replace(
5454
'p5.d.ts',
5555
'textToContours(str: string, x: number, y: number, options?: { sampleFactor?: number; simplifyThreshold?: number }): object[][];',
56-
'textToContours(str: string, x: number, y: number, options?: { sampleFactor?: number; simplifyThreshold?: number }): { x: number; y: number; alpha: number }[][];',
56+
'textToContours(str: string, x: number, y: number, options?: { sampleFactor?: number; simplifyThreshold?: number }): { x: number; y: number; alpha: number }[][];'
5757
);
5858

5959
replace(
@@ -77,7 +77,7 @@ export function applyPatches() {
7777
'class __Graphics extends p5.Element {',
7878
`class __Graphics extends p5.Element {
7979
elt: HTMLCanvasElement;
80-
`,
80+
`
8181
);
8282

8383
// Type .elt more specifically for audio and video elements
@@ -86,24 +86,24 @@ export function applyPatches() {
8686
`class MediaElement extends Element {
8787
elt: HTMLAudioElement | HTMLVideoElement;`,
8888
`class MediaElement<T extends HTMLElement = HTMLAudioElement | HTMLVideoElement> extends Element {
89-
elt: T;`,
89+
elt: T;`
9090
);
9191
replace(
9292
['p5.d.ts', 'global.d.ts'],
9393
/createAudio\(src\?: string \| string\[\], callback\?: Function\): ([pP]5)\.MediaElement;/g,
94-
'createAudio(src?: string | string[], callback?: (video: $1.MediaElement<HTMLAudioElement>) => any): $1.MediaElement<HTMLAudioElement>;',
94+
'createAudio(src?: string | string[], callback?: (video: $1.MediaElement<HTMLAudioElement>) => any): $1.MediaElement<HTMLAudioElement>;'
9595
);
9696
replace(
9797
['p5.d.ts', 'global.d.ts'],
9898
/createVideo\(src\?: string \| string\[\], callback\?: Function\): ([pP]5)\.MediaElement;/g,
99-
'createVideo(src?: string | string[], callback?: (video: $1.MediaElement<HTMLVideoElement>) => any): $1.MediaElement<HTMLVideoElement>;',
99+
'createVideo(src?: string | string[], callback?: (video: $1.MediaElement<HTMLVideoElement>) => any): $1.MediaElement<HTMLVideoElement>;'
100100
);
101101

102102
// More callback types
103103
replace(
104104
['p5.d.ts', 'global.d.ts'],
105105
/createFileInput\(callback: Function, multiple\?: boolean\): ([pP]5)\.Element;/g,
106-
'createFileInput(callback: (input: $1.File) => any, multiple?: boolean): $1.Element;',
106+
'createFileInput(callback: (input: $1.File) => any, multiple?: boolean): $1.Element;'
107107
);
108108
replace(
109109
['p5.d.ts', 'global.d.ts'],
@@ -120,7 +120,7 @@ export function applyPatches() {
120120
replace(
121121
'p5.d.ts',
122122
'fontBounds(str: string, x: number, y: number, width?: number, height?: number): object;',
123-
'fontBounds(str: string, x: number, y: number, width?: number, height?: number): { x: number; y: number; w: number; h: number };',
123+
'fontBounds(str: string, x: number, y: number, width?: number, height?: number): { x: number; y: number; w: number; h: number };'
124124
);
125125
replace(
126126
'p5.d.ts',
@@ -130,7 +130,7 @@ export function applyPatches() {
130130
replace(
131131
'p5.d.ts',
132132
'textBounds(str: string, x: number, y: number, width?: number, height?: number): object;',
133-
'textBounds(str: string, x: number, y: number, width?: number, height?: number): { x: number; y: number; w: number; h: number };',
133+
'textBounds(str: string, x: number, y: number, width?: number, height?: number): { x: number; y: number; w: number; h: number };'
134134
);
135135

136136
// Document Typr
@@ -166,10 +166,25 @@ export function applyPatches() {
166166
`
167167
);
168168

169+
// Fix filterColor hook typing
170+
replace(
171+
['p5.d.ts'],
172+
'declare const filterColor: object;',
173+
`declare const filterColor: {
174+
texCoord: any;
175+
canvasSize: any;
176+
texelSize: any;
177+
canvasContent: any;
178+
begin(): void;
179+
end(): void;
180+
set(color: any): void;
181+
};`
182+
);
183+
169184
for (const [path, data] of Object.entries(patched)) {
170185
try {
171186
console.log(`Patched ${path}`);
172-
fs.writeFileSync("./" + path, data);
187+
fs.writeFileSync('./' + path, data);
173188
} catch (err) {
174189
console.error(err);
175190
}

0 commit comments

Comments
 (0)