Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"ts-jest": "^29.2.5",
"ts-loader": "^9.5.1",
"tsx": "^4.11.0",
"yaml": "^2.7.0",
"webpack": "^5.91.0",
"webpack-cli": "^5.1.4"
},
Expand Down
36 changes: 30 additions & 6 deletions cli/src/commands/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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'
Expand Down Expand Up @@ -187,15 +188,38 @@ function transformDocFromParser(
}
}

export function parseRawFile(filePath: string, label: string | undefined): CodeConnectJSON {
const fileContent = fs.readFileSync(filePath, 'utf-8')
const getRawFileData = (fileContent: string) => {
const [firstLine, ...templateLines] = fileContent.split('\n')
const figmaNodeUrl = firstLine.replace(/\/\/\s*url=/, '').trim()
const template = templateLines.join('\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 {
figmaNode: figmaNodeUrl,
template,
...getRawFileData(fileContent),
// nestable by default unless user specifies otherwise
templateData: { nestable: true },
language: 'raw',
Expand Down