11import crypto from 'node:crypto'
22import fs from 'node:fs'
3+ import { glob } from 'glob'
4+ import inquirer from 'inquirer'
35import path from 'node:path'
46import { execSync } from 'node:child_process'
57
@@ -9,7 +11,7 @@ import { GDF } from './gdf.js'
911
1012// Fly.io mixin
1113GDF . extend ( class extends GDF {
12- run ( ) {
14+ async run ( ) {
1315 if ( ! this . flySetup ( ) ) return
1416
1517 // create volume for sqlite3
@@ -27,8 +29,11 @@ GDF.extend(class extends GDF {
2729
2830 // set secrets, healthcheck for remix apps
2931 if ( this . shopify ) {
30- this . flyShopifyEnv ( this . flyApp )
31- this . flyShopifyConfig ( this . flyApp )
32+ const shopifyConfig = await this . selectShopifyConfig ( )
33+ if ( ! shopifyConfig ) {
34+ this . flyShopifyEnv ( this . flyApp , shopifyConfig )
35+ this . flyShopifyConfig ( this . flyApp , shopifyConfig )
36+ }
3237 } else if ( this . remix ) {
3338 this . flyRemixSecrets ( this . flyApp )
3439 this . flyHealthCheck ( '/healthcheck' )
@@ -81,7 +86,7 @@ GDF.extend(class extends GDF {
8186
8287 const extensions = ( process . env . PATHEXT || '' ) . split ( ';' )
8388
84- const candidates = function * ( ) {
89+ const candidates = function * ( ) {
8590 for ( const dir of paths ) {
8691 for ( const ext of extensions ) {
8792 yield path . join ( dir , exe + ext )
@@ -311,16 +316,44 @@ GDF.extend(class extends GDF {
311316 } )
312317 }
313318
319+ async selectShopifyConfig ( ) {
320+ // Search for both shopify.app.toml and shopify.app.*.toml
321+ const files = await glob ( 'shopify.app{.,*.}toml' ) ;
322+
323+ if ( files . length === 0 ) {
324+ return null
325+ }
326+
327+ if ( files . length === 1 ) {
328+ return files [ 0 ]
329+ }
330+
331+ // Multiple files found, prompt user to select one
332+ const { selectedFile } = await inquirer . prompt ( [
333+ {
334+ type : 'list' ,
335+ name : 'selectedFile' ,
336+ message : 'Multiple configuration files found. Please select one:' ,
337+ choices : files . map ( file => ( {
338+ name : file ,
339+ value : file
340+ } ) )
341+ }
342+ ] )
343+
344+ return selectedFile
345+ }
346+
314347 // set environment and secrets for Shopify apps
315- flyShopifyEnv ( app ) {
348+ flyShopifyEnv ( app , configFile ) {
316349 let toml = ''
317- if ( fs . existsSync ( 'shopify.app.toml' ) ) {
318- toml = fs . readFileSync ( 'shopify.app.toml' , 'utf-8' )
350+ if ( fs . existsSync ( configFile ) ) {
351+ toml = fs . readFileSync ( configFile , 'utf-8' )
319352 }
320353
321354 if ( ! toml . includes ( 'client_id' ) ) {
322355 this . setExit ( 42 )
323- console . log ( `${ chalk . bold . red ( 'shopify.app.toml' ) } is not complete; run ${ chalk . bold . blue ( 'shopify app config create' ) } first.` )
356+ console . log ( `${ chalk . bold . red ( configFile ) } is not complete; run ${ chalk . bold . blue ( 'shopify app config create' ) } first.` )
324357 return
325358 }
326359
@@ -330,7 +363,7 @@ GDF.extend(class extends GDF {
330363 }
331364
332365 try {
333- console . log ( `${ chalk . bold . green ( 'execute' . padStart ( 11 ) ) } shopify app env show` )
366+ console . log ( `${ chalk . bold . green ( 'execute' . padStart ( 11 ) ) } shopify app env show --config ${ configFile } ` )
334367 const stdout = execSync ( 'shopify app env show' , { encoding : 'utf8' } )
335368 for ( const match of stdout . matchAll ( / ^ \s * ( \w + ) = ( .* ) / mg) ) {
336369 if ( match [ 1 ] === 'SHOPIFY_API_SECRET' ) {
@@ -348,15 +381,15 @@ GDF.extend(class extends GDF {
348381 }
349382
350383 // update config for Shopify apps
351- flyShopifyConfig ( app ) {
352- const original = fs . readFileSync ( 'shopify.app.toml' , 'utf-8' )
384+ flyShopifyConfig ( app , configFile ) {
385+ const original = fs . readFileSync ( configFile , 'utf-8' )
353386 const url = `https://${ app } .fly.dev`
354387 const config = original . replaceAll ( / " h t t p s : \/ \/ [ - \w . ] + / g, '"' + url )
355388 . replace ( / ( r e d i r e c t _ u r l s \s * = \s * \[ ) .* ?\] / s,
356389 `$1\n "${ url } /auth/callback",\n "${ url } /auth/shopify/callback",\n "${ url } /api/auth/callback"\n]` )
357390 if ( original !== config ) {
358391 console . log ( `${ chalk . bold . green ( 'update' . padStart ( 11 , ' ' ) ) } shopify.app.toml` )
359- fs . writeFileSync ( 'shopify.app.toml' , config )
392+ fs . writeFileSync ( configFile , config )
360393 console . log ( `${ chalk . bold . green ( 'execute' . padStart ( 11 ) ) } shopify app deploy --force` )
361394 execSync ( 'shopify app deploy --force' , { stdio : 'inherit' } )
362395 }
0 commit comments