Skip to content
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: use heuristics for build and deploy command as well #6729

Merged
Show file tree
Hide file tree
Changes from 8 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
9 changes: 2 additions & 7 deletions src/commands/build/build.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OptionValues } from 'commander'

import { getBuildOptions, runBuild } from '../../lib/build.js'
import { detectFrameworkSettings } from '../../utils/build-info.js'
import { detectFrameworkSettings, getDefaultConfig } from '../../utils/build-info.js'
import { error, exit, getToken } from '../../utils/command-helpers.js'
import { getEnvelopeEnv } from '../../utils/env/index.js'
import BaseCommand from '../base-command.js'
Expand Down Expand Up @@ -31,14 +31,9 @@ export const build = async (options: OptionValues, command: BaseCommand) => {
const [token] = await getToken()
const settings = await detectFrameworkSettings(command, 'build')

// override the build command with the detection result if no command is specified through the config
if (!cachedConfig.config.build.command) {
cachedConfig.config.build.command = settings?.buildCommand
cachedConfig.config.build.commandOrigin = 'heuristics'
}

const buildOptions = await getBuildOptions({
cachedConfig,
defaultConfig: getDefaultConfig(settings),
packagePath: command.workspacePackage,
currentDir: command.workingDir,
token,
Expand Down
6 changes: 5 additions & 1 deletion src/commands/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { featureFlags as edgeFunctionsFeatureFlags } from '../../lib/edge-functi
import { normalizeFunctionsConfig } from '../../lib/functions/config.js'
import { BACKGROUND_FUNCTIONS_WARNING } from '../../lib/log.js'
import { startSpinner, stopSpinner } from '../../lib/spinner.js'
import { detectFrameworkSettings, getDefaultConfig } from '../../utils/build-info.js'
import {
NETLIFYDEV,
NETLIFYDEVERR,
Expand Down Expand Up @@ -558,14 +559,15 @@ const runDeploy = async ({
* @returns
*/
// @ts-expect-error TS(7031) FIXME: Binding element 'cachedConfig' implicitly has an '... Remove this comment to see the full error message
const handleBuild = async ({ cachedConfig, currentDir, deployHandler, options, packagePath }) => {
const handleBuild = async ({ cachedConfig, currentDir, defaultConfig, deployHandler, options, packagePath }) => {
if (!options.build) {
return {}
}
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
const [token] = await getToken()
const resolvedOptions = await getBuildOptions({
cachedConfig,
defaultConfig,
packagePath,
token,
options,
Expand Down Expand Up @@ -784,6 +786,7 @@ export const deploy = async (options: OptionValues, command: BaseCommand) => {
const { workingDir } = command
const { api, site, siteInfo } = command.netlify
const alias = options.alias || options.branch
const settings = await detectFrameworkSettings(command, 'build')

command.setAnalyticsPayload({ open: options.open, prod: options.prod, json: options.json, alias: Boolean(alias) })

Expand Down Expand Up @@ -847,6 +850,7 @@ export const deploy = async (options: OptionValues, command: BaseCommand) => {
await handleBuild({
packagePath: command.workspacePackage,
cachedConfig: command.netlify.cachedConfig,
defaultConfig: getDefaultConfig(settings),
currentDir: command.workingDir,
options,
// @ts-expect-error TS(7031) FIXME: Binding element 'netlifyConfig' implicitly has an ... Remove this comment to see the full error message
Expand Down
3 changes: 3 additions & 0 deletions src/lib/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export const getBuildOptions = async ({
cachedConfig,
// @ts-expect-error TS(7031) FIXME: Binding element 'currentDir' implicitly has an 'an... Remove this comment to see the full error message
currentDir,
// @ts-expect-error TS(7031) FIXME: Binding element 'defaultConfig' implicitly has an '... Remove this comment to see the full error message
defaultConfig,
// @ts-expect-error TS(7031) FIXME: Binding element 'deployHandler' implicitly has an ... Remove this comment to see the full error message
deployHandler,
// @ts-expect-error TS(7031) FIXME: Binding element 'context' implicitly has an 'any' ... Remove this comment to see the full error message
Expand Down Expand Up @@ -71,6 +73,7 @@ export const getBuildOptions = async ({

return {
cachedConfig,
defaultConfig,
siteId: cachedConfig.siteInfo.id,
packagePath,
token,
Expand Down
29 changes: 29 additions & 0 deletions src/utils/build-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fuzzy from 'fuzzy'
import inquirer from 'inquirer'

import BaseCommand from '../commands/base-command.js'
import { $TSFixMe } from '../commands/types.js'

import { chalk, log } from './command-helpers.js'

Expand Down Expand Up @@ -122,3 +123,31 @@ command = "${chosenSettings.devCommand}"
return chosenSettings
}
}

/**
* Generates a defaultConfig for @netlify/build based on the settings from the heuristics
* Returns the defaultConfig in the format that @netlify/build expects (json version of toml)
* @param settings The settings from the heuristics
*/
export const getDefaultConfig = (settings?: Settings): $TSFixMe | undefined => {
if (!settings) {
return undefined
}

// TODO: We need proper types for the netlify configuration
const config: $TSFixMe = { build: {} }

if (settings.buildCommand) {
config.build.command = settings.buildCommand
config.build.commandOrigin = 'default'
}

if (settings.dist) {
config.build.publish = settings.dist
config.build.publishOrigin = 'default'
}

config.plugins = settings.plugins_recommended?.map((plugin) => ({ package: plugin })) || []

return config
}
4 changes: 3 additions & 1 deletion src/utils/deploy/deploy-site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ export const deploySite = async (
}

if (functionsWithNativeModules.length !== 0) {
const functionsWithNativeModulesMessage = functionsWithNativeModules.map(({ name }) => `- ${name}`).join('\n')
const functionsWithNativeModulesMessage = functionsWithNativeModules
.map(({ name }: { name: string }) => `- ${name}`)
.join('\n')
warn(`Modules with native dependencies\n
${functionsWithNativeModulesMessage}

Expand Down
4 changes: 2 additions & 2 deletions src/utils/deploy/hash-fns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const hashFns = async (
statusCb: $TSFixMe
tmpDir: $TSFixMe
},
) => {
): Promise<$TSFixMe> => {
const {
assetType = 'function',
concurrentHash,
Expand Down Expand Up @@ -166,8 +166,8 @@ const hashFns = async (
priority,
runtime,
runtimeVersion,
trafficRules,
timeout,
trafficRules,
}) => ({
filepath: functionPath,
root: tmpDir,
Expand Down
Loading