Skip to content

Commit

Permalink
feat(CLI): add check node version middleware, rm .nvmrc, yarn engin…
Browse files Browse the repository at this point in the history
…es (#9728)

Continuation of #8907.
Following up on our meeting today (@Tobbe, @thedavidprice; this PR
doesn't implement the configurable engines functionality yet).

This PR

- removes the `.nvmrc` file from CRWA and test project fixtures

Setup deploy commands should add node version files if they need them,
preferably something nvm agnostic like Netlify's `.node-version`

- removes yarn from engines in CRWA's root package.json since it doesn't
do anything and just creates confusion

- adds a node version check to build and dev

This piece of middleware is lightweight; it doesn't involve a child
process or the file system, it just checks against `process.version`. I
also removed the babel check because it's been ages since we've made
that change and original concerns against checking for the node version
were about adding overhead tot he CLI. So let's remove unnecessary
middleware if we're going to add more

Right now, the node version check just emits a warning. Should it error
out? (I should've taken better notes.)

Build:

![image](https://github.com/redwoodjs/redwood/assets/32992335/e0d2ced6-7b52-448e-96a1-08a45aac64e0)

Dev:

![image](https://github.com/redwoodjs/redwood/assets/32992335/d78069eb-bd6c-4ac9-b936-b90f704b5c5c)

- [ ] research deploy providers and update `yarn rw setup deploy`
commands
- [x] settle on warning or error for build and dev
- [x] settle on the contents of the message
- [x] update CRWA

- [x] baremetal

  Pretty sure it's completely up to you.

- [x] coherence

Via Nixpacks, `engines.node` in the root package.json:
https://nixpacks.com/docs/providers/node#setup.

- [x] flightcontrol

Via Nixpacks, `engines.node` in the root package.json:
https://www.flightcontrol.dev/docs/getting-started/javascript/setting-node-version#using-the-packagejson-file.

- [x] netlify

Via the `.node-version` file, or via in `netlify.toml`; see
https://docs.netlify.com/configure-builds/manage-dependencies/#node-js-and-javascript,
https://docs.netlify.com/configure-builds/file-based-configuration/#sample-netlify-toml-file.

- [ ] vercel

Takes its cue from engines.node in the root package.json:
https://vercel.com/docs/functions/serverless-functions/runtimes/node-js#version-overrides-in-package.json.

- [x] render

Same as netlify more or less, but also respects `engines.node` in the
root package.json: https://docs.render.com/docs/node-version.
  • Loading branch information
jtoar authored and Tobbe committed Dec 22, 2023
1 parent ad20084 commit f88ee70
Show file tree
Hide file tree
Showing 19 changed files with 147 additions and 129 deletions.
1 change: 0 additions & 1 deletion __fixtures__/test-project/.nvmrc

This file was deleted.

3 changes: 1 addition & 2 deletions __fixtures__/test-project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
"root": true
},
"engines": {
"node": "=20.x",
"yarn": ">=1.22.21"
"node": "=20.x"
},
"prisma": {
"seed": "yarn rw exec seed"
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/tutorial/chapter1/prerequisites.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ You could definitely learn them all at once, but it will be harder to determine

### Redwood Versions

You will want to be on at least version 5.0.0 to complete the tutorial. If this is your first time using Redwood then no worries: the latest version will be installed automatically when you create your app skeleton!
You will want to be on at least version 7.0.0 to complete the tutorial. If this is your first time using Redwood then no worries: the latest version will be installed automatically when you create your app skeleton!

If you have an existing site created with a prior version, you'll need to upgrade and (most likely) apply code modifications. Follow this two step process:

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/tutorial/foreword.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ It's usually something that goes into more detail about a specific point, refers

:::info

This tutorial assumes you are using version 5.0.0 or greater of RedwoodJS.
This tutorial assumes you are using version 7.0.0 or greater of RedwoodJS.

:::

Expand Down
17 changes: 15 additions & 2 deletions packages/cli/src/commands/build.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import terminalLink from 'terminal-link'

import c from '../lib/colors'
import { exitWithError } from '../lib/exit'
import { sides } from '../lib/project'
import checkForBabelConfig from '../middleware/checkForBabelConfig'
import { checkNodeVersion } from '../middleware/checkNodeVersion'

export const command = 'build [side..]'
export const description = 'Build for production'
Expand Down Expand Up @@ -47,7 +49,18 @@ export const builder = (yargs) => {
default: false,
description: 'Measure build performance',
})
.middleware(checkForBabelConfig)
.middleware(() => {
const check = checkNodeVersion()

if (check.ok) {
return
}

exitWithError(undefined, {
message: `${c.error('Error')}: ${check.message}`,
includeEpilogue: false,
})
})
.epilogue(
`Also see the ${terminalLink(
'Redwood CLI Reference',
Expand Down
14 changes: 12 additions & 2 deletions packages/cli/src/commands/dev.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import terminalLink from 'terminal-link'

import checkForBabelConfig from '../middleware/checkForBabelConfig'
import c from '../lib/colors'
import { checkNodeVersion } from '../middleware/checkNodeVersion'

export const command = 'dev [side..]'
export const description = 'Start development servers for api, and web'

export const builder = (yargs) => {
yargs
.positional('side', {
Expand Down Expand Up @@ -32,7 +34,15 @@ export const builder = (yargs) => {
description:
'Port on which to expose API server debugger. If you supply the flag with no value it defaults to 18911.',
})
.middleware(checkForBabelConfig)
.middleware(() => {
const check = checkNodeVersion()

if (check.ok) {
return
}

console.warn(`${c.warning('Warning')}: ${check.message}\n`)
})
.epilogue(
`Also see the ${terminalLink(
'Redwood CLI Reference',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,27 @@ exports[`netlify should add netlify.toml 1`] = `
publish = "web/dist"
functions = "api/dist/functions"
[dev]
# To use [Netlify Dev](https://www.netlify.com/products/dev/),
# install netlify-cli from https://docs.netlify.com/cli/get-started/#installation
# and then use netlify link https://docs.netlify.com/cli/get-started/#link-and-unlink-sites
# to connect your local project to a site already on Netlify
# then run netlify dev and our app will be accessible on the port specified below
framework = "redwoodjs"
# Set targetPort to the [web] side port as defined in redwood.toml
targetPort = 8910
# Point your browser to this port to access your RedwoodJS app
port = 8888
[build.environment]
NODE_VERSION = "20"
[[redirects]]
from = "/*"
to = "/200.html"
status = 200
# To use Netlify Dev, install Netlify's CLI (\`netlify-cli\`) from NPM and use \`netlify link\`
# to connect your local project to a site on Netlify. Then run \`netlify dev\`.
#
# Quick links to the docs:
# - Netlfy Dev https://www.netlify.com/products/dev
# - Netlify's CLI https://docs.netlify.com/cli/get-started/#installation
# - \`netlify link\` https://docs.netlify.com/cli/get-started/#link-and-unlink-sites
[dev]
framework = "redwoodjs"
# Make sure \`targetPort\` matches \`web.port\` in the \`redwood.toml\`:
targetPort = 8910
# Point your browser to this port to access your app:
port = 8888
"
`;

Expand All @@ -31,21 +36,26 @@ exports[`netlify should call the handler without error 1`] = `
publish = "web/dist"
functions = "api/dist/functions"
[dev]
# To use [Netlify Dev](https://www.netlify.com/products/dev/),
# install netlify-cli from https://docs.netlify.com/cli/get-started/#installation
# and then use netlify link https://docs.netlify.com/cli/get-started/#link-and-unlink-sites
# to connect your local project to a site already on Netlify
# then run netlify dev and our app will be accessible on the port specified below
framework = "redwoodjs"
# Set targetPort to the [web] side port as defined in redwood.toml
targetPort = 8910
# Point your browser to this port to access your RedwoodJS app
port = 8888
[build.environment]
NODE_VERSION = "20"
[[redirects]]
from = "/*"
to = "/200.html"
status = 200
# To use Netlify Dev, install Netlify's CLI (\`netlify-cli\`) from NPM and use \`netlify link\`
# to connect your local project to a site on Netlify. Then run \`netlify dev\`.
#
# Quick links to the docs:
# - Netlfy Dev https://www.netlify.com/products/dev
# - Netlify's CLI https://docs.netlify.com/cli/get-started/#installation
# - \`netlify link\` https://docs.netlify.com/cli/get-started/#link-and-unlink-sites
[dev]
framework = "redwoodjs"
# Make sure \`targetPort\` matches \`web.port\` in the \`redwood.toml\`:
targetPort = 8910
# Point your browser to this port to access your app:
port = 8888
"
`;
30 changes: 18 additions & 12 deletions packages/cli/src/commands/setup/deploy/templates/netlify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@ import { getConfig } from '../../../../lib'

const config = getConfig()

export const NETLIFY_TOML = `[build]
export const NETLIFY_TOML = `\
[build]
command = "yarn rw deploy netlify"
publish = "web/dist"
functions = "api/dist/functions"
[dev]
# To use [Netlify Dev](https://www.netlify.com/products/dev/),
# install netlify-cli from https://docs.netlify.com/cli/get-started/#installation
# and then use netlify link https://docs.netlify.com/cli/get-started/#link-and-unlink-sites
# to connect your local project to a site already on Netlify
# then run netlify dev and our app will be accessible on the port specified below
framework = "redwoodjs"
# Set targetPort to the [web] side port as defined in redwood.toml
targetPort = ${config.web.port}
# Point your browser to this port to access your RedwoodJS app
port = 8888
[build.environment]
NODE_VERSION = "20"
[[redirects]]
from = "/*"
to = "/200.html"
status = 200
# To use Netlify Dev, install Netlify's CLI (\`netlify-cli\`) from NPM and use \`netlify link\`
# to connect your local project to a site on Netlify. Then run \`netlify dev\`.
#
# Quick links to the docs:
# - Netlfy Dev https://www.netlify.com/products/dev
# - Netlify's CLI https://docs.netlify.com/cli/get-started/#installation
# - \`netlify link\` https://docs.netlify.com/cli/get-started/#link-and-unlink-sites
[dev]
framework = "redwoodjs"
# Make sure \`targetPort\` matches \`web.port\` in the \`redwood.toml\`:
targetPort = ${config.web.port}
# Point your browser to this port to access your app:
port = 8888
`
44 changes: 22 additions & 22 deletions packages/cli/src/commands/setup/deploy/templates/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,29 @@ import { getPaths } from '../../../../lib'
export const PROJECT_NAME = path.basename(getPaths().base)

export const RENDER_YAML = (database) => {
return `#####
# Documentation
# Redwood: https://render.com/docs/deploy-redwood
# YAML (all config values): https://render.com/docs/yaml-spec
#####
return `# Quick links to the docs:
# - Redwood on Render: https://render.com/docs/deploy-redwood
# - Render's Blueprint spec: https://render.com/docs/yaml-spec
services:
- name: ${PROJECT_NAME}-web
type: web
env: static
buildCommand: yarn install && yarn rw deploy render web
buildCommand: corepack enable && yarn install && yarn rw deploy render web
staticPublishPath: ./web/dist
envVars:
- key: NODE_VERSION
value: 18
- key: SKIP_INSTALL_DEPS
value: true
routes:
- type: rewrite
source: /.redwood/functions/*
#####
# NOTE: replace destination api url after first deploy to Render
# example:
# destination: https://myredwoodproject-api.onrender.com/*
#####
# Replace \`destination\` here after your first deploy:
#
# \`\`\`
# destination: https://my-redwood-project-api.onrender.com/*
# \`\`\`
destination: replace_with_api_url/*
- type: rewrite
source: /*
Expand All @@ -40,35 +38,37 @@ services:
plan: free
env: node
region: oregon
buildCommand: yarn && yarn rw build api
buildCommand: corepack enable && yarn && yarn rw build api
startCommand: yarn rw deploy render api
envVars:
- key: NODE_VERSION
value: 18
${database}
`
}

export const POSTGRES_YAML = ` - key: DATABASE_URL
export const POSTGRES_YAML = `\
- key: DATABASE_URL
fromDatabase:
name: ${PROJECT_NAME}-db
property: connectionString
databases:
- name: ${PROJECT_NAME}-db
region: oregon
`
region: oregon`

export const SQLITE_YAML = ` - key: DATABASE_URL
export const SQLITE_YAML = `\
- key: DATABASE_URL
value: file:./data/sqlite.db
disk:
name: sqlite-data
mountPath: /opt/render/project/src/api/db/data
sizeGB: 1`

export const RENDER_HEALTH_CHECK = `// render-health-check
export const RENDER_HEALTH_CHECK = `\
// render-health-check
export const handler = async () => {
return {
statusCode: 200,
}
}`
}
`
28 changes: 15 additions & 13 deletions packages/cli/src/lib/exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,21 @@ export function exitWithError(
const line = chalk.red('-'.repeat(process.stderr.columns))

// Generate and print a nice message to the user
const content = [
line,
message,
includeEpilogue && `\n${line}`,
includeEpilogue && epilogue,
includeReferenceCode &&
` - Here's your unique error reference to quote: '${errorReferenceCode}'`,
line,
]
.filter(Boolean)
.join('\n')

console.error()
const content = !includeEpilogue
? message
: [
'',
line,
message,
`\n${line}`,
epilogue,
includeReferenceCode &&
` - Here's your unique error reference to quote: '${errorReferenceCode}'`,
line,
]
.filter(Boolean)
.join('\n')

console.error(content)

// Record the error in telemetry
Expand Down
41 changes: 0 additions & 41 deletions packages/cli/src/middleware/checkForBabelConfig.js

This file was deleted.

Loading

0 comments on commit f88ee70

Please sign in to comment.