Skip to content

Commit

Permalink
feat(project-update): adds auto-completion for argument and project ids
Browse files Browse the repository at this point in the history
  • Loading branch information
Ephigenia committed Apr 19, 2019
1 parent d5342c6 commit a36923d
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 5 deletions.
1 change: 1 addition & 0 deletions source/lib/auto-complete/completions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
list: require('./list'),
lock: require('./lock'),
new: require('./new'),
project: require('./project'),
projects: require('./projects'),
services: require('./services'),
unlock: require('./unlock'),
Expand Down
42 changes: 42 additions & 0 deletions source/lib/auto-complete/completions/project-update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env node
'use strict';

const config = require('./../../../config.js');
const miteApi = require('./../../mite-api')(config.get());

/**
* https://www.npmjs.com/package/tabtab#3-parsing-env
*
* @param {string} env.lastPartial - the characters entered in the current
* argument before hitting tabtab
* @param {string} env.prev - last given argument value, or previously
* completed value
* @param {string} env.words - the number of argument currently active
* @param {string} env.line - the current complete input line in the cli
* @returns {Promise<Array<string>>}
*/
module.exports = async ({ prev, line, word }) => {
switch(prev) {
case '--archived':
return ['yes', 'no'];
}
// return a list of project ids
return miteApi.getProjects({ archived: false })
.then(projects => projects.map(c => ({
name: String(c.id),
description: c.name
})))
.then(projects => {
return projects.concat([
line.indexOf('--archived') === -1 ? {
name: '--archived',
description: 'archive or unarchive a project',
} : undefined,
word < 4 ? {
name: '--help',
description: 'show help message',
} : undefined,
]);
});
};

37 changes: 37 additions & 0 deletions source/lib/auto-complete/completions/project.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env node
'use strict';

const projectUpdateCompletion = require('./project-update');

/**
* https://www.npmjs.com/package/tabtab#3-parsing-env
*
* @param {string} env.lastPartial - the characters entered in the current
* argument before hitting tabtab
* @param {string} env.prev - last given argument value, or previously
* completed value
* @param {string} env.words - the number of argument currently active
* @param {string} env.line - the current complete input line in the cli
* @returns {Promise<Array<string>>}
*/
module.exports = async function ({ line }) {
// check wheter the update sub-sub command is called and forward completion
// to that command
const thirdArg = line.split(/\s/).splice(2)[0];
switch (thirdArg) {
case 'update':
// forward auto-completion to sub-sub-command
return projectUpdateCompletion.apply(projectUpdateCompletion, arguments);
}
return [
{
name: 'update',
description: 'update a single project by it’s id',
},
{
name: '--help',
description: 'show help message',
},
];
};

11 changes: 7 additions & 4 deletions source/lib/auto-complete/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ const autoComplete = {
return tabtab.log(subCommands);
}

// find out which command was called
const subCommand = env.line.split(/\s/)[1];
// there might be a sub-command called, find out by checking the second
// argument and if it’s a sub-command
const secondArg = env.line.split(/\s/)[1];
const subCommand = program.commands.find(c => c._name === secondArg);
if (subCommand) {
return this.completionForSubcommand(subCommand, env);
return this.completionForSubcommand(subCommand._name, env);
}
},

Expand All @@ -58,7 +60,8 @@ const autoComplete = {
if (!completer) return [];

return completer(env).then((r) => {
if (r) tabtab.log(r);
// filter out empty values
if (r) tabtab.log(r.filter(v => v));
});
},

Expand Down
4 changes: 4 additions & 0 deletions source/mite-project-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const config = require('./config.js');
program
.version(pkg.version)
.arguments('<projectId>')
// @TODO add edit "budget"
// @TODO add edit "hourly_rate"
// @TODO add edit "name"
// @TODO add edit "note"
.option(
'--archived <true|false>',
'changes the archived state of the project',
Expand Down
3 changes: 2 additions & 1 deletion source/mite-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const pkg = require('./../package.json');

program
.version(pkg.version)
.command('update', 'update a single project')
.command('update', 'update a single project', { isDefault: true })
.command('blub', 'update a single project')
// @TODO add delete
.parse(process.argv);

Expand Down

0 comments on commit a36923d

Please sign in to comment.