Skip to content

Commit

Permalink
fix: clear dist directory before Dev for Nuxt (#6242)
Browse files Browse the repository at this point in the history
* fix: clear `dist` directory before Dev for Nuxt

* fix: get clearPublishDirectory from build-info settings
  • Loading branch information
Skn0tt committed Dec 20, 2023
1 parent a882392 commit 6014df5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
31 changes: 21 additions & 10 deletions src/utils/detect-server-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -240,28 +241,38 @@ const handleCustomFramework = ({ devConfig, workingDir }) => {
* @param {string} config.workingDir
* @param {Partial<import('./types.js').BaseServerSettings>=} 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<string, string | undefined>
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,
}
}

Expand Down
30 changes: 19 additions & 11 deletions src/utils/framework-server.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import { rm } from 'node:fs/promises'

import waitPort from 'wait-port'

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<StartReturnObject>}
*/
// @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<StartReturnObject> {
if (settings.useStaticServer) {
if (settings.command) {
runCommand(settings.command, { env: settings.env, cwd })
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/utils/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

2 comments on commit 6014df5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

  • Dependency count: 1,399
  • Package size: 405 MB
  • Number of ts-expect-error directives: 1,282

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

  • Dependency count: 1,399
  • Package size: 405 MB
  • Number of ts-expect-error directives: 1,282

Please sign in to comment.