refactor: simplify package structure by eliminating templates directory

This commit is contained in:
Eyal Toledano
2025-03-04 16:50:42 -05:00
parent fdd8c5cf3b
commit 290163f53f
7 changed files with 203 additions and 99 deletions

View File

@@ -72,9 +72,39 @@ function ensureDirectoryExists(dirPath) {
// Function to copy a file from the package to the target directory
function copyTemplateFile(templateName, targetPath, replacements = {}) {
// Get the template content from the templates directory
const templatePath = path.join(__dirname, '..', 'templates', templateName);
let content = fs.readFileSync(templatePath, 'utf8');
// Get the file content from the appropriate source directory
let sourcePath;
// Map template names to their actual source paths
switch(templateName) {
case 'dev.js':
sourcePath = path.join(__dirname, 'dev.js');
break;
case 'scripts_README.md':
sourcePath = path.join(__dirname, 'README.md');
break;
case 'dev_workflow.mdc':
sourcePath = path.join(__dirname, '..', '.cursor', 'rules', 'dev_workflow.mdc');
break;
case 'README.md':
sourcePath = path.join(__dirname, '..', 'README.md');
break;
default:
// For other files like env.example, gitignore, etc. that don't have direct equivalents
sourcePath = path.join(__dirname, '..', 'assets', templateName);
}
// Check if the source file exists
if (!fs.existsSync(sourcePath)) {
// Fall back to templates directory for files that might not have been moved yet
sourcePath = path.join(__dirname, '..', 'assets', templateName);
if (!fs.existsSync(sourcePath)) {
log('error', `Source file not found: ${sourcePath}`);
return;
}
}
let content = fs.readFileSync(sourcePath, 'utf8');
// Replace placeholders with actual values
Object.entries(replacements).forEach(([key, value]) => {

View File

@@ -56,44 +56,11 @@ function ensureExecutable(filePath) {
return true;
}
// Function to sync files from source to templates
// Function to sync template files
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;
// We no longer need to sync files since we're using them directly
log('info', 'Template syncing has been deprecated - using source files directly');
return true;
}
// Main function to prepare the package
@@ -101,19 +68,17 @@ function preparePackage() {
const rootDir = path.join(__dirname, '..');
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
const requiredFiles = [
'package.json',
'README.md',
'index.js',
'scripts/init.js',
'scripts/dev.js'
'scripts/dev.js',
'assets/env.example',
'assets/gitignore',
'assets/example_prd.txt',
'.cursor/rules/dev_workflow.mdc'
];
let allFilesExist = true;
@@ -148,38 +113,6 @@ function preparePackage() {
log('warn', 'Some scripts could not be made executable. This may cause issues.');
}
// Check templates directory
const templatesDir = path.join(rootDir, 'templates');
if (!fileExists(templatesDir)) {
log('error', 'Templates directory does not exist');
process.exit(1);
}
// Check template files
const requiredTemplates = [
'README.md',
'env.example',
'gitignore',
'dev_workflow.mdc',
'dev.js',
'scripts_README.md',
'example_prd.txt'
];
let allTemplatesExist = true;
for (const template of requiredTemplates) {
const templatePath = path.join(templatesDir, template);
if (!fileExists(templatePath)) {
log('error', `Required template ${template} does not exist`);
allTemplatesExist = false;
}
}
if (!allTemplatesExist) {
log('error', 'Some required templates are missing. Package preparation failed.');
process.exit(1);
}
// Run npm pack to test package creation
try {
log('info', 'Running npm pack to test package creation...');