diff --git a/src/commands/deploy.js b/src/commands/deploy.js index da1783a..77b9903 100644 --- a/src/commands/deploy.js +++ b/src/commands/deploy.js @@ -1,9 +1,35 @@ import chalk from 'chalk'; +import fs from 'fs/promises'; +import path from 'path'; import { generateAppName } from '../commons.js'; -import { syncDirectory } from './files.js'; import { createSite } from './sites.js'; import { getPuter } from '../modules/PuterModule.js'; +/** + * Recursively get all files in a directory + * @param {string} dir - Directory to scan + * @param {string} baseDir - Base directory for relative path calculation + * @returns {Promise>} + */ +async function getAllFiles(dir, baseDir = dir) { + const files = []; + const entries = await fs.readdir(dir, { withFileTypes: true }); + + for (const entry of entries) { + const fullPath = path.join(dir, entry.name); + if (entry.isDirectory()) { + files.push(...await getAllFiles(fullPath, baseDir)); + } else { + files.push({ + path: fullPath, + relativePath: path.relative(baseDir, fullPath) + }); + } + } + + return files; +} + /** * Deploy a local web project to Puter. * @param {string[]} args - Command-line arguments (e.g., [--subdomain=]). @@ -37,18 +63,36 @@ export async function deploy(args = []) { console.log(chalk.cyan(`Deploying '${sourceDir}' to '${subdomain}.puter.site'...`)); try { - // 1. Upload files - await syncDirectory([sourceDir, directory.path, '--delete', '-r', '--overwrite']); + // 1. Read all files from source directory + const files = await getAllFiles(sourceDir); + + if (files.length === 0) { + console.log(chalk.yellow('No files found in source directory.')); + return; + } + + // 2. Create File objects for upload + const fileObjects = await Promise.all( + files.map(async (file) => { + const content = await fs.readFile(file.path); + return new File([content], file.relativePath); + }) + ); + + // 3. Upload files + console.log(chalk.cyan(`Uploading ${files.length} file(s)...`)); + await puter.fs.upload(fileObjects, directory.path, { createMissingParents: true }); - // 2. Create the site + // 4. Create the site const site = await createSite([subdomain, directory.path, `--subdomain=${subdomain}`]); if (site) { console.log(chalk.green('Deployment successful!')); } else { - console.log(chalk.yellow('Deployment successfuly updated!')); + console.log(chalk.yellow('Deployment successfully updated!')); } } catch (error) { + console.log(error); console.error(chalk.red(`Deployment failed: ${error.message}`)); } } \ No newline at end of file