Skip to content

Commit

Permalink
Merge branch 'netlify:main' into refactor/replace-got/functions-with-…
Browse files Browse the repository at this point in the history
…args.test.js
  • Loading branch information
hereje authored Dec 13, 2023
2 parents 6866af2 + d5fcdc5 commit 696e5dd
Show file tree
Hide file tree
Showing 16 changed files with 175 additions and 231 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).



## [17.10.1](https://github.com/netlify/cli/compare/v17.10.0...v17.10.1) (2023-12-08)


### Bug Fixes

* use correct file extensions for transpilation ([#6256](https://github.com/netlify/cli/issues/6256)) ([8308097](https://github.com/netlify/cli/commit/8308097334fc76117cee4798772abf18ff5a7e4d))

## [17.10.0](https://github.com/netlify/cli/compare/v17.9.0...v17.10.0) (2023-12-06)


Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "netlify-cli",
"description": "Netlify command line tool",
"version": "17.10.0",
"version": "17.10.1",
"author": "Netlify Inc.",
"type": "module",
"engines": {
Expand Down
221 changes: 79 additions & 142 deletions src/commands/base-command.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/commands/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { getGeoCountryArgParser } from '../../utils/validation.js'
import BaseCommand from '../base-command.js'

import { createDevExecCommand } from './dev-exec.js'
import { type DevConfig } from './types.js'

/**
*
Expand Down Expand Up @@ -90,7 +91,6 @@ export const dev = async (options: OptionValues, command: BaseCommand) => {
const { api, cachedConfig, config, repositoryRoot, site, siteInfo, state } = command.netlify
config.dev = { ...config.dev }
config.build = { ...config.build }
/** @type {import('./types.js').DevConfig} */
const devConfig = {
framework: '#auto',
autoLaunch: Boolean(options.open),
Expand All @@ -99,7 +99,7 @@ export const dev = async (options: OptionValues, command: BaseCommand) => {
...(config.build.base && { base: config.build.base }),
...config.dev,
...options,
}
} as DevConfig

let { env } = cachedConfig

Expand Down
5 changes: 0 additions & 5 deletions src/commands/link/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ or run ${chalk.cyanBright('netlify sites:create')} to create a site.`)
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
if (error_.status === 404) {
// @ts-expect-error TS(2345) FIXME: Argument of type 'Error' is not assignable to para... Remove this comment to see the full error message
error(new Error(`Site ID '${siteId}' not found`))
} else {
// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
Expand All @@ -225,7 +224,6 @@ or run ${chalk.cyanBright('netlify sites:create')} to create a site.`)
}

if (!site) {
// @ts-expect-error TS(2345) FIXME: Argument of type 'Error' is not assignable to para... Remove this comment to see the full error message
error(new Error(`No site found`))
}

Expand Down Expand Up @@ -285,7 +283,6 @@ export const link = async (options: OptionValues, command: BaseCommand) => {
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
if (error_.status === 404) {
// @ts-expect-error TS(2345) FIXME: Argument of type 'Error' is not assignable to para... Remove this comment to see the full error message
error(new Error(`Site id ${options.id} not found`))
} else {
// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
Expand Down Expand Up @@ -315,7 +312,6 @@ export const link = async (options: OptionValues, command: BaseCommand) => {
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
if (error_.status === 404) {
// @ts-expect-error TS(2345) FIXME: Argument of type 'Error' is not assignable to para... Remove this comment to see the full error message
error(new Error(`${options.name} not found`))
} else {
// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
Expand All @@ -324,7 +320,6 @@ export const link = async (options: OptionValues, command: BaseCommand) => {
}

if (results.length === 0) {
// @ts-expect-error TS(2345) FIXME: Argument of type 'Error' is not assignable to para... Remove this comment to see the full error message
error(new Error(`No sites found named ${options.name}`))
}
const [firstSiteData] = results
Expand Down
4 changes: 2 additions & 2 deletions src/commands/serve/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import { generateInspectSettings, startProxyServer } from '../../utils/proxy-ser
import { runBuildTimeline } from '../../utils/run-build.js'
import type { ServerSettings } from '../../utils/types.js'
import BaseCommand from '../base-command.js'
import { type DevConfig } from '../dev/types.js'

export const serve = async (options: OptionValues, command: BaseCommand) => {
const { api, cachedConfig, config, repositoryRoot, site, siteInfo, state } = command.netlify
config.dev = { ...config.dev }
config.build = { ...config.build }
/** @type {import('../dev/types').DevConfig} */
const devConfig = {
...(config.functionsDirectory && { functions: config.functionsDirectory }),
...(config.build.publish && { publish: config.build.publish }),
Expand All @@ -40,7 +40,7 @@ export const serve = async (options: OptionValues, command: BaseCommand) => {
// Override the `framework` value so that we start a static server and not
// the framework's development server.
framework: '#static',
}
} as DevConfig

let { env } = cachedConfig

Expand Down
26 changes: 20 additions & 6 deletions src/commands/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import type { NetlifyAPI } from 'netlify'

import StateConfig from '../utils/state-config.js'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type $TSFixMe = any;

export type NetlifySite = {
root?: string
configPath?: string
Expand All @@ -11,21 +14,32 @@ export type NetlifySite = {
set id(id: string): void
}

type PatchedConfig = NetlifyTOML & {
functionsDirectory?: string
build: NetlifyTOML['build'] & {
functionsSource?: string
}
dev: NetlifyTOML['dev'] & {
functions?: string
}
}

/**
* The netlify object inside each command with the state
*/
export type NetlifyOptions = {
api: NetlifyAPI
apiOpts: unknown
// poorly duck type the missing api functions
api: NetlifyAPI & Record<string, (...args: $TSFixMe) => Promise<$TSFixMe>>
apiOpts: $TSFixMe
repositoryRoot: string
/** Absolute path of the netlify configuration file */
configFilePath: string
/** Relative path of the netlify configuration file */
relConfigFilePath: string
site: NetlifySite
siteInfo: unknown
config: NetlifyTOML
cachedConfig: Record<string, unknown>
globalConfig: unknown
siteInfo: $TSFixMe
config: PatchedConfig
cachedConfig: Record<string, $TSFixMe>
globalConfig: $TSFixMe
state: StateConfig
}
2 changes: 1 addition & 1 deletion src/lib/functions/netlify-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default class NetlifyFunction {
}

if (extension === '.js') {
return '.js'
return '.mjs'
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/functions/runtimes/js/builders/zisi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export default async function handler({ config, directory, errorExit, func, meta
featureFlags.zisi_pure_esm_mjs = true
} else {
// We must use esbuild for certain file extensions.
const mustTranspile = ['.js', '.ts', '.mts', '.cts'].includes(path.extname(func.mainFile))
const mustTranspile = ['.mjs', '.ts', '.mts', '.cts'].includes(path.extname(func.mainFile))
const mustUseEsbuild = hasTypeModule || mustTranspile

if (mustUseEsbuild && !functionsConfig['*'].nodeBundler) {
Expand Down
13 changes: 3 additions & 10 deletions src/utils/command-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,23 +184,16 @@ export const warn = (message = '') => {
log(` ${bang} Warning: ${message}`)
}

/**
* throws an error or log it
* @param {unknown} message
* @param {object} [options]
* @param {boolean} [options.exit]
*/
export const error = (message = '', options = {}) => {
/** Throws an error or logs it */
export const error = (message: Error | string = '', options: { exit?: boolean } = {}) => {
const err =
// @ts-expect-error TS(2358) FIXME: The left-hand side of an 'instanceof' expression m... Remove this comment to see the full error message
message instanceof Error
? message
: // eslint-disable-next-line unicorn/no-nested-ternary
typeof message === 'string'
? new Error(message)
: /** @type {Error} */ { message, stack: undefined, name: 'Error' }
: { message, stack: undefined, name: 'Error' }

// @ts-expect-error TS(2339) FIXME: Property 'exit' does not exist on type '{}'.
if (options.exit === false) {
const bang = chalk.red(BANG)
if (process.env.DEBUG) {
Expand Down
52 changes: 26 additions & 26 deletions src/utils/detect-server-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import { readFile } from 'fs/promises'
import { EOL } from 'os'
import { dirname, relative, resolve } from 'path'

import { getFramework, getSettings } from '@netlify/build-info'
import { Project, Settings, getFramework, getSettings } from '@netlify/build-info'
import type { OptionValues } from 'commander'
import getPort from 'get-port'

import BaseCommand from '../commands/base-command.js'
import { type DevConfig } from '../commands/dev/types.js'

import { detectFrameworkSettings } from './build-info.js'
import { NETLIFYDEVWARN, chalk, log } from './command-helpers.js'
import { acquirePort } from './dev.js'
import { getInternalFunctionsDir } from './functions/functions.js'
import { getPluginsToAutoInstall } from './init/utils.js'
import { BaseServerSettings, ServerSettings } from './types.js'

/** @param {string} str */
// @ts-expect-error TS(7006) FIXME: Parameter 'str' implicitly has an 'any' type.
Expand Down Expand Up @@ -180,11 +185,8 @@ const handleStaticServer = async ({ devConfig, flags, workingDir }) => {

/**
* Retrieves the settings from a framework
* @param {import('@netlify/build-info').Settings} [settings]
* @returns {import('./types.js').BaseServerSettings | undefined}
*/
// @ts-expect-error TS(7006) FIXME: Parameter 'settings' implicitly has an 'any' type.
const getSettingsFromDetectedSettings = (settings) => {
const getSettingsFromDetectedSettings = (command: BaseCommand, settings?: Settings) => {
if (!settings) {
return
}
Expand All @@ -196,7 +198,7 @@ const getSettingsFromDetectedSettings = (settings) => {
framework: settings.framework.name,
env: settings.env,
pollingStrategies: settings.pollingStrategies,
plugins: getPluginsToAutoInstall(settings.plugins_from_config_file, settings.plugins_recommended),
plugins: getPluginsToAutoInstall(command, settings.plugins_from_config_file, settings.plugins_recommended),
}
}

Expand Down Expand Up @@ -265,32 +267,29 @@ const mergeSettings = async ({ devConfig, frameworkSettings = {}, workingDir })

/**
* Handles a forced framework and retrieves the settings for it
* @param {object} config
* @param {import('../commands/dev/types.js').DevConfig} config.devConfig
* @param {import('@netlify/build-info').Project} config.project
* @param {string} config.workingDir
* @param {string=} config.workspacePackage
* @returns {Promise<import('./types.js').BaseServerSettings>}
*/
// @ts-expect-error TS(7031) FIXME: Binding element 'devConfig' implicitly has an 'any... Remove this comment to see the full error message
const handleForcedFramework = async ({ devConfig, project, workingDir, workspacePackage }) => {
const handleForcedFramework = async (options: {
command: BaseCommand
devConfig: DevConfig
project: Project
workingDir: string
workspacePackage?: string
}): Promise<BaseServerSettings> => {
// this throws if `devConfig.framework` is not a supported framework
const framework = await getFramework(devConfig.framework, project)
const settings = await getSettings(framework, project, workspacePackage || '')
const frameworkSettings = getSettingsFromDetectedSettings(settings)
return mergeSettings({ devConfig, workingDir, frameworkSettings })
const framework = await getFramework(options.devConfig.framework, options.project)
const settings = await getSettings(framework, options.project, options.workspacePackage || '')
const frameworkSettings = getSettingsFromDetectedSettings(options.command, settings)
return mergeSettings({ devConfig: options.devConfig, workingDir: options.workingDir, frameworkSettings })
}

/**
* Get the server settings based on the flags and the devConfig
* @param {import('../commands/dev/types.js').DevConfig} devConfig
* @param {import('commander').OptionValues} flags
* @param {import('../commands/base-command.js').default} command
* @returns {Promise<import('./types.js').ServerSettings>}
*/

// @ts-expect-error TS(7006) FIXME: Parameter 'devConfig' implicitly has an 'any' type... Remove this comment to see the full error message
const detectServerSettings = async (devConfig, flags, command) => {
const detectServerSettings = async (
devConfig: DevConfig,
flags: OptionValues,
command: BaseCommand,
): Promise<ServerSettings> => {
validateProperty(devConfig, 'framework', 'string')

/** @type {Partial<import('./types.js').BaseServerSettings>} */
Expand All @@ -304,7 +303,7 @@ const detectServerSettings = async (devConfig, flags, command) => {

const runDetection = !hasCommandAndTargetPort(devConfig)
const frameworkSettings = runDetection
? getSettingsFromDetectedSettings(await detectFrameworkSettings(command, 'dev'))
? getSettingsFromDetectedSettings(command, await detectFrameworkSettings(command, 'dev'))
: undefined
if (frameworkSettings === undefined && runDetection) {
log(`${NETLIFYDEVWARN} No app server detected. Using simple static server`)
Expand All @@ -325,6 +324,7 @@ const detectServerSettings = async (devConfig, flags, command) => {
validateFrameworkConfig({ devConfig })
// this is when the user explicitly configures a framework, e.g. `framework = "gatsby"`
settings = await handleForcedFramework({
command,
devConfig,
project: command.project,
workingDir: command.workingDir,
Expand Down
Loading

0 comments on commit 696e5dd

Please sign in to comment.