UNPKG

create-sps-project

Version:

CLI tool to create SPS Digital Tech template projects

117 lines (92 loc) 3.76 kB
const path = require('path'); const FileSystem = require('../utils/file-system'); const Logger = require('../utils/logger'); class TemplateInstaller { constructor(config) { this.config = config; } async install(zipPath) { const { projectDir, projectName, isSingleComponent } = this.config; try { // Extract the zip file const extracted = FileSystem.extractZip(zipPath, projectDir, process.platform); if (!extracted) { throw new Error('Failed to extract the zip file'); } // Delete the zip file after extraction FileSystem.deleteFile(zipPath); // Get list of extracted files const files = FileSystem.readDirectory(projectDir); if (files.length === 0) { throw new Error('No files were extracted from the zip'); } // Only install dependencies for templates with package.json, not components if (!isSingleComponent && files.includes('package.json')) { FileSystem.installDependencies(projectDir); } // Display success message and next steps this.displaySuccessMessage(files); return true; } catch (error) { this.cleanup(zipPath); throw error; } } displaySuccessMessage(files) { const { projectDir, projectName, isSingleComponent } = this.config; if (isSingleComponent) { this.displayComponentSuccessMessage(); } else { Logger.success('Template setup completed successfully!'); Logger.info(`📁 Project created at: ${projectDir}`); const nextSteps = [ `cd ${projectName}`, 'Update the .env file with your configuration', 'npm run dev' ]; Logger.showNextSteps(nextSteps); } } displayComponentSuccessMessage() { const { projectDir } = this.config; // Check if manifest.json exists const manifestPath = path.join(projectDir, 'manifest.json'); const manifestData = FileSystem.readJsonFile(manifestPath); if (manifestData && manifestData.components && manifestData.components.length > 0) { Logger.success('Component installation completed successfully!'); Logger.info('\nInstalled components:'); manifestData.components.forEach(comp => Logger.info(`- ${comp}`)); const nextSteps = [ 'Import the component(s) in your project', 'Make sure to import global.css in your layout or page' ]; Logger.showNextSteps(nextSteps); } else { // Check if components directory exists const componentsDir = path.join(projectDir, 'components'); if (FileSystem.readDirectory(componentsDir).length > 0) { Logger.success('Component installation completed successfully!'); Logger.info('\nComponents were extracted to the ./components directory'); const nextSteps = [ 'Import the component(s) in your project', 'Make sure to import global.css in your layout or page' ]; Logger.showNextSteps(nextSteps); } else { Logger.success('Files extracted successfully!'); Logger.info('\nPlease check the component\'s markdown file for usage instructions.'); Logger.info('\nHappy coding! 🎉'); } } } cleanup(zipPath) { const { projectDir, isSingleComponent } = this.config; // Delete the zip file if it exists FileSystem.deleteFile(zipPath); // Only delete the project directory if not in single component mode if (!isSingleComponent) { FileSystem.deleteDirectory(projectDir); } } } module.exports = TemplateInstaller;