@@ -39,108 +39,106 @@ export function isJsonDependency(arg: unknown): arg is IJsonDependency {
3939 ) ;
4040}
4141
42- export class VendorLibrariesBase {
43- public async findForUUIDs ( uuid : string [ ] ) : Promise < IJsonDependency [ ] > {
44- const homeDirDeps = await this . getHomeDirDeps ( ) ;
45- const foundDeps = homeDirDeps . filter ( ( value ) => {
46- return uuid . indexOf ( value . uuid ) >= 0 ;
47- } ) ;
48- return foundDeps ;
49- }
42+ export async function findForUUIDs ( uuid : string [ ] ) : Promise < IJsonDependency [ ] > {
43+ const homeDirDeps = await getHomeDirDeps ( ) ;
44+ const foundDeps = homeDirDeps . filter ( ( value ) => {
45+ return uuid . indexOf ( value . uuid ) >= 0 ;
46+ } ) ;
47+ return foundDeps ;
48+ }
5049
51- public async installDependency (
52- dep : IJsonDependency ,
53- url : string ,
54- override : boolean
55- ) : Promise < boolean > {
50+ export async function installDependency (
51+ dep : IJsonDependency ,
52+ url : string ,
53+ override : boolean
54+ ) : Promise < boolean > {
55+ try {
5656 try {
57- try {
58- await access ( url ) ;
59- } catch {
60- // File doesn't exist, directly write file
61- await mkdir ( url , { recursive : true } ) ;
62- await writeFile ( path . join ( url , dep . fileName ) , JSON . stringify ( dep , null , 4 ) ) ;
63- return true ;
64- }
65- const files = await readdir ( url ) ;
66-
67- for ( const file of files ) {
68- const fullPath = path . join ( url , file ) ;
69- const result = await this . readFile ( fullPath ) ;
70- if ( result && result . uuid === dep . uuid ) {
71- if ( override ) {
72- await unlink ( fullPath ) ;
73- break ;
74- } else {
75- return false ;
76- }
77- }
78- }
79-
57+ await access ( url ) ;
58+ } catch {
59+ // File doesn't exist, directly write file
60+ await mkdir ( url , { recursive : true } ) ;
8061 await writeFile ( path . join ( url , dep . fileName ) , JSON . stringify ( dep , null , 4 ) ) ;
8162 return true ;
82- } catch ( error ) {
83- logger . error ( `Failed to install dependency ${ dep . name } :` , error ) ;
84- return false ;
8563 }
86- }
64+ const files = await readdir ( url ) ;
65+
66+ for ( const file of files ) {
67+ const fullPath = path . join ( url , file ) ;
68+ const result = await parseVendordepJson ( fullPath ) ;
69+ if ( result && result . uuid === dep . uuid ) {
70+ if ( override ) {
71+ await unlink ( fullPath ) ;
72+ break ;
73+ } else {
74+ return false ;
75+ }
76+ }
77+ }
8778
88- public getHomeDirDeps ( ) : Promise < IJsonDependency [ ] > {
89- return this . getDependencies ( path . join ( getWPILibHomeDir ( ) , 'vendordeps' ) ) ;
79+ await writeFile ( path . join ( url , dep . fileName ) , JSON . stringify ( dep , null , 4 ) ) ;
80+ return true ;
81+ } catch ( error ) {
82+ logger . error ( `Failed to install dependency ${ dep . name } :` , error ) ;
83+ return false ;
9084 }
85+ }
9186
92- protected async readFile ( file : string ) : Promise < IJsonDependency | undefined > {
93- try {
94- const jsonContents = await readFile ( file , 'utf8' ) ;
95- const dep = JSON . parse ( jsonContents ) ;
87+ export function getHomeDirDeps ( ) : Promise < IJsonDependency [ ] > {
88+ return getDependencies ( path . join ( getWPILibHomeDir ( ) , 'vendordeps' ) ) ;
89+ }
9690
97- if ( isJsonDependency ( dep ) ) {
98- return dep ;
99- }
91+ export async function parseVendordepJson ( file : string ) : Promise < IJsonDependency | undefined > {
92+ try {
93+ const jsonContents = await readFile ( file , 'utf8' ) ;
94+ const dep = JSON . parse ( jsonContents ) ;
10095
101- return undefined ;
102- } catch ( err ) {
103- logger . warn ( 'JSON parse error' , err ) ;
104- return undefined ;
96+ if ( isJsonDependency ( dep ) ) {
97+ return dep ;
10598 }
99+
100+ return undefined ;
101+ } catch ( err ) {
102+ logger . warn ( 'JSON parse error' , err ) ;
103+ return undefined ;
106104 }
105+ }
107106
108- protected async getDependencies ( dir : string ) : Promise < IJsonDependency [ ] > {
109- try {
110- const files = await readdir ( dir ) ;
107+ export async function getDependencies ( dir : string ) : Promise < IJsonDependency [ ] > {
108+ try {
109+ const files = await readdir ( dir ) ;
111110
112- const promises : Promise < IJsonDependency | undefined > [ ] = [ ] ;
111+ const promises : Promise < IJsonDependency | undefined > [ ] = [ ] ;
113112
114- for ( const file of files ) {
115- promises . push ( this . readFile ( path . join ( dir , file ) ) ) ;
116- }
113+ for ( const file of files ) {
114+ promises . push ( parseVendordepJson ( path . join ( dir , file ) ) ) ;
115+ }
117116
118- const results = await Promise . all ( promises ) ;
117+ const results = await Promise . all ( promises ) ;
119118
120- return results . filter ( ( x ) => x !== undefined ) as IJsonDependency [ ] ;
121- } catch ( err ) {
122- return [ ] ;
123- }
119+ return results . filter ( ( x ) => x !== undefined ) as IJsonDependency [ ] ;
120+ } catch ( err ) {
121+ return [ ] ;
124122 }
123+ }
125124
126- protected async loadFileFromUrl ( url : string ) : Promise < IJsonDependency > {
127- try {
128- const response = await fetch ( url , {
129- signal : AbortSignal . timeout ( 5000 ) ,
130- } ) ;
131- if ( response . ok ) {
132- const json = await response . json ( ) ;
133- if ( isJsonDependency ( json ) ) {
134- return json ;
135- } else {
136- throw new Error ( 'Incorrect JSON format' ) ;
137- }
125+ export async function loadFileFromUrl ( url : string ) : Promise < IJsonDependency > {
126+ try {
127+ const response = await fetch ( url , {
128+ signal : AbortSignal . timeout ( 5000 ) ,
129+ } ) ;
130+ if ( response . ok ) {
131+ const json = await response . json ( ) ;
132+ if ( isJsonDependency ( json ) ) {
133+ return json ;
138134 } else {
139- throw new Error ( `Bad status ${ response . status } ` ) ;
135+ throw new Error ( 'Incorrect JSON format' ) ;
140136 }
141- } catch ( error ) {
142- logger . error ( `Failed to load file from URL: ${ url } ` , error ) ;
143- throw error ;
137+ } else {
138+ throw new Error ( `Bad status ${ response . status } ` ) ;
144139 }
140+ } catch ( error ) {
141+ logger . error ( `Failed to load file from URL: ${ url } ` , error ) ;
142+ throw error ;
145143 }
146144}
0 commit comments