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

Add support to save command line options to the dojorc #255

Closed
wants to merge 7 commits into from
Closed
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
8 changes: 8 additions & 0 deletions src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ function formatCommandOptions(commandWrapper: CommandWrapper, isDefaultCommand =

register(formatOption, null as any);
formatOption('dojorc', { default: '.dojorc', type: 'string', description: 'The dojorc config file' });

if (!isDefaultCommand) {
commandOptionHelp = `${commandOptionHelp}\n ${addOptionPrefix(chalk.greenBright('save'))} ${createPadding(
'--save',
20
)}Save any passed arguments that are configurable to your configuration file`;
}

return commandOptionHelp;
}

Expand Down
84 changes: 65 additions & 19 deletions src/registerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,19 @@ function reportError(error: CommandError) {
process.exit(exitCode);
}

function userSetOption(option: string, aliases: Aliases) {
function searchForOption(option: string) {
if (process.argv.indexOf(option) > -1) {
return true;
}
return false;
}
function searchForOption(option: string) {
return process.argv.indexOf(`-${option}`) > -1 || process.argv.indexOf(`--${option}`) > -1;
}

if (searchForOption(`-${option}`) || searchForOption(`--${option}`)) {
function userSetOption(option: string, aliases: Aliases) {
if (searchForOption(option)) {
return true;
}

// Handle aliases for same option
for (let aliasIndex in aliases[option]) {
let alias = aliases[option][aliasIndex];
if (searchForOption(`-${alias}`) || searchForOption(`--${alias}`)) {
if (searchForOption(alias)) {
return true;
}
}
Expand All @@ -68,6 +65,33 @@ function getRcOption(rcConfig: any, option: string, aliases: Aliases) {
return undefined;
}

function saveCommandLineOptions(configuration: any, args: any, aliases: any) {
const configToSave = Object.keys(args).reduce(
(config, key) => {
if (searchForOption(key)) {
// If it is a shorthand alias
// save the full length in the config
const keys = aliases[key] ? [key, ...aliases[key]] : [key];
const sortedKeys = keys.sort((a, b) => b.length - a.length);
const fullKey = sortedKeys.shift();

sortedKeys.forEach((shortKey) => {
config[shortKey] = undefined; // Delete shorthand alias
});

try {
config[fullKey] = JSON.parse(args[key]);
} catch {
config[fullKey] = args[key];
}
}
return config;
},
{} as any
);
configuration.set(configToSave);
}

function getOptions(aliases: Aliases, rcOptions: any, commandLineArgs: any = {}) {
const result = Object.keys(commandLineArgs).reduce(
(config, key) => {
Expand Down Expand Up @@ -134,20 +158,30 @@ function registerGroups(yargs: Argv, helper: HelperFactory, groupName: string, c
},
async (argv: any) => {
if (defaultCommand && argv._.length === 1) {
if (argv.h || argv.help) {
console.log(formatHelp(argv, groupMap));
const { h, help, save, ...args } = argv;

if (h || help) {
console.log(formatHelp(args, groupMap));
return Promise.resolve({});
}
const config = helper.sandbox(groupName, defaultCommand.name).configuration.get();
const args = getOptions(aliases, config, argv);

const configurationHelper = helper.sandbox(groupName, defaultCommand.name).configuration;
const config = configurationHelper.get();
const combinedArgs = getOptions(aliases, config, args);

if (save) {
saveCommandLineOptions(configurationHelper, combinedArgs, aliases);
}

if (typeof defaultCommand.validate === 'function') {
const valid = await defaultCommand.validate(helper.sandbox(groupName, defaultCommand.name));
if (!valid) {
return;
}
}
return defaultCommand.run(helper.sandbox(groupName, defaultCommand.name), args).catch(reportError);
return defaultCommand
.run(helper.sandbox(groupName, defaultCommand.name), combinedArgs)
.catch(reportError);
}
}
);
Expand All @@ -173,21 +207,28 @@ function registerCommands(yargs: Argv, helper: HelperFactory, groupName: string,
.strict();
},
async (argv: any) => {
if (argv.h || argv.help) {
console.log(formatHelp(argv, groupMap));
const { h, help, save, ...args } = argv;

if (h || help) {
console.log(formatHelp(args, groupMap));
return Promise.resolve({});
}

const config = helper.sandbox(groupName, name).configuration.get();
const args = getOptions(aliases, config, argv);
const configurationHelper = helper.sandbox(groupName, name).configuration;
const config = configurationHelper.get();
const combinedArgs = getOptions(aliases, config, args);

if (save) {
saveCommandLineOptions(configurationHelper, combinedArgs, aliases);
}

if (typeof command.validate === 'function') {
const valid = await command.validate(helper.sandbox(groupName, command.name));
if (!valid) {
return;
}
}
return run(helper.sandbox(groupName, name), args).catch(reportError);
return run(helper.sandbox(groupName, name), combinedArgs).catch(reportError);
}
);
});
Expand All @@ -209,6 +250,11 @@ export default function(yargs: Argv, groupMap: GroupMap): void {
registerGroups(yargs, helperFactory, group, commandMap);
});

yargs.option('save', {
describe: 'Save arguments to .dojorc',
type: 'boolean'
});

yargs
.demand(1, '')
.command(
Expand Down
Loading