diff --git a/package.json b/package.json
index cc1d0917..e32c72d6 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "powership",
- "version": "3.3.10",
+ "version": "3.3.11",
"private": true,
"scripts": {
"pack": "run-s pack:*",
diff --git a/packages/runmate/src/commands/executeInPackages.ts b/packages/runmate/src/commands/executeInPackages.ts
index 7a39c0fc..c95e1cca 100644
--- a/packages/runmate/src/commands/executeInPackages.ts
+++ b/packages/runmate/src/commands/executeInPackages.ts
@@ -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';
@@ -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;
}
diff --git a/packages/runmate/src/commands/set-json-value.ts b/packages/runmate/src/commands/set-json-value.ts
index 593bb028..f179b4a5 100644
--- a/packages/runmate/src/commands/set-json-value.ts
+++ b/packages/runmate/src/commands/set-json-value.ts
@@ -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';
@@ -56,7 +56,7 @@ export function setJsonValue(program: Command) {
}
});
} catch (e: any) {
- console.error(chalk.red(e));
+ hey.error(e);
}
});
@@ -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);
}
});
}
diff --git a/packages/utils/src/hey.spec.ts b/packages/utils/src/hey.spec.ts
index 685c9db0..0e85293e 100644
--- a/packages/utils/src/hey.spec.ts
+++ b/packages/utils/src/hey.spec.ts
@@ -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 outside!
lets go?
@@ -17,4 +17,8 @@ describe('hey', () => {
'',
]);
});
+
+ test('error', () => {
+ hey.error(new Error('foo'));
+ });
});
diff --git a/packages/utils/src/hey.ts b/packages/utils/src/hey.ts
index 986abfc2..df8a8f7a 100644
--- a/packages/utils/src/hey.ts
+++ b/packages/utils/src/hey.ts
@@ -1,18 +1,23 @@
-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',
@@ -20,10 +25,9 @@ hey.styles = {
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];
@@ -35,18 +39,18 @@ 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'
@@ -54,17 +58,14 @@ async function safeWrite(input: string) {
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 {
@@ -89,9 +90,50 @@ export function templateStringToText(
...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;
diff --git a/packages/utils/src/node.ts b/packages/utils/src/node.ts
index 37519117..50f36174 100644
--- a/packages/utils/src/node.ts
+++ b/packages/utils/src/node.ts
@@ -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';
@@ -54,9 +53,12 @@ export {
fsExtra as fs,
nodePath,
nodeURL,
- chalk,
- glob,
semver,
urlPattern,
commander,
+ glob,
+ Glob,
+ globIterate,
+ globIterateSync,
+ globSync,
};