Skip to content
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
19 changes: 1 addition & 18 deletions package-lock.json

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

5 changes: 1 addition & 4 deletions packages/functions-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,10 @@
"license": "MIT",
"dependencies": {
"@netlify/zip-it-and-ship-it": "15.3.0",
"cpy": "^11.0.0",
"path-exists": "^5.0.0"
"junk": "^4.0.0"
},
"devDependencies": {
"@types/node": "^22.12.0",
"sort-on": "^6.0.0",
"tmp-promise": "^3.0.0",
"typescript": "^5.0.0",
"vitest": "^3.0.0"
},
Expand Down
27 changes: 10 additions & 17 deletions packages/functions-utils/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { promises as fs } from 'fs'
import { basename, dirname, join } from 'path'
import { basename, join } from 'path'

import { listFunctions, listFunctionsFiles } from '@netlify/zip-it-and-ship-it'
import cpy from 'cpy'
import { pathExists } from 'path-exists'
import { isNotJunk } from 'junk'

// Add a Netlify Function file to the `functions` directory, so it is processed
// by `@netlify/plugin-functions-core`
Expand All @@ -12,27 +11,21 @@ export const add = async function (src?: string, dist?: string, { fail = default
return fail('No function source directory was specified')
}

if (!(await pathExists(src))) {
try {
await fs.access(src)
} catch {
return fail(`No function file or directory found at "${src}"`)
}

if (dist === undefined) {
return fail('No function directory was specified')
}

const srcBasename = basename(src)
const [srcGlob, dest] = await getSrcAndDest(src, srcBasename, dist)
await cpy(srcGlob, dest, { cwd: dirname(src), overwrite: true })
}

const getSrcAndDest = async function (src: string, srcBasename: string, dist: string): Promise<[string, string]> {
const srcStat = await fs.stat(src)

if (srcStat.isDirectory()) {
return [`${srcBasename}/**`, join(dist, srcBasename)]
}

return [srcBasename, dist]
await fs.cp(src, join(dist, basename(src)), {
recursive: true,
force: true,
filter: (source) => isNotJunk(basename(source)),
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

export const list = async function (functionsSrc, { fail = defaultFail } = {} as any) {
Expand Down
13 changes: 8 additions & 5 deletions packages/functions-utils/tests/helpers/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { tmpName, dir as tmpDir } from 'tmp-promise'
import { randomUUID } from 'crypto'
import { mkdtemp } from 'fs/promises'
import { tmpdir } from 'os'
import { join } from 'path'

const PREFIX = 'test-functions-utils-'

// Retrieve name of a temporary directory
export const getDist = function () {
return tmpName({ prefix: PREFIX })
export const getDist = async function () {
return join(tmpdir(), `${PREFIX}${randomUUID()}`)
}

// Create temporary directory
export const createDist = async function () {
const { path } = await tmpDir({ prefix: PREFIX })
return path
return await mkdtemp(join(tmpdir(), PREFIX))
}
29 changes: 17 additions & 12 deletions packages/functions-utils/tests/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { readFile, rm } from 'fs/promises'
import { existsSync } from 'fs'
import { copyFile, readFile, rm } from 'fs/promises'
import { normalize, resolve } from 'path'
import { fileURLToPath } from 'url'

import cpy from 'cpy'
import { pathExists } from 'path-exists'
import sortOn from 'sort-on'
import { expect, test, vi } from 'vitest'

import { add, list, listAll } from '../src/main.js'
Expand All @@ -17,7 +15,7 @@ test('Should copy a source file to a dist directory', async () => {
const dist = await getDist()
try {
await add(`${FIXTURES_DIR}/file/test.mjs`, dist)
expect(await pathExists(`${dist}/test.mjs`)).toBe(true)
expect(existsSync(`${dist}/test.mjs`)).toBe(true)
} finally {
await rm(dist, { force: true, recursive: true })
}
Expand All @@ -27,7 +25,7 @@ test('Should copy a source directory to a dist directory', async () => {
const dist = await getDist()
try {
await add(`${FIXTURES_DIR}/directory/test`, dist)
expect(await pathExists(`${dist}/test/index.js`)).toBe(true)
expect(existsSync(`${dist}/test/index.js`)).toBe(true)
} finally {
await rm(dist, { force: true, recursive: true })
}
Expand Down Expand Up @@ -68,7 +66,7 @@ test('Should copy a source file even if dist directory already exists', async ()
const dist = await createDist()
try {
await add(`${FIXTURES_DIR}/file/test.mjs`, dist)
expect(await pathExists(`${dist}/test.mjs`)).toBe(true)
expect(existsSync(`${dist}/test.mjs`)).toBe(true)
} finally {
await rm(dist, { force: true, recursive: true })
}
Expand All @@ -79,22 +77,22 @@ test('Should overwrite dist file if it already exists', async () => {
const fixtureDir = `${FIXTURES_DIR}/file`
const testModule = `${dist}/test.mjs`

await cpy(`${fixtureDir}/test.mjs`, fixtureDir, { rename: 'test.mjs.backup' })
await copyFile(`${fixtureDir}/test.mjs`, `${fixtureDir}/test.mjs.backup`)

try {
await add(`${fixtureDir}/test.mjs`, dist)

const file1 = await readFile(testModule, 'utf8')

await cpy(`${fixtureDir}/test_2.mjs`, fixtureDir, { rename: 'test.mjs' })
await copyFile(`${fixtureDir}/test_2.mjs`, `${fixtureDir}/test.mjs`)
await add(`${fixtureDir}/test.mjs`, dist)

const file2 = await readFile(testModule, 'utf8')

expect(file1).toContain('one')
expect(file2).toContain('two')
} finally {
await cpy(`${fixtureDir}/test.mjs.backup`, fixtureDir, { rename: 'test.mjs' })
await copyFile(`${fixtureDir}/test.mjs.backup`, `${fixtureDir}/test.mjs`)
await rm(`${fixtureDir}/test.mjs.backup`, { force: true })
await rm(dist, { force: true, recursive: true })
}
Expand All @@ -106,6 +104,13 @@ test('Should allow "fail" option to customize failures', async () => {
expect(fail).toHaveBeenCalledExactlyOnceWith('No function source directory was specified')
})

// Sort by `mainFile`, then `extension`
const sortFunctions = function (functions) {
return [...functions].sort(
(a, b) => a.mainFile.localeCompare(b.mainFile) || a.extension.localeCompare(b.extension),
)
}

const normalizeFiles = function (
fixtureDir,
{ name, mainFile, runtime, extension, srcDir, srcFile, srcPath, schedule },
Expand All @@ -120,7 +125,7 @@ const normalizeFiles = function (
test('Can list function main files with list()', async () => {
const fixtureDir = `${FIXTURES_DIR}/list`
const functions = await list(fixtureDir)
expect(sortOn(functions, ['mainFile', 'extension'])).toEqual(
expect(sortFunctions(functions)).toEqual(
[
{
name: 'four',
Expand Down Expand Up @@ -149,7 +154,7 @@ test('Can list function main files with list()', async () => {
test('Can list all function files with listAll()', async () => {
const fixtureDir = `${FIXTURES_DIR}/list`
const functions = await listAll(fixtureDir)
expect(sortOn(functions, ['mainFile', 'extension'])).toEqual(
expect(sortFunctions(functions)).toEqual(
[
{
name: 'four',
Expand Down
Loading