Skip to content

Publish types on NPM with next release #7802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/release-workflow-v2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ jobs:
CI: true
- name: Run build
run: npm run build
- name: Generate types
run: npm run generate-types

# 2. Prepare release files
- run: mkdir release && mkdir p5 && cp -r ./lib/* p5/
Expand Down
43 changes: 23 additions & 20 deletions utils/generate-types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const data = JSON.parse(fs.readFileSync(path.join(__dirname, '../docs/data.json'

function findDtsFiles(dir, files = []) {
// Only search in src directory
const srcDir = path.join(__dirname, '../src');
const srcDir = path.join(__dirname, '../types');
if (!dir.startsWith(srcDir)) {
dir = srcDir;
}
Expand All @@ -26,7 +26,7 @@ function findDtsFiles(dir, files = []) {
findDtsFiles(fullPath, files);
} else if (entry.name.endsWith('.d.ts')) {
// Get path relative to project root and normalize to forward slashes
const relativePath = path.relative(path.join(__dirname, '..'), fullPath)
const relativePath = path.relative(path.join(__dirname, '../types'), fullPath)
.split(path.sep)
.join('/');
files.push(relativePath);
Expand All @@ -37,30 +37,18 @@ function findDtsFiles(dir, files = []) {

export function generateAllDeclarationFiles() {
const { p5Types: rawP5Types, globalTypes, fileTypes } = generateTypeDefinitions(data);

// Add .d.ts references to p5Types
let p5Types = '// This file is auto-generated from JSDoc documentation\n\n';
p5Types += '/// <reference types="./global.d.ts" />\n';

// Add references to all other .d.ts files
const dtsFiles = findDtsFiles(path.join(__dirname, '..'));
for (const file of dtsFiles) {
p5Types += `/// <reference path="../${file}" />\n`;
}
p5Types += '\n';
p5Types += rawP5Types;

const typesDir = path.join(process.cwd(), 'types');
fs.mkdirSync(typesDir, { recursive: true });

fs.writeFileSync(path.join(typesDir, 'p5.d.ts'), p5Types, 'utf8');
fs.writeFileSync(path.join(typesDir, 'global.d.ts'), globalTypes, 'utf8');

// Write file-specific type definitions
fileTypes.forEach((content, filePath) => {
const parsedPath = path.parse(filePath);
const relativePath = path.relative(process.cwd(), filePath);
const relativePath = path.relative(
path.join(__dirname, "../src"),
filePath
);
const dtsPath = path.join(
path.relative(process.cwd(), typesDir),
path.dirname(relativePath),
`${parsedPath.name}.d.ts`
);
Expand All @@ -72,6 +60,21 @@ export function generateAllDeclarationFiles() {
fs.writeFileSync(dtsPath, contentWithExport, 'utf8');
console.log(`Generated ${dtsPath}`);
});

// Add .d.ts references to p5Types
let p5Types = '// This file is auto-generated from JSDoc documentation\n\n';
p5Types += '/// <reference types="./global.d.ts" />\n';

// Add references to all other .d.ts files
const dtsFiles = findDtsFiles(path.join(__dirname, '..'));
for (const file of dtsFiles) {
p5Types += `/// <reference path="./${file}" />\n`;
}
p5Types += '\n';
p5Types += rawP5Types;

fs.writeFileSync(path.join(typesDir, 'p5.d.ts'), p5Types, 'utf8');
fs.writeFileSync(path.join(typesDir, 'global.d.ts'), globalTypes, 'utf8');
}

generateAllDeclarationFiles();
generateAllDeclarationFiles();
12 changes: 3 additions & 9 deletions utils/patch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ const replace = (path, src, dest) => {
};

replace(
"./src/core/structure.d.ts",
"./types/core/structure.d.ts",
"function p5(sketch: object, node: string | HTMLElement): void;",
"function p5: typeof p5"
);

replace(
"./src/webgl/p5.Geometry.d.ts",
"./types/webgl/p5.Geometry.d.ts",
"constructor(detailX?: number, detailY?: number, callback?: function);",
`constructor(
detailX?: number,
Expand All @@ -33,15 +33,9 @@ replace(

// https://github.com/p5-types/p5.ts/issues/31
replace(
"./src/math/random.d.ts",
"./types/math/random.d.ts",
"function random(choices: Array): any;",
"function random<T>(choices: T[]): T;"
);

replace(
"./src/utilities/array_functions.d.ts",
"function append(array: Array, value: Any): Array;",
"function append<T>(array: T[], value: T): T[];"
);