-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathconnect.ts
More file actions
571 lines (498 loc) · 17.3 KB
/
connect.ts
File metadata and controls
571 lines (498 loc) · 17.3 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
import * as commander from 'commander'
import fs from 'fs'
import { upload } from '../connect/upload'
import { validateDocs } from '../connect/validation'
import { createCodeConnectFromUrl } from '../connect/create'
import {
CodeConnectConfig,
CodeConnectExecutableParserConfig,
CodeConnectReactConfig,
ProjectInfo,
getProjectInfo,
getReactProjectInfo,
getRemoteFileUrl,
getTsProgram,
} from '../connect/project'
import { LogLevel, exitWithError, highlight, logger, success } from '../common/logging'
import { CodeConnectJSON } from '../connect/figma_connect'
import { convertStorybookFiles } from '../storybook/convert'
import { delete_docs } from '../connect/delete_docs'
import { runWizard } from '../connect/wizard/run_wizard'
import { callParser, handleMessages } from '../connect/parser_executables'
import { fromError } from 'zod-validation-error'
import { ParseRequestPayload, ParseResponsePayload } from '../connect/parser_executable_types'
import { parse } from 'yaml'
import z from 'zod'
import { withUpdateCheck } from '../common/updates'
import { exitWithFeedbackMessage } from '../connect/helpers'
import { parseHtmlDoc } from '../html/parser'
import {
InternalError,
isFigmaConnectFile,
parseCodeConnect,
ParseFn,
ParserError,
ResolveImportsFn,
} from '../connect/parser_common'
import { findAndResolveImports, parseReactDoc } from '../react/parser'
export type BaseCommand = commander.Command & {
token: string
verbose: boolean
outFile: string
outDir: string
config: string
dryRun: boolean
dir: string
jsonFile: string
skipUpdateCheck: boolean
exitOnUnreadableFiles: boolean
}
function addBaseCommand(command: commander.Command, name: string, description: string) {
return command
.command(name)
.description(description)
.usage('[options]')
.option('-r --dir <dir>', 'directory to parse')
.option('-t --token <token>', 'figma access token')
.option('-v --verbose', 'enable verbose logging for debugging')
.option('-o --outFile <file>', 'specify a file to output generated Code Connect')
.option('-o --outDir <dir>', 'specify a directory to output generated Code Connect')
.option('-c --config <path>', 'path to a figma config file')
.option('--skip-update-check', 'skips checking for an updated version of the Figma CLI')
.option(
'--exit-on-unreadable-files',
'exit if any Code Connect files cannot be parsed. We recommend using this option for CI/CD.',
)
.option('--dry-run', 'tests publishing without actually publishing')
.addHelpText(
'before',
'For feedback or bugs, please raise an issue: https://github.com/figma/code-connect/issues',
)
}
export function addConnectCommandToProgram(program: commander.Command) {
// Main command, invoked with `figma connect`
const connectCommand = addBaseCommand(program, 'connect', 'Figma Code Connect').action(
withUpdateCheck(runWizard),
)
// Sub-commands, invoked with e.g. `figma connect publish`
addBaseCommand(
connectCommand,
'publish',
'Run Code Connect locally to find any files that have figma connections and publish them to Figma. ' +
'By default this looks for a config file named "figma.config.json", and uses the `include` and `exclude` fields to determine which files to parse. ' +
'If no config file is found, this parses the current directory. An optional `--dir` flag can be used to specify a directory to parse.',
)
.option('--skip-validation', 'skip validation of Code Connect docs')
.option('-l --label <label>', 'label to apply to the published files')
.option('--include-template-files', 'flag to include any figma.template.js files')
.option(
'-b --batch-size <batch_size>',
'optional batch size (in number of documents) to use when uploading. Use this if you hit "request too large" errors. See README for more information.',
)
.action(withUpdateCheck(handlePublish))
addBaseCommand(
connectCommand,
'unpublish',
'Run to find any files that have figma connections and unpublish them from Figma. ' +
'By default this looks for a config file named "figma.config.json", and uses the `include` and `exclude` fields to determine which files to parse. ' +
'If no config file is found, this parses the current directory. An optional `--dir` flag can be used to specify a directory to parse.',
)
.option(
'--node <link_to_node>',
'specify the node to unpublish. This will unpublish for both React and Storybook.',
)
.option('-l --label <label>', 'label to unpublish for')
.option('--include-template-files', 'flag to include any figma.template.js files')
.action(withUpdateCheck(handleUnpublish))
addBaseCommand(
connectCommand,
'parse',
'Run Code Connect locally to find any files that have figma connections, then converts them to JSON and outputs to stdout.',
)
.option('-l --label <label>', 'label to apply to the parsed files')
.option('--include-template-files', 'flag to include any figma.template.js files')
.action(withUpdateCheck(handleParse))
addBaseCommand(
connectCommand,
'create',
'Generate a Code Connect file with boilerplate in the current directory for a Figma node URL',
)
.argument('<figma-node-url>', 'Figma node URL to create the Code Connect file from')
.action(withUpdateCheck(handleCreate))
}
export function getAccessToken(cmd: BaseCommand) {
return cmd.token ?? process.env.FIGMA_ACCESS_TOKEN
}
function getAccessTokenOrExit(cmd: BaseCommand) {
const token = getAccessToken(cmd)
if (!token) {
exitWithError(
`Couldn't find a Figma access token. Please provide one with \`--token <access_token>\` or set the FIGMA_ACCESS_TOKEN environment variable`,
)
}
return token
}
export function getDir(cmd: BaseCommand) {
return cmd.dir ?? process.cwd()
}
function setupHandler(cmd: BaseCommand) {
if (cmd.verbose) {
logger.setLogLevel(LogLevel.Debug)
}
}
type ParserDoc = z.infer<typeof ParseResponsePayload>['docs'][0]
function transformDocFromParser(
doc: ParserDoc,
remoteUrl: string,
config: CodeConnectConfig,
): ParserDoc {
let source = doc.source
if (source) {
try {
const url = new URL(source)
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
throw new Error('Invalid URL scheme')
}
} catch (e) {
source = getRemoteFileUrl(source, remoteUrl)
}
}
// TODO This logic is duplicated in parser.ts parseDoc due to some type issues
let figmaNode = doc.figmaNode
if (config.documentUrlSubstitutions) {
Object.entries(config.documentUrlSubstitutions).forEach(([from, to]) => {
figmaNode = figmaNode.replace(from, to)
})
}
return {
...doc,
source,
figmaNode,
}
}
const getRawFileData = (fileContent: string) => {
const [firstLine, ...templateLines] = fileContent.split('\n')
const delimeterStart = '/*---'
const delimeterEnd = '---*/'
if (firstLine !== delimeterStart) {
return {
template: templateLines.join('\n'),
figmaNode: firstLine.replace(/\/\/\s*url=/, '').trim(),
}
}
const nextDelimeterIndex = templateLines.findIndex((line) => line === delimeterEnd)
if (nextDelimeterIndex === -1) {
return {
template: '',
figmaNode: '',
} // invalid data
}
const data = templateLines.slice(0, nextDelimeterIndex).join('\n')
const { url: figmaNode, component, variant, links } = parse(data);
return {
component,
variant,
links,
figmaNode,
template: templateLines.slice(nextDelimeterIndex + 1).join('\n'),
};
};
export function parseRawFile(filePath: string, label: string | undefined): CodeConnectJSON {
const fileContent = fs.readFileSync(filePath, 'utf-8')
return {
...getRawFileData(fileContent),
// nestable by default unless user specifies otherwise
templateData: { nestable: true },
language: 'raw',
label: label || 'Code',
source: '',
sourceLocation: { line: -1 },
metadata: {
cliVersion: require('../../package.json').version,
},
}
}
export async function getCodeConnectObjects(
cmd: BaseCommand & { label?: string; includeTemplateFiles?: boolean },
projectInfo: ProjectInfo,
silent = false,
): Promise<CodeConnectJSON[]> {
if (cmd.jsonFile) {
try {
return JSON.parse(fs.readFileSync(cmd.jsonFile, 'utf8'))
} catch (e) {
logger.error('Failed to parse JSON file:', e)
}
}
let codeConnectObjects: CodeConnectJSON[] = []
if (projectInfo.config.parser === 'react') {
codeConnectObjects = await getReactCodeConnectObjects(
projectInfo as ProjectInfo<CodeConnectReactConfig>,
cmd,
silent,
)
} else if (projectInfo.config.parser === 'html') {
codeConnectObjects = await getCodeConnectObjectsFromParseFn({
parseFn: parseHtmlDoc,
fileExtension: 'ts',
projectInfo,
cmd,
silent,
})
} else {
const payload: ParseRequestPayload = {
mode: 'PARSE',
paths: projectInfo.files,
config: projectInfo.config,
}
try {
const stdout = await callParser(
// We use `as` because the React parser makes the types difficult
// TODO remove once React is an executable parser
projectInfo.config as CodeConnectExecutableParserConfig,
payload,
projectInfo.absPath,
)
const parsed = ParseResponsePayload.parse(stdout)
const { hasErrors } = handleMessages(parsed.messages)
if (hasErrors) {
exitWithError('Errors encountered calling parser, exiting')
}
codeConnectObjects = parsed.docs.map((doc) => ({
...transformDocFromParser(doc, projectInfo.remoteUrl, projectInfo.config),
metadata: {
cliVersion: require('../../package.json').version,
},
}))
} catch (e) {
// zod-validation-error formats the error message into a readable format
exitWithError(
`Error returned from parser: ${fromError(e)}. Try re-running the command with --verbose for more information.`,
)
}
}
if (cmd.includeTemplateFiles) {
const rawTemplateFiles = projectInfo.files.filter((f: string) =>
f.endsWith('.figma.template.js'),
)
const rawTemplateDocs = rawTemplateFiles.map((file) => parseRawFile(file, cmd.label))
codeConnectObjects = codeConnectObjects.concat(rawTemplateDocs)
}
if (cmd.label || projectInfo.config.label) {
logger.info(`Using label "${cmd.label || projectInfo.config.label}"`)
codeConnectObjects.forEach((doc) => {
doc.label = cmd.label || projectInfo.config.label || doc.label
})
}
return codeConnectObjects
}
type GetCodeConnectObjectsArgs = {
parseFn: ParseFn
resolveImportsFn?: ResolveImportsFn
fileExtension: string | string[]
projectInfo: ProjectInfo<CodeConnectConfig>
cmd: BaseCommand
silent?: boolean
}
// React/Storybook and HTML parsers are handled as special cases for now, they
// do not use the parser executable model but instead directly call a function
// in the code base. We may want to transition them to that model in future.
async function getCodeConnectObjectsFromParseFn({
/** The function used to parse a source file into a Code Connect object */
parseFn,
/** Optional function used to resolve imports in a source file */
resolveImportsFn,
/** The file extension to filter for when checking if files should be parsed */
fileExtension,
/** Project info */
projectInfo,
/** The commander command object */
cmd,
/** Silences console output */
silent = false,
}: GetCodeConnectObjectsArgs) {
const codeConnectObjects: CodeConnectJSON[] = []
const tsProgram = getTsProgram(projectInfo)
const { files, remoteUrl, config, absPath } = projectInfo
for (const file of files.filter((f: string) => isFigmaConnectFile(tsProgram, f, fileExtension))) {
try {
const docs = await parseCodeConnect({
program: tsProgram,
file,
config,
parseFn,
resolveImportsFn,
absPath,
parseOptions: {
repoUrl: remoteUrl,
debug: cmd.verbose,
silent,
},
})
codeConnectObjects.push(...docs)
if (!silent || cmd.verbose) {
logger.info(success(file))
}
} catch (e) {
if (!silent || cmd.verbose) {
logger.error(`❌ ${file}`)
if (e instanceof ParserError) {
if (cmd.verbose) {
console.trace(e)
} else {
logger.error(e.toString())
}
if (cmd.exitOnUnreadableFiles) {
logger.info('Exiting due to unreadable files')
process.exit(1)
}
} else {
if (cmd.verbose) {
console.trace(e)
} else {
logger.error(new InternalError(String(e)).toString())
}
}
}
}
}
return codeConnectObjects
}
async function getReactCodeConnectObjects(
projectInfo: ProjectInfo<CodeConnectReactConfig>,
cmd: BaseCommand,
silent = false,
) {
const codeConnectObjects = await getCodeConnectObjectsFromParseFn({
parseFn: parseReactDoc,
resolveImportsFn: findAndResolveImports,
fileExtension: ['tsx', 'jsx'],
projectInfo,
cmd,
silent,
})
const storybookCodeConnectObjects = await convertStorybookFiles({
projectInfo: getReactProjectInfo(projectInfo),
})
const allCodeConnectObjects = codeConnectObjects.concat(storybookCodeConnectObjects)
return allCodeConnectObjects
}
async function handlePublish(
cmd: BaseCommand & {
skipValidation: boolean
label: string
batchSize: string
},
) {
setupHandler(cmd)
let dir = getDir(cmd)
const projectInfo = await getProjectInfo(dir, cmd.config)
const codeConnectObjects = await getCodeConnectObjects(cmd, projectInfo)
if (codeConnectObjects.length === 0) {
logger.warn(
`No Code Connect files found in ${dir} - Make sure you have configured \`include\` and \`exclude\` in your figma.config.json file correctly, or that you are running in a directory that contains Code Connect files.`,
)
process.exit(0)
}
if (cmd.dryRun) {
logger.info(`Files that would be published:`)
logger.info(codeConnectObjects.map((o) => `- ${o.component} (${o.figmaNode})`).join('\n'))
}
const accessToken = getAccessTokenOrExit(cmd)
if (cmd.skipValidation) {
logger.info('Validation skipped')
} else {
logger.info('Validating Code Connect files...')
var start = new Date().getTime()
const valid = await validateDocs(cmd, accessToken, codeConnectObjects)
if (!valid) {
exitWithFeedbackMessage(1)
} else {
var end = new Date().getTime()
var time = end - start
logger.info(`All Code Connect files are valid (${time}ms)`)
}
}
if (cmd.dryRun) {
logger.info(`Dry run complete`)
process.exit(0)
}
let batchSize
if (cmd.batchSize) {
batchSize = parseInt(cmd.batchSize, 10)
if (isNaN(batchSize)) {
logger.error('Error: failed to parse batch-size. batch-size passed must be a number')
exitWithFeedbackMessage(1)
}
}
upload({ accessToken, docs: codeConnectObjects, batchSize: batchSize, verbose: cmd.verbose })
}
async function handleUnpublish(cmd: BaseCommand & { node: string; label: string }) {
setupHandler(cmd)
let dir = getDir(cmd)
if (cmd.dryRun) {
logger.info(`Files that would be unpublished:`)
}
let nodesToDeleteRelevantInfo
if (cmd.node) {
nodesToDeleteRelevantInfo = [
{ figmaNode: cmd.node, label: 'React' },
{ figmaNode: cmd.node, label: 'Storybook' },
]
} else {
const projectInfo = await getProjectInfo(dir, cmd.config)
const codeConnectObjects = await getCodeConnectObjects(cmd, projectInfo)
nodesToDeleteRelevantInfo = codeConnectObjects.map((doc) => ({
figmaNode: doc.figmaNode,
label: cmd.label || projectInfo.config.label || doc.label,
}))
if (cmd.label || projectInfo.config.label) {
logger.info(`Using label ${cmd.label || projectInfo.config.label}`)
}
if (cmd.dryRun) {
logger.info(`Dry run complete`)
process.exit(0)
}
}
const accessToken = getAccessTokenOrExit(cmd)
delete_docs({
accessToken,
docs: nodesToDeleteRelevantInfo,
})
}
async function handleParse(cmd: BaseCommand & { label: string }) {
setupHandler(cmd)
const dir = cmd.dir ?? process.cwd()
const projectInfo = await getProjectInfo(dir, cmd.config)
const codeConnectObjects = await getCodeConnectObjects(cmd, projectInfo)
if (cmd.dryRun) {
logger.info(`Dry run complete`)
process.exit(0)
}
if (cmd.outFile) {
fs.writeFileSync(cmd.outFile, JSON.stringify(codeConnectObjects, null, 2))
logger.info(`Wrote Code Connect JSON to ${highlight(cmd.outFile)}`)
} else {
// don't format the output, so it can be piped to other commands
console.log(JSON.stringify(codeConnectObjects, undefined, 2))
}
}
async function handleCreate(nodeUrl: string, cmd: BaseCommand) {
setupHandler(cmd)
const dir = cmd.dir ?? process.cwd()
const projectInfo = await getProjectInfo(dir, cmd.config)
if (cmd.dryRun) {
process.exit(0)
}
const accessToken = getAccessTokenOrExit(cmd)
return createCodeConnectFromUrl({
accessToken,
// We remove \s to allow users to paste URLs inside quotes - the terminal
// paste will add backslashes, which the quotes preserve, but expected user
// behaviour would be to strip the quotes
figmaNodeUrl: nodeUrl.replace(/\\/g, ''),
outFile: cmd.outFile,
outDir: cmd.outDir,
projectInfo,
})
}