Skip to content
Draft
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
54 changes: 49 additions & 5 deletions src/commands/deploy.js
Original file line number Diff line number Diff line change
@@ -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<Array<{path: string, relativePath: string}>>}
*/
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., <local_dir> [--subdomain=<subdomain>]).
Expand Down Expand Up @@ -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}`));
}
}