-
Notifications
You must be signed in to change notification settings - Fork 45
/
test-usage.sh
executable file
·205 lines (174 loc) · 4.01 KB
/
test-usage.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env bash
set -xueo pipefail
rm -rf test-artifacts
mkdir -p test-artifacts
pushd test-artifacts
echo "Running test 1 (include types in typeRoots so all types are available ambiently)"
mkdir test-1
pushd test-1
cat > package.json << EOF
{}
EOF
cat > tsconfig.json << EOF
{
"compilerOptions": {
"target": "es6",
"lib": ["es6"],
"strict": true,
"typeRoots": [
"./node_modules/@types",
"./node_modules/@figma"
]
}
}
EOF
cat > code.ts << EOF
/**
* Test file to make sure that plugin-typings work as expected.
*/
function main() {
// figma globals is there
if (figma.command && figma.editorType) {
// console.log works
console.log(figma.root, figma.currentPage)
// setTimeout works
setTimeout(() => {
figma.currentPage.selection = [];
}, 100)
// setInterval works too
clearTimeout(setInterval(() => {
figma.ui.onmessage = msg => {}
}, 100))
}
}
function testErrorFn(): void {
// @ts-expect-error
if (figma.NON_EXISTENT_ATTR) {
}
}
// Node types are available as globals
function testFn1(node: SceneNode): node is FrameNode {
return node.type === 'FRAME'
}
// Paint types are available as globals too
function testFn(paint: SolidPaint | GradientPaint | ImagePaint | null): boolean {
return !!paint
}
function testNotify() {
figma.notify("normal")
figma.notify("error", {error: true})
figma.notify("timeout", {timeout: 10000})
figma.notify("Infinity", {timeout: Infinity})
figma.notify("button", {button: {
text: "button",
action: () => {
return false
}
}})
figma.notify("button", {
button: {
text: "button",
action: () => {},
}
})
figma.notify("onDequeue", {
onDequeue: (reason: NotifyDequeueReason) => {
switch (reason) {
case 'timeout':
break;
case 'dismiss':
break;
case 'action_button_click':
break;
default:
function assertNever(x: never): never {
throw new Error("Unexpected object: " + x);
}
assertNever(reason);
break;
}
}
})
}
function testCodegen() {
figma.codegen.on("generate", async () => {
return [
{
language: "TYPESCRIPT",
code: JSON.stringify(figma.codegen.preferences.unit),
title: "Code snippet 1"
},
{
language: "JAVASCRIPT",
code: JSON.stringify(figma.codegen.preferences.scaleFactor),
title: "Code snippet 2"
},
{
language: "JAVASCRIPT",
code: JSON.stringify(figma.codegen.preferences.customSettings),
title: "Code snippet 3"
}
]
})
}
function testFindAllWithCriteria() {
const nodes = figma.root.findAllWithCriteria({ types: ["TEXT"] });
console.log(nodes.map(n => {
return [n.id, n.characters]
}))
const frame = figma.createFrame()
const nodes2 = frame.findAllWithCriteria({ sharedPluginData: { namespace: 'foo' } })
console.log(nodes2[0].id)
}
function testParameters() {
figma.on('run', async ({ command, parameters }) => {
console.log(command, parameters)
})
}
EOF
npm install typescript
npm install ../../
npx tsc --noEmit
popd
echo "Running test 2 (import and use types selectively)"
mkdir test-2
pushd test-2
cat > package.json << EOF
{}
EOF
cat > tsconfig.json << EOF
{
"compilerOptions": {
"moduleResolution": "bundler",
"target": "es6",
"lib": ["es6"],
"strict": true,
"typeRoots": []
}
}
EOF
cat > code.ts << EOF
/**
* Test file to test plugin-api-standalone.d.ts
*/
import { SceneNode, FrameNode, GradientPaint } from "@figma/plugin-typings/plugin-api-standalone"
function isFrameNode(x: SceneNode): x is FrameNode {
return x.type === "FRAME"
}
const gradient: GradientPaint = {
type: "GRADIENT_LINEAR",
gradientTransform: [[0, 0 ,0], [0, 0 ,0]],
gradientStops: [
{
position: 0,
color: { r: 1, g: 1, b: 1, a: 1 }
}
]
}
// @ts-expect-error No ambient types
type VectorAlias = Vector
EOF
npm install typescript
npm install ../../
npx tsc --noEmit
popd