generated from seb-cr/ts-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
170 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1 @@ | ||
/** | ||
* Returns a greeting. | ||
* | ||
* @param name Optional name to greet. | ||
*/ | ||
export function greet(name?: string): string { | ||
return `Hello ${name || 'world'}!`; | ||
} | ||
export * from './shell'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { ExecOptions, exec as execWithCallback } from 'child_process'; | ||
import { promisify } from 'util'; | ||
|
||
/** | ||
* Promisified version of `child_process.exec`. | ||
* | ||
* Executes `command` within a shell and return an object containing stdout and | ||
* stderr, or throws an error if `command` completes with a non-zero exit code. | ||
* | ||
* See the official Node documentation here for more details: | ||
* https://nodejs.org/api/child_process.html#child_processexeccommand-options-callback | ||
* | ||
* ```ts | ||
* import { exec } from '@sebalon/scripting'; | ||
* | ||
* const output = await exec('echo hello'); | ||
* // => { stdout: 'hello\n', stderr: '' } | ||
* ``` | ||
*/ | ||
export const exec = promisify(execWithCallback); | ||
|
||
/** | ||
* Options for `sh`. | ||
*/ | ||
export type ShOptions = ExecOptions & { | ||
/** | ||
* Trim leading and trailing whitespace from the output. | ||
* | ||
* Default: true | ||
*/ | ||
trim?: boolean; | ||
}; | ||
|
||
/** | ||
* Executes `command` within a shell and returns its output (stdout), or throws | ||
* an error if `command` completes with a non-zero exit code. | ||
* | ||
* ```ts | ||
* import { sh } from '@sebalon/scripting'; | ||
* | ||
* const output = await sh('echo hello'); | ||
* // => 'hello' | ||
* ``` | ||
* | ||
* @param command Command to run. | ||
* @param options Options to be passed to `exec`. | ||
*/ | ||
export async function sh(command: string, options?: ShOptions): Promise<string> { | ||
const result = await exec(command, options); | ||
let output = result.stdout.toString(); | ||
if (options?.trim ?? true) { | ||
output = output.trim(); | ||
} | ||
return output; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
// add test setup here, e.g. load dotenv, register chai plugins, ... | ||
import chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
|
||
chai.use(chaiAsPromised); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { exec, sh } from '@/src'; | ||
|
||
describe('exec', () => { | ||
it('should run a command and return stdout and stderr', async () => { | ||
const output = await exec('echo "hello stdout"; echo "hello stderr" >&2'); | ||
expect(output.stdout).to.equal('hello stdout\n'); | ||
expect(output.stderr).to.equal('hello stderr\n'); | ||
}); | ||
|
||
it('should throw if the command fails', async () => { | ||
await expect(exec('false')).to.eventually.be.rejected; | ||
}); | ||
}); | ||
|
||
describe('sh', () => { | ||
it('should run a command and return its stdout, trimmed by default', async () => { | ||
const output = await sh('echo hello'); | ||
expect(output).to.equal('hello'); | ||
}); | ||
|
||
it('should throw if the command fails', async () => { | ||
await expect(sh('false')).to.eventually.be.rejected; | ||
}); | ||
|
||
describe('options.trim', () => { | ||
it('if true, should remove leading and trailing spaces', async () => { | ||
const output = await sh('echo " hello "', { trim: true }); | ||
expect(output).to.equal('hello'); | ||
}); | ||
|
||
it('if false, should keep leading and trailing spaces', async () => { | ||
const output = await sh('echo " hello "', { trim: false }); | ||
expect(output).to.equal(' hello \n'); | ||
}); | ||
}); | ||
}); |