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
3787import { dirname , join } from ' node:path' ;
3888import { fileURLToPath } from ' node:url' ;
3989import { 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
4596const cliPath = getDirname (import .meta .url , ' ./cli-test.ts' );
4697const { stderr, stdout } = await runTsScript (cliPath );
4798console .log (stderr , stdout );
4899```
49100
50- ### execute command
101+ ### Executing Commands
51102
52- ``` ts
103+ ``` typescript
53104import { execute } from ' @hyperse/exec-program' ;
54105
106+ // Install npm packages
55107const { 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
67118await 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
105147import { runTsScript } from ' @hyperse/exec-program' ;
148+
106149console .log (typeof runTsScript );
107150console .log (' cli...' );
108151```
109152
110- create test file ` main.spec.ts `
153+ 3 . Write your test ( ` main.spec.ts ` ):
111154
112- ``` ts
155+ ``` typescript
113156import { dirname , join } from ' node:path' ;
114157import { fileURLToPath } from ' node:url' ;
115158import { runTsScript } from ' @hyperse/exec-program' ;
@@ -121,11 +164,16 @@ const getDirname = (url: string, ...paths: string[]) => {
121164const cliPath = getDirname (import .meta .url , ' ./cli-test.ts' );
122165
123166describe (' 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.
0 commit comments