Skip to content

Commit

Permalink
chore: Extract fs helpers into shared internal-helpers package
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Jun 24, 2024
1 parent ae3b096 commit 906ebe9
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changeset/lovely-parents-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@astrojs/vercel': patch
'@astrojs/internal-helpers': patch
---

Extracts fs helpers into shared internal-helpers module
4 changes: 2 additions & 2 deletions packages/integrations/vercel/src/lib/nft.ts
Original file line number Diff line number Diff line change
@@ -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(
{
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/vercel/src/serverless/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/vercel/src/static/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion packages/internal-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ export async function getFilesFromFolder(dir: URL) {
* @param {URL[]} [exclude] A list of files to exclude (absolute path).
* @returns {Promise<string>} The common ancestor of the copied files.
*/
export async function copyFilesToFunction(
export async function copyFilesToFolder(
files: URL[],
outDir: URL,
exclude: URL[] = []
): Promise<string> {
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)) {
Expand Down Expand Up @@ -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' });
}

0 comments on commit 906ebe9

Please sign in to comment.