Skip to content

Commit

Permalink
changed function name from getProject to selectProject // using selec…
Browse files Browse the repository at this point in the history
…tProject in removeProject command
  • Loading branch information
saurabhdaware committed Sep 8, 2019
1 parent f393c99 commit da11074
Showing 1 changed file with 15 additions and 32 deletions.
47 changes: 15 additions & 32 deletions lib/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ try{
}
//

// Helper functions

// Helper functions [START]
console.error = function(message){
console.log(chalk.bold.red(">>> ") + message);
};
Expand Down Expand Up @@ -65,7 +64,7 @@ function writeSettings(data, command='<command>', successMessage = "Settings upd
})
}

async function getProject(projectName=undefined, action="open"){
async function selectProject(projectName, action){
let selectedProject;
if(!projectName){
// Ask which project he wants to open
Expand All @@ -74,7 +73,10 @@ async function getProject(projectName=undefined, action="open"){
type: 'list',
message: `Select project to ${action}`,
name: 'selectedProject',
choices: settings.projects.map(project => project.value = project)
choices: settings.projects.map(project => {
project.value = {name:project.name,path:project.path};
return project;
})
}
];

Expand All @@ -83,14 +85,13 @@ async function getProject(projectName=undefined, action="open"){
({selectedProject} = await promptModule(questions));
}else{
// If project name is mentioned then open directly
projectName = projectName.toLowerCase();
selectedProject = settings.projects.find(project => project.name.toLowerCase() == projectName);
selectedProject = settings.projects.find(project => project.name.toLowerCase() == projectName.toLowerCase());
}

console.log(selectedProject);
return selectedProject;
}

//
// Helper funcitions [END]

// oncmd: projectman open [projectName]
async function openProject(projectName){
Expand All @@ -100,7 +101,7 @@ async function openProject(projectName){
return;
}

const selectedProject = await getProject(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`");
Expand Down Expand Up @@ -189,34 +190,16 @@ async function addProject(projectDirectory = '.'){

// pm remove [projectName]
async function removeProject(projectName){
let selectedProjectName;
if(!projectName){
// Ask which project he wants to open
const questions = [
{
type: 'list',
message: 'Select project to remove',
name: 'selectedProjectName',
choices: settings.projects.map(project => project.value = project.name)
}
];

({selectedProjectName} = await inquirer.prompt(questions));
}else{
if(!settings.projects.some(project => project.name.toLowerCase() === projectName.toLowerCase())){
console.error("Project with that name does not exist");
return;
}

selectedProjectName = 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
Expand Down Expand Up @@ -255,7 +238,7 @@ async function getProjectPath(projectName){
return;
}

const selectedProject = await getProject(projectName, "get directory");
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`");
Expand Down

0 comments on commit da11074

Please sign in to comment.