Skip to content

Commit

Permalink
feat(messages): support custom messages
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai committed Jan 18, 2020
1 parent 816cd76 commit 62eb831
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as yargs from 'yargs';
import * as path from 'path';
import * as chalk from 'chalk';
import { checkSoftware } from './requirements';
import { renderTable } from './reporter';
import { renderTable, renderMessages } from './reporter';
import { Configuration } from './types';
import { scaffold } from './scaffold';

Expand Down Expand Up @@ -32,6 +32,7 @@ export async function exec(_debug_argv_?) {

if (!ALL_OK && !argv.force) {
console.error(renderTable(rawResults));
console.log(renderMessages(rawResults));
throw new Error(`❌ Not all requirements are satisfied`);
}

Expand Down
14 changes: 14 additions & 0 deletions src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ import * as logSymbols from 'log-symbols';
import * as chalk from 'chalk';
import { RawResult } from './types';

export function renderMessages(rawResults: RawResult[] = []) {
const notInstalledItems = rawResults.filter(item => !item.installed && item.installMessage);
const unsatisfiedInstalledItems = rawResults.filter(
item => !item.satisfies && item.updateMessage
);

const messages = [...notInstalledItems, ...unsatisfiedInstalledItems].map(item => {
const message = chalk.red('⚠️ ') + (item.installMessage ?? item.updateMessage);
return message;
});

return messages.join('\n') + '\n';
}

export function renderTable(rawResults: RawResult[] = []) {
let results = rawResults.map(item => {
const { bin, semver, installed, version, satisfies, optional } = item;
Expand Down
11 changes: 5 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@ export type ConfigurationValue = ConfigurationStringValue | ConfigurationObjectV

export type ConfigurationStringValue = string;

export type ConfigurationObjectValue = {
export interface ConfigurationObjectValue {
semver: string;
flag: string;
optional?: boolean;
};
installMessage?: string;
updateMessage?: string;
}

export interface RawResult {
export interface RawResult extends ConfigurationObjectValue {
bin: string;
semver: string;
flag: string;
installed: boolean;
version?: string;
satisfies?: boolean;
optional?: boolean;
}
8 changes: 6 additions & 2 deletions tests/requirements.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ module.exports = {
git: '~1.9.4 || 2.0.0 - 2.10.0',
node: '8 || 10 || 12',
npm: '>= 6.x',
yarn: '>= 1.19.x',
yarn: {
semver: '1.16.x',
updateMessage: `Outdated 'yarn' found. Run 'brew upgrade yarn' to update.`
},
mvn: '^3.x',
nginx: {
semver: '^6.x',
optional: true
optional: true,
installMessage: `This project is configured for NGINX but 'nginx' was not found on your path. Run 'brew install nginx' to install.`
},
httpd: {
semver: '^2.x',
Expand Down

0 comments on commit 62eb831

Please sign in to comment.