mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Refactor buildProject, copy public files
This commit is contained in:
5 files changed
+374
-99
No files matched your search
@@ -15,61 +15,17 @@ export class BuildProjectTool implements Tool<void> {
|
||||
|
||||
async execute(): Promise<string> {
|
||||
try {
|
||||
// Check if we're in a valid project directory
|
||||
try {
|
||||
await this.fs.readFile(`${this.cwd}/package.json`, "utf8");
|
||||
} catch {
|
||||
throw new Error(`❌ Could not find package.json at ${this.cwd}. Make sure you're in a valid project directory.`);
|
||||
}
|
||||
|
||||
// Check if index.html exists
|
||||
try {
|
||||
await this.fs.readFile(`${this.cwd}/index.html`, "utf8");
|
||||
} catch {
|
||||
throw new Error(`❌ Could not find index.html at ${this.cwd}. This is required for building the project.`);
|
||||
}
|
||||
|
||||
// Run the build
|
||||
const dist = await buildProject({
|
||||
// Build and write the project files
|
||||
const result = await buildProject({
|
||||
fs: this.fs,
|
||||
projectPath: this.cwd,
|
||||
domParser: new DOMParser(),
|
||||
});
|
||||
|
||||
// Delete all existing files in "dist" directory
|
||||
try {
|
||||
const distFiles = await this.fs.readdir(`${this.cwd}/dist`);
|
||||
for (const file of distFiles) {
|
||||
await this.fs.unlink(`${this.cwd}/dist/${file}`);
|
||||
}
|
||||
} catch {
|
||||
// Ignore errors (e.g., directory doesn't exist)
|
||||
}
|
||||
const fileCount = Object.keys(result.files).length;
|
||||
const fileList = Object.keys(result.files).map(file => ` 📄 ${file}`).join('\n');
|
||||
|
||||
// Create dist directory if it doesn't exist
|
||||
await this.fs.mkdir(`${this.cwd}/dist`, { recursive: true });
|
||||
|
||||
// Write all built files to dist directory
|
||||
for (const [path, contents] of Object.entries(dist)) {
|
||||
await this.fs.writeFile(`${this.cwd}/dist/${path}`, contents);
|
||||
}
|
||||
|
||||
const fileCount = Object.keys(dist).length;
|
||||
const fileList = Object.keys(dist).map(file => ` 📄 ${file}`).join('\n');
|
||||
|
||||
// Extract project ID from the current working directory path
|
||||
// Expected format: /projects/{projectId}
|
||||
const projectId = this.cwd.split('/').pop();
|
||||
|
||||
// Emit build completion event for PreviewPane to listen to
|
||||
if (projectId && typeof window !== 'undefined') {
|
||||
const buildCompleteEvent = new CustomEvent('buildComplete', {
|
||||
detail: { projectId }
|
||||
});
|
||||
window.dispatchEvent(buildCompleteEvent);
|
||||
}
|
||||
|
||||
return `✅ Successfully built project!\n\n📁 Output: ${this.cwd}/dist\n📦 Files generated: ${fileCount}\n\n${fileList}\n\n🚀 Your project is ready for deployment!`;
|
||||
return `✅ Successfully built project!\n\n📁 Output: ${result.outputPath}\n📦 Files generated: ${fileCount}\n\n${fileList}\n\n🚀 Your project is ready for deployment!`;
|
||||
} catch (error) {
|
||||
throw new Error(`❌ Build failed: ${String(error)}`);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user