Skip to content

Commit

Permalink
v3.3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniopresto committed Mar 12, 2024
1 parent ff1465c commit a959b31
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 46 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "powership",
"version": "3.3.10",
"version": "3.3.11",
"private": true,
"scripts": {
"pack": "run-s pack:*",
Expand Down
10 changes: 4 additions & 6 deletions packages/runmate/src/commands/executeInPackages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AnyFunction, filterNull } from '@powership/utils';
import { chalk, CWD, glob, nodePath } from '@powership/utils/out/node';
import { AnyFunction, filterNull, hey } from '@powership/utils';
import { CWD, glob, nodePath } from '@powership/utils/out/node';
import { Command } from 'commander';

import { packageRunner, PackageRunnerExecInput } from '../packageRunner';
Expand Down Expand Up @@ -145,10 +145,8 @@ function _executeInPackages(
})();

if (!functions?.length) {
console.error(
chalk.red(
`‼️ No files found with pattern: ${commands?.join(' ')}`
)
hey.error(
`‼️ No files found with pattern: ${commands?.join(' ')}`
);
return;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/runmate/src/commands/set-json-value.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { jsonParse, setByPath } from '@powership/utils';
import { chalk, nodePath } from '@powership/utils/out/node';
import { hey, jsonParse, setByPath } from '@powership/utils';
import { nodePath } from '@powership/utils/out/node';
import { Command } from 'commander';

import { writePackageJSON } from '../handleJSON';
Expand Down Expand Up @@ -56,7 +56,7 @@ export function setJsonValue(program: Command) {
}
});
} catch (e: any) {
console.error(chalk.red(e));
hey.error(e);
}
});

Expand All @@ -78,7 +78,7 @@ export function setJsonValue(program: Command) {
try {
await packageVersion(releaseTypeOrVersion, pattern);
} catch (e: any) {
console.error(chalk.red(e));
hey.error(e);
}
});
}
6 changes: 5 additions & 1 deletion packages/utils/src/hey.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe('hey', () => {
// afterEach();

test('basic test', async () => {
const sut = await hey`
const sut = hey`
Today is a nice day to
go <strike><bold><blue>outside!</blue></bold></strike>
lets go?
Expand All @@ -17,4 +17,8 @@ describe('hey', () => {
'',
]);
});

test('error', () => {
hey.error(new Error('foo'));
});
});
102 changes: 72 additions & 30 deletions packages/utils/src/hey.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import * as process from 'process';
// @only-server
import fs from 'fs';

export async function hey(
strings: TemplateStringsArray | string,
...values: any[]
) {
import { tryCatch } from './tryCatch';

function _hey(strings: TemplateStringsArray | string, ...values: any[]) {
const formatted = heyFormat(strings, ...values) + '\n';
await safeWrite(formatted);
write(formatted);
return formatted;
}

hey.styles = {
red: '\x1b[31m',
const red = '\x1b[31m';
const yellow = '\x1b[33m';

export const styles = {
red: red,
error: red,
warn: yellow,
green: '\x1b[32m',
yellow: '\x1b[33m',
yellow: yellow,
blue: '\x1b[34m',
bold: '\x1b[1m',
underline: '\x1b[4m',
strike: '\x1b[9m', // ANSI escape code for strikethrough
reset: '\x1b[0m',
};

export function heyFormat(
input: string | TemplateStringsArray,
...values: any[]
) {
_hey.styles = styles;

function heyFormat(input: string | TemplateStringsArray, ...values: any[]) {
let text = (() => {
if (typeof input === 'string') return input;
let result = input[0];
Expand All @@ -35,36 +39,33 @@ export function heyFormat(

const regex = new RegExp(
// /<(red|green|yellow|blue|bold|underline|strike)>(.*?)<\/\1>/g,
`<(${Object.keys(hey.styles).join('|')})>(.*?)<\\/\\1>`,
`<(${Object.keys(_hey.styles).join('|')})>(.*?)<\\/\\1>`,
'g'
);

text = text.replace(regex, (_, style, el) => {
return `${hey.styles[style]}${heyFormat(el)}${hey.styles.reset}`;
return `${_hey.styles[style]}${heyFormat(el)}${_hey.styles.reset}`;
});

return trimTemplateString(text);
return trimTabs(text);
}

async function safeWrite(input: string) {
function write(input: string) {
if (
typeof process === 'undefined' ||
typeof process?.stdout?.write !== 'function'
) {
return console.info(input);
}

if (!process.stdout.writableEnded) {
return new Promise((resolve, reject) => {
process.stdout.write(input, (err) => {
if (err) return reject(err);
resolve(input);
});
});
const [err] = tryCatch(() => fs.writeSync(process.stdout.fd, input));

if (err) {
console.info(input);
}
}

export function trimTemplateString(
export function trimTabs(
input: string | TemplateStringsArray,
...values: any[]
): string {
Expand All @@ -89,9 +90,50 @@ export function templateStringToText<T = any>(
...values: T[]
) {
if (typeof input === 'string') return input;
let result = input[0];
values.forEach((value, i) => {
result += value + input[i + 1];
});
return result;
if (Array.isArray(input)) {
let result = input[0];
values.forEach((value, i) => {
result += value + input[i + 1];
});
return result;
}
return input?.toString?.();
}

export type Styles = typeof styles & {};

export type HeyParams = readonly [
strings: TemplateStringsArray | string | { toString(): string },
...values: any[]
];

const proxy = new Proxy(_hey, {
get(_receiver, key: any) {
if (key in styles) {
_hey[key] =
_hey[key] ||
((...args: any[]) => {
// @ts-ignore
const string = templateStringToText(...args);
return _hey(`<${key}>${string}</${key}>`, []);
});
}

return _hey[key];
},
set(_, key, value) {
_hey[key] = value;
return true;
},
});

export type Hey = {
(...args: HeyParams): string;
format(...args: HeyParams): string;
} & {
[K in keyof Styles]: (...args: HeyParams) => string;
} & {
styles: Styles;
} & {};

export const hey = proxy as unknown as Hey;
10 changes: 6 additions & 4 deletions packages/utils/src/node.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import nodePath from 'path';
import nodeURL from 'url';

import chalk from 'chalk';
import * as commander from 'commander';
import fsExtra from 'fs-extra';
import glob from 'glob';
import { glob, Glob, globIterate, globIterateSync, globSync } from 'glob';
import * as semver from 'semver';
import urlPattern from 'url-pattern';

Expand Down Expand Up @@ -54,9 +53,12 @@ export {
fsExtra as fs,
nodePath,
nodeURL,
chalk,
glob,
semver,
urlPattern,
commander,
glob,
Glob,
globIterate,
globIterateSync,
globSync,
};

0 comments on commit a959b31

Please sign in to comment.