1
- import * as os from 'os' ;
2
- import * as which from 'which' ;
3
- import * as semver from 'semver' ;
4
- import { spawn } from 'child_process' ;
5
- import { join } from 'path' ;
6
1
import { injectable , inject } from 'inversify' ;
7
2
import { ILogger } from '@theia/core' ;
8
3
import { FileUri } from '@theia/core/lib/node/file-uri' ;
9
4
import { Config } from '../common/protocol/config-service' ;
5
+ import { spawnCommand , getExecPath } from './exec-util' ;
10
6
11
7
@injectable ( )
12
8
export class ArduinoCli {
@@ -20,33 +16,19 @@ export class ArduinoCli {
20
16
if ( this . execPath ) {
21
17
return this . execPath ;
22
18
}
23
- const version = / \d + \. \d + \. \d + / ;
24
- const cli = `arduino-cli${ os . platform ( ) === 'win32' ? '.exe' : '' } ` ;
25
- const buildCli = join ( __dirname , '..' , '..' , 'build' , cli ) ;
26
- const buildVersion = await this . spawn ( `"${ buildCli } "` , [ 'version' ] ) ;
27
- const buildShortVersion = ( buildVersion . match ( version ) || [ ] ) [ 0 ] ;
28
- this . execPath = buildCli ;
29
- const pathCli = await new Promise < string | undefined > ( resolve => which ( cli , ( error , path ) => resolve ( error ? undefined : path ) ) ) ;
30
- if ( ! pathCli ) {
31
- return buildCli ;
32
- }
33
- const pathVersion = await this . spawn ( `"${ pathCli } "` , [ 'version' ] ) ;
34
- const pathShortVersion = ( pathVersion . match ( version ) || [ ] ) [ 0 ] ;
35
- if ( semver . gt ( pathShortVersion , buildShortVersion ) ) {
36
- this . execPath = pathCli ;
37
- return pathCli ;
38
- }
39
- return buildCli ;
19
+ const path = await getExecPath ( 'arduino-cli' , this . logger , 'version' ) ;
20
+ this . execPath = path ;
21
+ return path ;
40
22
}
41
23
42
24
async getVersion ( ) : Promise < string > {
43
25
const execPath = await this . getExecPath ( ) ;
44
- return this . spawn ( `"${ execPath } "` , [ 'version' ] ) ;
26
+ return spawnCommand ( `"${ execPath } "` , [ 'version' ] , this . logger ) ;
45
27
}
46
28
47
29
async getDefaultConfig ( ) : Promise < Config > {
48
30
const execPath = await this . getExecPath ( ) ;
49
- const result = await this . spawn ( `"${ execPath } "` , [ 'config' , 'dump' , '--format' , 'json' ] ) ;
31
+ const result = await spawnCommand ( `"${ execPath } "` , [ 'config' , 'dump' , '--format' , 'json' ] , this . logger ) ;
50
32
const { directories } = JSON . parse ( result ) ;
51
33
if ( ! directories ) {
52
34
throw new Error ( `Could not parse config. 'directories' was missing from: ${ result } ` ) ;
@@ -64,33 +46,4 @@ export class ArduinoCli {
64
46
} ;
65
47
}
66
48
67
- private spawn ( command : string , args ?: string [ ] ) : Promise < string > {
68
- return new Promise < string > ( ( resolve , reject ) => {
69
- const buffers : Buffer [ ] = [ ] ;
70
- const cp = spawn ( command , args , { windowsHide : true , shell : true } ) ;
71
- cp . stdout . on ( 'data' , ( b : Buffer ) => buffers . push ( b ) ) ;
72
- cp . on ( 'error' , error => {
73
- this . logger . error ( `Error executing ${ command } with args: ${ JSON . stringify ( args ) } .` , error ) ;
74
- reject ( error ) ;
75
- } ) ;
76
- cp . on ( 'exit' , ( code , signal ) => {
77
- if ( code === 0 ) {
78
- const result = Buffer . concat ( buffers ) . toString ( 'utf8' ) . trim ( )
79
- resolve ( result ) ;
80
- return ;
81
- }
82
- if ( signal ) {
83
- this . logger . error ( `Unexpected signal '${ signal } ' when executing ${ command } with args: ${ JSON . stringify ( args ) } .` ) ;
84
- reject ( new Error ( `Process exited with signal: ${ signal } ` ) ) ;
85
- return ;
86
- }
87
- if ( code ) {
88
- this . logger . error ( `Unexpected exit code '${ code } ' when executing ${ command } with args: ${ JSON . stringify ( args ) } .` ) ;
89
- reject ( new Error ( `Process exited with exit code: ${ code } ` ) ) ;
90
- return ;
91
- }
92
- } ) ;
93
- } ) ;
94
- }
95
-
96
49
}
0 commit comments