UNPKG

basecamp-guide

Version:
189 lines (150 loc) 6.34 kB
var fs = require('fs'); var p = require('path'); var Colors = require('colors'); var readlineSync = require('readline-sync'); var FileContent = require('./FileContent.js'); var Feedback = require('./Feedback.js'); var FileSystem = (function() { // private methods var fileExist = function(path) { try { fs.statSync(path); } catch (e) { return false; } return true; }; var fileNotExist = function(path) { return ! fileExist(path); }; var addStubPath = function(file) { return p.join(__dirname, '/../stubs', file); }; var shouldOverwrite = function(destinationFile) { // check if file exists at destination to prevent overriding if (fileNotExist(destinationFile)) return true; // because there is no file var question = 'Override ' + destinationFile + '?'; var answer = readlineSync.question(question.cyan + ' (yes) '); if (answer == 'yes' || answer == 'y' || answer == 'YES' || answer == 'Y') return true; return false; }; return { // public methods modifyFile : function(fileToModify, hook) { if (fileExist(fileToModify)) { var content = FileContent.init(fs.readFileSync(fileToModify).toString()); var oldContent = content.get(); // make changes to file content modifiedContent = hook(content); if (modifiedContent !== undefined && modifiedContent !== 'NO_UPDATE' ) { // check if content was modified if (modifiedContent.get() === oldContent) { Feedback.tell('FILE_NOT_MODIFIED', fileToModify); return false; } // write file fs.writeFileSync(fileToModify, modifiedContent.get()); Feedback.tell('FILE_MODIFIED', fileToModify); return true; } Feedback.tell('FILE_NO_UPDATE', fileToModify); return false; } else { Feedback.tell('FILE_NOT_EXIST', fileToModify); return false; } }, makeDir : function(directory) { var directories = directory.split(p.sep); directories = directories.filter(function(dirName) { return (dirName !== ''); }); var dirPath = ''; var that = this; directories.forEach(function(directory) { dirPath = p.join(dirPath, directory); // check if dir exists if (fileNotExist(dirPath)) { fs.mkdirSync(dirPath); } }); }, copyFromStub : function(stubFile, destinationDir, hook) { // prepare copying stubFile = addStubPath(stubFile); var destinationFile = p.join(destinationDir, p.basename(stubFile)); // copy this.copyFile(stubFile, destinationFile, hook); }, copyFromFeaturesToStub : function(featureFile, destinationDir, hook) { // prepare copying featureFile = this.addFeaturePath(featureFile); destinationDir = this.addStubPath(destinationDir); var destinationFile = p.join(destinationDir, p.basename(featureFile)); // copy this.copyFile(featureFile, destinationFile, hook); }, copyFile : function(file, destinationFile, hook) { if (fileExist(file)) { if(! shouldOverwrite(destinationFile)) { Feedback.tell('FILE_EXIST', destinationFile); return false; } var content = FileContent.init(fs.readFileSync(file).toString()); if (typeof(hook) === "function") { content = hook(content); } if (content !== undefined && content !== 'NO_UPDATE') { // checking existance of directory this.makeDir(p.dirname(destinationFile)); // write file fs.writeFileSync(destinationFile, content.get()); // info to console Feedback.tell('FILE_CREATED', destinationFile); return true; } Feedback.tell('FILE_NOT_CREATED', destinationFile); return false; } Feedback.tell('FILE_NOT_EXIST', file); return false; }, copyDir : function(sourceDir, destinationDir) { files = fs.readdirSync(sourceDir); var that = this; files.forEach(function(fileName) { that.copyFile( p.join(sourceDir, fileName), p.join(destinationDir, fileName) ); }); }, deleteFile : function(file) { if (fileNotExist(file)) { Feedback.tell('FILE_NOT_EXIST', file); return false; } else { fs.unlinkSync(file); Feedback.tell('FILE_DELETED', file); return true; } }, deleteDir : function(directory) { if (fileExist(directory)) { files = fs.readdirSync(directory); if (files.length === 0) { fs.rmdirSync(directory); Feedback.tell('DIRECTORY_DELETED', directory); return true; } else { Feedback.tell('DIRECTORY_HAS_FILES', directory); return false; } } else { Feedback.tell('DIRECTORY_NOT_EXIST', directory); return false; } } }; })(); module.exports = FileSystem;