Skip to content

Commit

Permalink
Merge pull request #18 from saurabhdaware/add-editor-forproject
Browse files Browse the repository at this point in the history
v1.2.0-beta.1
  • Loading branch information
saurabhdaware committed Sep 11, 2019
2 parents 39656bd + e6bbbaa commit 542a41f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
tests
mocharc.json
images/terminal.gif
build
build
dist
8 changes: 7 additions & 1 deletion bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,20 @@ program
.option('-f|--for-project [projectName]', 'set different editor for specific project')
.action(action.setEditor);

program
.command('rmeditor [projectName]')
.description("Remove text editor to use")
.option('-a|--all', 'remove editors from all projects')
.action(action.rmEditor);

program
.command('edit')
.description("Edit settings.json")
.action(action.editConfigurations);

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

Expand Down
60 changes: 55 additions & 5 deletions lib/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ async function selectProject(projectName, action){
name: 'selectedProject',
choices: projects.map(({...project}) => { // Spreading project to make it immutable
project.value = {name:project.name,path:project.path};
if(project.editor && project.editor !== settings.commandToOpen){
project.name += chalk.grey(` (${chalk.bold.grey('editor:')} ${project.editor})`);
project.value.editor = project.editor;
}
return project;
})
}
Expand All @@ -100,6 +104,7 @@ async function openProject(projectName){
return;
}

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

if(!selectedProject){
Expand All @@ -122,6 +127,7 @@ async function openProject(projectName){
}catch(err){
console.error("Could not open project :(");
console.warn(`Are you sure your editor uses command ${chalk.yellow(commandToOpen)} to open directories from terminal?`);
console.warn(`If not, use ${chalk.yellow('pm seteditor')} to set Editor/IDE of your choice`)
throwCreateIssueError(err);
return;
}
Expand Down Expand Up @@ -196,13 +202,33 @@ async function setEditor(command,cmdObj=undefined){
{
name:'Vim',
value:'vim'
},
{
name:'Other',
value:'other'
}
]
}
];
// console.log(); // Just wanted to line break; Sorry god of good practices ;_;
// 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`);
({selectedEditor:commandToOpen} = await inquirer.prompt(questions));
const {selectedEditor} = await inquirer.prompt(questions);
if(selectedEditor == 'other'){
console.warn("Enter command that you use to open Editor from Terminal");
console.log(`E.g With VSCode Installed, you can type ${chalk.yellow('code <directory>')} in terminal to open directory`);
console.log(`In this case, the command would be ${chalk.yellow('code')}\n`);
const question = {
type:'input',
message:'Enter command :',
name:'command',
validate:function(val){
return val !== ''
}
}
const {command} = await inquirer.prompt([question])
commandToOpen = command;
}else{
commandToOpen = selectedEditor;
}

}else{
commandToOpen = command;
}
Expand All @@ -213,9 +239,33 @@ async function setEditor(command,cmdObj=undefined){
settings.projects[selectedIndex].editor = commandToOpen;
}

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

async function rmEditor(projectName,cmdObj){
let newSettings;
if(cmdObj.all){
newSettings = {
...settings,
projects:settings.projects.map(({...project}) => {
if(project.editor) delete project.editor;
return project;
})}
}else{
console.log(chalk.bold.grey('>>> Default editor: ')+chalk.grey(settings.commandToOpen));
let selectedProject = await selectProject(projectName, 'remove editor from');
if(!selectedProject){
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`);
return;
}

const selectedIndex = settings.projects.findIndex(project => selectedProject.name.toLowerCase() == project.name.toLowerCase());
delete settings.projects[selectedIndex].editor;
newSettings = {...settings};
}

writeSettings(newSettings,'rmeditor',`TextEditor Removed`);
}

// projectman edit
async function editConfigurations(){
Expand Down Expand Up @@ -262,4 +312,4 @@ async function getProjectPath(projectName){
console.log(selectedProject.path);
}

module.exports = {openProject, addProject, removeProject, editConfigurations, setEditor, getProjectPath};
module.exports = {openProject, addProject, removeProject, editConfigurations, setEditor, rmEditor, getProjectPath};
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "projectman",
"version": "1.2.0-alpha.2",
"version": "1.2.0-beta.1",
"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.",
"main": "bin/index.js",
"bin": {
Expand All @@ -9,7 +9,8 @@
},
"preferGlobal": true,
"scripts": {
"test": "mocha --recursive \"./tests/*.js\""
"test": "mocha --recursive \"./tests/*.js\"",
"build": "pkg ./package.json --out-path ./dist"
},
"repository": {
"type": "git",
Expand All @@ -35,6 +36,7 @@
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^6.2.0"
"mocha": "^6.2.0",
"pkg": "^4.4.0"
}
}

0 comments on commit 542a41f

Please sign in to comment.