|
| 1 | +import { cancel, confirm, intro, isCancel, log, note, outro, select, text } from '@clack/prompts'; |
| 2 | +import chalkTemplate from 'chalk-template'; |
| 3 | +import { existsSync, writeFileSync } from 'fs'; |
| 4 | +import path from 'path'; |
| 5 | +import { boards } from './boards.js'; |
| 6 | +import { createDiagram } from './createDiagram.js'; |
| 7 | +import { findFirmwarePath } from './findFirmwarePath.js'; |
| 8 | +import { detectProjectType } from './projectType.js'; |
| 9 | + |
| 10 | +export async function initProjectWizard(rootDir: string, opts: { diagramFile?: string }) { |
| 11 | + const configPath = path.join(rootDir, 'wokwi.toml'); |
| 12 | + const diagramFilePath = path.resolve(rootDir, opts.diagramFile ?? 'diagram.json'); |
| 13 | + |
| 14 | + intro(`Wokwi CLI - Project Initialization Wizard`); |
| 15 | + |
| 16 | + note(`This wizard will help you configure your project for Wokwi.`, 'Welcome'); |
| 17 | + |
| 18 | + const existingFiles = []; |
| 19 | + if (existsSync(configPath)) { |
| 20 | + existingFiles.push('wokwi.toml'); |
| 21 | + } |
| 22 | + if (existsSync(diagramFilePath)) { |
| 23 | + existingFiles.push('diagram.json'); |
| 24 | + } |
| 25 | + |
| 26 | + if (existingFiles.length > 0) { |
| 27 | + const shouldContinue = await confirm({ |
| 28 | + message: `${existingFiles.join( |
| 29 | + ' and ', |
| 30 | + )} already exist in the project directory. This operation will overwrite them. Continue?`, |
| 31 | + initialValue: false, |
| 32 | + }); |
| 33 | + if (!shouldContinue || isCancel(shouldContinue)) { |
| 34 | + cancel('Operation cancelled.'); |
| 35 | + process.exit(0); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + const projectType = await detectProjectType(rootDir); |
| 40 | + if (projectType != null) { |
| 41 | + log.info(chalkTemplate`Detected project type: {greenBright ${projectType}}`); |
| 42 | + } |
| 43 | + |
| 44 | + const filteredBoards = |
| 45 | + projectType === 'esp-idf' |
| 46 | + ? boards.filter((board) => board.family === 'esp32') |
| 47 | + : projectType === 'pico-sdk' |
| 48 | + ? boards.filter((board) => board.family === 'rp2') |
| 49 | + : boards; |
| 50 | + |
| 51 | + const boardType = await select({ |
| 52 | + message: 'Select the board to simulate:', |
| 53 | + options: filteredBoards.map((board) => ({ value: board.board, label: board.title })), |
| 54 | + maxItems: 12, |
| 55 | + }); |
| 56 | + if (isCancel(boardType)) { |
| 57 | + cancel('Operation cancelled.'); |
| 58 | + process.exit(0); |
| 59 | + } |
| 60 | + |
| 61 | + const defaultFirmwarePath = await findFirmwarePath(rootDir, projectType); |
| 62 | + const firmwarePath = await text({ |
| 63 | + message: 'Enter the path to the firmware file (e.g. firmware.bin):', |
| 64 | + initialValue: defaultFirmwarePath.firmware, |
| 65 | + validate: (value) => { |
| 66 | + if (!value) { |
| 67 | + return 'Please enter a valid path'; |
| 68 | + } |
| 69 | + return undefined; |
| 70 | + }, |
| 71 | + }); |
| 72 | + if (isCancel(firmwarePath)) { |
| 73 | + cancel('Operation cancelled.'); |
| 74 | + process.exit(0); |
| 75 | + } |
| 76 | + |
| 77 | + let elfPath = firmwarePath; |
| 78 | + if (!elfPath.endsWith('.elf')) { |
| 79 | + const elfPathResponse = await text({ |
| 80 | + message: 'Enter the path to the ELF file (e.g. firmware.elf):', |
| 81 | + initialValue: defaultFirmwarePath.elf, |
| 82 | + validate: (value) => { |
| 83 | + if (!value) { |
| 84 | + return 'Please enter a valid path'; |
| 85 | + } |
| 86 | + return undefined; |
| 87 | + }, |
| 88 | + }); |
| 89 | + if (isCancel(elfPathResponse)) { |
| 90 | + cancel('Operation cancelled.'); |
| 91 | + process.exit(0); |
| 92 | + } |
| 93 | + |
| 94 | + elfPath = elfPathResponse; |
| 95 | + } |
| 96 | + |
| 97 | + log.info(`Writing wokwi.toml...`); |
| 98 | + writeFileSync( |
| 99 | + configPath, |
| 100 | + `# Wokwi Configuration File |
| 101 | +# Reference: https://docs.wokwi.com/vscode/project-config |
| 102 | +
|
| 103 | +[wokwi] |
| 104 | +version = 1 |
| 105 | +firmware = '${firmwarePath}' |
| 106 | +elf = '${elfPath}' |
| 107 | +`, |
| 108 | + ); |
| 109 | + |
| 110 | + log.info(`Writing diagram.json...`); |
| 111 | + writeFileSync(diagramFilePath, JSON.stringify(createDiagram(boardType as string), null, 2)); |
| 112 | + |
| 113 | + outro(`You're all set!`); |
| 114 | +} |
0 commit comments