-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(deploy): handle server file #10061
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
import path from 'path' | ||
|
||
import execa from 'execa' | ||
import fs from 'fs-extra' | ||
import terminalLink from 'terminal-link' | ||
|
||
import { handler as apiServerHandler } from '@redwoodjs/api-server/dist/apiCLIConfigHandler' | ||
import { recordTelemetryAttributes } from '@redwoodjs/cli-helpers' | ||
import { getConfig } from '@redwoodjs/project-config' | ||
|
||
import { getPaths } from '../../lib' | ||
import { getPaths } from '@redwoodjs/project-config' | ||
|
||
export const command = 'flightcontrol <side>' | ||
export const alias = 'fc' | ||
export const description = | ||
'Build, Migrate, and Serve commands for Flightcontrol deploy' | ||
|
||
export const builder = (yargs) => { | ||
yargs | ||
.positional('side', { | ||
choices: ['api', 'web'], | ||
description: 'select side to build', | ||
description: 'Side to deploy', | ||
type: 'string', | ||
}) | ||
.option('prisma', { | ||
|
@@ -31,7 +30,7 @@ export const builder = (yargs) => { | |
default: false, | ||
}) | ||
.option('data-migrate', { | ||
description: 'Migrate the data in your database', | ||
description: 'Apply data migrations', | ||
type: 'boolean', | ||
default: true, | ||
alias: 'dm', | ||
|
@@ -55,45 +54,53 @@ export const handler = async ({ side, serve, prisma, dm: dataMigrate }) => { | |
const rwjsPaths = getPaths() | ||
|
||
const execaConfig = { | ||
cwd: rwjsPaths.base, | ||
shell: true, | ||
stdio: 'inherit', | ||
cwd: rwjsPaths.base, | ||
extendEnv: true, | ||
cleanup: true, | ||
} | ||
|
||
async function runApiCommands() { | ||
if (serve) { | ||
console.log('\nStarting api...') | ||
await apiServerHandler({ | ||
port: getConfig().api?.port || 8911, | ||
apiRootPath: '/', | ||
}) | ||
} else { | ||
console.log('\nBuilding api...') | ||
execa.sync('yarn rw build api', execaConfig) | ||
if (!serve) { | ||
console.log('Building api...') | ||
execa.commandSync('yarn rw build api --verbose', execaConfig) | ||
|
||
prisma && | ||
execa.sync( | ||
path.join(rwjsPaths.base, 'node_modules/.bin/prisma'), | ||
['migrate', 'deploy', '--schema', `"${rwjsPaths.api.dbSchema}"`], | ||
if (prisma) { | ||
console.log('Running database migrations...') | ||
execa.commandSync( | ||
`node_modules/.bin/prisma migrate deploy --schema "${rwjsPaths.api.dbSchema}"`, | ||
execaConfig | ||
) | ||
dataMigrate && execa.sync('yarn rw dataMigrate up', execaConfig) | ||
} | ||
|
||
if (dataMigrate) { | ||
console.log('Running data migrations...') | ||
execa.commandSync('yarn rw dataMigrate up', execaConfig) | ||
} | ||
|
||
return | ||
} | ||
|
||
const serverFilePath = path.join(rwjsPaths.api.dist, 'server.js') | ||
const hasServerFile = fs.pathExistsSync(serverFilePath) | ||
Comment on lines
+83
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still checking dist specifically here because this command is called after build |
||
|
||
if (hasServerFile) { | ||
execa(`yarn node ${serverFilePath}`, execaConfig) | ||
} else { | ||
const { handler } = await import( | ||
'@redwoodjs/api-server/dist/apiCLIConfigHandler.js' | ||
) | ||
handler() | ||
} | ||
} | ||
|
||
async function runWebCommands() { | ||
execa.sync('yarn rw build web', execaConfig) | ||
console.log('Building web...') | ||
execa.commandSync('yarn rw build web --verbose', execaConfig) | ||
} | ||
|
||
if (side === 'api') { | ||
runApiCommands() | ||
} else if (side === 'web') { | ||
console.log('\nBuilding web...') | ||
runWebCommands() | ||
} else { | ||
console.log('Error with arguments provided') | ||
// you broke something, which should be caught by Yargs | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ import fs from 'fs-extra' | |
import terminalLink from 'terminal-link' | ||
|
||
import { getPaths } from '../../lib' | ||
import { isTypeScriptProject } from '../../lib/project' | ||
import { isTypeScriptProject, serverFileExists } from '../../lib/project' | ||
|
||
const link = (topicId, isTerminal = false) => { | ||
const communityLink = `https://community.redwoodjs.com/t/${topicId}` | ||
|
@@ -44,17 +44,8 @@ export const printTaskEpilogue = (command, description, topicId) => { | |
) | ||
} | ||
|
||
export const serverFileExists = () => { | ||
const serverFilePath = path.join( | ||
getPaths().api.src, | ||
`server.${isTypeScriptProject() ? 'ts' : 'js'}` | ||
) | ||
|
||
return fs.existsSync(serverFilePath) | ||
} | ||
|
||
export const isServerFileSetup = () => { | ||
if (!serverFileExists) { | ||
if (!serverFileExists()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems to have been a bug here where we were just checking the truthiness of a function |
||
throw new Error( | ||
'RedwoodJS Realtime requires a serverful environment. Please run `yarn rw exp setup-server-file` first.' | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import { | |
import { errorTelemetry } from '@redwoodjs/telemetry' | ||
|
||
import { printSetupNotes } from '../../../../lib' | ||
import { serverFileExists } from '../../../../lib/project' | ||
import { addFilesTask } from '../helpers' | ||
|
||
const redwoodProjectPaths = getPaths() | ||
|
@@ -106,11 +107,8 @@ async function getCoherenceConfigFileContent() { | |
db = 'postgres' | ||
} | ||
|
||
const hasServerFile = fs.pathExistsSync( | ||
path.join(getPaths().api.dist, 'server.js') | ||
) | ||
const apiProdCommand = ['yarn', 'rw', 'build', 'api', '&&'] | ||
if (!hasServerFile) { | ||
if (serverFileExists()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic was flipped here and was a bug There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tested locally |
||
apiProdCommand.push( | ||
'yarn', | ||
'node', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,7 @@ services: | |
plan: free | ||
env: node | ||
region: oregon | ||
buildCommand: corepack enable && yarn && yarn rw build api | ||
buildCommand: corepack enable && yarn install && yarn rw build api | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just stylistic to be consistent with the web build command above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tested locally |
||
startCommand: yarn rw deploy render api | ||
envVars: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These default to true