Skip to content

Commit

Permalink
Merge pull request #9 from ZakariaTalhami/add-getpath-command
Browse files Browse the repository at this point in the history
Added command to retrieve the path of a project
  • Loading branch information
saurabhdaware authored Sep 8, 2019
2 parents fa0c68f + cef0276 commit 5a551c7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 17 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ You can set `editor` key in [settings.json](settingsjson) with [commandToOpen](#

**Read [example settings from 'pm edit'](#settingsjson)**


### Get project path
```shell
pm path
```

optionally you can also specify the project name directly as an argument
```shell
pm path <projectName>
```

---

## Settings.json
Expand Down
6 changes: 6 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ program
.description("Edit settings.json")
.action(action.editConfigurations);

program
.command('getpath [projectName]')
.alias('path')
.description("Get project path")
.action(action.getProjectPath);

program
.arguments("<command>")
.action((command) => {
Expand Down
58 changes: 41 additions & 17 deletions lib/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ try{

console.error = function(message){
console.log(chalk.bold.red(">>> ") + message);
}
};

console.warn = function(message){
console.log(chalk.bold.yellow(">>> ")+message);
Expand Down Expand Up @@ -65,36 +65,43 @@ function writeSettings(data, command='<command>', successMessage = "Settings upd
})
}

//

// projectman open [projectName]
async function openProject(projectName){
async function getProject(projectName=undefined, action="open"){
let selectedProject;
if(settings.projects.length == 0){
console.error("No projects to open :(");
console.warn(`cd /till/project/directory/ and run ${chalk.bold.yellow('pm add')} to add projects and get started`);
return;
}

if(!projectName){
// Ask which project he wants to open
const questions = [
{
type: 'list',
message: 'Select project to open',
message: `Select project to ${action}`,
name: 'selectedProject',
choices: settings.projects.map(project => project.value = project)
}
];

({selectedProject} = await inquirer.prompt(questions));

// Redirecting to stderr in order for it to be used with command substitution
var promptModule = inquirer.createPromptModule({ output: process.stderr });
({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);
}

return selectedProject;
}

//

// oncmd: projectman open [projectName]
async function openProject(projectName){
if(settings.projects.length == 0){
console.error("No projects to open :(");
console.warn(`cd /till/project/directory/ and run ${chalk.bold.yellow('pm add')} to add projects and get started`);
return;
}

const selectedProject = await getProject(projectName);

if(!selectedProject){
console.error("Project does not exist. Add it using `pm add [projectPath]` or cd till the project folder and type `pm add`");
return;
Expand All @@ -117,8 +124,8 @@ async function openProject(projectName){
throwCreateIssueError(err);
return;
}


console.log(`Opening ${selectedProject.name} :D !`);
}

// pm seteditor [command]
Expand Down Expand Up @@ -241,4 +248,21 @@ async function editConfigurations(){
}
}

module.exports = {openProject, addProject, removeProject, editConfigurations, setEditor}
async function getProjectPath(projectName){
if(settings.projects.length === 0){
console.error("No projects to get path :(");
console.warn(`cd /till/project/directory/ and run ${chalk.bold.yellow('pm add')} to add projects and get started`);
return;
}

const selectedProject = await getProject(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`");
return;
}

console.log(selectedProject.path);
}

module.exports = {openProject, addProject, removeProject, editConfigurations, setEditor, getProjectPath};

0 comments on commit 5a551c7

Please sign in to comment.