Skip to content

Commit 9b2743c

Browse files
authored
Merge pull request #7802 from limzykenneth/type-publishing
Publish types on NPM with next release
2 parents 1afa9fd + 2e71f41 commit 9b2743c

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

.github/workflows/release-workflow-v2.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ jobs:
4747
CI: true
4848
- name: Run build
4949
run: npm run build
50+
- name: Generate types
51+
run: npm run generate-types
5052

5153
# 2. Prepare release files
5254
- run: mkdir release && mkdir p5 && cp -r ./lib/* p5/

utils/generate-types.mjs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const data = JSON.parse(fs.readFileSync(path.join(__dirname, '../docs/data.json'
1313

1414
function findDtsFiles(dir, files = []) {
1515
// Only search in src directory
16-
const srcDir = path.join(__dirname, '../src');
16+
const srcDir = path.join(__dirname, '../types');
1717
if (!dir.startsWith(srcDir)) {
1818
dir = srcDir;
1919
}
@@ -26,7 +26,7 @@ function findDtsFiles(dir, files = []) {
2626
findDtsFiles(fullPath, files);
2727
} else if (entry.name.endsWith('.d.ts')) {
2828
// Get path relative to project root and normalize to forward slashes
29-
const relativePath = path.relative(path.join(__dirname, '..'), fullPath)
29+
const relativePath = path.relative(path.join(__dirname, '../types'), fullPath)
3030
.split(path.sep)
3131
.join('/');
3232
files.push(relativePath);
@@ -37,30 +37,18 @@ function findDtsFiles(dir, files = []) {
3737

3838
export function generateAllDeclarationFiles() {
3939
const { p5Types: rawP5Types, globalTypes, fileTypes } = generateTypeDefinitions(data);
40-
41-
// Add .d.ts references to p5Types
42-
let p5Types = '// This file is auto-generated from JSDoc documentation\n\n';
43-
p5Types += '/// <reference types="./global.d.ts" />\n';
44-
45-
// Add references to all other .d.ts files
46-
const dtsFiles = findDtsFiles(path.join(__dirname, '..'));
47-
for (const file of dtsFiles) {
48-
p5Types += `/// <reference path="../${file}" />\n`;
49-
}
50-
p5Types += '\n';
51-
p5Types += rawP5Types;
52-
5340
const typesDir = path.join(process.cwd(), 'types');
5441
fs.mkdirSync(typesDir, { recursive: true });
5542

56-
fs.writeFileSync(path.join(typesDir, 'p5.d.ts'), p5Types, 'utf8');
57-
fs.writeFileSync(path.join(typesDir, 'global.d.ts'), globalTypes, 'utf8');
58-
5943
// Write file-specific type definitions
6044
fileTypes.forEach((content, filePath) => {
6145
const parsedPath = path.parse(filePath);
62-
const relativePath = path.relative(process.cwd(), filePath);
46+
const relativePath = path.relative(
47+
path.join(__dirname, "../src"),
48+
filePath
49+
);
6350
const dtsPath = path.join(
51+
path.relative(process.cwd(), typesDir),
6452
path.dirname(relativePath),
6553
`${parsedPath.name}.d.ts`
6654
);
@@ -72,6 +60,21 @@ export function generateAllDeclarationFiles() {
7260
fs.writeFileSync(dtsPath, contentWithExport, 'utf8');
7361
console.log(`Generated ${dtsPath}`);
7462
});
63+
64+
// Add .d.ts references to p5Types
65+
let p5Types = '// This file is auto-generated from JSDoc documentation\n\n';
66+
p5Types += '/// <reference types="./global.d.ts" />\n';
67+
68+
// Add references to all other .d.ts files
69+
const dtsFiles = findDtsFiles(path.join(__dirname, '..'));
70+
for (const file of dtsFiles) {
71+
p5Types += `/// <reference path="./${file}" />\n`;
72+
}
73+
p5Types += '\n';
74+
p5Types += rawP5Types;
75+
76+
fs.writeFileSync(path.join(typesDir, 'p5.d.ts'), p5Types, 'utf8');
77+
fs.writeFileSync(path.join(typesDir, 'global.d.ts'), globalTypes, 'utf8');
7578
}
7679

77-
generateAllDeclarationFiles();
80+
generateAllDeclarationFiles();

utils/patch.mjs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ const replace = (path, src, dest) => {
1212
};
1313

1414
replace(
15-
"./src/core/structure.d.ts",
15+
"./types/core/structure.d.ts",
1616
"function p5(sketch: object, node: string | HTMLElement): void;",
1717
"function p5: typeof p5"
1818
);
1919

2020
replace(
21-
"./src/webgl/p5.Geometry.d.ts",
21+
"./types/webgl/p5.Geometry.d.ts",
2222
"constructor(detailX?: number, detailY?: number, callback?: function);",
2323
`constructor(
2424
detailX?: number,
@@ -33,15 +33,9 @@ replace(
3333

3434
// https://github.com/p5-types/p5.ts/issues/31
3535
replace(
36-
"./src/math/random.d.ts",
36+
"./types/math/random.d.ts",
3737
"function random(choices: Array): any;",
3838
"function random<T>(choices: T[]): T;"
3939
);
4040

41-
replace(
42-
"./src/utilities/array_functions.d.ts",
43-
"function append(array: Array, value: Any): Array;",
44-
"function append<T>(array: T[], value: T): T[];"
45-
);
46-
4741

0 commit comments

Comments
 (0)