Skip to content

Commit 542a41f

Browse files
Merge pull request #18 from saurabhdaware/add-editor-forproject
v1.2.0-beta.1
2 parents 39656bd + e6bbbaa commit 542a41f

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

.npmignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
tests
22
mocharc.json
33
images/terminal.gif
4-
build
4+
build
5+
dist

bin/index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,20 @@ program
2828
.option('-f|--for-project [projectName]', 'set different editor for specific project')
2929
.action(action.setEditor);
3030

31+
program
32+
.command('rmeditor [projectName]')
33+
.description("Remove text editor to use")
34+
.option('-a|--all', 'remove editors from all projects')
35+
.action(action.rmEditor);
36+
3137
program
3238
.command('edit')
3339
.description("Edit settings.json")
3440
.action(action.editConfigurations);
3541

3642
program
3743
.command('getpath [projectName]')
38-
.alias('path')
44+
.alias('gp')
3945
.description("Get project path")
4046
.action(action.getProjectPath);
4147

lib/action.js

+55-5
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ async function selectProject(projectName, action){
7676
name: 'selectedProject',
7777
choices: projects.map(({...project}) => { // Spreading project to make it immutable
7878
project.value = {name:project.name,path:project.path};
79+
if(project.editor && project.editor !== settings.commandToOpen){
80+
project.name += chalk.grey(` (${chalk.bold.grey('editor:')} ${project.editor})`);
81+
project.value.editor = project.editor;
82+
}
7983
return project;
8084
})
8185
}
@@ -100,6 +104,7 @@ async function openProject(projectName){
100104
return;
101105
}
102106

107+
console.log(chalk.bold.grey('>>> Default editor: ')+chalk.grey(settings.commandToOpen));
103108
const selectedProject = await selectProject(projectName, 'open');
104109

105110
if(!selectedProject){
@@ -122,6 +127,7 @@ async function openProject(projectName){
122127
}catch(err){
123128
console.error("Could not open project :(");
124129
console.warn(`Are you sure your editor uses command ${chalk.yellow(commandToOpen)} to open directories from terminal?`);
130+
console.warn(`If not, use ${chalk.yellow('pm seteditor')} to set Editor/IDE of your choice`)
125131
throwCreateIssueError(err);
126132
return;
127133
}
@@ -196,13 +202,33 @@ async function setEditor(command,cmdObj=undefined){
196202
{
197203
name:'Vim',
198204
value:'vim'
205+
},
206+
{
207+
name:'Other',
208+
value:'other'
199209
}
200210
]
201211
}
202212
];
203-
// console.log(); // Just wanted to line break; Sorry god of good practices ;_;
204-
// console.warn(`If the TextEditor/IDE you are looking for is not listed here, Run ${chalk.yellow('pm seteditor [commandToOpenEditor]')} \n'commandToOpenEditor' is the command you can type in terminal to open project E.g for Webstorm it is 'wstorm' for VSCode it is 'code'\n`);
205-
({selectedEditor:commandToOpen} = await inquirer.prompt(questions));
213+
const {selectedEditor} = await inquirer.prompt(questions);
214+
if(selectedEditor == 'other'){
215+
console.warn("Enter command that you use to open Editor from Terminal");
216+
console.log(`E.g With VSCode Installed, you can type ${chalk.yellow('code <directory>')} in terminal to open directory`);
217+
console.log(`In this case, the command would be ${chalk.yellow('code')}\n`);
218+
const question = {
219+
type:'input',
220+
message:'Enter command :',
221+
name:'command',
222+
validate:function(val){
223+
return val !== ''
224+
}
225+
}
226+
const {command} = await inquirer.prompt([question])
227+
commandToOpen = command;
228+
}else{
229+
commandToOpen = selectedEditor;
230+
}
231+
206232
}else{
207233
commandToOpen = command;
208234
}
@@ -213,9 +239,33 @@ async function setEditor(command,cmdObj=undefined){
213239
settings.projects[selectedIndex].editor = commandToOpen;
214240
}
215241

216-
writeSettings(settings,'seteditor',"Text Editor Selected");
242+
writeSettings(settings,'seteditor',`Text Editor Selected ${(selectedIndex < 0)?'':`for ${settings.projects[selectedIndex].name}`}`);
217243
}
218244

245+
async function rmEditor(projectName,cmdObj){
246+
let newSettings;
247+
if(cmdObj.all){
248+
newSettings = {
249+
...settings,
250+
projects:settings.projects.map(({...project}) => {
251+
if(project.editor) delete project.editor;
252+
return project;
253+
})}
254+
}else{
255+
console.log(chalk.bold.grey('>>> Default editor: ')+chalk.grey(settings.commandToOpen));
256+
let selectedProject = await selectProject(projectName, 'remove editor from');
257+
if(!selectedProject){
258+
console.error(`Project with name ${selectedProject} does not exist. Try ${chalk.yellow('pm rmeditor')} and select the project you want to remove the editor from`);
259+
return;
260+
}
261+
262+
const selectedIndex = settings.projects.findIndex(project => selectedProject.name.toLowerCase() == project.name.toLowerCase());
263+
delete settings.projects[selectedIndex].editor;
264+
newSettings = {...settings};
265+
}
266+
267+
writeSettings(newSettings,'rmeditor',`TextEditor Removed`);
268+
}
219269

220270
// projectman edit
221271
async function editConfigurations(){
@@ -262,4 +312,4 @@ async function getProjectPath(projectName){
262312
console.log(selectedProject.path);
263313
}
264314

265-
module.exports = {openProject, addProject, removeProject, editConfigurations, setEditor, getProjectPath};
315+
module.exports = {openProject, addProject, removeProject, editConfigurations, setEditor, rmEditor, getProjectPath};

package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "projectman",
3-
"version": "1.2.0-alpha.2",
3+
"version": "1.2.0-beta.1",
44
"description": "Hate opening folders? Select and open your projects in your favourite editor straight from your command line without 'CD'ing into the deeply nested folders.",
55
"main": "bin/index.js",
66
"bin": {
@@ -9,7 +9,8 @@
99
},
1010
"preferGlobal": true,
1111
"scripts": {
12-
"test": "mocha --recursive \"./tests/*.js\""
12+
"test": "mocha --recursive \"./tests/*.js\"",
13+
"build": "pkg ./package.json --out-path ./dist"
1314
},
1415
"repository": {
1516
"type": "git",
@@ -35,6 +36,7 @@
3536
},
3637
"devDependencies": {
3738
"chai": "^4.2.0",
38-
"mocha": "^6.2.0"
39+
"mocha": "^6.2.0",
40+
"pkg": "^4.4.0"
3941
}
4042
}

0 commit comments

Comments
 (0)