import { SDTFEngine, createSDTFEngine } from '@specifyapp/specify-design-token-format'; import type { DeriveBuiltInParserHandlerFromDefinition } from '../../internals/createBuiltInParserDefinition.js'; import type { ToScssVariablesParserDefinition } from './definition.js'; import { SpecifyError, specifyErrors } from '../../../errors/index.js'; import { merge } from 'lodash-es'; import { convertTokens } from './convertTokens.js'; import { DEFAULT_TEMPLATE } from '../../../converters/scss/utils/template.js'; export const toScssVariablesHandler: DeriveBuiltInParserHandlerFromDefinition< ToScssVariablesParserDefinition > = async (previousDataBox, toolbox, parserOptions, outputOptions, _context) => { let sdtfEngine: SDTFEngine; switch (previousDataBox.type) { case 'SDTF': { sdtfEngine = createSDTFEngine(previousDataBox.graph); break; } case 'SDTF Engine': { sdtfEngine = previousDataBox.engine; break; } default: { throw new SpecifyError({ errorKey: specifyErrors.PARSERS_ENGINE_INVALID_PARSER_INPUT.errorKey, publicMessage: `${ (previousDataBox as any).type } is not a valid input for the to-css-custom-properties parser.`, }); } } if (outputOptions?.type !== 'file') throw new SpecifyError({ errorKey: specifyErrors.PARSERS_ENGINE_INVALID_OUTPUT_TYPE.errorKey, publicMessage: `The output type ${outputOptions?.type} is not supported by the to-scss-variable parser.`, }); const defaultOptions = { tokenNameTemplate: DEFAULT_TEMPLATE, shouldRenderSingleMode: false, }; const finalOptions = merge(defaultOptions, parserOptions); toolbox.populateOutput({ type: 'files', files: [ { path: outputOptions.filePath, content: { type: 'text', text: convertTokens(sdtfEngine, finalOptions) }, }, ], }); return { type: 'SDTF Engine', engine: sdtfEngine, }; };