Skip to content

Commit

Permalink
Merge pull request #84 from AthennaIO/develop
Browse files Browse the repository at this point in the history
fix(types): return exec child process type also
  • Loading branch information
jlenon7 committed Sep 26, 2023
2 parents 8ca140f + 011d284 commit b4f4315
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down
43 changes: 36 additions & 7 deletions src/helpers/Exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -41,6 +41,16 @@ export class Exec {
return Promise.all(array.map(callback))
}

public static shell(
command: string,
options?: CommandInput
): ExecaChildProcess<string>

public static shell(
command: string,
options?: CommandInput
): Promise<CommandOutput>

/**
* Execute a shell command as a child process.
*/
Expand All @@ -51,24 +61,43 @@ export class Exec {
return execa('sh', ['-c', command], options)
}

public static command(
command: string,
options?: CommandInput
): ExecaChildProcess<string>

public static command(
command: string,
options?: CommandInput
): Promise<CommandOutput>

/**
* Execute one specific command as a child process.
*/
public static async command(
command: string,
options: CommandInput = {}
): Promise<CommandOutput> {
public static command(command: string, options: CommandInput = {}) {
return execaCommand(command, options)
}

public static node(
path: string,
argv?: string[],
options?: NodeCommandInput
): ExecaChildProcess<string>

public static node(
path: string,
argv?: string[],
options?: NodeCommandInput
): Promise<CommandOutput>

/**
* Execute a node script as a child process.
*/
public static async node(
public static node(
path: string,
argv: string[] = [],
options: NodeCommandInput = {}
): Promise<CommandOutput> {
) {
return execaNode(path, argv, options)
}

Expand Down
25 changes: 23 additions & 2 deletions src/helpers/Path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b4f4315

Please sign in to comment.