Skip to content

Commit 3769861

Browse files
committed
Wrap url
1 parent 75cd319 commit 3769861

File tree

3 files changed

+123
-101
lines changed

3 files changed

+123
-101
lines changed

src/__tests__/code.test.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@ import {
88
mock,
99
spyOn,
1010
} from 'bun:test'
11-
import { registerCodegen, run, runCommand } from '../code'
1211
import * as devupModule from '../commands/devup'
1312
import * as exportAssetsModule from '../commands/exportAssets'
1413
import * as exportComponentsModule from '../commands/exportComponents'
1514

16-
beforeAll(() => {
15+
let codeModule: typeof import('../code-impl')
16+
17+
beforeAll(async () => {
1718
;(globalThis as { figma?: unknown }).figma = {
1819
editorType: 'dev',
1920
mode: 'codegen',
2021
command: 'noop',
2122
codegen: { on: mock(() => {}) },
2223
closePlugin: mock(() => {}),
2324
} as unknown as typeof figma
25+
codeModule = await import('../code-impl')
2426
})
2527

2628
beforeEach(() => {
@@ -61,7 +63,7 @@ describe('runCommand', () => {
6163
closePlugin,
6264
} as unknown as typeof figma
6365

64-
await runCommand(figmaMock as typeof figma)
66+
await codeModule.runCommand(figmaMock as typeof figma)
6567

6668
switch (fn) {
6769
case 'exportDevup':
@@ -131,7 +133,7 @@ describe('registerCodegen', () => {
131133
codegen: { on: mock(() => {}) },
132134
closePlugin: mock(() => {}),
133135
} as unknown as typeof figma
134-
registerCodegen(figmaMock)
136+
codeModule.registerCodegen(figmaMock)
135137
expect(figmaMock.codegen.on).toHaveBeenCalledWith(
136138
'generate',
137139
expect.any(Function),
@@ -145,23 +147,38 @@ describe('registerCodegen', () => {
145147
})
146148
})
147149

148-
it('should not register codegen if figma is not defined', () => {
149-
run(undefined as unknown as typeof figma)
150+
it('should not register codegen if figma is not defined', async () => {
151+
codeModule.run(undefined as unknown as typeof figma)
150152
expect(devupModule.exportDevup).not.toHaveBeenCalled()
151153
expect(devupModule.importDevup).not.toHaveBeenCalled()
152154
expect(exportAssetsModule.exportAssets).not.toHaveBeenCalled()
153155
expect(exportComponentsModule.exportComponents).not.toHaveBeenCalled()
154156
})
155157

156-
it('should run command', () => {
158+
it('should run command', async () => {
157159
const figmaMock = {
158160
editorType: 'figma',
159161
command: 'export-devup',
160162
closePlugin: mock(() => {}),
161163
} as unknown as typeof figma
162-
run(figmaMock as typeof figma)
164+
codeModule.run(figmaMock as typeof figma)
163165
expect(devupModule.exportDevup).toHaveBeenCalledWith('json')
164166
expect(devupModule.importDevup).not.toHaveBeenCalled()
165167
expect(exportAssetsModule.exportAssets).not.toHaveBeenCalled()
166168
expect(exportComponentsModule.exportComponents).not.toHaveBeenCalled()
167169
})
170+
171+
it('auto-runs on module load when figma is present', async () => {
172+
const codegenOn = mock(() => {})
173+
;(globalThis as { figma?: unknown }).figma = {
174+
editorType: 'dev',
175+
mode: 'codegen',
176+
command: 'noop',
177+
codegen: { on: codegenOn },
178+
closePlugin: mock(() => {}),
179+
} as unknown as typeof figma
180+
181+
await import(`../code?with-figma=${Date.now()}`)
182+
183+
expect(codegenOn).toHaveBeenCalledWith('generate', expect.any(Function))
184+
})

src/code-impl.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { Codegen } from './codegen/Codegen'
2+
import { exportDevup, importDevup } from './commands/devup'
3+
import { exportAssets } from './commands/exportAssets'
4+
import { exportComponents } from './commands/exportComponents'
5+
6+
export function registerCodegen(ctx: typeof figma) {
7+
if (ctx.editorType === 'dev' && ctx.mode === 'codegen') {
8+
ctx.codegen.on('generate', async ({ node, language, ...rest }) => {
9+
console.info(rest, node)
10+
switch (language) {
11+
case 'devup-ui': {
12+
const time = Date.now()
13+
const codegen = new Codegen(node)
14+
await codegen.run()
15+
const componentsCodes = codegen.getComponentsCodes()
16+
console.info(`[benchmark] devup-ui end ${Date.now() - time}ms`)
17+
return [
18+
...(node.type === 'COMPONENT' ||
19+
node.type === 'COMPONENT_SET' ||
20+
node.type === 'INSTANCE'
21+
? []
22+
: [
23+
{
24+
title: node.name,
25+
language: 'TYPESCRIPT',
26+
code: codegen.getCode(),
27+
} as const,
28+
]),
29+
...(componentsCodes.length > 0
30+
? ([
31+
{
32+
title: `${node.name} - Components`,
33+
language: 'TYPESCRIPT',
34+
code: componentsCodes.map((code) => code[1]).join('\n\n'),
35+
},
36+
{
37+
title: `${node.name} - Components CLI`,
38+
language: 'BASH',
39+
code: componentsCodes
40+
.map(
41+
([componentName, code]) =>
42+
`echo '${code}' > ${componentName}.tsx`,
43+
)
44+
.join('\n'),
45+
},
46+
] as const)
47+
: []),
48+
]
49+
}
50+
}
51+
return []
52+
})
53+
}
54+
}
55+
56+
export function runCommand(ctx: typeof figma = figma) {
57+
switch (ctx.command) {
58+
case 'export-devup':
59+
exportDevup('json').finally(() => ctx.closePlugin())
60+
break
61+
case 'export-devup-without-treeshaking':
62+
exportDevup('json', false).finally(() => ctx.closePlugin())
63+
break
64+
case 'export-devup-excel':
65+
exportDevup('excel').finally(() => ctx.closePlugin())
66+
break
67+
case 'export-devup-excel-without-treeshaking':
68+
exportDevup('excel', false).finally(() => ctx.closePlugin())
69+
break
70+
case 'import-devup':
71+
importDevup('json').finally(() => ctx.closePlugin())
72+
break
73+
case 'import-devup-excel':
74+
importDevup('excel').finally(() => ctx.closePlugin())
75+
break
76+
case 'export-assets':
77+
exportAssets().finally(() => ctx.closePlugin())
78+
break
79+
case 'export-components':
80+
exportComponents().finally(() => ctx.closePlugin())
81+
break
82+
}
83+
}
84+
85+
export function run(ctx: typeof figma) {
86+
if (typeof ctx !== 'undefined') {
87+
registerCodegen(ctx)
88+
runCommand(ctx)
89+
}
90+
}
91+
92+
export function autoRun(ctx: typeof figma | undefined = figma) {
93+
if (typeof ctx !== 'undefined') {
94+
run(ctx)
95+
}
96+
}

src/code.ts

Lines changed: 2 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,3 @@
1-
import { Codegen } from './codegen/Codegen'
2-
import { exportDevup, importDevup } from './commands/devup'
3-
import { exportAssets } from './commands/exportAssets'
4-
import { exportComponents } from './commands/exportComponents'
1+
import { autoRun } from './code-impl'
52

6-
export function registerCodegen(ctx: typeof figma) {
7-
if (ctx.editorType === 'dev' && ctx.mode === 'codegen') {
8-
ctx.codegen.on('generate', async ({ node, language, ...rest }) => {
9-
console.info(rest, node)
10-
switch (language) {
11-
case 'devup-ui': {
12-
const time = Date.now()
13-
const codegen = new Codegen(node)
14-
await codegen.run()
15-
const componentsCodes = codegen.getComponentsCodes()
16-
console.info(`[benchmark] devup-ui end ${Date.now() - time}ms`)
17-
return [
18-
...(node.type === 'COMPONENT' ||
19-
node.type === 'COMPONENT_SET' ||
20-
node.type === 'INSTANCE'
21-
? []
22-
: [
23-
{
24-
title: node.name,
25-
language: 'TYPESCRIPT',
26-
code: codegen.getCode(),
27-
} as const,
28-
]),
29-
...(componentsCodes.length > 0
30-
? ([
31-
{
32-
title: `${node.name} - Components`,
33-
language: 'TYPESCRIPT',
34-
code: componentsCodes.map((code) => code[1]).join('\n\n'),
35-
},
36-
{
37-
title: `${node.name} - Components CLI`,
38-
language: 'BASH',
39-
code: componentsCodes
40-
.map(
41-
([componentName, code]) =>
42-
`echo '${code}' > ${componentName}.tsx`,
43-
)
44-
.join('\n'),
45-
},
46-
] as const)
47-
: []),
48-
]
49-
}
50-
}
51-
return []
52-
})
53-
}
54-
}
55-
56-
export function runCommand(ctx: typeof figma = figma) {
57-
switch (ctx.command) {
58-
case 'export-devup':
59-
exportDevup('json').finally(() => ctx.closePlugin())
60-
break
61-
case 'export-devup-without-treeshaking':
62-
exportDevup('json', false).finally(() => ctx.closePlugin())
63-
break
64-
case 'export-devup-excel':
65-
exportDevup('excel').finally(() => ctx.closePlugin())
66-
break
67-
case 'export-devup-excel-without-treeshaking':
68-
exportDevup('excel', false).finally(() => ctx.closePlugin())
69-
break
70-
case 'import-devup':
71-
importDevup('json').finally(() => ctx.closePlugin())
72-
break
73-
case 'import-devup-excel':
74-
importDevup('excel').finally(() => ctx.closePlugin())
75-
break
76-
case 'export-assets':
77-
exportAssets().finally(() => ctx.closePlugin())
78-
break
79-
case 'export-components':
80-
exportComponents().finally(() => ctx.closePlugin())
81-
break
82-
}
83-
}
84-
85-
export function run(ctx: typeof figma) {
86-
if (typeof ctx !== 'undefined') {
87-
registerCodegen(ctx)
88-
runCommand(ctx)
89-
}
90-
}
91-
92-
if (typeof figma !== 'undefined') {
93-
run(figma)
94-
}
3+
autoRun(typeof figma === 'undefined' ? undefined : figma)

0 commit comments

Comments
 (0)