Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 45 additions & 11 deletions fly.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import crypto from 'node:crypto'
import fs from 'node:fs'
import inquirer from 'inquirer'
import path from 'node:path'
import { execSync } from 'node:child_process'

Expand All @@ -9,7 +10,7 @@ import { GDF } from './gdf.js'

// Fly.io mixin
GDF.extend(class extends GDF {
run() {
async run() {
if (!this.flySetup()) return

// create volume for sqlite3
Expand All @@ -27,8 +28,11 @@ GDF.extend(class extends GDF {

// set secrets, healthcheck for remix apps
if (this.shopify) {
this.flyShopifyEnv(this.flyApp)
this.flyShopifyConfig(this.flyApp)
const shopifyConfig = await this.selectShopifyConfig()
if (!shopifyConfig) {
this.flyShopifyEnv(this.flyApp, shopifyConfig)
this.flyShopifyConfig(this.flyApp, shopifyConfig)
}
} else if (this.remix) {
this.flyRemixSecrets(this.flyApp)
this.flyHealthCheck('/healthcheck')
Expand Down Expand Up @@ -311,16 +315,46 @@ GDF.extend(class extends GDF {
})
}

async selectShopifyConfig() {
// Search for both shopify.app.toml and shopify.app.*.toml
const files = fs.readdirSync('.')
.filter(file => file.startsWith('shopify.app.') && file.endsWith('.toml'))
.sort()

if (files.length === 0) {
return null
}

if (files.length === 1) {
return files[0]
}

// Multiple files found, prompt user to select one
const { selectedFile } = await inquirer.prompt([
{
type: 'list',
name: 'selectedFile',
message: 'Multiple configuration files found. Please select one:',
choices: files.map(file => ({
name: file,
value: file
}))
}
])

return selectedFile
}

// set environment and secrets for Shopify apps
flyShopifyEnv(app) {
flyShopifyEnv(app, configFile) {
let toml = ''
if (fs.existsSync('shopify.app.toml')) {
toml = fs.readFileSync('shopify.app.toml', 'utf-8')
if (fs.existsSync(configFile)) {
toml = fs.readFileSync(configFile, 'utf-8')
}

if (!toml.includes('client_id')) {
this.setExit(42)
console.log(`${chalk.bold.red('shopify.app.toml')} is not complete; run ${chalk.bold.blue('shopify app config create')} first.`)
console.log(`${chalk.bold.red(configFile)} is not complete; run ${chalk.bold.blue('shopify app config create')} first.`)
return
}

Expand All @@ -330,7 +364,7 @@ GDF.extend(class extends GDF {
}

try {
console.log(`${chalk.bold.green('execute'.padStart(11))} shopify app env show`)
console.log(`${chalk.bold.green('execute'.padStart(11))} shopify app env show --config ${configFile}`)
const stdout = execSync('shopify app env show', { encoding: 'utf8' })
for (const match of stdout.matchAll(/^\s*(\w+)=(.*)/mg)) {
if (match[1] === 'SHOPIFY_API_SECRET') {
Expand All @@ -348,15 +382,15 @@ GDF.extend(class extends GDF {
}

// update config for Shopify apps
flyShopifyConfig(app) {
const original = fs.readFileSync('shopify.app.toml', 'utf-8')
flyShopifyConfig(app, configFile) {
const original = fs.readFileSync(configFile, 'utf-8')
const url = `https://${app}.fly.dev`
const config = original.replaceAll(/"https:\/\/[-\w.]+/g, '"' + url)
.replace(/(redirect_urls\s*=\s*\[).*?\]/s,
`$1\n "${url}/auth/callback",\n "${url}/auth/shopify/callback",\n "${url}/api/auth/callback"\n]`)
if (original !== config) {
console.log(`${chalk.bold.green('update'.padStart(11, ' '))} shopify.app.toml`)
fs.writeFileSync('shopify.app.toml', config)
fs.writeFileSync(configFile, config)
console.log(`${chalk.bold.green('execute'.padStart(11))} shopify app deploy --force`)
execSync('shopify app deploy --force', { stdio: 'inherit' })
}
Expand Down
2 changes: 1 addition & 1 deletion gdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ export class GDF {

// run mixin runners
for (const runner of GDF.runners) {
runner.apply(this)
await runner.apply(this)
}

if (this.#exitCode) process.exit(this.#exitCode)
Expand Down
Loading