Skip to content

Commit 0194e3e

Browse files
committed
docs: 📚 update README with enhanced structure, features, and usage examples
1 parent 20dea78 commit 0194e3e

File tree

3 files changed

+89
-40
lines changed

3 files changed

+89
-40
lines changed

.husky/commit-msg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
#!/bin/sh
12

23
yarn commitlint --edit "$1"

README.md

Lines changed: 86 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,75 @@
1515
</a>
1616
</p>
1717

18-
Runs commands in your script, application or library. Unlike shells, it is optimized for programmatic usage.
18+
A Node.js utility for programmatically executing commands in your scripts, applications, or libraries. Unlike traditional shells, it's optimized for programmatic usage with TypeScript support.
1919

20-
## README
20+
## Table of Contents
2121

22-
- [Getting started](#getting-started)
23-
- [Common use cases](#usage)
24-
- [Vitest unitest](#unit-test)
22+
- [Installation](#installation)
23+
- [Features](#features)
24+
- [API Reference](#api-reference)
25+
- [runTsScript](#runtsscript)
26+
- [execute](#execute)
27+
- [Usage Examples](#usage-examples)
28+
- [Running TypeScript Files](#running-typescript-files)
29+
- [Executing Commands](#executing-commands)
30+
- [Unit Testing with Vitest](#unit-testing-with-vitest)
31+
- [Configuration](#configuration)
2532

26-
### Getting started
33+
## Installation
2734

2835
```bash
29-
npm i --save @hyperse/exec-program
36+
npm install --save @hyperse/exec-program
3037
```
3138

32-
### Usage
39+
## Features
40+
41+
- 🚀 Execute TypeScript files directly
42+
- 💻 Run shell commands programmatically
43+
- 📘 TypeScript support out of the box
44+
- ⚡ Promise-based API
45+
- ⚙️ Configurable execution options
46+
- 🧪 Built-in support for unit testing
47+
48+
## API Reference
3349

3450
### runTsScript
3551

36-
```ts
52+
Executes a TypeScript file and returns its output.
53+
54+
```typescript
55+
/**
56+
* Process execute typescript script file using `@hyperse/ts-node`
57+
* @param program - The absolute typescript file path
58+
* @param options - The configuration of `execa` { env: { TS_NODE_PROJECT: tsconfig } }
59+
* @param args - The runtime argv for program
60+
*/
61+
declare const runTsScript: <T extends ExecOptions>(
62+
program: string,
63+
args?: readonly string[],
64+
options?: T
65+
) => ExecResultPromise<{} & T>;
66+
```
67+
68+
### execute
69+
70+
Executes a shell command with specified arguments and options.
71+
72+
```typescript
73+
import { execute } from '@hyperse/exec-program';
74+
75+
declare function execute<T extends ExecOptions>(
76+
file: string,
77+
args?: readonly string[],
78+
options?: T
79+
): ExecResultPromise<{} & T>;
80+
```
81+
82+
## Usage Examples
83+
84+
### Running TypeScript Files
85+
86+
```typescript
3787
import { dirname, join } from 'node:path';
3888
import { fileURLToPath } from 'node:url';
3989
import { runTsScript } from '@hyperse/exec-program';
@@ -42,74 +92,67 @@ const getDirname = (url: string, ...paths: string[]) => {
4292
return join(dirname(fileURLToPath(url)), ...paths);
4393
};
4494

95+
// Execute a TypeScript file
4596
const cliPath = getDirname(import.meta.url, './cli-test.ts');
4697
const { stderr, stdout } = await runTsScript(cliPath);
4798
console.log(stderr, stdout);
4899
```
49100

50-
### execute command
101+
### Executing Commands
51102

52-
```ts
103+
```typescript
53104
import { execute } from '@hyperse/exec-program';
54105

106+
// Install npm packages
55107
const { stdout, stderr } = await execute(
56108
'npm',
57-
['i', '--no-save', '--no-package-lock', ...toInstall],
109+
['i', '--no-save', '--no-package-lock', ...packages],
58110
{
59-
cwd: target.directory,
111+
cwd: targetDirectory,
60112
maxBuffer: TEN_MEGA_BYTE,
61-
env: this.options.npmEnv,
113+
env: npmEnv,
62114
}
63115
);
64-
```
65116

66-
```ts
117+
// Create npm package
67118
await execute('npm', ['pack', directory], {
68-
cwd: this.uniqueDir,
119+
cwd: uniqueDir,
69120
maxBuffer: TEN_MEGA_BYTE,
70121
});
71122
```
72123

73-
#### run ts file for unit testing
124+
### Unit Testing with Vitest
74125

75-
config `tsconfig.json`
126+
1. Configure your `tsconfig.json`:
76127

77128
```json
78129
{
79130
"extends": "@hyperse/eslint-config-hyperse/tsconfig.base.json",
80131
"compilerOptions": {
81-
...
82132
"baseUrl": "./src",
83133
"rootDir": "./",
84134
"outDir": "dist",
85-
"types": [
86-
"vitest/globals"
87-
],
135+
"types": ["vitest/globals"],
88136
"paths": {
89-
"@hyperse/exec-program": [
90-
"../src/index.js"
91-
],
137+
"@hyperse/exec-program": ["../src/index.js"]
92138
}
93139
},
94-
"include": [
95-
"next-env.d.ts",
96-
"**/*.ts",
97-
"**/*.tsx"
98-
]
140+
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"]
141+
}
99142
```
100143

101-
create `cli-test.ts`
144+
2. Create a test file (`cli-test.ts`):
102145

103-
```ts
104-
// cause of `tsconfig.json` we can directly import source .ts file from '@hyperse/exec-program';
146+
```typescript
105147
import { runTsScript } from '@hyperse/exec-program';
148+
106149
console.log(typeof runTsScript);
107150
console.log('cli...');
108151
```
109152

110-
create test file `main.spec.ts`
153+
3. Write your test (`main.spec.ts`):
111154

112-
```ts
155+
```typescript
113156
import { dirname, join } from 'node:path';
114157
import { fileURLToPath } from 'node:url';
115158
import { runTsScript } from '@hyperse/exec-program';
@@ -121,11 +164,16 @@ const getDirname = (url: string, ...paths: string[]) => {
121164
const cliPath = getDirname(import.meta.url, './cli-test.ts');
122165

123166
describe('test suites of exec program', () => {
124-
it('should correct invoke cli.ts', async () => {
167+
it('should correctly invoke cli.ts', async () => {
125168
const { stderr, stdout } = await runTsScript(cliPath);
126-
console.log(stderr, stdout);
127169
expect(stderr).toBe('');
128170
expect(stdout).toMatch(/cli.../);
129171
});
130172
});
131173
```
174+
175+
## Configuration
176+
177+
The library supports various configuration options for both `runTsScript` and `execute` functions. These options allow you to customize the execution environment, working directory, and other parameters.
178+
179+
For detailed configuration options, please refer to the TypeScript types in the source code.

src/runTsScript.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { execa } from 'execa';
22
import type { ExecOptions, ExecResultPromise } from './types.js';
33

44
/**
5-
* Process execute typescript script file using `@hyperse/ts-node-paths`
5+
* Process execute typescript script file using `@hyperse/ts-node`
66
* @param program - The absolute typescript file path
7-
* @param options - The configuration of `execa` { env: { TS_NODE_PATHS_PROJECT: tsconfig } }
7+
* @param options - The configuration of `execa` { env: { TS_NODE_PROJECT: tsconfig } }
88
* @param args - The runtime argv for program
99
*/
1010
export const runTsScript = <T extends ExecOptions>(

0 commit comments

Comments
 (0)