From 6014df5d6ec27948247d2835c3bb41e33d265c16 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Wed, 20 Dec 2023 16:03:36 +0100 Subject: [PATCH] fix: clear `dist` directory before Dev for Nuxt (#6242) * fix: clear `dist` directory before Dev for Nuxt * fix: get clearPublishDirectory from build-info settings --- src/utils/detect-server-settings.ts | 31 +++++++++++++++++++---------- src/utils/framework-server.ts | 30 ++++++++++++++++++---------- src/utils/types.d.ts | 1 + 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/utils/detect-server-settings.ts b/src/utils/detect-server-settings.ts index 5fe75bea9cb..c46d40e322e 100644 --- a/src/utils/detect-server-settings.ts +++ b/src/utils/detect-server-settings.ts @@ -199,6 +199,7 @@ const getSettingsFromDetectedSettings = (command: BaseCommand, settings?: Settin env: settings.env, pollingStrategies: settings.pollingStrategies, plugins: getPluginsToAutoInstall(command, settings.plugins_from_config_file, settings.plugins_recommended), + clearPublishDirectory: settings.clearPublishDirectory, } } @@ -240,28 +241,38 @@ const handleCustomFramework = ({ devConfig, workingDir }) => { * @param {string} config.workingDir * @param {Partial=} config.frameworkSettings */ -// @ts-expect-error TS(7031) FIXME: Binding element 'devConfig' implicitly has an 'any... Remove this comment to see the full error message -const mergeSettings = async ({ devConfig, frameworkSettings = {}, workingDir }) => { - // @ts-expect-error TS(2339) FIXME: Property 'command' does not exist on type '{}'. +const mergeSettings = async ({ + devConfig, + frameworkSettings = {}, + workingDir, +}: { + devConfig: { command?: string; publish?: string; targetPort?: number; base?: string } + workingDir: string + frameworkSettings?: { + baseDirectory?: string + command?: string + dist?: string + env?: Record + framework?: string + frameworkPort?: number + pollingStrategies?: string[] + clearPublishDirectory?: boolean + } +}) => { const command = devConfig.command || frameworkSettings.command - // @ts-expect-error TS(2339) FIXME: Property 'frameworkPort' does not exist on type '{... Remove this comment to see the full error message const frameworkPort = devConfig.targetPort || frameworkSettings.frameworkPort - // if the framework doesn't start a server, we use a static one const useStaticServer = !(command && frameworkPort) + return { - // @ts-expect-error TS(2339) FIXME: Property 'baseDirectory' does not exist on type '{... Remove this comment to see the full error message baseDirectory: devConfig.base || frameworkSettings.baseDirectory, command, frameworkPort: useStaticServer ? await getStaticServerPort({ devConfig }) : frameworkPort, - // @ts-expect-error TS(2339) FIXME: Property 'dist' does not exist on type '{}'. dist: devConfig.publish || frameworkSettings.dist || getDefaultDist(workingDir), - // @ts-expect-error TS(2339) FIXME: Property 'framework' does not exist on type '{}'. framework: frameworkSettings.framework, - // @ts-expect-error TS(2339) FIXME: Property 'env' does not exist on type '{}'. env: frameworkSettings.env, - // @ts-expect-error TS(2339) FIXME: Property 'pollingStrategies' does not exist on typ... Remove this comment to see the full error message pollingStrategies: frameworkSettings.pollingStrategies || [], useStaticServer, + clearPublishDirectory: frameworkSettings.clearPublishDirectory, } } diff --git a/src/utils/framework-server.ts b/src/utils/framework-server.ts index 01cf4803a70..8d7ed452f74 100644 --- a/src/utils/framework-server.ts +++ b/src/utils/framework-server.ts @@ -1,3 +1,5 @@ +import { rm } from 'node:fs/promises' + import waitPort from 'wait-port' import { startSpinner, stopSpinner } from '../lib/spinner.js' @@ -5,24 +7,25 @@ import { startSpinner, stopSpinner } from '../lib/spinner.js' import { error, exit, log, NETLIFYDEVERR, NETLIFYDEVLOG } from './command-helpers.js' import { runCommand } from './shell.js' import { startStaticServer } from './static-server.js' +import { ServerSettings } from './types.js' // 10 minutes const FRAMEWORK_PORT_TIMEOUT = 6e5 -/** - * @typedef StartReturnObject - * @property {4 | 6 | undefined=} ipVersion The version the open port was found on - */ +interface StartReturnObject { + ipVersion?: 4 | 6 +} /** * Start a static server if the `useStaticServer` is provided or a framework specific server - * @param {object} config - * @param {import('./types.js').ServerSettings} config.settings - * @param {string} config.cwd - * @returns {Promise} */ -// @ts-expect-error TS(7031) FIXME: Binding element 'cwd' implicitly has an 'any' type... Remove this comment to see the full error message -export const startFrameworkServer = async function ({ cwd, settings }) { +export const startFrameworkServer = async function ({ + cwd, + settings, +}: { + cwd: string + settings: ServerSettings +}): Promise { if (settings.useStaticServer) { if (settings.command) { runCommand(settings.command, { env: settings.env, cwd }) @@ -38,12 +41,17 @@ export const startFrameworkServer = async function ({ cwd, settings }) { text: `Waiting for framework port ${settings.frameworkPort}. This can be configured using the 'targetPort' property in the netlify.toml`, }) + if (settings.clearPublishDirectory && settings.dist) { + await rm(settings.dist, { recursive: true, force: true }) + } + runCommand(settings.command, { env: settings.env, spinner, cwd }) let port try { port = await waitPort({ - port: settings.frameworkPort, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + port: settings.frameworkPort!, host: 'localhost', output: 'silent', timeout: FRAMEWORK_PORT_TIMEOUT, diff --git a/src/utils/types.d.ts b/src/utils/types.d.ts index a0157719070..d8bcef98bf9 100644 --- a/src/utils/types.d.ts +++ b/src/utils/types.d.ts @@ -43,4 +43,5 @@ export type ServerSettings = BaseServerSettings & { /** The port where the functions are running on */ port: number https?: { key: string; cert: string; keyFilePath: string; certFilePath: string } + clearPublishDirectory?: boolean }