/**
* Syncs resources based on CLI arguments.
*
* @param {object} args - An object containing CLI arguments.
* @returns {object} - An object containing the synced resources.
*/
function syncResources(args) {
const {
sourceDir,
targetDir,
includePattern,
excludePattern,
dryRun,
} = args;
const resources = [];
//Handle source directory
if (sourceDir) {
try {
const files = require('fs').readdirSync(sourceDir);
for (const file of files) {
if (!excludePattern || !includePattern(file, excludePattern)) {
resources.push({ source: `${sourceDir}/${file}`, target: `${targetDir}/${file}` });
}
}
} catch (err) {
console.error(`Error reading source directory: ${err.message}`);
return {}; // or throw the error, depending on desired behavior
}
}
// Apply dry run if requested
if (dryRun) {
console.log("Dry run: Would sync the following resources:");
console.log(resources);
return resources; //Return resources to show what *would* be synced
}
// Perform the actual sync
for (const resource of resources) {
try {
//Implement logic to copy/sync file
console.log(`Syncing ${resource.source} to ${resource.target}`);
//Example using fs.copyFileSync (replace with your preferred syncing method)
require('fs').copyFileSync(resource.source, resource.target);
} catch (err) {
console.error(`Error syncing ${resource.source}: ${err.message}`);
}
}
return resources; //Return the synced resources
}
//Helper function for pattern matching
function includePattern(filename, excludePattern) {
if (!excludePattern) {
return true; //include if no exclude pattern
}
return !excludePattern.test(filename);
}
Add your comment