Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace cli with commander #892

Merged
merged 5 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions lib/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,20 @@ const withPassword = (f) => {
prompt.get([{ name: 'password', hidden: true, replace: '*' }], (_, { password }) => f(password));
};

// command line nonsense (i'm not a huge fan of this library).
const cli = require('cli');
const cliArgs = {
email: [ 'u', 'For user create and set password commands, supplies the email.', 'email' ]
};
const cliCommands = [ 'user-create', 'user-promote', 'user-set-password' ];
cli.parse(cliArgs, cliCommands);

// map commands to tasks.
cli.main((args, options) => {
if (cli.command === 'user-create')
withPassword((password) => run(createUser(options.email, password)));
else if (cli.command === 'user-promote')
run(promoteUser(options.email));
else if (cli.command === 'user-set-password')
withPassword((password) => run(setUserPassword(options.email, password)));
});
const { Command } = require('commander');
const program = new Command('node lib/bin/cli.js');

const email = () => program.opts().email;

program.requiredOption('-u, --email <email-address>');

program.command('user-create')
.action(() => withPassword((password) => run(createUser(email(), password))));

program.command('user-promote')
.action(() => run(promoteUser(email())));

program.command('user-set-password')
.action(() => withPassword((password) => run(setUserPassword(email(), password))));

program.parse();
13 changes: 7 additions & 6 deletions lib/bin/create-docker-databases.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@
// be run in a Docker environment in which the POSTGRES_PASSWORD environment
// variable is set to 'odktest'.

const cli = require('cli');
const knex = require('knex');
const { program } = require('commander');

const connect = (database) => knex({
client: 'pg',
connection: { host: 'localhost', user: 'postgres', password: 'odktest', database }
});

cli.parse({
log: ['l', 'Print all db statements to log.', 'bool']
});
cli.main(async (_, { log }) => {
program.option('-l', 'Print all db statements to log.');
program.parse();
const { log } = program.opts();

(async () => {
const dbmain = connect('postgres');
await dbmain.raw("create user jubilant with password 'jubilant';");
await Promise.all(['jubilant', 'jubilant_test'].map(async (database) => {
Expand All @@ -41,4 +42,4 @@ cli.main(async (_, { log }) => {
}

dbmain.destroy();
});
})();
19 changes: 9 additions & 10 deletions lib/bin/purge-forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
const { run } = require('../task/task');
const { purgeForms } = require('../task/purge');

const cli = require('cli');
const cliArgs = {
force: [ 'f', 'Force any soft-deleted form to be purged right away.', 'bool', false ],
formId: [ 'i', 'Purge a specific form based on its id.', 'int' ],
projectId: [ 'p', 'Restrict purging to a specific project.', 'int' ],
};
cli.parse(cliArgs);
const { program } = require('commander');
program.option('-f', 'Force any soft-deleted form to be purged right away.');
program.option('-i <integer>', 'Purge a specific form based on its id.', parseInt);
program.option('-p <integer>', 'Restrict purging to a specific project.', parseInt);
program.parse();

cli.main((args, options) =>
run(purgeForms(options.force, options.formId, options.projectId)
.then((count) => `Forms purged: ${count}`)));
const options = program.opts();

run(purgeForms(options.force, options.formId, options.projectId)
.then((count) => `Forms purged: ${count}`));
14 changes: 6 additions & 8 deletions lib/bin/run-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
const { run } = require('../task/task');
const { runAnalytics } = require('../task/analytics');

const cli = require('cli');
const cliArgs = {
force: [ 'f', 'Force analytics to be sent (if configured) even if not scheduled yet.', 'bool' ]
};
cli.parse(cliArgs);
const { program } = require('commander');
program.option('-f', 'Force analytics to be sent (if configured) even if not scheduled yet.');
program.parse();

cli.main((args, options) => {
run(runAnalytics(options.force));
});
const options = program.opts();

run(runAnalytics(options.force));
63 changes: 21 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"archiver": "~5",
"bcrypt": "~5",
"body-parser": "~1.20",
"cli": "~1",
"cloneable-readable": "~2",
"commander": "^10.0.1",
"config": "~1.31",
"csv-parse": "~4",
"csv-stringify": "~5",
Expand Down