|
| 1 | +import chalkTemplate from 'chalk-template'; |
| 2 | +import { readFileSync, writeFileSync } from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import { boards } from '../project/boards.js'; |
| 5 | +import { createDiagram } from '../project/createDiagram.js'; |
| 6 | +import { findBoard } from '../project/findBoard.js'; |
| 7 | +import { createWokwiToml } from '../WokwiConfig.js'; |
| 8 | +import { type ESPIDFProjectDescription } from './projectDescription.js'; |
| 9 | + |
| 10 | +type ICreateConfigForIDFProjectParams = Pick< |
| 11 | + ESPIDFProjectDescription, |
| 12 | + 'build_dir' | 'project_path' | 'app_elf' |
| 13 | +>; |
| 14 | + |
| 15 | +export function createConfigForIDFProject(idfProjectDescription: ICreateConfigForIDFProjectParams) { |
| 16 | + // eslint-disable-next-line @typescript-eslint/naming-convention |
| 17 | + const { build_dir, project_path, app_elf } = idfProjectDescription; |
| 18 | + const relativeBuildDir = path.relative(project_path, build_dir); |
| 19 | + |
| 20 | + return createWokwiToml({ |
| 21 | + firmwarePath: relativeBuildDir + '/' + 'flasher_args.json', |
| 22 | + elfPath: relativeBuildDir + '/' + app_elf, |
| 23 | + gdbServerPort: 3333, |
| 24 | + }); |
| 25 | +} |
| 26 | + |
| 27 | +type ICreateDiagramForIDFProjectParams = Pick<ESPIDFProjectDescription, 'target'>; |
| 28 | + |
| 29 | +export function createDiagramForIDFProject( |
| 30 | + idfProjectDescription: ICreateDiagramForIDFProjectParams, |
| 31 | +) { |
| 32 | + const { target } = idfProjectDescription; |
| 33 | + const board = boards.find((b) => b.idfTarget === target); |
| 34 | + if (!board) { |
| 35 | + throw new Error( |
| 36 | + `Target ${target} is not currently supported by Wokwi. You can create a feature request at https://github.com/wokwi/wokwi-features/issues.`, |
| 37 | + ); |
| 38 | + } |
| 39 | + return createDiagram(board.board); |
| 40 | +} |
| 41 | + |
| 42 | +export interface IDFProjectConfigParams { |
| 43 | + rootDir: string; |
| 44 | + configPath: string; |
| 45 | + diagramFilePath: string; |
| 46 | + projectDescriptionPath: string; |
| 47 | + createConfig?: boolean; |
| 48 | + createDiagram?: boolean; |
| 49 | + quiet?: boolean; |
| 50 | +} |
| 51 | + |
| 52 | +export function idfProjectConfig(params: IDFProjectConfigParams) { |
| 53 | + const { |
| 54 | + rootDir, |
| 55 | + configPath, |
| 56 | + diagramFilePath, |
| 57 | + projectDescriptionPath, |
| 58 | + createConfig, |
| 59 | + createDiagram, |
| 60 | + quiet, |
| 61 | + } = params; |
| 62 | + const espIdfProjectDescriptionContent = readFileSync(projectDescriptionPath, 'utf8'); |
| 63 | + const idfProjectDescription = JSON.parse(espIdfProjectDescriptionContent); |
| 64 | + if (createConfig) { |
| 65 | + const wokwiConfig = createConfigForIDFProject(idfProjectDescription); |
| 66 | + writeFileSync(configPath, wokwiConfig); |
| 67 | + if (!quiet) { |
| 68 | + console.log( |
| 69 | + chalkTemplate`Created default {yellow wokwi.toml} for IDF project in {yellow ${rootDir}}.`, |
| 70 | + ); |
| 71 | + } |
| 72 | + } |
| 73 | + if (!createDiagram) { |
| 74 | + const diagramContent = readFileSync(diagramFilePath, 'utf8'); |
| 75 | + const diagram = JSON.parse(diagramContent); |
| 76 | + const board = findBoard(diagram); |
| 77 | + const boardInfo = boards.find((b) => b.board === board?.type); |
| 78 | + if (boardInfo && boardInfo.idfTarget !== idfProjectDescription.target) { |
| 79 | + console.error( |
| 80 | + chalkTemplate`{red Error:} The IDF project is targeting {yellow ${idfProjectDescription.target}}, but the diagram is for {yellow ${boardInfo.idfTarget}}.`, |
| 81 | + ); |
| 82 | + console.error( |
| 83 | + chalkTemplate`You can use the {green --diagram-file} option to specify a diagram for a different board, or delete the {yellow diagram.json} file to automatically create a default diagram.`, |
| 84 | + ); |
| 85 | + return false; |
| 86 | + } |
| 87 | + } else { |
| 88 | + const diagram = createDiagramForIDFProject(idfProjectDescription); |
| 89 | + writeFileSync(diagramFilePath, JSON.stringify(diagram, null, 2)); |
| 90 | + if (!quiet) { |
| 91 | + console.log( |
| 92 | + chalkTemplate`Created default {yellow diagram.json} for IDF project in {yellow ${rootDir}}.`, |
| 93 | + ); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return true; |
| 98 | +} |
0 commit comments