@@ -32,15 +32,18 @@ export class CreateApi {
3232 return this ;
3333 }
3434
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 ,
3744 trustCache = true
3845 ) : 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 ) ) ;
4447 if ( trustCache ) {
4548 return Promise . resolve ( sketch ) ;
4649 }
@@ -148,7 +151,9 @@ export class CreateApi {
148151 }
149152
150153 const [ , spath ] = createPaths . splitSketchPath ( res . path ) ;
151- const sketch = await this . findSketchByPath ( spath ) ;
154+ const sketch = await this . findSketchInCache (
155+ this . sketchCompareByPath ( spath )
156+ ) ;
152157 if (
153158 sketch &&
154159 sketch . secrets &&
@@ -159,7 +164,9 @@ export class CreateApi {
159164 } ) ;
160165
161166 if ( posixPath !== posix . sep ) {
162- const sketch = await this . findSketchByPath ( posixPath ) ;
167+ const sketch = await this . findSketchInCache (
168+ this . sketchCompareByPath ( posixPath )
169+ ) ;
163170 if (
164171 sketch &&
165172 sketch . secrets &&
@@ -214,7 +221,9 @@ export class CreateApi {
214221
215222 let resources ;
216223 if ( basename === Create . arduino_secrets_file ) {
217- const sketch = await this . findSketchByPath ( parentPosixPath ) ;
224+ const sketch = await this . findSketchInCache (
225+ this . sketchCompareByPath ( parentPosixPath )
226+ ) ;
218227 resources = sketch ? [ this . getSketchSecretStat ( sketch ) ] : [ ] ;
219228 } else {
220229 resources = await this . readDirectory ( parentPosixPath , {
@@ -230,12 +239,51 @@ export class CreateApi {
230239 return resource ;
231240 }
232241
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+
233278 async readFile ( posixPath : string ) : Promise < string > {
234279 const basename = createPaths . basename ( posixPath ) ;
235280
236281 if ( basename === Create . arduino_secrets_file ) {
237282 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+ ) ;
239287
240288 let file = '' ;
241289 if ( sketch && sketch . secrets ) {
@@ -250,12 +298,15 @@ export class CreateApi {
250298 `${ this . domain ( ) } /files/f/$HOME/sketches_v2${ posixPath } `
251299 ) ;
252300 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 , {
254302 method : 'GET' ,
255303 headers,
256304 } ) ;
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 ;
259310 }
260311
261312 async writeFile (
@@ -266,7 +317,9 @@ export class CreateApi {
266317
267318 if ( basename === Create . arduino_secrets_file ) {
268319 const parentPosixPath = createPaths . parentPosix ( posixPath ) ;
269- const sketch = await this . findSketchByPath ( parentPosixPath ) ;
320+ const sketch = await this . findSketchInCache (
321+ this . sketchCompareByPath ( parentPosixPath )
322+ ) ;
270323 if ( sketch ) {
271324 const url = new URL ( `${ this . domain ( ) } /sketches/${ sketch . id } ` ) ;
272325 const headers = await this . headers ( ) ;
@@ -300,7 +353,7 @@ export class CreateApi {
300353 ) ;
301354 }
302355
303- if ( name . length === 0 || value . length === 0 ) {
356+ if ( name . length === 0 ) {
304357 return prev ;
305358 }
306359
@@ -331,12 +384,14 @@ export class CreateApi {
331384 `${ this . domain ( ) } /files/f/$HOME/sketches_v2${ posixPath } `
332385 ) ;
333386 const headers = await this . headers ( ) ;
334- const data = btoa (
387+
388+ let data : string =
335389 typeof content === 'string'
336390 ? 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 ) } ;
340395 const init = {
341396 method : 'POST' ,
342397 body : JSON . stringify ( payload ) ,
0 commit comments