import { SkyConfig } from '@statelyai/sky'; import { existsSync } from 'fs'; import * as fs from 'fs/promises'; import * as path from 'path'; import * as prettier from 'prettier'; const getPathToSave = (filePath: string) => filePath.slice(0, -path.extname(filePath).length) + '.sky.ts'; export const doesSkyConfigExist = (filePath: string) => { const pathToSave = getPathToSave(filePath); return existsSync(pathToSave); }; // The exported code from the sky config file has an initial export statement that we don't want function removeLeadingExport(code: string) { const regex = /export (const machine = createMachine)/; return code.replace(regex, '$1'); } export const writeSkyConfig = async (opts: { filePath: string; skyConfig: SkyConfig; createTypeGenFile: | ((uriArray: string[], { cwd }: { cwd: string }) => Promise) | undefined; }) => { const { filePath, skyConfig } = opts; if ('error' in skyConfig) { console.error(skyConfig.error); return; } const prettierConfig = await prettier.resolveConfig(filePath); const pathToSave = getPathToSave(filePath); const code = removeLeadingExport(skyConfig.configString); const machineFile = `// This file was generated by the XState CLI, please do not edit it manually. ${code}; export const skyConfig = { actorId:'${skyConfig.actorId}', machine };`; await fs.writeFile( pathToSave, prettier.format(machineFile, { ...prettierConfig, parser: 'typescript', }), ); // Run typegen if it's provided if (opts.createTypeGenFile) await opts.createTypeGenFile([pathToSave], { cwd: __dirname }); };