@zseven-w/pen-codegen 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # @zseven-w/pen-codegen
2
+
3
+ Multi-platform code generators for [OpenPencil](https://github.com/nicepkg/openpencil) designs. Turn your design files into production-ready code for 8 frameworks.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @zseven-w/pen-codegen
9
+ ```
10
+
11
+ ## Supported Platforms
12
+
13
+ | Platform | Generator | Output |
14
+ |---|---|---|
15
+ | React + Tailwind | `generateReactCode` | `.tsx` with Tailwind classes |
16
+ | HTML + CSS | `generateHTMLCode` | Vanilla HTML/CSS |
17
+ | Vue 3 | `generateVueCode` | `.vue` SFC |
18
+ | Svelte | `generateSvelteCode` | `.svelte` component |
19
+ | Flutter | `generateFlutterCode` | Dart widget |
20
+ | SwiftUI | `generateSwiftUICode` | Swift view |
21
+ | Jetpack Compose | `generateComposeCode` | Kotlin composable |
22
+ | React Native | `generateReactNativeCode` | `.tsx` with StyleSheet |
23
+
24
+ ## Usage
25
+
26
+ Generate code from a single node:
27
+
28
+ ```ts
29
+ import { generateReactCode } from '@zseven-w/pen-codegen'
30
+
31
+ const code = generateReactCode(node, { indent: 2 })
32
+ ```
33
+
34
+ Generate from an entire document (resolves variables, computes layout):
35
+
36
+ ```ts
37
+ import { generateReactFromDocument } from '@zseven-w/pen-codegen'
38
+
39
+ const code = generateReactFromDocument(document)
40
+ ```
41
+
42
+ ### CSS Variables
43
+
44
+ Extract design variables as CSS custom properties:
45
+
46
+ ```ts
47
+ import { generateCSSVariables } from '@zseven-w/pen-codegen'
48
+
49
+ const css = generateCSSVariables(variables, themes)
50
+ // :root { --color-primary: #3b82f6; ... }
51
+ ```
52
+
53
+ ## License
54
+
55
+ MIT
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@zseven-w/pen-codegen",
3
+ "version": "0.0.1",
4
+ "description": "Multi-platform code generators for OpenPencil designs",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./src/index.ts",
9
+ "import": "./src/index.ts"
10
+ }
11
+ },
12
+ "files": [
13
+ "src"
14
+ ],
15
+ "scripts": {
16
+ "typecheck": "tsc --noEmit"
17
+ },
18
+ "dependencies": {
19
+ "@zseven-w/pen-types": "0.0.1",
20
+ "@zseven-w/pen-core": "0.0.1"
21
+ },
22
+ "devDependencies": {
23
+ "typescript": "^5.7.2"
24
+ }
25
+ }
@@ -0,0 +1,89 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import type { PenNode, PenDocument } from '@zseven-w/pen-types'
3
+ import { generateReactCode, generateReactFromDocument } from '../react-generator'
4
+ import { generateHTMLCode, generateHTMLFromDocument } from '../html-generator'
5
+ import { generateCSSVariables, variableNameToCSS } from '../css-variables-generator'
6
+
7
+ const simpleFrame: PenNode = {
8
+ id: 'f1',
9
+ type: 'frame',
10
+ name: 'Card',
11
+ x: 0, y: 0,
12
+ width: 300, height: 200,
13
+ fill: [{ type: 'solid', color: '#ffffff' }],
14
+ cornerRadius: 8,
15
+ children: [
16
+ {
17
+ id: 't1',
18
+ type: 'text',
19
+ content: 'Hello World',
20
+ fontSize: 16,
21
+ fontWeight: 600,
22
+ x: 16, y: 16,
23
+ },
24
+ ],
25
+ }
26
+
27
+ const docWithVars: PenDocument = {
28
+ version: '1.0.0',
29
+ variables: {
30
+ 'primary': { type: 'color', value: '#3b82f6' },
31
+ 'spacing': { type: 'number', value: 16 },
32
+ },
33
+ themes: { 'Theme-1': ['Light', 'Dark'] },
34
+ children: [simpleFrame],
35
+ }
36
+
37
+ describe('codegen', () => {
38
+ describe('variableNameToCSS', () => {
39
+ it('converts variable name to CSS custom property name', () => {
40
+ expect(variableNameToCSS('primary-color')).toBe('--primary-color')
41
+ })
42
+ })
43
+
44
+ describe('generateReactCode', () => {
45
+ it('generates React/Tailwind code for nodes', () => {
46
+ const code = generateReactCode([simpleFrame])
47
+ expect(code).toContain('Card')
48
+ expect(code).toContain('Hello World')
49
+ })
50
+ })
51
+
52
+ describe('generateReactFromDocument', () => {
53
+ it('generates from a document', () => {
54
+ const code = generateReactFromDocument(docWithVars)
55
+ expect(code).toContain('Hello World')
56
+ })
57
+ })
58
+
59
+ describe('generateHTMLCode', () => {
60
+ it('generates HTML and CSS', () => {
61
+ const { html, css } = generateHTMLCode([simpleFrame])
62
+ expect(html).toContain('Hello World')
63
+ expect(html).toContain('card')
64
+ expect(css).toBeTruthy()
65
+ })
66
+ })
67
+
68
+ describe('generateHTMLFromDocument', () => {
69
+ it('generates from a document with CSS variables', () => {
70
+ const { html, css } = generateHTMLFromDocument(docWithVars)
71
+ expect(html).toContain('Hello World')
72
+ expect(css).toBeTruthy()
73
+ })
74
+ })
75
+
76
+ describe('generateCSSVariables', () => {
77
+ it('generates CSS variables from document', () => {
78
+ const css = generateCSSVariables(docWithVars)
79
+ expect(css).toContain('--primary')
80
+ expect(css).toContain('#3b82f6')
81
+ })
82
+
83
+ it('returns empty for document without variables', () => {
84
+ const doc: PenDocument = { version: '1.0.0', children: [] }
85
+ const css = generateCSSVariables(doc)
86
+ expect(css).toContain('No design variables')
87
+ })
88
+ })
89
+ })