11import fs from "fs" ;
2+ import path from "path" ;
23import chalk from "chalk" ;
34import { Command } from "commander" ;
45import inquirer from "inquirer" ;
@@ -177,7 +178,7 @@ export class Pull {
177178 updatedConfig . tables = tables ;
178179 }
179180
180- if ( ! skipDeprecated && ( shouldPullAll || options . collections ) ) {
181+ if ( options . collections || ( shouldPullAll && ! skipDeprecated ) ) {
181182 const { databases, collections } = await this . pullCollections ( ) ;
182183 updatedConfig . databases = databases ;
183184 updatedConfig . collections = collections ;
@@ -227,104 +228,96 @@ export class Pull {
227228 ) : Promise < FunctionType [ ] > {
228229 this . log ( "Fetching functions ..." ) ;
229230
230- const originalCwd = process . cwd ( ) ;
231- process . chdir ( this . configDirectoryPath ) ;
231+ const functionsService = new Functions ( this . projectClient ) ;
232+ let functions : Models . Function [ ] ;
232233
233- try {
234- const functionsService = new Functions ( this . projectClient ) ;
235- let functions : Models . Function [ ] ;
236-
237- if ( options . functionIds && options . functionIds . length > 0 ) {
238- functions = await Promise . all (
239- options . functionIds . map ( ( id ) =>
240- functionsService . get ( {
241- functionId : id ,
242- } ) ,
243- ) ,
244- ) ;
245- } else {
246- const fetchResponse = await functionsService . list ( {
247- queries : [ Query . limit ( 1 ) ] ,
248- } ) ;
249-
250- if ( fetchResponse [ "functions" ] . length <= 0 ) {
251- this . log ( "No functions found." ) ;
252- this . success ( `Successfully pulled ${ chalk . bold ( 0 ) } functions.` ) ;
253- return [ ] ;
254- }
234+ if ( options . functionIds && options . functionIds . length > 0 ) {
235+ functions = await Promise . all (
236+ options . functionIds . map ( ( id ) =>
237+ functionsService . get ( {
238+ functionId : id ,
239+ } ) ,
240+ ) ,
241+ ) ;
242+ } else {
243+ const fetchResponse = await functionsService . list ( {
244+ queries : [ Query . limit ( 1 ) ] ,
245+ } ) ;
255246
256- const { functions : allFunctions } = await paginate (
257- async ( ) => new Functions ( this . projectClient ) . list ( ) ,
258- { } ,
259- 100 ,
260- "functions" ,
261- ) ;
262- functions = allFunctions ;
247+ if ( fetchResponse [ "functions" ] . length <= 0 ) {
248+ this . log ( "No functions found." ) ;
249+ this . success ( `Successfully pulled ${ chalk . bold ( 0 ) } functions.` ) ;
250+ return [ ] ;
263251 }
264252
265- const result : FunctionType [ ] = [ ] ;
266-
267- for ( const func of functions ) {
268- this . log ( `Pulling function ${ chalk . bold ( func . name ) } ...` ) ;
269-
270- const funcPath = `functions/${ func . name } ` ;
271- const holdingVars = func . vars || [ ] ;
272-
273- const functionConfig : FunctionType = {
274- $id : func . $id ,
275- name : func . name ,
276- runtime : func . runtime ,
277- path : funcPath ,
278- entrypoint : func . entrypoint ,
279- execute : func . execute ,
280- enabled : func . enabled ,
281- logging : func . logging ,
282- events : func . events ,
283- schedule : func . schedule ,
284- timeout : func . timeout ,
285- commands : func . commands ,
286- scopes : func . scopes ,
287- specification : func . specification ,
288- } ;
289-
290- result . push ( functionConfig ) ;
291-
292- if ( ! fs . existsSync ( funcPath ) ) {
293- fs . mkdirSync ( funcPath , { recursive : true } ) ;
294- }
295-
296- if ( options . code !== false ) {
297- await downloadDeploymentCode ( {
298- resourceId : func [ "$id" ] ,
299- resourcePath : funcPath ,
300- holdingVars,
301- withVariables : options . withVariables ,
302- listDeployments : ( ) =>
303- functionsService . listDeployments ( {
304- functionId : func [ "$id" ] ,
305- queries : [ Query . limit ( 1 ) , Query . orderDesc ( "$id" ) ] ,
306- } ) ,
307- getDownloadUrl : ( deploymentId ) =>
308- functionsService . getDeploymentDownload ( {
309- functionId : func [ "$id" ] ,
310- deploymentId,
311- } ) ,
312- projectClient : this . projectClient ,
313- } ) ;
314- }
253+ const { functions : allFunctions } = await paginate (
254+ async ( ) => new Functions ( this . projectClient ) . list ( ) ,
255+ { } ,
256+ 100 ,
257+ "functions" ,
258+ ) ;
259+ functions = allFunctions ;
260+ }
261+
262+ const result : FunctionType [ ] = [ ] ;
263+
264+ for ( const func of functions ) {
265+ this . log ( `Pulling function ${ chalk . bold ( func . name ) } ...` ) ;
266+
267+ const funcPath = `functions/${ func . name } ` ;
268+ const absoluteFuncPath = path . resolve ( this . configDirectoryPath , funcPath ) ;
269+ const holdingVars = func . vars || [ ] ;
270+
271+ const functionConfig : FunctionType = {
272+ $id : func . $id ,
273+ name : func . name ,
274+ runtime : func . runtime ,
275+ path : funcPath ,
276+ entrypoint : func . entrypoint ,
277+ execute : func . execute ,
278+ enabled : func . enabled ,
279+ logging : func . logging ,
280+ events : func . events ,
281+ schedule : func . schedule ,
282+ timeout : func . timeout ,
283+ commands : func . commands ,
284+ scopes : func . scopes ,
285+ specification : func . specification ,
286+ } ;
287+
288+ result . push ( functionConfig ) ;
289+
290+ if ( ! fs . existsSync ( absoluteFuncPath ) ) {
291+ fs . mkdirSync ( absoluteFuncPath , { recursive : true } ) ;
315292 }
316293
317- if ( options . code === false ) {
318- this . warn ( "Source code download skipped." ) ;
294+ if ( options . code !== false ) {
295+ await downloadDeploymentCode ( {
296+ resourceId : func [ "$id" ] ,
297+ resourcePath : absoluteFuncPath ,
298+ holdingVars,
299+ withVariables : options . withVariables ,
300+ listDeployments : ( ) =>
301+ functionsService . listDeployments ( {
302+ functionId : func [ "$id" ] ,
303+ queries : [ Query . limit ( 1 ) , Query . orderDesc ( "$id" ) ] ,
304+ } ) ,
305+ getDownloadUrl : ( deploymentId ) =>
306+ functionsService . getDeploymentDownload ( {
307+ functionId : func [ "$id" ] ,
308+ deploymentId,
309+ } ) ,
310+ projectClient : this . projectClient ,
311+ } ) ;
319312 }
313+ }
320314
321- this . success (
322- `Successfully pulled ${ chalk . bold ( result . length ) } functions.` ,
323- ) ;
324- return result ;
325- } finally {
326- process . chdir ( originalCwd ) ;
315+ if ( options . code === false ) {
316+ this . warn ( "Source code download skipped." ) ;
327317 }
318+
319+ this . success ( `Successfully pulled ${ chalk . bold ( result . length ) } functions.` ) ;
320+ return result ;
328321 }
329322
330323 /**
@@ -333,102 +326,96 @@ export class Pull {
333326 public async pullSites ( options : PullSitesOptions = { } ) : Promise < SiteType [ ] > {
334327 this . log ( "Fetching sites ..." ) ;
335328
336- const originalCwd = process . cwd ( ) ;
337- process . chdir ( this . configDirectoryPath ) ;
338-
339- try {
340- const sitesService = new Sites ( this . projectClient ) ;
341- let sites : Models . Site [ ] ;
342-
343- if ( options . siteIds && options . siteIds . length > 0 ) {
344- sites = await Promise . all (
345- options . siteIds . map ( ( id ) =>
346- sitesService . get ( {
347- siteId : id ,
348- } ) ,
349- ) ,
350- ) ;
351- } else {
352- const fetchResponse = await sitesService . list ( {
353- queries : [ Query . limit ( 1 ) ] ,
354- } ) ;
329+ const sitesService = new Sites ( this . projectClient ) ;
330+ let sites : Models . Site [ ] ;
355331
356- if ( fetchResponse [ "sites" ] . length <= 0 ) {
357- this . log ( "No sites found." ) ;
358- this . success ( `Successfully pulled ${ chalk . bold ( 0 ) } sites.` ) ;
359- return [ ] ;
360- }
332+ if ( options . siteIds && options . siteIds . length > 0 ) {
333+ sites = await Promise . all (
334+ options . siteIds . map ( ( id ) =>
335+ sitesService . get ( {
336+ siteId : id ,
337+ } ) ,
338+ ) ,
339+ ) ;
340+ } else {
341+ const fetchResponse = await sitesService . list ( {
342+ queries : [ Query . limit ( 1 ) ] ,
343+ } ) ;
361344
362- const { sites : fetchedSites } = await paginate (
363- async ( ) => new Sites ( this . projectClient ) . list ( ) ,
364- { } ,
365- 100 ,
366- "sites" ,
367- ) ;
368- sites = fetchedSites ;
345+ if ( fetchResponse [ "sites" ] . length <= 0 ) {
346+ this . log ( "No sites found." ) ;
347+ this . success ( `Successfully pulled ${ chalk . bold ( 0 ) } sites.` ) ;
348+ return [ ] ;
369349 }
370350
371- const result : SiteType [ ] = [ ] ;
372-
373- for ( const site of sites ) {
374- this . log ( `Pulling site ${ chalk . bold ( site . name ) } ...` ) ;
375-
376- const sitePath = `sites/${ site . name } ` ;
377- const holdingVars = site . vars || [ ] ;
378-
379- const siteConfig : SiteType = {
380- $id : site . $id ,
381- name : site . name ,
382- path : sitePath ,
383- framework : site . framework ,
384- enabled : site . enabled ,
385- logging : site . logging ,
386- timeout : site . timeout ,
387- buildRuntime : site . buildRuntime ,
388- adapter : site . adapter ,
389- installCommand : site . installCommand ,
390- buildCommand : site . buildCommand ,
391- outputDirectory : site . outputDirectory ,
392- fallbackFile : site . fallbackFile ,
393- specification : site . specification ,
394- } ;
395-
396- result . push ( siteConfig ) ;
397-
398- if ( ! fs . existsSync ( sitePath ) ) {
399- fs . mkdirSync ( sitePath , { recursive : true } ) ;
400- }
401-
402- if ( options . code !== false ) {
403- await downloadDeploymentCode ( {
404- resourceId : site [ "$id" ] ,
405- resourcePath : sitePath ,
406- holdingVars,
407- withVariables : options . withVariables ,
408- listDeployments : ( ) =>
409- sitesService . listDeployments ( {
410- siteId : site [ "$id" ] ,
411- queries : [ Query . limit ( 1 ) , Query . orderDesc ( "$id" ) ] ,
412- } ) ,
413- getDownloadUrl : ( deploymentId ) =>
414- sitesService . getDeploymentDownload ( {
415- siteId : site [ "$id" ] ,
416- deploymentId,
417- } ) ,
418- projectClient : this . projectClient ,
419- } ) ;
420- }
351+ const { sites : fetchedSites } = await paginate (
352+ async ( ) => new Sites ( this . projectClient ) . list ( ) ,
353+ { } ,
354+ 100 ,
355+ "sites" ,
356+ ) ;
357+ sites = fetchedSites ;
358+ }
359+
360+ const result : SiteType [ ] = [ ] ;
361+
362+ for ( const site of sites ) {
363+ this . log ( `Pulling site ${ chalk . bold ( site . name ) } ...` ) ;
364+
365+ const sitePath = `sites/${ site . name } ` ;
366+ const absoluteSitePath = path . resolve ( this . configDirectoryPath , sitePath ) ;
367+ const holdingVars = site . vars || [ ] ;
368+
369+ const siteConfig : SiteType = {
370+ $id : site . $id ,
371+ name : site . name ,
372+ path : sitePath ,
373+ framework : site . framework ,
374+ enabled : site . enabled ,
375+ logging : site . logging ,
376+ timeout : site . timeout ,
377+ buildRuntime : site . buildRuntime ,
378+ adapter : site . adapter ,
379+ installCommand : site . installCommand ,
380+ buildCommand : site . buildCommand ,
381+ outputDirectory : site . outputDirectory ,
382+ fallbackFile : site . fallbackFile ,
383+ specification : site . specification ,
384+ } ;
385+
386+ result . push ( siteConfig ) ;
387+
388+ if ( ! fs . existsSync ( absoluteSitePath ) ) {
389+ fs . mkdirSync ( absoluteSitePath , { recursive : true } ) ;
421390 }
422391
423- if ( options . code === false ) {
424- this . warn ( "Source code download skipped." ) ;
392+ if ( options . code !== false ) {
393+ await downloadDeploymentCode ( {
394+ resourceId : site [ "$id" ] ,
395+ resourcePath : absoluteSitePath ,
396+ holdingVars,
397+ withVariables : options . withVariables ,
398+ listDeployments : ( ) =>
399+ sitesService . listDeployments ( {
400+ siteId : site [ "$id" ] ,
401+ queries : [ Query . limit ( 1 ) , Query . orderDesc ( "$id" ) ] ,
402+ } ) ,
403+ getDownloadUrl : ( deploymentId ) =>
404+ sitesService . getDeploymentDownload ( {
405+ siteId : site [ "$id" ] ,
406+ deploymentId,
407+ } ) ,
408+ projectClient : this . projectClient ,
409+ } ) ;
425410 }
411+ }
426412
427- this . success ( `Successfully pulled ${ chalk . bold ( result . length ) } sites.` ) ;
428- return result ;
429- } finally {
430- process . chdir ( originalCwd ) ;
413+ if ( options . code === false ) {
414+ this . warn ( "Source code download skipped." ) ;
431415 }
416+
417+ this . success ( `Successfully pulled ${ chalk . bold ( result . length ) } sites.` ) ;
418+ return result ;
432419 }
433420
434421 /**
0 commit comments