Skip to content

Commit 775be79

Browse files
committed
feat(): adjust utils documentation
1 parent 720d27f commit 775be79

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

packages/shared/utils/ai/API.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
executeProcess,
1010
findFilesWithPattern,
1111
resolveFileCached,
12+
loadDefaultExport,
1213
objectToCliArgs,
1314
} from '@push-based/utils';
1415

@@ -29,6 +30,9 @@ const files = await findFilesWithPattern('./src', 'Component');
2930
// Resolve file with caching
3031
const content = await resolveFileCached('./config.json');
3132

33+
// Load ES module default export
34+
const config = await loadDefaultExport('./config.mjs');
35+
3236
// String utilities
3337
const slug = slugify('Hello World!'); // → 'hello-world'
3438
const args = objectToCliArgs({ name: 'test', verbose: true }); // → ['--name="test"', '--verbose']
@@ -38,6 +42,7 @@ const args = objectToCliArgs({ name: 'test', verbose: true }); // → ['--name="
3842

3943
- **Process Execution**: Robust child process management with observers and error handling
4044
- **File Operations**: Cached file resolution and pattern-based file searching
45+
- **ES Module Loading**: Dynamic import of ES modules with default export extraction
4146
- **String Utilities**: Text transformation, slugification, and pluralization
4247
- **CLI Utilities**: Object-to-arguments conversion and command formatting
4348
- **Logging**: Environment-based verbose logging control
@@ -47,6 +52,7 @@ const args = objectToCliArgs({ name: 'test', verbose: true }); // → ['--name="
4752

4853
- **Build Tools**: Execute CLI commands with real-time output monitoring
4954
- **File Processing**: Search and resolve files efficiently with caching
55+
- **Module Loading**: Dynamic import of configuration files and plugins
5056
- **Code Generation**: Transform data into CLI arguments and formatted strings
5157
- **Development Tools**: Create development utilities with proper logging
5258
- **Static Analysis**: Find and process files based on content patterns

packages/shared/utils/ai/EXAMPLES.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,48 @@ if (largeFiles.length > 0) {
433433

434434
---
435435

436+
## 7 — ES Module loading and dynamic imports
437+
438+
> Load ES modules dynamically and extract default exports safely.
439+
440+
```ts
441+
import { loadDefaultExport } from '@push-based/utils';
442+
443+
// Load configuration from ES module
444+
const config = await loadDefaultExport('./config/app.config.mjs');
445+
console.log(`API Port: ${config.port}`);
446+
447+
// Load with type safety
448+
interface AppData {
449+
version: string;
450+
features: string[];
451+
}
452+
453+
const appData = await loadDefaultExport<AppData>('./data/app.mjs');
454+
console.log(`App version: ${appData.version}`);
455+
console.log(`Features: ${appData.features.join(', ')}`);
456+
457+
// Handle loading errors gracefully
458+
try {
459+
const plugin = await loadDefaultExport('./plugins/optional.mjs');
460+
console.log('✅ Plugin loaded');
461+
} catch (error) {
462+
if (error.message.includes('No default export found')) {
463+
console.warn('⚠️ Module missing default export');
464+
} else {
465+
console.warn('⚠️ Plugin not found, continuing without it');
466+
}
467+
}
468+
469+
// Output:
470+
// → API Port: 3000
471+
// → App version: 1.2.0
472+
// → Features: auth, logging, metrics
473+
// → ⚠️ Plugin not found, continuing without it
474+
```
475+
476+
---
477+
436478

437479

438480
These examples demonstrate the comprehensive capabilities of the `@push-based/utils` library for process execution, file operations, string manipulation, and development tooling in Node.js applications.

packages/shared/utils/ai/FUNCTIONS.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
| `getLineHits` | function | Get all pattern matches within a text line |
2121
| `isExcludedDirectory` | function | Check if a directory should be excluded from searches |
2222
| `isVerbose` | function | Check if verbose logging is enabled via environment variable |
23+
| `loadDefaultExport` | function | Dynamically import ES modules and extract default export |
2324
| `objectToCliArgs` | function | Convert object properties to command-line arguments |
2425
| `resolveFile` | function | Read file content directly without caching |
2526
| `resolveFileCached` | function | Read file content with caching for performance |
@@ -251,6 +252,25 @@ Checks if verbose logging is enabled via the `NG_MCP_VERBOSE` environment variab
251252

252253
**Returns:** `true` if verbose logging is enabled
253254

255+
### `loadDefaultExport<T = unknown>(filePath: string): Promise<T>`
256+
257+
Dynamically imports an ES Module and extracts the default export. Uses proper file URL conversion for cross-platform compatibility.
258+
259+
**Parameters:**
260+
261+
- `filePath` - Absolute path to the ES module file to import
262+
263+
**Returns:** Promise resolving to the default export from the module
264+
265+
**Throws:** Error if the module cannot be loaded or has no default export
266+
267+
**Example:**
268+
269+
```typescript
270+
const config = await loadDefaultExport('/path/to/config.js');
271+
const data = await loadDefaultExport<MyDataType>('/path/to/data.mjs');
272+
```
273+
254274
## Constants
255275

256276
### `fileResolverCache: Map<string, Promise<string>>`

0 commit comments

Comments
 (0)