From e85f72271ec0b901f71110de8601279488a79136 Mon Sep 17 00:00:00 2001 From: Saurabh Daware Date: Wed, 11 Sep 2019 17:16:42 +0530 Subject: [PATCH] settings mutability fix --- lib/action.js | 78 +++++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/lib/action.js b/lib/action.js index bf4a094..7634f4a 100644 --- a/lib/action.js +++ b/lib/action.js @@ -67,19 +67,19 @@ function writeSettings(data, command='', successMessage = "Settings upd async function selectProject(projectName, action){ let selectedProject; if(!projectName){ + const projects = [...settings.projects]; // Ask which project he wants to open const questions = [ { type: 'list', - message: `Select project to ${action}`, + message: `Select project to ${action} :`, name: 'selectedProject', - choices: settings.projects.map(project => { + choices: projects.map(({...project}) => { // Spreading project to make it immutable project.value = {name:project.name,path:project.path}; return project; }) } ]; - // Redirecting to stderr in order for it to be used with command substitution var promptModule = inquirer.createPromptModule({ output: process.stderr }); ({selectedProject} = await promptModule(questions)); @@ -87,7 +87,6 @@ async function selectProject(projectName, action){ // If project name is mentioned then open directly selectedProject = settings.projects.find(project => project.name.toLowerCase() == projectName.toLowerCase()); } - // console.log(selectedProject); return selectedProject; } @@ -104,7 +103,7 @@ async function openProject(projectName){ const selectedProject = await selectProject(projectName, 'open'); if(!selectedProject){ - console.error("Project does not exist. Add it using `pm add [projectPath]` or cd till the project folder and type `pm add`"); + console.error(`Project does not exist. Add it using ${chalk.yellow('pm add [projectPath]')} or cd till the project folder and type ${chalk.yellow('pm add')}`); return; } @@ -129,17 +128,47 @@ async function openProject(projectName){ } +// projectman add [projectDirectory] +async function addProject(projectDirectory = '.'){ + let newProject = {}; + newProject.path = path.resolve(projectDirectory); + const name = newProject.path.split(path.sep).pop(); + ({finalName:newProject.name} = await inquirer.prompt([{type:'input',message:'Project Name :',name:'finalName',default:name}])) + + if(settings.projects.some(project => project.name.toLowerCase() == newProject.name.toLowerCase())){ + console.error("Project with this name already exists"); + return; + } + + settings.projects.push(newProject); + + writeSettings(settings, 'add', "Project Added"); +} + +// pm remove [projectName] +async function removeProject(projectName){ + const {name:selectedProjectName} = await selectProject(projectName,'remove'); + + if(!selectedProjectName){ + console.error(`Project with name ${selectedProjectName} does not exist. Try ${chalk.yellow('pm remove')} and select the project you want to remove`); + return; + } + // removing project + settings.projects = settings.projects.filter(project => project.name.toLowerCase() !== selectedProjectName.toLowerCase()); + + writeSettings(settings, 'remove', "Project Removed"); +} + + // pm seteditor [command] async function setEditor(command,cmdObj=undefined){ let commandToOpen; let selectedProject; let selectedIndex = -1; // this will have the index of selectedProject let projectName; - if(cmdObj.forProject){ // --for-project exists projectName = (cmdObj.forProject === true)?undefined:cmdObj.forProject; // if only[--for-project], set undefined else if [--for-project [projectName]] set projectName selectedProject = await selectProject(projectName,'set editor for'); // sending undefined, calls list of projects to select from - // find the index of selectedProject and add editor to that project selectedIndex = settings.projects.findIndex(project => project.name.toLowerCase() == selectedProject.name.toLowerCase()) } @@ -184,40 +213,9 @@ async function setEditor(command,cmdObj=undefined){ settings.projects[selectedIndex].editor = commandToOpen; } - console.log(settings); - // writeSettings(settings,'seteditor',"Text Editor Selected"); + writeSettings(settings,'seteditor',"Text Editor Selected"); } -// projectman add [projectDirectory] -async function addProject(projectDirectory = '.'){ - let newProject = {}; - newProject.path = path.resolve(projectDirectory); - const name = newProject.path.split(path.sep).pop(); - ({finalName:newProject.name} = await inquirer.prompt([{type:'input',message:'Project Name',name:'finalName',default:name}])) - - if(settings.projects.some(project => project.name.toLowerCase() == newProject.name.toLowerCase())){ - console.error("Project with this name already exists"); - return; - } - - settings.projects.push(newProject); - - writeSettings(settings, 'add', "Project Added"); -} - -// pm remove [projectName] -async function removeProject(projectName){ - const {name:selectedProjectName} = await selectProject(projectName,'remove'); - - if(!selectedProjectName){ - console.error(`Project with name ${selectedProjectName} does not exist. Try ${chalk.yellow('pm remove')} and select the project you want to remove`); - return; - } - // removing project - settings.projects = settings.projects.filter(project => project.name.toLowerCase() !== selectedProjectName.toLowerCase()); - - writeSettings(settings, 'remove', "Project Removed"); -} // projectman edit async function editConfigurations(){ @@ -257,7 +255,7 @@ async function getProjectPath(projectName){ const selectedProject = await selectProject(projectName, "get directory"); if(!selectedProject){ - console.error("Project does not exist. Add it using `pm add [projectPath]` or cd till the project folder and type `pm add`"); + console.error(`Project does not exist. Add it using ${chalk.yellow('pm add [projectPath]')} or cd till the project folder and type ${chalk.yellow('pm add')}`); return; }