Skip to content

Commit e7ab659

Browse files
committed
feat: support component, variant and links for raw file
Related to #262
1 parent cfed2bf commit e7ab659

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

cli/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"ts-jest": "^29.2.5",
8484
"ts-loader": "^9.5.1",
8585
"tsx": "^4.11.0",
86+
"yaml": "^2.7.0",
8687
"webpack": "^5.91.0",
8788
"webpack-cli": "^5.1.4"
8889
},

cli/src/commands/connect.ts

+30-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { runWizard } from '../connect/wizard/run_wizard'
2121
import { callParser, handleMessages } from '../connect/parser_executables'
2222
import { fromError } from 'zod-validation-error'
2323
import { ParseRequestPayload, ParseResponsePayload } from '../connect/parser_executable_types'
24+
import { parse } from 'yaml'
2425
import z from 'zod'
2526
import { withUpdateCheck } from '../common/updates'
2627
import { exitWithFeedbackMessage } from '../connect/helpers'
@@ -187,15 +188,38 @@ function transformDocFromParser(
187188
}
188189
}
189190

190-
export function parseRawFile(filePath: string, label: string | undefined): CodeConnectJSON {
191-
const fileContent = fs.readFileSync(filePath, 'utf-8')
191+
const getRawFileData = (fileContent: string) => {
192192
const [firstLine, ...templateLines] = fileContent.split('\n')
193-
const figmaNodeUrl = firstLine.replace(/\/\/\s*url=/, '').trim()
194-
const template = templateLines.join('\n')
193+
const delimeterStart = '/*---'
194+
const delimeterEnd = '---*/'
195+
if (firstLine !== delimeterStart) {
196+
return {
197+
template: templateLines.join('\n'),
198+
figmaNode: firstLine.replace(/\/\/\s*url=/, '').trim(),
199+
}
200+
}
201+
const nextDelimeterIndex = templateLines.findIndex((line) => line === delimeterEnd)
202+
if (nextDelimeterIndex === -1) {
203+
return {
204+
template: '',
205+
figmaNode: '',
206+
} // invalid data
207+
}
208+
const data = templateLines.slice(0, nextDelimeterIndex).join('\n')
209+
const { url: figmaNode, component, variant, links } = parse(data);
210+
return {
211+
component,
212+
variant,
213+
links,
214+
figmaNode,
215+
template: templateLines.slice(nextDelimeterIndex + 1).join('\n'),
216+
};
217+
};
195218

219+
export function parseRawFile(filePath: string, label: string | undefined): CodeConnectJSON {
220+
const fileContent = fs.readFileSync(filePath, 'utf-8')
196221
return {
197-
figmaNode: figmaNodeUrl,
198-
template,
222+
...getRawFileData(fileContent),
199223
// nestable by default unless user specifies otherwise
200224
templateData: { nestable: true },
201225
language: 'raw',

0 commit comments

Comments
 (0)