Skip to content

Commit

Permalink
feat(utils): export style util
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 7, 2021
1 parent a3cb829 commit cc6bb50
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 47 deletions.
8 changes: 4 additions & 4 deletions src/cli/bin/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import table from 'as-table';
import { into } from 'pipettes';
import { flags, safePairs } from 'cli-belt';
import { stripIndent as indent } from 'common-tags';
import { styleString } from '../../helpers/style-string';
import { resolveProject } from '../../helpers/resolve-project';
import { stringifyArgvCommands } from '../../helpers/stringify';
import { print, raises, series, create, context, log } from '../../tasks';
import { Task, LogLevel, CLI, Context } from '../../definitions';
import { constants } from '../../constants';
import { style } from '../../utils';

export function main(argv: string[], options: Required<CLI.Options>): Task {
const extensions = into(
Expand All @@ -22,7 +22,7 @@ export function main(argv: string[], options: Required<CLI.Options>): Task {
);
const help = into(
indent`
${styleString(options.description, { bold: true })}
${style(options.description, { bold: true })}
Usage:
$ ${options.bin} [options] [command]
Expand Down Expand Up @@ -156,8 +156,8 @@ export function main(argv: string[], options: Required<CLI.Options>): Task {
log('debug', 'Arguments:', [':' + extension.name, ...cmd._]),
log(
'info',
styleString(options.bin, { bold: true }),
styleString(':' + extension.name, { bold: true, color: 'blue' }),
style(options.bin, { bold: true }),
style(':' + extension.name, { bold: true, color: 'blue' }),
stringifyArgvCommands(cmd._)
),
print(),
Expand Down
5 changes: 2 additions & 3 deletions src/cli/commands/lift.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import arg from 'arg';
import { flags, safePairs } from 'cli-belt';
import { stripIndent as indent } from 'common-tags';
import { fetch } from '../../utils';
import { Task, CLI } from '../../definitions';
import { styleString } from '../../helpers/style-string';
import { style, fetch } from '../../utils';
import { lift as _lift, series, raises, print } from '../../tasks';

export async function lift(params: CLI.Extension.Params): Promise<Task> {
const help = indent`
${styleString(`Lift tasks to a package.json`, { bold: true })}
${style(`Lift tasks to a package.json`, { bold: true })}
Usage:
$ ${params.options.bin} :lift [options]
Expand Down
5 changes: 2 additions & 3 deletions src/cli/commands/list.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import arg from 'arg';
import { flags, safePairs } from 'cli-belt';
import { stripIndent as indent } from 'common-tags';
import { fetch } from '../../utils';
import { Task, CLI } from '../../definitions';
import { styleString } from '../../helpers/style-string';
import { style, fetch } from '../../utils';
import { list as _list, series, raises, print } from '../../tasks';

export async function list(params: CLI.Extension.Params): Promise<Task> {
const help = indent`
${styleString(`List available tasks`, { bold: true })}
${style(`List available tasks`, { bold: true })}
Usage:
$ ${params.options.bin} :list [options]
Expand Down
9 changes: 4 additions & 5 deletions src/cli/commands/watch.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import arg from 'arg';
import { flags, safePairs, splitBy } from 'cli-belt';
import { stripIndent as indent } from 'common-tags';
import { fetch } from '../../utils';
import { Task, CLI } from '../../definitions';
import { styleString } from '../../helpers/style-string';
import { style, fetch } from '../../utils';
import { stringifyArgvCommands } from '../../helpers/stringify';
import {
watch as _watch,
Expand All @@ -19,7 +18,7 @@ import {
export async function watch(params: CLI.Extension.Params): Promise<Task> {
const { bin, multitask } = params.options;
const help = indent`
${styleString(`Watch a path and run tasks on change events`, {
${style(`Watch a path and run tasks on change events`, {
bold: true
})}
Expand Down Expand Up @@ -106,8 +105,8 @@ export async function watch(params: CLI.Extension.Params): Promise<Task> {
return series(
log(
'info',
styleString(params.options.bin, { bold: true }),
styleString(':watch', { bold: true, color: 'blue' }),
style(params.options.bin, { bold: true }),
style(':watch', { bold: true, color: 'blue' }),
stringifyArgvCommands(params.argv)
),
print()
Expand Down
6 changes: 2 additions & 4 deletions src/helpers/prefix.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context } from '../definitions';
import { styleString } from './style-string';
import { style } from '../utils/style';
import { stringifyPrintRoute } from './stringify';
import { into } from 'pipettes';
import { TypeGuard } from 'type-core';
Expand All @@ -22,9 +22,7 @@ export function getPrefix(
if (!prefix || (policy !== target && policy !== 'all')) {
return extra ? extra + ' ' : null;
}
return (
styleString(`${prefix} | `, { bold: true }) + (extra ? extra + ' ' : '')
);
return style(`${prefix} | `, { bold: true }) + (extra ? extra + ' ' : '');
},
(prefix) => prefix || ''
);
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/stringify.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { splitBy } from 'cli-belt';
import { ensure } from 'errorish';
import { into } from 'pipettes';
import { styleString } from './style-string';
import { style } from '../utils/style';
import { constants } from '../constants';

export function stringifyKeyRoute(route: string[]): string {
Expand All @@ -21,6 +21,6 @@ export function stringifyError(error: Error): string {
return into(
ensure(error, null, { normalize: true }).message,
(msg) => msg[0].toUpperCase() + msg.slice(1).toString(),
(msg) => styleString(msg, { bold: true })
(msg) => style(msg, { bold: true })
);
}
10 changes: 5 additions & 5 deletions src/tasks/reflection/lift.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Task, Context } from '../../definitions';
import { parseToRecord } from '../../helpers/parse';
import { getAbsolutePath } from '../../helpers/paths';
import { styleString } from '../../helpers/style-string';
import { isCancelled } from '../../utils/is-cancelled';
import { style } from '../../utils/style';
import { run } from '../../utils/run';
import { constants } from '../../constants';
import { write } from '../filesystem/write';
Expand Down Expand Up @@ -162,24 +162,24 @@ function evaluateChanges(
if (areChangesPending) {
if (addScriptNames.length) {
strArr.push(
styleString('Scripts to add', { bold: true, color: 'green' }),
style('Scripts to add', { bold: true, color: 'green' }),
addScriptNames.join(', ') + '\n'
);
}
if (replaceScriptNames.length) {
strArr.push(
styleString('Scripts to replace', { bold: true, color: 'yellow' }),
style('Scripts to replace', { bold: true, color: 'yellow' }),
replaceScriptNames.join(', ') + '\n'
);
}
if (removeScriptNames.length) {
strArr.push(
styleString('Scripts to remove', { bold: true, color: 'red' }),
style('Scripts to remove', { bold: true, color: 'red' }),
removeScriptNames.join(', ') + '\n'
);
}
} else {
strArr.push(styleString('No pending scripts changes', { bold: true }));
strArr.push(style('No pending scripts changes', { bold: true }));
}

into(context, print(strArr.join('\n')));
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/reflection/list.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Task, Context } from '../../definitions';
import { parseToArray } from '../../helpers/parse';
import { styleString } from '../../helpers/style-string';
import { constants } from '../../constants';
import { style } from '../../utils/style';
import { run } from '../../utils/run';
import { print } from '../stdio/print';
import { NullaryFn, TypeGuard } from 'type-core';
Expand Down Expand Up @@ -46,7 +46,7 @@ export function list(

const rows = items.map((item) => {
return [
opts.bin + ' ' + styleString(item.name, { bold: true }) + ' '.repeat(4),
opts.bin + ' ' + style(item.name, { bold: true }) + ' '.repeat(4),
...Array(item.route.length).fill(''),
item.route[item.route.length - 1],
...Array(maxRouteLength - item.route.length).fill('')
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/stdio/announce.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context, LogLevel, Task } from '../../definitions';
import { stringifyPrintRoute } from '../../helpers/stringify';
import { styleString } from '../../helpers/style-string';
import { style } from '../../utils/style';
import { run } from '../../utils/run';
import { series } from '../aggregate/series';
import { log } from '../stdio/log';
Expand All @@ -23,7 +23,7 @@ export function announce(task: Task, options?: AnnounceOptions): Task.Async {
ctx.route.length
? log(
opts.level,
styleString('task |', { bold: true }),
style('task |', { bold: true }),
stringifyPrintRoute(ctx.route)
)
: null,
Expand Down
15 changes: 7 additions & 8 deletions src/tasks/stdio/log.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Task, LogLevel, Context } from '../../definitions';
import { styleString } from '../../helpers/style-string';
import { addPrefix } from '../../helpers/prefix';
import { style } from '../../utils/style';
import { Members } from 'type-core';
import util from 'util';

Expand All @@ -15,15 +15,14 @@ const rank: Members<number> = {

const color = {
trace: (str: string) =>
styleString(str, { bold: true, bg: 'magenta', color: 'white' }),
style(str, { bold: true, bg: 'magenta', color: 'white' }),
debug: (str: string) =>
styleString(str, { bold: true, bg: 'cyan', color: 'white' }),
style(str, { bold: true, bg: 'cyan', color: 'white' }),
info: (str: string) =>
styleString(str, { bold: true, bg: 'green', color: 'white' }),
style(str, { bold: true, bg: 'green', color: 'white' }),
warn: (str: string) =>
styleString(str, { bold: true, bg: 'yellow', color: 'white' }),
error: (str: string) =>
styleString(str, { bold: true, bg: 'red', color: 'white' })
style(str, { bold: true, bg: 'yellow', color: 'white' }),
error: (str: string) => style(str, { bold: true, bg: 'red', color: 'white' })
};

/**
Expand Down Expand Up @@ -56,5 +55,5 @@ export function log(level: LogLevel, item: any, ...data: any[]): Task.Sync {
function getLoggerMessagePrefix(level: Exclude<LogLevel, 'silent'>): string {
const fn = color[level];
const name = level.toUpperCase();
return fn ? fn(` ${name} `) : styleString(name, { bold: true });
return fn ? fn(` ${name} `) : style(name, { bold: true });
}
4 changes: 2 additions & 2 deletions src/tasks/stdio/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import cliSelect from 'cli-select';
import { into } from 'pipettes';
import figures from 'figures';
import { Task } from '../../definitions';
import { styleString } from '../../helpers/style-string';
import { isInteractive } from '../../utils/is-interactive';
import { isCancelled } from '../../utils/is-cancelled';
import { style } from '../../utils/style';
import { series } from '../aggregate/series';
import { raises } from '../exception/raises';
import { create } from '../creation/create';
Expand Down Expand Up @@ -63,7 +63,7 @@ export function select(

into(
ctx,
print(styleString(figures(figures.pointer)) + ' ' + opts.message, {
print(style(figures(figures.pointer)) + ' ' + opts.message, {
bold: true,
color: 'yellow'
})
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './is-ci';
export * from './is-interactive';
export * from './recreate';
export * from './run';
export * from './style';
14 changes: 7 additions & 7 deletions src/helpers/style-string.ts → src/utils/style.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import chalk, { ForegroundColor, Chalk } from 'chalk';
import { into } from 'pipettes';

export interface StyleStringOptions {
color?: typeof ForegroundColor;
bg?: typeof ForegroundColor;
export interface StyleOptions {
color?: StyleColor;
bg?: StyleColor;
dim?: boolean;
bold?: boolean;
italic?: boolean;
underline?: boolean;
strikethrough?: boolean;
}

export function styleString(
str: string,
options: StyleStringOptions = {}
): string {
export type StyleColor = typeof ForegroundColor;

/** Styles a string */
export function style(str: string, options: StyleOptions = {}): string {
return into(
chalk,
(chalk) => (options.color ? chalk[options.color] : chalk),
Expand Down

0 comments on commit cc6bb50

Please sign in to comment.