Skip to content

Commit

Permalink
fix(scan): source esp_idf before checking for esptool.py (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
HipsterBrown committed Aug 14, 2024
1 parent 01a859c commit e9e1ca8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/commands/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { findBySerialNumber } from 'usb'
import type { GluegunCommand } from 'gluegun'
import type { XSDevToolbox } from '../types'
import { parseScanResult } from '../toolbox/scan/parse'
import { sourceEnvironment } from '../toolbox/system/exec'
import { sourceEnvironment, sourceIdf } from '../toolbox/system/exec'

// eslint-disable-next-line
function sleep(timeout: number) {
Expand All @@ -16,22 +16,28 @@ const command: GluegunCommand<XSDevToolbox> = {
name: 'scan',
description: 'Look for available devices',
run: async (toolbox) => {
const { parameters, print, system } = toolbox
const { filesystem, parameters, print, system } = toolbox
if (parameters.options.help !== undefined) {
print.printCommands(toolbox, ['scan'])
process.exit(0)
}

const spinner = print.spin()

await sourceEnvironment()

if (typeof process.env.IDF_PATH === 'string' && filesystem.exists(process.env.IDF_PATH) === 'dir') {
spinner.start(`Found ESP_IDF, sourcing environment...`)
await sourceIdf()
spinner.stop()
}

if (system.which('esptool.py') === null) {
print.warning(
'esptool.py required to scan for Espressif devices. Setup environment for ESP8266 or ESP32:\n xs-dev setup --device esp32\n xs-dev setup --device esp8266.'
)
}

const spinner = print.spin()

spinner.start('Scanning for devices...')

const hasPicotool = system.which('picotool') !== null
Expand Down
21 changes: 21 additions & 0 deletions src/toolbox/system/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,24 @@ export async function sourceEnvironment(): Promise<void> {
}
}
}

/**
* Set updated env from user shell as process.env
*/
export async function sourceIdf(): Promise<void> {
const OS = platformType().toLowerCase() as Device

if (OS !== 'windows_nt') {
try {
const result = await system.spawn(`source $IDF_PATH/export.sh 1> /dev/null && env`, {
shell: process.env.SHELL,
})
if (typeof result.stdout === 'string' || result.stdout instanceof Buffer) {
const localEnv = Object.fromEntries(result.stdout.toString().split('\n').map((field: string) => field?.split('=')))
if ('PATH' in localEnv) process.env = localEnv
}
} catch (error) {
console.warn('Unable to source the ESP IDF settings:', error)
}
}
}

0 comments on commit e9e1ca8

Please sign in to comment.