Skip to content

fix(presets): astro prebuild #177

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

Closed
wants to merge 1 commit into from
Closed
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
59 changes: 39 additions & 20 deletions packages/presets/src/presets/astro/prebuild.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
import { copyDirectory, exec, getPackageManager } from 'azion/utils/node';
import { readFile } from 'fs/promises';

/**
* Runs custom prebuild actions for Astro
*/
async function prebuild() {
const packageManager = await getPackageManager();
const newOutDir = '.edge/storage';
let outDir = 'dist';

// check if an output path is specified in config file
const configFileContent = await readFile('./astro.config.mjs', 'utf-8');
const attributeMatch = Array.from(configFileContent.matchAll(/outDir:(.*)\n/g), (match) => match)[0];
if (attributeMatch) {
outDir = attributeMatch[1].trim();
import { constants } from 'fs';
import { access, readFile } from 'fs/promises';

const CONFIG_FILES = ['astro.config.ts', 'astro.config.mjs', 'astro.config.js', 'astro.config.cjs'] as const;

async function getAstroOutDir(): Promise<string> {
for (const configFile of CONFIG_FILES) {
try {
await access(configFile, constants.F_OK);
const configContent = await readFile(configFile, 'utf-8');

// Regex supports: outDir: 'path', outDir:"path", outDir: `path`
const outDirMatch = configContent.match(/outDir\s*:\s*['"`]([^'"`]+)['"`]/);

if (outDirMatch && outDirMatch[1]) {
return outDirMatch[1].trim();
}

break;
} catch {
continue;
}
}

await exec(`${packageManager} run build`, {
scope: 'Astro',
verbose: true,
});
return 'dist'; // Default Astro output directory
}

async function prebuild(): Promise<void> {
try {
const [packageManager, outDir] = await Promise.all([getPackageManager(), getAstroOutDir()]);

const newOutDir = '.edge/storage';

copyDirectory(outDir, newOutDir);
await exec(`${packageManager} run build`, {
scope: 'Astro',
verbose: true,
});

copyDirectory(outDir, newOutDir);
} catch (error) {
throw new Error(`Erro durante o prebuild do Astro: ${error instanceof Error ? error.message : String(error)}`);
}
}

export default prebuild;