Skip to content

fix: drop filter-obj, path-exists, locate-path dependencies #6596

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 0 additions & 3 deletions package-lock.json

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

3 changes: 0 additions & 3 deletions packages/zip-it-and-ship-it/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,13 @@
"esbuild": "0.25.6",
"execa": "^8.0.0",
"fast-glob": "^3.3.3",
"filter-obj": "^6.0.0",
"find-up": "^7.0.0",
"is-path-inside": "^4.0.0",
"junk": "^4.0.0",
"locate-path": "^7.0.0",
"merge-options": "^3.0.4",
"minimatch": "^9.0.0",
"normalize-path": "^3.0.0",
"p-map": "^7.0.0",
"path-exists": "^5.0.0",
"precinct": "^12.0.0",
"require-package-name": "^2.0.1",
"resolve": "^2.0.0-next.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createRequire } from 'module'
import { version as nodeVersion } from 'process'

import { findUp } from 'find-up'
import { pathExists } from 'path-exists'
import { access } from 'fs/promises'
// @ts-expect-error doesnt export async
import { async as asyncResolve } from 'resolve'
import semver from 'semver'
Expand Down Expand Up @@ -106,9 +106,14 @@ const resolvePackageFallback = async function (moduleName: string, baseDirs: str
const isPackageDir = async function (moduleName: string, dir: string) {
// Need to use `endsWith()` to take into account `@scope/package`.
// Backslashes need to be converted for Windows.
if (!dir.replace(BACKSLASH_REGEXP, '/').endsWith(moduleName) || !(await pathExists(`${dir}/package.json`))) {
if (!dir.replace(BACKSLASH_REGEXP, '/').endsWith(moduleName)) {
return
}

return dir
try {
await access(`${dir}/package.json`)
return dir
} catch {
return
}
}
55 changes: 32 additions & 23 deletions packages/zip-it-and-ship-it/src/runtimes/node/finder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { Stats } from 'fs'
import { stat } from 'fs/promises'
import { join, dirname, basename, extname } from 'path'

import { locatePath } from 'locate-path'

import { SourceFile } from '../../function.js'
import { cachedLstat } from '../../utils/fs.js'
import { nonNullable } from '../../utils/non_nullable.js'
Expand Down Expand Up @@ -72,27 +71,24 @@ export const findFunctionInPath: FindFunctionInPathFunction = async function ({
// the same filename as its directory.
const getMainFile = async function (srcPath: string, filename: string, stat: Stats): Promise<string | undefined> {
if (stat.isDirectory()) {
return await locatePath(
[
// The order of these declarations is important, so that we always bundle the same way
// even if multiple files are found
join(srcPath, `${filename}.js`),
join(srcPath, 'index.js'),
join(srcPath, `${filename}.ts`),
join(srcPath, 'index.ts'),
join(srcPath, `${filename}.mjs`),
join(srcPath, 'index.mjs'),
join(srcPath, `${filename}.cjs`),
join(srcPath, 'index.cjs'),
join(srcPath, `${filename}.tsx`),
join(srcPath, 'index.tsx'),
join(srcPath, `${filename}.mts`),
join(srcPath, 'index.mts'),
join(srcPath, `${filename}.cts`),
join(srcPath, 'index.cts'),
],
{ type: 'file' },
)
return await locateFile([
// The order of these declarations is important, so that we always bundle the same way
// even if multiple files are found
join(srcPath, `${filename}.js`),
join(srcPath, 'index.js'),
join(srcPath, `${filename}.ts`),
join(srcPath, 'index.ts'),
join(srcPath, `${filename}.mjs`),
join(srcPath, 'index.mjs'),
join(srcPath, `${filename}.cjs`),
join(srcPath, 'index.cjs'),
join(srcPath, `${filename}.tsx`),
join(srcPath, 'index.tsx'),
join(srcPath, `${filename}.mts`),
join(srcPath, 'index.mts'),
join(srcPath, `${filename}.cts`),
join(srcPath, 'index.cts'),
])
}

const extension = extname(srcPath)
Expand All @@ -101,3 +97,16 @@ const getMainFile = async function (srcPath: string, filename: string, stat: Sta
return srcPath
}
}

const locateFile = async (paths: string[]): Promise<string | undefined> => {
for (const path of paths) {
try {
const fileStat = await stat(path)
if (fileStat.isFile()) {
return path
}
} catch {
// File doesn't exist, continue
}
}
}
6 changes: 2 additions & 4 deletions packages/zip-it-and-ship-it/src/utils/remove_undefined.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { includeKeys } from 'filter-obj'

const isUndefined = (_key: unknown, value: unknown) => value !== undefined
const isDefined = (value: unknown) => value !== undefined

export const removeUndefined = function <T extends { [key: string]: unknown }>(obj: T): T {
return includeKeys(obj, isUndefined) as T
return Object.fromEntries(Object.entries(obj).filter(([, value]) => isDefined(value))) as T
}
12 changes: 11 additions & 1 deletion packages/zip-it-and-ship-it/tests/helpers/vitest_setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { pathExists } from 'path-exists'
import { PathLike } from 'fs'
import { access } from 'fs/promises'
import { expect } from 'vitest'

const pathExists = async (path: PathLike) => {
try {
await access(path)
return true
} catch {
return false
}
}

expect.extend({
async toPathExist(received) {
const { isNot } = this
Expand Down
7 changes: 4 additions & 3 deletions packages/zip-it-and-ship-it/tests/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdir, readFile, rm, symlink, writeFile } from 'fs/promises'
import { mkdir, readFile, rm, symlink, writeFile, access } from 'fs/promises'
import { dirname, isAbsolute, join, resolve } from 'path'
import { arch, version as nodeVersion, platform } from 'process'

Expand All @@ -8,7 +8,6 @@ import merge from 'deepmerge'
import { execa, execaNode } from 'execa'
import glob from 'fast-glob'
import isCI from 'is-ci'
import { pathExists } from 'path-exists'
import semver from 'semver'
import { dir as getTmpDir, tmpName } from 'tmp-promise'
import unixify from 'unixify'
Expand Down Expand Up @@ -565,7 +564,9 @@ describe('zip-it-and-ship-it', () => {
const symlinkFile = `${symlinkDir}/file.js`
const targetFile = `${symlinkDir}/target.js`

if (!(await pathExists(symlinkFile))) {
try {
await access(symlinkFile)
} catch {
await symlink(targetFile, symlinkFile)
}

Expand Down
13 changes: 11 additions & 2 deletions packages/zip-it-and-ship-it/tests/v2api.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { readFile } from 'fs/promises'
import { readFile, access } from 'fs/promises'
import { join, resolve } from 'path'
import { platform } from 'process'

import { getPath as getBootstrapPath } from '@netlify/serverless-functions-api'
import merge from 'deepmerge'
import glob from 'fast-glob'
import { pathExists } from 'path-exists'
import { dir as getTmpDir } from 'tmp-promise'
import { afterEach, describe, expect, test, vi } from 'vitest'

Expand All @@ -15,6 +14,7 @@ import { DEFAULT_NODE_VERSION } from '../src/runtimes/node/utils/node_version.js
import { invokeLambda, readAsBuffer } from './helpers/lambda.js'
import { zipFixture, unzipFiles, importFunctionFile, FIXTURES_ESM_DIR, FIXTURES_DIR } from './helpers/main.js'
import { testMany } from './helpers/test_many.js'
import { PathLike } from 'fs'

vi.mock('../src/utils/shell.js', () => ({ shellUtils: { runCommand: vi.fn() } }))

Expand Down Expand Up @@ -673,6 +673,15 @@ describe('V2 functions API', () => {
resolve(FIXTURES_ESM_DIR, fixtureName, 'blog/post2.md'),
])

const pathExists = async (path: PathLike) => {
try {
await access(path)
return true
} catch {
return false
}
}

for (const path of includedFiles as string[]) {
expect(await pathExists(path)).toBeTruthy()
}
Expand Down
Loading