Skip to content

Commit

Permalink
feat(types): adds generation of type definition file
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Maddalone committed Dec 16, 2023
1 parent 383a238 commit edc8084
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 2 deletions.
78 changes: 78 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Expand Up @@ -12,12 +12,13 @@
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"browser": "dist/index.js",
"types": "dist/react-svg-path.d.ts",
"engines": {
"node": ">=10"
},
"scripts": {
"commit": "git-cz",
"build": "rollup -c",
"build": "rollup -c && npm run makedef && npm run copydef",
"start": "rollup -c -w",
"prepublish": "run-s build",
"test": "run-s test:unit test:lint",
Expand All @@ -27,7 +28,9 @@
"test:watch": "react-scripts test --env=jsdom",
"test:coverage": "cross-env CI=1 react-scripts test --coverage --watchAll=false",
"predeploy": "cd example && npm install && npm run build",
"semantic-release": "semantic-release"
"semantic-release": "semantic-release",
"copydef": "node scripts/copyDefinitions.js",
"makedef": "node scripts/makeDefinitions.mjs"
},
"peerDependencies": {
"react": ">=16.0",
Expand All @@ -43,6 +46,8 @@
"@rollup/plugin-commonjs": "^19.0.0",
"@rollup/plugin-node-resolve": "^13.0.0",
"@testing-library/react": "^11.2.7",
"@types/react": "^18.0.35",
"@types/react-dom": "^18.0.11",
"babel-eslint": "10.1.0",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"commitizen": "^4.2.4",
Expand Down
62 changes: 62 additions & 0 deletions scripts/makeDefinitions.mjs
@@ -0,0 +1,62 @@
import fs from 'fs';
import { basicShapes, curves } from '../src/docs/docs.mjs';

const mapType = (t) => {
switch (t) {
case '2d-array':
return 'any[][]';
case 'point-array':
return 'number[][]';
default:
return t;
}
};

const definitions = [];
for (const doc in basicShapes) {
const shape = basicShapes[doc];
const props = shape.props;
definitions.push(
`export interface ${shape.Component}Props extends SVGAttributes<SVGElement> {`
);
for (const prop in props) {
const p = props[prop];
definitions.push(` ${prop}?: ${mapType(p.type)};`);
}
definitions.push(`}`);
definitions.push(``);
definitions.push(
`export declare const ${shape.Component}: React.FC<${shape.Component}Props>;`
);
definitions.push(``);
}

for (const doc in curves) {
const shape = curves[doc];
const props = shape.props;
definitions.push(
`export interface ${shape.Component}Props extends SVGAttributes<SVGElement> {`
);
for (const prop in props) {
const p = props[prop];
definitions.push(` ${prop}?: ${mapType(p.type)};`);
}
definitions.push(`}`);
definitions.push(``);
definitions.push(
`export declare const ${shape.Component}: React.FC<${shape.Component}Props>;`
);
definitions.push(``);
}

definitions.unshift(`import { SVGAttributes } from "react";`);

const content = definitions.join('\n');

// read the contents of ../node_modules/@joemaddalone/path/dist/path.d.ts
const pathContent = fs.readFileSync(
'../node_modules/@joemaddalone/path/dist/path.d.ts',
'utf8'
);

fs.writeFileSync('../dist/react-svg-path.d.ts', `${content}\n\n${pathContent}`);

0 comments on commit edc8084

Please sign in to comment.