diff --git a/package-lock.json b/package-lock.json index b200caa..01a1af0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/common", - "version": "4.15.2", + "version": "4.15.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@athenna/common", - "version": "4.15.2", + "version": "4.15.3", "license": "MIT", "dependencies": { "@fastify/formbody": "^7.4.0", diff --git a/package.json b/package.json index bd1aa95..83b8761 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/common", - "version": "4.15.2", + "version": "4.15.3", "description": "The Athenna common helpers to use in any Node.js ESM project.", "license": "MIT", "author": "João Lenon ", diff --git a/src/helpers/Exec.ts b/src/helpers/Exec.ts index cc33997..9573ff7 100644 --- a/src/helpers/Exec.ts +++ b/src/helpers/Exec.ts @@ -12,7 +12,7 @@ import { File } from '#src/helpers/File' import { Options } from '#src/helpers/Options' import { request as requestHttp } from 'node:http' import { request as requestHttps } from 'node:https' -import { execa, execaNode, execaCommand } from 'execa' +import { execa, execaNode, execaCommand, type ExecaChildProcess } from 'execa' import type { CommandInput, CommandOutput, @@ -41,6 +41,16 @@ export class Exec { return Promise.all(array.map(callback)) } + public static shell( + command: string, + options?: CommandInput + ): ExecaChildProcess + + public static shell( + command: string, + options?: CommandInput + ): Promise + /** * Execute a shell command as a child process. */ @@ -51,24 +61,43 @@ export class Exec { return execa('sh', ['-c', command], options) } + public static command( + command: string, + options?: CommandInput + ): ExecaChildProcess + + public static command( + command: string, + options?: CommandInput + ): Promise + /** * Execute one specific command as a child process. */ - public static async command( - command: string, - options: CommandInput = {} - ): Promise { + public static command(command: string, options: CommandInput = {}) { return execaCommand(command, options) } + public static node( + path: string, + argv?: string[], + options?: NodeCommandInput + ): ExecaChildProcess + + public static node( + path: string, + argv?: string[], + options?: NodeCommandInput + ): Promise + /** * Execute a node script as a child process. */ - public static async node( + public static node( path: string, argv: string[] = [], options: NodeCommandInput = {} - ): Promise { + ) { return execaNode(path, argv, options) } diff --git a/src/helpers/Path.ts b/src/helpers/Path.ts index af51212..7da8c7b 100644 --- a/src/helpers/Path.ts +++ b/src/helpers/Path.ts @@ -9,9 +9,9 @@ import callSite from 'callsite' -import { fileURLToPath } from 'node:url' import { homedir, tmpdir } from 'node:os' import type { PathDirs } from '#src/types' +import { pathToFileURL, fileURLToPath } from 'node:url' import { sep, normalize, dirname, parse } from 'node:path' export class Path { @@ -112,6 +112,27 @@ export class Path { return `${Path.removeExt(path)}.${Path.ext()}` } + /** + * Convert a path to URL. + */ + public static toURL(path: string): URL { + return pathToFileURL(path) + } + + /** + * Convert URL or URL href to path. + */ + public static toPath(url: string | URL): string { + return fileURLToPath(url) + } + + /** + * Convert a path to URL href. + */ + public static toHref(path: string): string { + return pathToFileURL(path).href + } + /** * Return the pwd path of your project. */ @@ -712,7 +733,7 @@ export class Path { let fileName = stack[stackIndex].getFileName() if (fileName.startsWith('file:')) { - fileName = fileURLToPath(fileName) + fileName = Path.toPath(fileName) } const requester = dirname(fileName)