From 41064cee78c1cccd428f710a24c483aeb275fd95 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Mon, 24 Jun 2024 10:12:14 +0100 Subject: [PATCH] chore: Extract fs helpers into shared internal-helpers package (#11323) --- .changeset/lovely-parents-attend.md | 6 ++++++ packages/integrations/vercel/src/lib/nft.ts | 4 ++-- packages/integrations/vercel/src/serverless/adapter.ts | 2 +- packages/integrations/vercel/src/static/adapter.ts | 2 +- packages/internal-helpers/package.json | 6 +++++- .../vercel/src/lib => internal-helpers/src}/fs.ts | 8 ++------ 6 files changed, 17 insertions(+), 11 deletions(-) create mode 100644 .changeset/lovely-parents-attend.md rename packages/{integrations/vercel/src/lib => internal-helpers/src}/fs.ts (92%) diff --git a/.changeset/lovely-parents-attend.md b/.changeset/lovely-parents-attend.md new file mode 100644 index 000000000000..e6c5ca05ba34 --- /dev/null +++ b/.changeset/lovely-parents-attend.md @@ -0,0 +1,6 @@ +--- +'@astrojs/vercel': patch +'@astrojs/internal-helpers': patch +--- + +Extracts fs helpers into shared internal-helpers module diff --git a/packages/integrations/vercel/src/lib/nft.ts b/packages/integrations/vercel/src/lib/nft.ts index 3b54ae8cc5b2..eb80f08e5271 100644 --- a/packages/integrations/vercel/src/lib/nft.ts +++ b/packages/integrations/vercel/src/lib/nft.ts @@ -1,7 +1,7 @@ import { relative as relativePath } from 'node:path'; import { fileURLToPath } from 'node:url'; import type { AstroIntegrationLogger } from 'astro'; -import { copyFilesToFunction } from './fs.js'; +import { copyFilesToFolder } from '@astrojs/internal-helpers/fs'; export async function copyDependenciesToFunction( { @@ -72,7 +72,7 @@ export async function copyDependenciesToFunction( } } - const commonAncestor = await copyFilesToFunction( + const commonAncestor = await copyFilesToFolder( [...result.fileList].map((file) => new URL(file, base)).concat(includeFiles), outDir, excludeFiles diff --git a/packages/integrations/vercel/src/serverless/adapter.ts b/packages/integrations/vercel/src/serverless/adapter.ts index aade4715081a..86bc0b400cfe 100644 --- a/packages/integrations/vercel/src/serverless/adapter.ts +++ b/packages/integrations/vercel/src/serverless/adapter.ts @@ -16,7 +16,7 @@ import { getAstroImageConfig, getDefaultImageConfig, } from '../image/shared.js'; -import { removeDir, writeJson } from '../lib/fs.js'; +import { removeDir, writeJson } from '@astrojs/internal-helpers/fs'; import { copyDependenciesToFunction } from '../lib/nft.js'; import { escapeRegex, getRedirects } from '../lib/redirects.js'; import { diff --git a/packages/integrations/vercel/src/static/adapter.ts b/packages/integrations/vercel/src/static/adapter.ts index 4a66c558ef69..c83ce55ecc66 100644 --- a/packages/integrations/vercel/src/static/adapter.ts +++ b/packages/integrations/vercel/src/static/adapter.ts @@ -6,7 +6,7 @@ import { getAstroImageConfig, getDefaultImageConfig, } from '../image/shared.js'; -import { emptyDir, writeJson } from '../lib/fs.js'; +import { emptyDir, writeJson } from '@astrojs/internal-helpers/fs'; import { isServerLikeOutput } from '../lib/prerender.js'; import { getRedirects } from '../lib/redirects.js'; import { diff --git a/packages/internal-helpers/package.json b/packages/internal-helpers/package.json index ab31aab220ef..7da537d11a99 100644 --- a/packages/internal-helpers/package.json +++ b/packages/internal-helpers/package.json @@ -12,12 +12,16 @@ }, "bugs": "https://github.com/withastro/astro/issues", "exports": { - "./path": "./dist/path.js" + "./path": "./dist/path.js", + "./fs": "./dist/fs.js" }, "typesVersions": { "*": { "path": [ "./dist/path.d.ts" + ], + "fs": [ + "./dist/fs.d.ts" ] } }, diff --git a/packages/integrations/vercel/src/lib/fs.ts b/packages/internal-helpers/src/fs.ts similarity index 92% rename from packages/integrations/vercel/src/lib/fs.ts rename to packages/internal-helpers/src/fs.ts index 152ee7d7c0d3..fb6d307cafb2 100644 --- a/packages/integrations/vercel/src/lib/fs.ts +++ b/packages/internal-helpers/src/fs.ts @@ -40,7 +40,7 @@ export async function getFilesFromFolder(dir: URL) { * @param {URL[]} [exclude] A list of files to exclude (absolute path). * @returns {Promise} The common ancestor of the copied files. */ -export async function copyFilesToFunction( +export async function copyFilesToFolder( files: URL[], outDir: URL, exclude: URL[] = [] @@ -48,7 +48,7 @@ export async function copyFilesToFunction( const excludeList = exclude.map(fileURLToPath); const fileList = files.map(fileURLToPath).filter((f) => !excludeList.includes(f)); - if (files.length === 0) throw new Error('[@astrojs/vercel] No files found to copy'); + if (files.length === 0) throw new Error('No files found to copy'); let commonAncestor = nodePath.dirname(fileList[0]); for (const file of fileList.slice(1)) { @@ -87,7 +87,3 @@ export async function copyFilesToFunction( return commonAncestor; } - -export async function writeFile(path: PathLike, content: string) { - await fs.writeFile(path, content, { encoding: 'utf-8' }); -}