Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): return exec child process type also #84

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading