Skip to content

Commit

Permalink
Merge pull request #26 from czprz/21_align_fix_command_with_env_and_i…
Browse files Browse the repository at this point in the history
…nstall

#21 Align fix command with env and install
  • Loading branch information
czprz authored Jan 5, 2022
2 parents 3dc4e70 + 5a51aa3 commit 6531a4a
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 165 deletions.
55 changes: 15 additions & 40 deletions bin/configuration/handleFixConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,30 @@ const config_handler = require('./handleConfigFile');

module.exports = new class {
/**
* Get all fix commands
* @returns {null|Fix[]}
* Find specific fix
* @param keyword {string}
* @param fixKey {string}
* @returns {Fix}
*/
getAll() {
const components = config_handler.getAllComponentsConfig();
if (components == null) {
return null;
}

const listOfFix = [];
for (const fixes of components.map(x => this.#addComponentToFix(x.fix, x.component))) {
if (fixes == null) {
continue;
}

for (const fix in fixes) {
listOfFix.push({...fixes[fix], key: fix});
}
}

return listOfFix.length === 0 ? null : listOfFix;
getFix(keyword, fixKey) {
return config_handler.getAllComponentsConfig()?.find(x => x.fix != null && x.keywords.includes(keyword))?.fix.find(x => x.key === fixKey);
}

/**
* Get specific fix command
* @param problem {string}
* @returns {null|Fix[]}
* Get all projects which does not have an empty fix section
* @returns {null|Config[]}
*/
get(problem) {
const fix = this.getAll();
if (fix == null) {
return null;
}

return fix.filter(x => x.key === problem);
getAllProjectsWithFix() {
return config_handler.getAllComponentsConfig()?.filter(x => x.fix != null);
}

/**
* Add component to each fix
* @param fixes {Fix[]}
* @param component {string}
* @returns {{}}
* Get all fixes for specific project
* @param keyword {string}
* @returns {null|Config}
*/
#addComponentToFix(fixes, component) {
let objMerge = {};
for (const key of Object.keys(fixes)) {
objMerge[key] = {...fixes[key], component};
}
getProjectWithFixes(keyword) {
return config_handler.getAllComponentsConfig()?.find(x => x.fix != null && x.keywords.includes(keyword));

return objMerge;
}
}
8 changes: 1 addition & 7 deletions bin/configuration/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Config {
/**
* @return {string}
*/
component;
name;

/**
* @return {string[]}
Expand Down Expand Up @@ -203,12 +203,6 @@ class CustomOptionRule {
}

class Fix {
/**
* Name of the component which the fix is coming from
* @return {string}
*/
component;

/**
* Keyword for fix command
* @return {string}
Expand Down
2 changes: 1 addition & 1 deletion bin/environments/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ module.exports = new class {
console.log(`List of all components found after last ${chalk.green('dever init')} scan`);

for (const component of components) {
console.log(`${chalk.blue(component.component)} - ${chalk.green(component.keywords)}`);
console.log(`${chalk.blue(component.name)} - ${chalk.green(component.keywords)}`);
}
}

Expand Down
189 changes: 83 additions & 106 deletions bin/fix/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const readline = require("readline");
const chalk = require('chalk');

const fix_config = require('../configuration/handleFixConfig');
const powershell = require('../common/helper/powershell');
const delayer = require("../common/helper/delayer");

module.exports = new class {
/**
Expand All @@ -15,13 +13,16 @@ module.exports = new class {
async handler(yargs, args) {
switch (true) {
case args.list:
this.#showListOfProblems();
this.#showList(args);
break;
case args.show:
case args.show != null:
this.#showFix(args);
break;
case args.fix != null:
this.#fix(args);
break;
default:
this.#showHelpOrFix(yargs, args);
yargs.showHelp();
}
}

Expand All @@ -34,33 +35,24 @@ module.exports = new class {
const keyword = this.#getKeywordFromArgv(yargs.argv);
// Todo: How to handle .argv causing javascript execution not being able to continue when using --help
return keyword == null ?
this.#optionsWithoutComponent(yargs) :
this.#optionsWithComponent(yargs, keyword);
}

/**
* Show help context menu when called
* @param yargs
* @return void
*/
#showHelp(yargs) {
yargs.showHelp();
this.#optionsWithoutKeyword(yargs) :
this.#optionsWithKeyword(yargs, keyword);
}

/**
* Get all options without component
* @param yargs {object}
* @returns {object}
*/
#optionsWithoutComponent(yargs) {
#optionsWithoutKeyword(yargs) {
return yargs
.positional('problem', {
describe: 'Name of problem that you would like to fix that is listed in dever.json',
.positional('keyword', {
describe: 'One of the defined project keywords',
type: 'string'
})
.option('list', {
alias: 'l',
describe: 'List of all problems which is supported',
describe: 'List all projects which has an available fixes',
});
}

Expand All @@ -70,11 +62,19 @@ module.exports = new class {
* @param keyword {string}
* @returns {object}
*/
#optionsWithComponent(yargs, keyword) {
#optionsWithKeyword(yargs, keyword) {
return yargs
.option('fix', {
alias: 'f',
describe: 'Name of fix you want to execute'
})
.option('show', {
alias: 's',
describe: `Shows what 'fix [problem]' will execute`,
describe: `Instead of executing fix it'll show what it will do`,
})
.option('list', {
alias: 'l',
describe: 'List all available fixes for project'
});
}

Expand All @@ -96,67 +96,43 @@ module.exports = new class {
* @param args {FixArgs}
*/
#showFix(args) {
const fixes = fix_config.get(args.problem);
if (fixes == null) {
console.log(`fix could not be found`);
const fix = fix_config.getFix(args.keyword, args.show);
if (fix == null) {
console.log(`Could not find project or fix.`);
return;
}

for (const fix of fixes) {
switch (fix.type) {
case "powershell-command":
case "powershell-script":
console.log(`${fix.type}: ${fix.command}`);
break;
default:
console.error('fix type not supported');
}
switch (fix.type) {
case "powershell-command":
case "powershell-script":
console.log(chalk.blue(fix.type + ':') + ' ' + chalk.green(fix.command));
break;
default:
console.error('fix type not supported');
}
}

/**
* Show a list of problems which can be solved using 'fix [problem]'
*/
#showListOfProblems() {
const fixes = fix_config.getAll();
if (fixes == null) {
console.log(`no 'fix' commands available in any dever.json`);
return;
}

console.log();

for (const fix of fixes) {
console.log(chalk.blue(`'${fix.key}' from ${fix.component}`));
console.log(chalk.green(`${fix.type}: ${fix.command}`));
console.log();
}
}

/**
* Fix or show help depending on whether 'problem' is defined or not
* @param yargs {object}
* @param args {FixArgs}
*/
#showHelpOrFix(yargs, args) {
if (args.problem != null) {
this.#fix(args.problem).catch(console.error);
#showList(args) {
if (args.keyword == null) {
this.#listProjectsWithFixes();
return;
}

this.#showHelp(yargs);
this.#listAllProjectFixes(args.keyword);
}

/**
* Fix problem
* @param problem {string}
* @param args {FixArgs}
*/
async #fix(problem) {
const fixes = fix_config.get(problem);

const fix = await this.#getFix(fixes);
#fix(args) {
const fix = fix_config.getFix(args.keyword, args.fix);
if (fix == null) {
console.error(`Fix not found!`);
console.error('Could not find project or fix');
return;
}

Expand All @@ -174,71 +150,72 @@ module.exports = new class {
}
}

/**
* Get fix that user chooses to run
* @param fixes {Fix[]}
* @returns {Promise<Fix|null>}
*/
#getFix(fixes) {
if (fixes == null || fixes.length < 1) {
return null;
#listProjectsWithFixes() {
const projects = fix_config.getAllProjectsWithFix();
if (projects == null) {
console.log(`no projects available with fix section`);
return;
}

if (fixes.length === 1) {
// Todo: Test if working
return new Promise((resolve) => resolve(fixes[0]));
}
console.log('Projects which has fixes available.');

console.log(`Found multiple fixes with same keyword.`);
console.log('Please select one using the indicated number:');
console.log();

let n = 0;
for (const fix of fixes) {
n++;

console.log(chalk.blue(`${n}. '${fix.key}' from ${fix.component}`));
console.log(chalk.green(`${fix.type}: ${fix.command}`));
for (const project of projects) {
console.log();
console.log(chalk.blue('Project: ') + chalk.green(project.name));
console.log(chalk.blue('Keywords: ') + chalk.green(project.keywords.join(', ')));
}
}

const timer = delayer.create();

const rl = readline.createInterface(process.stdin, process.stdout);
rl.question('Choose which fix you would like to run [number]:', (answer) => {
let result = null;

const option = +answer;

if (typeof option === 'number' && option > 0 && option <= fixes.length) {
result = fixes[option - 1];
}

timer.done(result);
/**
* List all project fixes
* @param keyword {string}
*/
#listAllProjectFixes(keyword) {
const project = fix_config.getProjectWithFixes(keyword);
if (project == null) {
console.log(chalk.redBright('Could not find any project with given keyword'));
return;
}

rl.close();
});
for (const fix of project.fix) {
this.#showFixInConsole(fix, project.name);
}
}

return timer.delay(36000000, null);
/**
* Show fix in the console
* @param fix {Fix}
* @param projectName {string}
*/
#showFixInConsole(fix, projectName) {
console.log();
console.log(chalk.blue(`'${fix.key}' from ${projectName}`));
console.log(chalk.green(`${fix.type}: ${fix.command}`));
}
}

class FixArgs {
/**
* Defined which 'problem' fix should solve
* Unique project keyword
* @return {string}
*/
problem;
keyword;

/**
* Show command/file that will be executed when running 'fix [problem]' command
* @return {boolean}
* @return {string}
*/
show;

/**
* Show list of possible fixes
* @var {bool}
* @return {boolean}
*/
list;

/**
* Name of fix to be executed
* @return {string}
*/
fix;
}
Loading

0 comments on commit 6531a4a

Please sign in to comment.