@@ -14,6 +14,7 @@ import { findPythonExecutable, installPortablePython } from '../get-python';
1414import BaseStage from './base' ;
1515import { callInstallerScript } from '../get-pioarduino' ;
1616import { promises as fs } from 'fs' ;
17+ import os from 'os' ;
1718import path from 'path' ;
1819
1920export default class pioarduinoCoreStage extends BaseStage {
@@ -41,30 +42,91 @@ export default class pioarduinoCoreStage extends BaseStage {
4142 }
4243
4344 async check ( ) {
45+ // First check if we have local installation without network calls
46+ if ( await this . checkLocalPioInstallation ( ) ) {
47+ console . info ( 'Found existing pioarduino installation locally' ) ;
48+
49+ // Setup `platformio` CLI globally for a Node.JS process
50+ if ( this . params . useBuiltinPIOCore ) {
51+ proc . extendOSEnvironPath ( 'PLATFORMIO_PATH' , [
52+ core . getEnvBinDir ( ) ,
53+ core . getEnvDir ( ) ,
54+ ] ) ;
55+ }
56+ this . status = BaseStage . STATUS_SUCCESSED ;
57+ return true ;
58+ }
59+
4460 if ( this . params . useBuiltinPIOCore ) {
4561 try {
4662 await fs . access ( core . getEnvBinDir ( ) ) ;
4763 } catch ( err ) {
4864 throw new Error ( 'pioarduino Core has not been installed yet!' ) ;
4965 }
5066 }
51- // check that PIO Core is installed
52- await this . loadCoreState ( ) ;
67+
68+ // Only try network operations if local check failed
69+ try {
70+ // check that PIO Core is installed
71+ await this . loadCoreState ( ) ;
5372
54- // check if outdated built-in Python
55- if ( await this . isBuiltinPythonOutdated ( ) ) {
56- return false ;
73+ // check if outdated built-in Python
74+ if ( await this . isBuiltinPythonOutdated ( ) ) {
75+ return false ;
76+ }
77+
78+ // Setup `platformio` CLI globally for a Node.JS process
79+ if ( this . params . useBuiltinPIOCore ) {
80+ proc . extendOSEnvironPath ( 'PLATFORMIO_PATH' , [
81+ core . getEnvBinDir ( ) ,
82+ core . getEnvDir ( ) ,
83+ ] ) ;
84+ }
85+ this . status = BaseStage . STATUS_SUCCESSED ;
86+ return true ;
87+ } catch ( err ) {
88+ // If network operations fail, check if we have local installation anyway
89+ if ( await this . checkLocalPioInstallation ( ) ) {
90+ console . info ( 'Network failed, but found existing pioarduino installation locally' ) ;
91+
92+ // Setup `platformio` CLI globally for a Node.JS process
93+ if ( this . params . useBuiltinPIOCore ) {
94+ proc . extendOSEnvironPath ( 'PLATFORMIO_PATH' , [
95+ core . getEnvBinDir ( ) ,
96+ core . getEnvDir ( ) ,
97+ ] ) ;
98+ }
99+ this . status = BaseStage . STATUS_SUCCESSED ;
100+ return true ;
101+ }
102+ throw err ; // Re-throw if no local installation found
57103 }
104+ }
58105
59- // Setup `platformio` CLI globally for a Node.JS process
60- if ( this . params . useBuiltinPIOCore ) {
61- proc . extendOSEnvironPath ( 'PLATFORMIO_PATH' , [
62- core . getEnvBinDir ( ) ,
63- core . getEnvDir ( ) ,
64- ] ) ;
106+ async checkLocalPioInstallation ( ) {
107+ try {
108+ const pioarduinoDir = path . join ( os . homedir ( ) , '.platformio' ) ;
109+ const penvDir = path . join ( pioarduinoDir , 'penv' ) ;
110+
111+ // Check if .platformio/penv directory exists
112+ await fs . access ( penvDir ) ;
113+
114+ // Check if we have the builtin core directory
115+ if ( this . params . useBuiltinPIOCore ) {
116+ await fs . access ( core . getEnvBinDir ( ) ) ;
117+ }
118+
119+ console . info ( 'Local pioarduino installation found:' , {
120+ pioarduinoDir,
121+ penvDir,
122+ coreDir : this . params . useBuiltinPIOCore ? core . getEnvBinDir ( ) : 'global'
123+ } ) ;
124+
125+ return true ;
126+ } catch ( err ) {
127+ // Silent fail for local check
128+ return false ;
65129 }
66- this . status = BaseStage . STATUS_SUCCESSED ;
67- return true ;
68130 }
69131
70132 async loadCoreState ( ) {
@@ -198,7 +260,7 @@ export default class pioarduinoCoreStage extends BaseStage {
198260 console . warn ( err ) ;
199261 // cleanup
200262 try {
201- await fs . rm ( PlatformIOCoreStage . getBuiltInPythonDir ( ) , {
263+ await fs . rm ( pioarduinoCoreStage . getBuiltInPythonDir ( ) , {
202264 recursive : true ,
203265 force : true ,
204266 } ) ;
@@ -218,7 +280,7 @@ export default class pioarduinoCoreStage extends BaseStage {
218280 ) ,
219281 ) ;
220282
221- // check that PIO Core is installed and load its state an patch OS environ
283+ // check that PIO Core is installed and load its state and patch OS environ
222284 withProgress ( 'Loading pioarduino Core state' , 40 ) ;
223285 await this . loadCoreState ( ) ;
224286
0 commit comments