@@ -32,15 +32,18 @@ export class CreateApi {
32
32
return this ;
33
33
}
34
34
35
- async findSketchByPath (
36
- path : string ,
35
+ public sketchCompareByPath = ( param : string ) => {
36
+ return ( sketch : Create . Sketch ) => {
37
+ const [ , spath ] = splitSketchPath ( sketch . path ) ;
38
+ return param === spath ;
39
+ } ;
40
+ } ;
41
+
42
+ async findSketchInCache (
43
+ compareFn : ( sketch : Create . Sketch ) => boolean ,
37
44
trustCache = true
38
45
) : Promise < Create . Sketch | undefined > {
39
- const skatches = sketchCache ;
40
- const sketch = skatches . find ( ( sketch ) => {
41
- const [ , spath ] = splitSketchPath ( sketch . path ) ;
42
- return path === spath ;
43
- } ) ;
46
+ const sketch = sketchCache . find ( ( sketch ) => compareFn ( sketch ) ) ;
44
47
if ( trustCache ) {
45
48
return Promise . resolve ( sketch ) ;
46
49
}
@@ -148,7 +151,9 @@ export class CreateApi {
148
151
}
149
152
150
153
const [ , spath ] = createPaths . splitSketchPath ( res . path ) ;
151
- const sketch = await this . findSketchByPath ( spath ) ;
154
+ const sketch = await this . findSketchInCache (
155
+ this . sketchCompareByPath ( spath )
156
+ ) ;
152
157
if (
153
158
sketch &&
154
159
sketch . secrets &&
@@ -159,7 +164,9 @@ export class CreateApi {
159
164
} ) ;
160
165
161
166
if ( posixPath !== posix . sep ) {
162
- const sketch = await this . findSketchByPath ( posixPath ) ;
167
+ const sketch = await this . findSketchInCache (
168
+ this . sketchCompareByPath ( posixPath )
169
+ ) ;
163
170
if (
164
171
sketch &&
165
172
sketch . secrets &&
@@ -214,7 +221,9 @@ export class CreateApi {
214
221
215
222
let resources ;
216
223
if ( basename === Create . arduino_secrets_file ) {
217
- const sketch = await this . findSketchByPath ( parentPosixPath ) ;
224
+ const sketch = await this . findSketchInCache (
225
+ this . sketchCompareByPath ( parentPosixPath )
226
+ ) ;
218
227
resources = sketch ? [ this . getSketchSecretStat ( sketch ) ] : [ ] ;
219
228
} else {
220
229
resources = await this . readDirectory ( parentPosixPath , {
@@ -230,12 +239,51 @@ export class CreateApi {
230
239
return resource ;
231
240
}
232
241
242
+ private async toggleSecretsInclude (
243
+ path : string ,
244
+ data : string ,
245
+ mode : 'add' | 'remove'
246
+ ) {
247
+ const includeString = `#include "${ Create . arduino_secrets_file } "` ;
248
+ const includeRegexp = new RegExp ( includeString + '\\s*' , 'g' ) ;
249
+
250
+ const basename = createPaths . basename ( path ) ;
251
+ if ( mode === 'add' ) {
252
+ const doesIncludeSecrets = includeRegexp . test ( data ) ;
253
+
254
+ if ( doesIncludeSecrets ) {
255
+ return data ;
256
+ }
257
+
258
+ const sketch = await this . findSketchInCache ( ( sketch ) => {
259
+ const [ , spath ] = splitSketchPath ( sketch . path ) ;
260
+ return spath === createPaths . parentPosix ( path ) ;
261
+ } , true ) ;
262
+
263
+ if (
264
+ sketch &&
265
+ ( sketch . name + '.ino' === basename ||
266
+ sketch . name + '.pde' === basename ) &&
267
+ sketch . secrets &&
268
+ sketch . secrets . length > 0
269
+ ) {
270
+ return includeString + '\n' + data ;
271
+ }
272
+ } else if ( mode === 'remove' ) {
273
+ return data . replace ( includeRegexp , '' ) ;
274
+ }
275
+ return data ;
276
+ }
277
+
233
278
async readFile ( posixPath : string ) : Promise < string > {
234
279
const basename = createPaths . basename ( posixPath ) ;
235
280
236
281
if ( basename === Create . arduino_secrets_file ) {
237
282
const parentPosixPath = createPaths . parentPosix ( posixPath ) ;
238
- const sketch = await this . findSketchByPath ( parentPosixPath , false ) ;
283
+ const sketch = await this . findSketchInCache (
284
+ this . sketchCompareByPath ( parentPosixPath ) ,
285
+ false
286
+ ) ;
239
287
240
288
let file = '' ;
241
289
if ( sketch && sketch . secrets ) {
@@ -250,12 +298,15 @@ export class CreateApi {
250
298
`${ this . domain ( ) } /files/f/$HOME/sketches_v2${ posixPath } `
251
299
) ;
252
300
const headers = await this . headers ( ) ;
253
- const result = await this . run < { data : string } > ( url , {
301
+ const result = await this . run < { data : string ; path : string } > ( url , {
254
302
method : 'GET' ,
255
303
headers,
256
304
} ) ;
257
- const { data } = result ;
258
- return atob ( data ) ;
305
+ let { data } = result ;
306
+
307
+ // add includes to main arduino file
308
+ data = await this . toggleSecretsInclude ( posixPath , atob ( data ) , 'add' ) ;
309
+ return data ;
259
310
}
260
311
261
312
async writeFile (
@@ -266,7 +317,9 @@ export class CreateApi {
266
317
267
318
if ( basename === Create . arduino_secrets_file ) {
268
319
const parentPosixPath = createPaths . parentPosix ( posixPath ) ;
269
- const sketch = await this . findSketchByPath ( parentPosixPath ) ;
320
+ const sketch = await this . findSketchInCache (
321
+ this . sketchCompareByPath ( parentPosixPath )
322
+ ) ;
270
323
if ( sketch ) {
271
324
const url = new URL ( `${ this . domain ( ) } /sketches/${ sketch . id } ` ) ;
272
325
const headers = await this . headers ( ) ;
@@ -300,7 +353,7 @@ export class CreateApi {
300
353
) ;
301
354
}
302
355
303
- if ( name . length === 0 || value . length === 0 ) {
356
+ if ( name . length === 0 ) {
304
357
return prev ;
305
358
}
306
359
@@ -331,12 +384,14 @@ export class CreateApi {
331
384
`${ this . domain ( ) } /files/f/$HOME/sketches_v2${ posixPath } `
332
385
) ;
333
386
const headers = await this . headers ( ) ;
334
- const data = btoa (
387
+
388
+ let data : string =
335
389
typeof content === 'string'
336
390
? content
337
- : new TextDecoder ( ) . decode ( content )
338
- ) ;
339
- const payload = { data } ;
391
+ : new TextDecoder ( ) . decode ( content ) ;
392
+ data = await this . toggleSecretsInclude ( posixPath , data , 'remove' ) ;
393
+
394
+ const payload = { data : btoa ( data ) } ;
340
395
const init = {
341
396
method : 'POST' ,
342
397
body : JSON . stringify ( payload ) ,
0 commit comments