Skip to content

Commit

Permalink
Merge pull request #117 from AthennaIO/develop
Browse files Browse the repository at this point in the history
feat(exec): add Exec.install() method
  • Loading branch information
jlenon7 committed Mar 3, 2024
2 parents f0a931c + 53ea7f1 commit 5d8ff13
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 65 deletions.
57 changes: 29 additions & 28 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/common",
"version": "4.34.0",
"version": "4.35.0",
"description": "The Athenna common helpers to use in any Node.js ESM project.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down Expand Up @@ -62,7 +62,7 @@
"collect.js": "^4.36.1",
"csv-parser": "^3.0.0",
"execa": "^8.0.1",
"fastify": "^4.26.0",
"fastify": "^4.26.1",
"got": "^12.6.1",
"http-status-codes": "^2.2.0",
"is-wsl": "^2.2.0",
Expand All @@ -82,7 +82,7 @@
"youch-terminal": "^2.2.2"
},
"devDependencies": {
"@athenna/test": "^4.18.0",
"@athenna/test": "^4.22.0",
"@athenna/tsconfig": "^4.12.0",
"@types/bytes": "^3.1.1",
"@types/callsite": "^1.0.31",
Expand Down
22 changes: 0 additions & 22 deletions src/globals/Path.ts

This file was deleted.

95 changes: 88 additions & 7 deletions src/helpers/Exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@
* file that was distributed with this source code.
*/

import { Transform } from 'node:stream'
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, type ExecaChildProcess } from 'execa'
import type {
CommandInput,
CommandOutput,
NodeCommandInput,
PaginationOptions,
PaginatedResponse
PaginatedResponse,
InstallPackageOptions,
LinkPackageOptions
} from '#src/types'

import { Is } from '#src/helpers/Is'
import { Transform } from 'node:stream'
import { File } from '#src/helpers/File'
import { Path } from '#src/helpers/Path'
import { Options } from '#src/helpers/Options'
import { request as requestHttp } from 'node:http'
import { request as requestHttps } from 'node:https'
import { execa, execaNode, execaCommand, type ExecaChildProcess } from 'execa'

export class Exec {
/**
* Sleep the code in the line that this function
Expand Down Expand Up @@ -61,6 +66,82 @@ export class Exec {
return execa('sh', ['-c', command], options)
}

/**
* Install libraries into a path using a registry as a child process.
*/
public static async install(
libraries: string | string[],
options: InstallPackageOptions = {}
): Promise<CommandOutput> {
options = Options.create(options, {
args: [],
dev: false,
reject: true,
silent: true,
cached: false,
registry: 'npm',
cwd: Path.pwd()
})

if (Is.String(libraries)) {
libraries = [libraries]
}

const args = ['install']

if (options.registry === 'yarn') {
args[0] = 'add'
}

if (options.dev) {
args.push('-D')
}

if (options.cached) {
args.push('--prefer-offline')
}

args.push(...options.args)
args.push(...libraries)

return execa(options.registry, args, {
reject: options.reject,
stdio: options.silent ? 'ignore' : 'inherit',
cwd: options.cwd
})
}

/**
* Link libraries into a path using a registry as a child process.
*/
public static async link(
libraries: string | string[],
options: LinkPackageOptions = {}
): Promise<CommandOutput> {
options = Options.create(options, {
args: [],
reject: true,
silent: true,
registry: 'npm',
cwd: Path.pwd()
})

if (Is.String(libraries)) {
libraries = [libraries]
}

const args = ['link']

args.push(...options.args)
args.push(...libraries)

return execa(options.registry, args, {
reject: options.reject,
stdio: options.silent ? 'ignore' : 'inherit',
cwd: options.cwd
})
}

public static command(
command: string,
options?: CommandInput
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

export * from '#src/types'

export * from '#src/globals/Path'
export * from '#src/globals/Error'
export * from '#src/globals/Array'

Expand Down
66 changes: 66 additions & 0 deletions src/types/InstallPackageOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* @athenna/common
*
* (c) João Lenon <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

export type InstallPackageOptions = {
/**
* Define the path where the install command should ran.
*
* @default Path.pwd()
*/
cwd?: string

/**
* Define if the libraries should be installed as
* devDependencies or not.
*
* @default false
*/
dev?: boolean

/**
* Add additional arguments for the registry CLI you
* are using to install the libraries.
*
* @default []
*/
args?: string[]

/**
* Define if the registry should use cached libraries when
* installing it.
*
* @default false
*/
cached?: boolean

/**
* Define if the libraries installation should display the
* logs from the registry or not.
*
* @default true
*/
silent?: boolean

/**
* Throw errors if something goes wrong when trying to
* install the libraries.
*
* @default true
*/
reject?: boolean

/**
* Define the registry that will be used to install the
* dependencies. Tested only with `npm`, but
* you can try to use whatever you like.
*
* @default "npm"
*/
registry?: string
}
Loading

0 comments on commit 5d8ff13

Please sign in to comment.