README
SimpleExec
Command. Execution. Made. Simple.
Key features • Table of contents • Install • Releases • API
✨Since v.2.0.0
SimpleExec
is a hybrid module that supports both CommonJS (legacy) and ES modules, thanks to Modern Module.
Key features
Execute shell commands in a simpler manner using SimpleExec.
Supports sync, callback and async command execution.
Since version 1.1.0 parallel execution is also possible.
Table of contents
Install
npm i simple-exec
Releases
The recommended way to get the package is to install with a package manager but you can also get the latest release here.
API
ExecResult { error: string, output: string }
ExecCallback(result: ExecResult)
executeSync(command: string): ExecResult
Synchronously runs the specified command.
import * as simple from 'simple-exec'
const result = simple.executeSync('dir /b')
if (result.error) {
console.error(result.error) // log the error
} else {
console.log(result.output) // log the contents of the directory
}
executeCallback(command: string, callback: ExecCallback): void
Asynchronously, with a callback runs the specified command.
import * as simple from 'simple-exec'
simple.executeCallback('node --version', (result) => {
if (result.error) {
console.error(result.error) // log the error
} else {
console.log(result.output) // log Node version
}
})
execute(command: string): Promise<string>
Asynchronously runs the specified command.
Example 1 - async/await
import * as simple from 'simple-exec'
try {
const result = await simple.execute('npm --version')
console.log(result) // log NPM version
} catch (exp) {
console.error(result.error) // log the error
}
Example 2 - async/then
import * as simple from 'simple-exec'
simple
.execute('npm --version')
.then((version) => {
console.log(version) // log NPM version
})
.catch((error) => {
console.error(error) // log the error
})
executeParallel(args: ...string): Promise<string[]>
Asynchronously and in parallel runs the specified commands.
import * as simple from 'simple-exec'
simple
.executeParallel('npm --version', 'node --version', 'npm pack')
.then((results) => {
console.log(results) // log the results which is a string array
})
.catch((error) => {
console.error(error) // log the error
})
executeParallel(args: string[]): Promise<string[]>
Asynchronously and in parallel runs the specified commands.
import * as simple from 'simple-exec'
simple
.executeParallel(['npm --version', 'node --version', 'npm pack'])
.then((results) => {
console.log(results) // log the results which is a string array
})
.catch((error) => {
console.error(error) // log the error
})
Test
npm i && npm test