Skip to content

Commit

Permalink
Merge pull request #22 from saurabhdaware/no-inquirer
Browse files Browse the repository at this point in the history
Got rid of inquirer
  • Loading branch information
saurabhdaware authored Sep 19, 2019
2 parents ac3f7d2 + 1679669 commit 9378c2b
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 226 deletions.
140 changes: 91 additions & 49 deletions lib/action.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// external dependencies
const inquirer = require('inquirer');
const prompts = require('prompts');

// internal modules
const path = require('path');
Expand All @@ -8,8 +8,10 @@ const util = require('util');
const exec = util.promisify(require('child_process').exec);
const os = require('os');


// helper functions
const color = require('./colors.js');
// require('./helpers.js');

// Default settings.
const settingsData = {
Expand Down Expand Up @@ -66,53 +68,78 @@ function writeSettings(data, command = '<command>', successMessage = "Settings u
})
}

async function openURL(url) {
let stderr;
switch (process.platform) {
case "darwin":
({stderr} = await exec(`open ${url}`));
break;
case "win32":
({stderr} = await exec(`start ${url}`));
break;
default:
({stderr} = await exec(`xdg-open ${url}`));
break;
}
console.log('here ',stderr)
return stderr;
}

const suggestFilter = (input,choices) => {
return Promise.resolve(
choices.filter(choice =>
choice.title.toLowerCase().includes(input.toLowerCase())
)
)
}


const onCancel = () => {
console.error("See ya ('__') /");
process.exit();
return false;
}

function getChoices() {
const projects = [...settings.projects];
const result = projects.map(({...project}) => { // Spreading project to make it immutable
project.title = project.name;
project.value = {name: project.name, path: project.path};
if (project.editor && project.editor !== settings.commandToOpen) {
project.title += color.grey(` (${color.boldGrey('editor:')} ${color.grey(project.editor+')')}`);
project.value.editor = project.editor;
}
return project;
});

return result;
}

async function selectProject(projectName, action) {
let selectedProject;
if (!projectName) {
const projects = [...settings.projects];
// Ask which project he wants to open
const questions = [
{
type: 'list',
type: 'autocomplete',
message: `Select project to ${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 += color.grey(` (${color.boldGrey('editor:')} ${project.editor})`);
project.value.editor = project.editor;
}
return project;
})
choices:getChoices(),
limit:40,
suggest:suggestFilter
}
];

// Redirecting to stderr in order for it to be used with command substitution
var promptModule = inquirer.createPromptModule({output: process.stderr});
({selectedProject} = await promptModule(questions));
({selectedProject} = await prompts(questions,{onCancel}));

} else {
// If project name is mentioned then open directly
selectedProject = settings.projects.find(project => project.name.toLowerCase() == projectName.toLowerCase());
}
return selectedProject;
}

async function openURL(url) {
let stderr;
switch (process.platform) {
case "darwin":
({stderr} = await exec(`open ${url}`));
break;
case "win32":
({stderr} = await exec(`start ${url}`));
break;
default:
({stderr} = await exec(`xdg-open ${url}`));
break;
}
console.log('here ',stderr)
return stderr;
}

// Helper funcitions [END]

// oncmd: projectman open [projectName]
Expand Down Expand Up @@ -178,12 +205,12 @@ async function addProject(projectDirectory = '.', cmdObj = undefined) {
name = newProject.path.split(path.sep).pop();
}

({finalName: newProject.name} = await inquirer.prompt([{
type: 'input',
({finalName: newProject.name} = await prompts([{
type: 'text',
message: 'Project Name :',
name: 'finalName',
default: name
}]));
initial: name
}],{onCancel}));

if (cmdObj.repo) {
newProject.name = newProject.name.concat(` (${name.split('.')[0]})`)
Expand Down Expand Up @@ -213,7 +240,6 @@ async function removeProject(projectName) {
writeSettings(settings, 'remove', "Project Removed");
}


// pm seteditor [command]
async function setEditor(command, cmdObj = undefined) {
let commandToOpen;
Expand All @@ -231,47 +257,47 @@ async function setEditor(command, cmdObj = undefined) {
if (!command) {
const questions = [
{
type: 'list',
type: 'select',
message: 'Select text editor',
name: 'selectedEditor',
choices: [
{
name: 'VSCode',
title: 'VSCode',
value: 'code'
},
{
name: 'Sublime',
title: 'Sublime',
value: 'subl'
},
{
name: 'Atom',
title: 'Atom',
value: 'atom'
},
{
name: 'Vim',
title: 'Vim',
value: 'vim'
},
{
name: 'Other',
title: 'Other',
value: 'other'
}
]
}
];
const {selectedEditor} = await inquirer.prompt(questions);
const {selectedEditor} = await prompts(questions,{onCancel});
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 ${color.yellow('code <directory>')} in terminal to open directory`);
console.log(`In this case, the command would be ${color.yellow('code')}\n`);
const question = {
type: 'input',
type: 'text',
message: 'Enter command :',
name: 'command',
validate: function (val) {
return val !== ''
}
}
const {command} = await inquirer.prompt([question])
const {command} = await prompts([question],{onCancel})
commandToOpen = command;
} else {
commandToOpen = selectedEditor;
Expand Down Expand Up @@ -351,13 +377,29 @@ async function getProjectPath(projectName) {
return;
}

const selectedProject = await selectProject(projectName, "get directory");

if (!selectedProject) {
console.error(`Project does not exist. Add it using ${color.yellow('pm add [projectPath]')} or cd till the project folder and type ${color.yellow('pm add')}`);
return;
if(!projectName){
const question = {
name:'selectedProject',
message:'Enter number of project you want to cd to:',
type:'autocomplete',
out:process.stderr,
choices:getChoices(),
onRender:() => {
process.stderr.write('\033c');
}
}

const {selectedProject} = await prompts([question],{onCancel})
if (!selectedProject) {
console.error(`Project does not exist. Add it using ${color.yellow('pm add [projectPath]')} or cd till the project folder and type ${color.yellow('pm add')}`);
return;
}

}else{
selectedProject = settings.projects.find(project => project.name.toLowerCase() == projectName.toLowerCase());
}

// Print path
console.log(selectedProject.path);
}

Expand Down
6 changes: 5 additions & 1 deletion lib/colors.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Just some weird strings that color text inside it. You probably will not have to touch this file.

exports.green = (message) => `\u001b[32m${message}\u001b[39m`;

exports.boldGreen = (message) => `\u001b[1m\u001b[32m${message}\u001b[39m\u001b[22m`;
Expand All @@ -10,4 +12,6 @@ exports.boldYellow = (message) => `\u001b[1m\u001b[33m${message}\u001b[39m\u001b

exports.grey = (message) => `\u001b[90m${message}\u001b[39m`;

exports.boldGrey = (message) => `\u001b[1m\u001b[90m${message}\u001b[39m\u001b[22m`;
exports.boldGrey = (message) => `\u001b[1m\u001b[90m${message}\u001b[39m\u001b[22m`;

exports.bold = (message) => `\u001b[1m${message}\u001b[22m`;
9 changes: 9 additions & 0 deletions lib/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const readline = require('readline');

readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);

process.stdin.on('keypress', (str, key) => {
console.log(str)
console.log(key)
})
Loading

0 comments on commit 9378c2b

Please sign in to comment.