refactor: implement DRY approach for template files

This commit is contained in:
Eyal Toledano
2025-03-04 15:27:23 -05:00
parent 55d70dd31c
commit 163f47260a
2 changed files with 48 additions and 1 deletions

View File

@@ -9,7 +9,8 @@
}, },
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"prepare-package": "node scripts/prepare-package.js" "prepare-package": "node scripts/prepare-package.js",
"prepublishOnly": "npm run prepare-package"
}, },
"keywords": [ "keywords": [
"claude", "claude",

View File

@@ -56,11 +56,57 @@ function ensureExecutable(filePath) {
return true; return true;
} }
// Function to sync files from source to templates
function syncTemplateFiles() {
// Define files to sync in format: [source, target]
const filesToSync = [
// Scripts
['scripts/dev.js', 'templates/dev.js'],
// Documentation files
['README.md', 'templates/README.md'],
['scripts/README.md', 'templates/scripts_README.md'],
// Other files that might need syncing
// Add more files here as needed
];
let allSynced = true;
const rootDir = path.join(__dirname, '..');
for (const [source, target] of filesToSync) {
const sourcePath = path.join(rootDir, source);
const targetPath = path.join(rootDir, target);
try {
if (fileExists(sourcePath)) {
log('info', `Syncing ${source} to ${target}...`);
fs.copyFileSync(sourcePath, targetPath);
log('success', `Successfully synced ${source} to ${target}`);
} else {
log('error', `Source file ${source} does not exist`);
allSynced = false;
}
} catch (error) {
log('error', `Failed to sync ${source} to ${target}:`, error.message);
allSynced = false;
}
}
return allSynced;
}
// Main function to prepare the package // Main function to prepare the package
function preparePackage() { function preparePackage() {
const rootDir = path.join(__dirname, '..'); const rootDir = path.join(__dirname, '..');
log('info', `Preparing package in ${rootDir}`); log('info', `Preparing package in ${rootDir}`);
// Sync template files to ensure templates have the latest versions
log('info', 'Syncing template files...');
if (!syncTemplateFiles()) {
log('warn', 'Some template files could not be synced. Continuing with preparation...');
}
// Check for required files // Check for required files
const requiredFiles = [ const requiredFiles = [
'package.json', 'package.json',