Better stdout capturing
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/node-utils",
|
"name": "@ztimson/node-utils",
|
||||||
"version": "1.0.5",
|
"version": "1.0.6",
|
||||||
"description": "CSS Utility Classes",
|
"description": "CSS Utility Classes",
|
||||||
"author": "ztimson",
|
"author": "ztimson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
16
src/cli.ts
16
src/cli.ts
@@ -1,17 +1,11 @@
|
|||||||
import {execSync, spawn} from 'node:child_process';
|
import {exec, execSync} from 'node:child_process';
|
||||||
|
|
||||||
export function $(str: TemplateStringsArray, ...args: string[]): Promise<string> {
|
export function $(str: TemplateStringsArray, ...args: string[]): Promise<string> {
|
||||||
let cmd = str.reduce((acc, part, i) => acc + part + (args[i] || ''), '');
|
let cmd = str.reduce((acc, part, i) => acc + part + (args[i] || ''), '');
|
||||||
return new Promise((res, rej) => {
|
return new Promise((res, rej) => exec(cmd, (err, stdout, stderr) => {
|
||||||
const proc = spawn(cmd, {shell: true});
|
if(err) return rej(stderr || err);
|
||||||
let stdout = '', stderr = '';
|
return res((stdout || stderr).trim());
|
||||||
proc.stdout.on('data', (data) => stdout += data);
|
}))
|
||||||
proc.stderr.on('data', (data) => stderr += data);
|
|
||||||
proc.on('close', (code) => {
|
|
||||||
if(code !== 0) return rej(new Error(stderr || stdout));
|
|
||||||
res(stdout.trim());
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function $Sync(str: TemplateStringsArray, ...args: string[]): string {
|
export function $Sync(str: TemplateStringsArray, ...args: string[]): string {
|
||||||
|
|||||||
Reference in New Issue
Block a user