zephyr-astro-integration
Version:
Astro integration for Zephyr Cloud deployment and module federation
158 lines (157 loc) • 5.99 kB
JavaScript
import "node:module";
import { readFile } from "node:fs/promises";
import { isAbsolute, join, relative, sep } from "node:path";
import { fileURLToPath } from "node:url";
import { buildAssetsMap, logFn, readDirRecursiveWithContents } from "zephyr-agent";
function extractBuffer(asset) {
return asset.content;
}
function getAssetType(asset) {
return asset.type;
}
function normalizePath(filePath) {
return filePath.split(sep).join('/');
}
function looksLikeRoute(path) {
if (!path || !path.startsWith('/')) return false;
if (path.includes('[') || path.includes(']')) return true;
const segments = path.split('/').filter(Boolean);
if (segments.length <= 2) return true;
return false;
}
async function extractAstroAssetsFromBuildHook(assets, outputPath, target) {
if ('tap-app' === target) return extractAstroAssetsMap(outputPath, target);
const astroAssets = {};
try {
if (!assets) return await extractAstroAssetsMap(outputPath, target);
const assetEntries = extractAssetEntries(assets);
for (const [filePath, assetInfo] of assetEntries)try {
let fullPath = null;
if (assetInfo && 'object' == typeof assetInfo && 'href' in assetInfo) fullPath = fileURLToPath(assetInfo);
else if ('string' == typeof assetInfo && assetInfo) {
if (looksLikeRoute(assetInfo)) continue;
fullPath = isAbsolute(assetInfo) && !looksLikeRoute(assetInfo) ? assetInfo : join(outputPath, assetInfo);
} else if ('string' == typeof filePath && filePath) {
if (looksLikeRoute(filePath)) continue;
fullPath = isAbsolute(filePath) && !looksLikeRoute(filePath) ? filePath : join(outputPath, filePath);
}
if (!fullPath) continue;
const relativePath = normalizePath(relative(outputPath, fullPath));
if (shouldSkipFile(relativePath, target)) continue;
const content = await readFile(fullPath);
const fileType = getFileType(relativePath);
astroAssets[relativePath] = {
content,
type: fileType
};
} catch (readError) {
logFn('warn', `Failed to read asset file ${filePath}: ${readError}`);
continue;
}
if (0 === Object.keys(astroAssets).length) return await extractAstroAssetsMap(outputPath, target);
return buildAssetsMap(astroAssets, extractBuffer, getAssetType);
} catch (error) {
logFn('warn', 'Error processing assets from Astro build hook:' + JSON.stringify(error, null, 2));
return await extractAstroAssetsMap(outputPath, target);
}
}
function extractAssetEntries(assets) {
const entries = [];
if (Array.isArray(assets)) assets.forEach((asset)=>{
if ('string' == typeof asset) entries.push([
asset,
asset
]);
else if (asset && 'object' == typeof asset) {
const assetObj = asset;
const path = assetObj['path'] || assetObj['url'] || assetObj['href'] || assetObj['pathname'];
if (path && 'string' == typeof path) entries.push([
path,
asset
]);
}
});
else if (assets instanceof Map) for (const [key, value] of assets.entries())entries.push([
key,
value
]);
else if (assets && 'object' == typeof assets) for (const [key, value] of Object.entries(assets))if (Array.isArray(value)) value.forEach((item)=>{
entries.push([
key,
item
]);
});
else entries.push([
key,
value
]);
return entries;
}
async function extractAstroAssetsMap(buildDir, target) {
const assets = {};
if ('tap-app' === target) {
const files = await readDirRecursiveWithContents(buildDir, {
includeIgnoredPaths: true,
failOnError: true
});
for (const file of files){
const relativePath = normalizePath(file.relativePath);
assets[relativePath] = {
content: file.content,
type: getFileType(relativePath)
};
}
return buildAssetsMap(assets, extractBuffer, getAssetType);
}
try {
const files = await readDirRecursiveWithContents(buildDir);
for (const file of files){
const relativePath = normalizePath(file.relativePath);
if (shouldSkipFile(relativePath, target)) continue;
const fileType = getFileType(relativePath);
assets[relativePath] = {
content: file.content,
type: fileType
};
}
} catch (error) {
logFn('warn', `Failed to read build directory ${buildDir}: ${error}`);
}
return buildAssetsMap(assets, extractBuffer, getAssetType);
}
function shouldSkipFile(filePath, target) {
if ('tap-app' === target) return false;
const skipPatterns = [
/\.map$/,
/node_modules/,
/\.git/,
/\.DS_Store$/,
/thumbs\.db$/i
];
return skipPatterns.some((pattern)=>pattern.test(filePath));
}
function getFileType(filePath) {
const extension = filePath.split('.').pop()?.toLowerCase() || '';
const typeMap = {
html: 'text/html',
css: 'text/css',
js: "application/javascript",
mjs: "application/javascript",
json: 'application/json',
png: 'image/png',
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
gif: 'image/gif',
svg: 'image/svg+xml',
ico: 'image/x-icon',
woff: 'font/woff',
woff2: 'font/woff2',
ttf: 'font/ttf',
eot: 'application/vnd.ms-fontobject',
xml: 'text/xml',
txt: 'text/plain'
};
return typeMap[extension] || 'application/octet-stream';
}
export { extractAstroAssetsFromBuildHook, extractAstroAssetsMap };
//# sourceMappingURL=extract-astro-assets-map.mjs.map