Skip to content

Commit 5a551c7

Browse files
Merge pull request #9 from ZakariaTalhami/add-getpath-command
Added command to retrieve the path of a project
2 parents fa0c68f + cef0276 commit 5a551c7

File tree

3 files changed

+58
-17
lines changed

3 files changed

+58
-17
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ You can set `editor` key in [settings.json](settingsjson) with [commandToOpen](#
7474

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

77+
78+
### Get project path
79+
```shell
80+
pm path
81+
```
82+
83+
optionally you can also specify the project name directly as an argument
84+
```shell
85+
pm path <projectName>
86+
```
87+
7788
---
7889

7990
## Settings.json

bin/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ program
3232
.description("Edit settings.json")
3333
.action(action.editConfigurations);
3434

35+
program
36+
.command('getpath [projectName]')
37+
.alias('path')
38+
.description("Get project path")
39+
.action(action.getProjectPath);
40+
3541
program
3642
.arguments("<command>")
3743
.action((command) => {

lib/action.js

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ try{
3737

3838
console.error = function(message){
3939
console.log(chalk.bold.red(">>> ") + message);
40-
}
40+
};
4141

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

68-
//
69-
70-
// projectman open [projectName]
71-
async function openProject(projectName){
68+
async function getProject(projectName=undefined, action="open"){
7269
let selectedProject;
73-
if(settings.projects.length == 0){
74-
console.error("No projects to open :(");
75-
console.warn(`cd /till/project/directory/ and run ${chalk.bold.yellow('pm add')} to add projects and get started`);
76-
return;
77-
}
78-
7970
if(!projectName){
8071
// Ask which project he wants to open
8172
const questions = [
8273
{
8374
type: 'list',
84-
message: 'Select project to open',
75+
message: `Select project to ${action}`,
8576
name: 'selectedProject',
8677
choices: settings.projects.map(project => project.value = project)
8778
}
8879
];
8980

90-
({selectedProject} = await inquirer.prompt(questions));
91-
81+
// Redirecting to stderr in order for it to be used with command substitution
82+
var promptModule = inquirer.createPromptModule({ output: process.stderr });
83+
({selectedProject} = await promptModule(questions));
9284
}else{
9385
// If project name is mentioned then open directly
9486
projectName = projectName.toLowerCase();
9587
selectedProject = settings.projects.find(project => project.name.toLowerCase() == projectName);
9688
}
9789

90+
return selectedProject;
91+
}
92+
93+
//
94+
95+
// oncmd: projectman open [projectName]
96+
async function openProject(projectName){
97+
if(settings.projects.length == 0){
98+
console.error("No projects to open :(");
99+
console.warn(`cd /till/project/directory/ and run ${chalk.bold.yellow('pm add')} to add projects and get started`);
100+
return;
101+
}
102+
103+
const selectedProject = await getProject(projectName);
104+
98105
if(!selectedProject){
99106
console.error("Project does not exist. Add it using `pm add [projectPath]` or cd till the project folder and type `pm add`");
100107
return;
@@ -117,8 +124,8 @@ async function openProject(projectName){
117124
throwCreateIssueError(err);
118125
return;
119126
}
120-
121-
127+
128+
console.log(`Opening ${selectedProject.name} :D !`);
122129
}
123130

124131
// pm seteditor [command]
@@ -241,4 +248,21 @@ async function editConfigurations(){
241248
}
242249
}
243250

244-
module.exports = {openProject, addProject, removeProject, editConfigurations, setEditor}
251+
async function getProjectPath(projectName){
252+
if(settings.projects.length === 0){
253+
console.error("No projects to get path :(");
254+
console.warn(`cd /till/project/directory/ and run ${chalk.bold.yellow('pm add')} to add projects and get started`);
255+
return;
256+
}
257+
258+
const selectedProject = await getProject(projectName, "get directory");
259+
260+
if(!selectedProject){
261+
console.error("Project does not exist. Add it using `pm add [projectPath]` or cd till the project folder and type `pm add`");
262+
return;
263+
}
264+
265+
console.log(selectedProject.path);
266+
}
267+
268+
module.exports = {openProject, addProject, removeProject, editConfigurations, setEditor, getProjectPath};

0 commit comments

Comments
 (0)