UNPKG

create-sps-project

Version:

CLI tool to create SPS Digital Tech template projects

96 lines (87 loc) 2.65 kB
const fs = require('fs'); const path = require('path'); const { execSync } = require('child_process'); const Logger = require('./logger'); class FileSystem { static createDirectory(dirPath) { if (!fs.existsSync(dirPath)) { fs.mkdirSync(dirPath, { recursive: true }); Logger.info(`Created directory: ${dirPath}`); } return dirPath; } static createWriteStream(filePath) { return fs.createWriteStream(filePath); } static extractZip(zipPath, destinationPath, platform) { try { if (platform === 'win32') { Logger.info('Using PowerShell Expand-Archive...'); execSync( `powershell.exe -Command "Expand-Archive -Path '${zipPath}' -DestinationPath '${destinationPath}' -Force"`, { stdio: 'inherit' } ); } else { execSync(`unzip -o "${zipPath}" -d "${destinationPath}"`, { stdio: 'inherit' }); } Logger.info('Extraction completed'); return true; } catch (error) { Logger.error('Error during extraction', error.message); return false; } } static deleteFile(filePath) { try { if (fs.existsSync(filePath)) { fs.unlinkSync(filePath); Logger.info(`Removed file: ${filePath}`); return true; } } catch (error) { Logger.warning(`Could not remove file: ${filePath}`); } return false; } static deleteDirectory(dirPath) { try { if (fs.existsSync(dirPath)) { fs.rmSync(dirPath, { recursive: true, force: true }); Logger.info(`Removed directory: ${dirPath}`); return true; } } catch (error) { Logger.warning(`Could not remove directory: ${dirPath}`); } return false; } static readDirectory(dirPath) { try { return fs.readdirSync(dirPath); } catch (error) { Logger.error(`Failed to read directory: ${dirPath}`, error.message); return []; } } static readJsonFile(filePath) { try { if (fs.existsSync(filePath)) { return JSON.parse(fs.readFileSync(filePath, 'utf8')); } } catch (error) { Logger.error(`Failed to read JSON file: ${filePath}`, error.message); } return null; } static installDependencies(dirPath) { try { Logger.info('\n📦 Installing dependencies...'); execSync('npm install', { cwd: dirPath, stdio: 'inherit' }); return true; } catch (error) { Logger.error('Failed to install dependencies', error.message); return false; } } } module.exports = FileSystem;