Skip to content
This repository was archived by the owner on Jul 13, 2024. It is now read-only.

Commit 8620f41

Browse files
committed
#30 basic dtif-to-svg package setup with async test pain
1 parent 80ad68a commit 8620f41

File tree

12 files changed

+571
-5
lines changed

12 files changed

+571
-5
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"./packages/core-client",
2525
"./packages/discord-handler",
2626
"./packages/dtif-to-react",
27+
"./packages/dtif-to-svg",
2728
"./packages/etsy-client",
2829
"./packages/figma-handler",
2930
"./packages/figma-to-dtif",

packages/dtif-to-svg/.eslintrc.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const OFF = 0;
2+
const WARNING = 1;
3+
const ERROR = 2;
4+
5+
/** @type {import('eslint').Linter.Config} */
6+
module.exports = {
7+
root: true,
8+
extends: ['pda-base'],
9+
};

packages/dtif-to-svg/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# `dtif-to-svg`
2+
Convert DTIF to SVG using D3.js.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const esModules = ['d3', 'd3-selection'].join('|');
2+
3+
/** @type {import('jest').Config} */
4+
module.exports = {
5+
preset: '@pda/jest-presets/jest/node',
6+
moduleNameMapper: {
7+
// https://stackoverflow.com/questions/69075510/jest-tests-failing-on-d3-import
8+
'^d3-(.*)$': '<rootDir>/node_modules/d3-$1/dist/d3-$1.min.js',
9+
},
10+
};

packages/dtif-to-svg/package.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "@pda/dtif-to-svg",
3+
"version": "0.0.0",
4+
"description": "Provides utilities to convert the Design Tree Interchange Format (DTIF) into SVG.",
5+
"private": true,
6+
"source": "./src/index.ts",
7+
"main": "./dist/cjs/index.js",
8+
"module": "./dist/esm/index.js",
9+
"types": "./dist/index.d.ts",
10+
"scripts": {
11+
"build": "shx rm -rf dist && npx esno ../../scripts/build-ts-package.ts --prod",
12+
"start:dev": "tsc -w",
13+
"lint": "eslint --cache \"**/*.{js,ts}\"",
14+
"clean": "shx rm -rf dist && shx rm -rf node_modules && shx rm -rf .turbo",
15+
"install:clean": "pnpm run clean && pnpm install",
16+
"test": "jest"
17+
},
18+
"keywords": [],
19+
"author": "@bennodev19",
20+
"license": "MIT",
21+
"dependencies": {
22+
"d3-selection": "^3.0.0"
23+
},
24+
"peerDependencies": {
25+
"jsdom": "^22.1.0"
26+
},
27+
"devDependencies": {
28+
"@pda/jest-presets": "workspace:*",
29+
"@pda/tsconfig": "workspace:*",
30+
"@types/d3": "^7.4.0",
31+
"@types/d3-selection": "^3.0.5",
32+
"@types/jest": "^29.5.1",
33+
"@types/node": "^18.15.13",
34+
"eslint": "^8.38.0",
35+
"eslint-config-pda-base": "workspace:*",
36+
"jest": "^29.5.0",
37+
"jsdom": "^22.1.0",
38+
"typescript": "^4.9.5"
39+
},
40+
"files": [
41+
"dist",
42+
"README.md"
43+
]
44+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { dtifToSVG } from '..';
2+
3+
describe('dtif-to-svg library tests', async () => {
4+
it('should work', async () => {
5+
// Arrange
6+
const dtif = '';
7+
8+
// Act
9+
const result = await dtifToSVG();
10+
11+
// Assert
12+
expect(result).toBe('');
13+
});
14+
});

packages/dtif-to-svg/src/d3.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const d3 = (() => {
2+
let d3: typeof import('d3-selection');
3+
4+
async function getD3() {
5+
if (d3 == null) {
6+
// Check if running in Node.js environment
7+
if (typeof (globalThis as any).window === 'undefined') {
8+
const { JSDOM } = await import('jsdom');
9+
const { window } = new JSDOM();
10+
(globalThis as any).window = window;
11+
(globalThis as any).document = window.document;
12+
}
13+
14+
// Import required D3 modules
15+
d3 = (await Promise.all([import('d3-selection')]).then((d3) =>
16+
Object.assign({}, ...d3)
17+
)) as any;
18+
}
19+
20+
return d3;
21+
}
22+
23+
return getD3;
24+
})();
25+
26+
export { d3 };

packages/dtif-to-svg/src/index.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { d3 } from './d3';
2+
3+
export async function dtifToSVG() {
4+
const svg = (await d3())
5+
.select('body')
6+
.append('svg')
7+
.attr('width', 500)
8+
.attr('height', 500);
9+
10+
const defs = svg.append('defs');
11+
12+
const gradient = defs
13+
.append('linearGradient')
14+
.attr('id', 'gradient')
15+
.attr('x1', '0%')
16+
.attr('y1', '0%')
17+
.attr('x2', '100%')
18+
.attr('y2', '100%');
19+
20+
gradient.append('stop').attr('offset', '0%').attr('stop-color', 'blue');
21+
22+
gradient.append('stop').attr('offset', '100%').attr('stop-color', 'red');
23+
24+
for (let i = 0; i < 100; i++) {
25+
svg
26+
.append('rect')
27+
.attr('x', i * 5)
28+
.attr('y', i * 5)
29+
.attr('width', 50)
30+
.attr('height', 50)
31+
.attr('fill', 'url(#gradient)');
32+
}
33+
34+
return (await d3()).select('body').html();
35+
}

packages/dtif-to-svg/tsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "./dist",
4+
"rootDir": "./src"
5+
},
6+
"extends": "@pda/tsconfig/shared-library.json",
7+
"include": ["src"],
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig",
3+
"exclude": [
4+
"**/__tests__/*",
5+
"**/*.test.ts"
6+
]
7+
}

0 commit comments

Comments
 (0)