11import chalk from 'chalk' ;
2+ import fs from 'fs/promises' ;
3+ import path from 'path' ;
24import { generateAppName } from '../commons.js' ;
3- import { syncDirectory } from './files.js' ;
45import { createSite } from './sites.js' ;
56import { getPuter } from '../modules/PuterModule.js' ;
67
8+ /**
9+ * Recursively get all files in a directory
10+ * @param {string } dir - Directory to scan
11+ * @param {string } baseDir - Base directory for relative path calculation
12+ * @returns {Promise<Array<{path: string, relativePath: string}>> }
13+ */
14+ async function getAllFiles ( dir , baseDir = dir ) {
15+ const files = [ ] ;
16+ const entries = await fs . readdir ( dir , { withFileTypes : true } ) ;
17+
18+ for ( const entry of entries ) {
19+ const fullPath = path . join ( dir , entry . name ) ;
20+ if ( entry . isDirectory ( ) ) {
21+ files . push ( ...await getAllFiles ( fullPath , baseDir ) ) ;
22+ } else {
23+ files . push ( {
24+ path : fullPath ,
25+ relativePath : path . relative ( baseDir , fullPath )
26+ } ) ;
27+ }
28+ }
29+
30+ return files ;
31+ }
32+
733/**
834 * Deploy a local web project to Puter.
935 * @param {string[] } args - Command-line arguments (e.g., <local_dir> [--subdomain=<subdomain>]).
@@ -37,18 +63,36 @@ export async function deploy(args = []) {
3763 console . log ( chalk . cyan ( `Deploying '${ sourceDir } ' to '${ subdomain } .puter.site'...` ) ) ;
3864
3965 try {
40- // 1. Upload files
41- await syncDirectory ( [ sourceDir , directory . path , '--delete' , '-r' , '--overwrite' ] ) ;
66+ // 1. Read all files from source directory
67+ const files = await getAllFiles ( sourceDir ) ;
68+
69+ if ( files . length === 0 ) {
70+ console . log ( chalk . yellow ( 'No files found in source directory.' ) ) ;
71+ return ;
72+ }
73+
74+ // 2. Create File objects for upload
75+ const fileObjects = await Promise . all (
76+ files . map ( async ( file ) => {
77+ const content = await fs . readFile ( file . path ) ;
78+ return new File ( [ content ] , file . relativePath ) ;
79+ } )
80+ ) ;
81+
82+ // 3. Upload files
83+ console . log ( chalk . cyan ( `Uploading ${ files . length } file(s)...` ) ) ;
84+ await puter . fs . upload ( fileObjects , directory . path , { createMissingParents : true } ) ;
4285
43- // 2 . Create the site
86+ // 4 . Create the site
4487 const site = await createSite ( [ subdomain , directory . path , `--subdomain=${ subdomain } ` ] ) ;
4588
4689 if ( site ) {
4790 console . log ( chalk . green ( 'Deployment successful!' ) ) ;
4891 } else {
49- console . log ( chalk . yellow ( 'Deployment successfuly updated!' ) ) ;
92+ console . log ( chalk . yellow ( 'Deployment successfully updated!' ) ) ;
5093 }
5194 } catch ( error ) {
95+ console . log ( error ) ;
5296 console . error ( chalk . red ( `Deployment failed: ${ error . message } ` ) ) ;
5397 }
5498}
0 commit comments