Skip to content

Commit

Permalink
feat: 🔥 added init wizard in configuration
Browse files Browse the repository at this point in the history
added init wizard in configuration
  • Loading branch information
Tal Rofe committed Apr 16, 2022
1 parent 75a35ee commit f393958
Show file tree
Hide file tree
Showing 19 changed files with 309 additions and 128 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
🚧 This package is in progres.. :) 🚧
🚧 This package is in progres... Use it once released with version 1.x.x :) 🚧
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
"setdev": "run-p husky:install git:config",
"onboarding": "node ./scripts/on-boarding.js",
"prepare": "is-ci || run-s setdev onboarding",
"build": "tsc --project ./tsconfig.build.json && tsc-alias --project ./tsconfig.build.json",
"build:remove": "rimraf dist",
"build:create": "tsc --project ./tsconfig.build.json && tsc-alias --project ./tsconfig.build.json",
"build": "run-s build:remove build:create",
"lint": "eslint {src,tests}/**/*",
"lint:fix": "eslint {src,tests}/**/* --fix",
"format": "prettier --write \"**/*.{ts,js,json}\"",
Expand All @@ -47,10 +49,13 @@
"depcheck": "depcheck"
},
"dependencies": {
"minimist": "1.2.6"
"inquirer": "8.2.2",
"minimist": "1.2.6",
"yaml": "2.0.1"
},
"devDependencies": {
"@commitlint/cli": "16.2.3",
"@types/inquirer": "8.2.1",
"@types/minimist": "1.2.2",
"@typescript-eslint/eslint-plugin": "5.19.0",
"@typescript-eslint/parser": "5.19.0",
Expand All @@ -70,6 +75,7 @@
"lint-staged": "12.3.7",
"npm-run-all": "4.1.5",
"prettier": "2.6.2",
"rimraf": "3.0.2",
"tsc-alias": "1.6.6",
"typescript": "4.6.3"
}
Expand Down
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { getCLIArgv } from '@/utils/cli-argv';
import { startCLIPhase } from '@/utils/cli-phase';
import { getCLIArgv } from '@/utils/argv';
import StartCLI from '@/modules/cli';
import StartConfiguration from '@/modules/configuration';

(() => {
const argv = getCLIArgv();

startCLIPhase(argv);
const configFromCLI = StartCLI(argv);

if (configFromCLI) {
StartConfiguration();
}
})();
38 changes: 0 additions & 38 deletions src/models/configuration.ts

This file was deleted.

64 changes: 10 additions & 54 deletions src/utils/cli-help.ts → src/modules/cli/functions/help.ts
Original file line number Diff line number Diff line change
@@ -1,88 +1,44 @@
import CLILoggerService from '@/services/cli-logger';
import { calcSpaceBias } from '@/utils/cli-logger';

type IOptionsKeys =
| 'noInflintrc'
| 'config'
| 'rule'
| 'ignorePath'
| 'noIgnore'
| 'ignorePattern'
| 'quiet'
| 'maxWarnings'
| 'outputFile'
| 'format'
| 'color'
| 'init'
| 'debug'
| 'help'
| 'version'
| 'printRule';
import { options } from '../models/help';

/**
* The function logs the help document to the console
* @returns void
*/
export const printHelp = () => {
const options: Record<IOptionsKeys, [string, string]> = {
noInflintrc: [
'--no-inflintrc',
'Disable use of configuration from configuration file or package.json. Default: false',
],
config: ['-c, --config (path::String)', 'Inflint will use the provided configuration file'],
rule: ['--rule (String)', 'Specify rules'],
ignorePath: ['--ignore-path (path::String)', 'Specify path of ignore file'],
noIgnore: ['-"-no-ignore', 'Disable use of ignore files and patterns. Default: false'],
ignorePattern: [
'--ignore-pattern (String)',
'Pattern of files to ignore (in addition to those in .inflintignore)',
],
quiet: ['--quiet', 'Report errors only. Default: false'],
maxWarnings: [
'--max-warnings (Int)',
'Number of warnings to trigger non-zero exit code. Default: -1',
],
outputFile: ['-o, --output-file (path::String)', 'Specify file to write report to'],
format: ['-f, --format (String)', 'Use a specific output format. Default: "stylish"'],
color: ['--color, --no-color', 'Force enabling/disabling of color'],
init: ['--init', 'Run configuration initialization wizard. Default: false'],
debug: ['--debug', 'Output debugging information'],
help: ['-h, --help', 'Show help'],
version: ['-v, --version', 'Output the version number'],
printRule: ['--print-rule (path::String)', 'Print the expected rule to be enforced on a given file'],
};

const spaceBias =
Object.values(options).reduce<number>((final, value) => {
return Math.max(final, value[0]!.length);
}, 0) + 3;
const spaceBias = calcSpaceBias(options);

CLILoggerService.logDefault('inflint [options] [files]');
CLILoggerService.logEmptyBlock();

CLILoggerService.logDefault('Basic configuration:');
CLILoggerService.logSpaced(options.noInflintrc, 2, spaceBias - options.noInflintrc[0].length);
CLILoggerService.logSpaced(options.config, 2, spaceBias - options.config[0].length);
CLILoggerService.logEmptyLine();
CLILoggerService.logEmptyBlock();

CLILoggerService.logDefault('Specifying rules:');
CLILoggerService.logSpaced(options.rule, 2, spaceBias - options.rule[0].length);
CLILoggerService.logEmptyLine();
CLILoggerService.logSpaced(options.alias, 2, spaceBias - options.alias[0].length);
CLILoggerService.logEmptyBlock();

CLILoggerService.logDefault('Ignoring files:');
CLILoggerService.logSpaced(options.ignorePath, 2, spaceBias - options.ignorePath[0].length);
CLILoggerService.logSpaced(options.noIgnore, 2, spaceBias - options.noIgnore[0].length);
CLILoggerService.logSpaced(options.ignorePattern, 2, spaceBias - options.ignorePattern[0].length);
CLILoggerService.logEmptyLine();
CLILoggerService.logEmptyBlock();

CLILoggerService.logDefault('Handling warnings:');
CLILoggerService.logSpaced(options.quiet, 2, spaceBias - options.quiet[0].length);
CLILoggerService.logSpaced(options.maxWarnings, 2, spaceBias - options.maxWarnings[0].length);
CLILoggerService.logEmptyLine();
CLILoggerService.logEmptyBlock();

CLILoggerService.logDefault('Output:');
CLILoggerService.logSpaced(options.outputFile, 2, spaceBias - options.outputFile[0].length);
CLILoggerService.logSpaced(options.format, 2, spaceBias - options.format[0].length);
CLILoggerService.logSpaced(options.color, 2, spaceBias - options.color[0].length);
CLILoggerService.logEmptyLine();
CLILoggerService.logEmptyBlock();

CLILoggerService.logDefault('Miscellaneous:');
CLILoggerService.logSpaced(options.init, 2, spaceBias - options.init[0].length);
Expand Down
80 changes: 80 additions & 0 deletions src/modules/cli/functions/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import inquirer from 'inquirer';

import { convertAliasesAnswers, exportFilesFromInit } from '../utils/init';
import type { IAliasAnswer, IInitialAnswers, IAliasAnswers } from '../models/init';

/**
* The function initialize the project with basic Inflint configuration
* @returns void
*/
export const initConfiguration = async () => {
const initialQuestions: inquirer.QuestionCollection = [
{
type: 'list',
name: 'file_format',
message: 'Please choose the configuration file format:',
default: 'Javascript',
choices: ['Javascript', 'JSON', 'YAML'],
},
{
type: 'list',
name: 'with_ignore_file',
message: 'Do you want to create an ignore file?',
default: 'Yes',
choices: ['Yes', 'No'],
},
{
type: 'list',
name: 'with_aliases',
message: 'Do you want to set some aliases to your configuration?',
default: 'No',
choices: ['Yes', 'No'],
},
];

const initialAnswers = await inquirer.prompt<IInitialAnswers>(initialQuestions);
const aliasesAnswers: IAliasAnswers = [];

if (initialAnswers.with_aliases === 'Yes') {
let stopAliases = false;

do {
const aliasQuestions: inquirer.QuestionCollection = [
{
type: 'input',
name: 'name',
message: 'Enter name of the alias:',
validate: (input: string) => (input ? true : 'Please enter an alias name'),
},
{
type: 'input',
name: 'regex',
message: 'Enter regex the alias will match:',
validate: (input: string) => (input ? true : 'Please enter a regex for the alias'),
},
{
type: 'list',
name: 'with_resume',
message: 'Do you want to set an another alias?',
default: 'Yes',
choices: ['Yes', 'No'],
},
];

const aliasAnswers = await inquirer.prompt<IAliasAnswer>(aliasQuestions);

aliasesAnswers.push({
name: aliasAnswers.name,
regex: aliasAnswers.regex,
});

if (aliasAnswers.with_resume === 'No') {
stopAliases = true;
}
} while (!stopAliases);
}

const convertedAliases = convertAliasesAnswers(aliasesAnswers);

await exportFilesFromInit(initialAnswers.file_format, initialAnswers.with_ignore_file, convertedAliases);
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import CLILoggerService from '@/services/cli-logger';
import packageJson from '../../package.json';
import packageJson from '../../../../package.json';

/**
* The function logs the version to the console
Expand Down
29 changes: 29 additions & 0 deletions src/modules/cli/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ParsedArgs } from 'minimist';

import { printHelp } from './functions/help';
import { printVersion } from './functions/version';
import { initConfiguration } from './functions/init';

const StartCLI = (argv: ParsedArgs) => {
if (argv['h'] === true || argv['help'] === true) {
printHelp();

return null;
}

if (argv['v'] === true || argv['version'] === true) {
printVersion();

return null;
}

if (argv['init'] === true) {
initConfiguration();

return null;
}

return null;
};

export default StartCLI;
44 changes: 44 additions & 0 deletions src/modules/cli/models/help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
type IOptionsKeys =
| 'noInflintrc'
| 'config'
| 'rule'
| 'alias'
| 'ignorePath'
| 'noIgnore'
| 'ignorePattern'
| 'quiet'
| 'maxWarnings'
| 'outputFile'
| 'format'
| 'color'
| 'init'
| 'debug'
| 'help'
| 'version'
| 'printRule';

export const options: Record<IOptionsKeys, [string, string]> = {
noInflintrc: [
'--no-inflintrc',
'Disable use of configuration from configuration file or package.json. Default: false',
],
config: ['-c, --config (path::String)', 'Inflint will use the provided configuration file'],
rule: ['--rule (String)', 'Specify rules'],
alias: ['--alias (String)', 'Specify custom aliases'],
ignorePath: ['--ignore-path (path::String)', 'Specify path of ignore file'],
noIgnore: ['--no-ignore', 'Disable use of ignore files and patterns. Default: false'],
ignorePattern: [
'--ignore-pattern (String)',
'Pattern of files to ignore (in addition to those in .inflintignore)',
],
quiet: ['--quiet', 'Report errors only. Default: false'],
maxWarnings: ['--max-warnings (Int)', 'Number of warnings to trigger non-zero exit code. Default: -1'],
outputFile: ['-o, --output-file (path::String)', 'Specify file to write report to'],
format: ['-f, --format (String)', 'Use a specific output format. Default: "stylish"'],
color: ['--color, --no-color', 'Force enabling/disabling of color'],
init: ['--init', 'Run configuration initialization wizard. Default: false'],
debug: ['--debug', 'Output debugging information'],
help: ['-h, --help', 'Show help'],
version: ['-v, --version', 'Output the version number'],
printRule: ['--print-rule (path::String)', 'Print the expected rule to be enforced on a given file'],
};
17 changes: 17 additions & 0 deletions src/modules/cli/models/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export type IBooleanQuestion = 'Yes' | 'No';

export type IFileFormat = 'Javascript' | 'JSON' | 'YAML';

export interface IAliasAnswer {
readonly name: string;
readonly regex: string;
readonly with_resume: IBooleanQuestion;
}

export interface IInitialAnswers {
readonly file_format: IFileFormat;
readonly with_ignore_file: IBooleanQuestion;
readonly with_aliases: IBooleanQuestion;
}

export type IAliasAnswers = Omit<IAliasAnswer, 'with_resume'>[];
Loading

0 comments on commit f393958

Please sign in to comment.