https://github.com/gilbarbara/node-helpers
Collection of useful functions for node
https://github.com/gilbarbara/node-helpers
Last synced: 3 months ago
JSON representation
Collection of useful functions for node
- Host: GitHub
- URL: https://github.com/gilbarbara/node-helpers
- Owner: gilbarbara
- License: mit
- Created: 2023-11-14T05:50:24.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-14T16:00:11.000Z (about 2 years ago)
- Last Synced: 2025-08-24T15:25:28.764Z (5 months ago)
- Language: TypeScript
- Size: 72.3 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Node Helpers
Collection of useful functions for node.
## Usage
```bash
$ npm install @gilbarbara/node-helpers
```
## API
**replaceContent(options: ReplaceContentOptions): Promise**
Replace content in files.
Type Definition
```typescript
type ReplaceActionResult = string | false | undefined;
interface ReplaceContentOptions {
callback(content: string): Promise | ReplaceActionResult;
directory?: string;
globOptions?: fg.Options;
name?: string;
pattern: fg.Pattern | fg.Pattern[];
silent?: boolean;
}
```
Example
```typescript
async function fixCjsDts() {
const results = await replaceContent({
directory: 'dist',
name: 'fix-cjs-dts',
pattern: '**/*.d.ts',
callback: content => {
const { exp, named } =
/(?export (?.+) from 'some-packages';)/u.exec(content)?.groups ?? {};
const { exportDefault } =
/(?export \{ somePackage as default \};)/u.exec(content)?.groups ?? {};
if (exp && named) {
const statement = `declare namespace SomePackage {
export ${named.replace('default as somePackage', 'somePackage')};
${exportDefault}
}
export = SomePackage;`;
return (
content
.replace(`\n${exp}`, '')
.replace(`\n${exportDefault}`, '') + statement
);
}
return false;
}
});
// results is an array with the replace files content.
console.log(result);
}
```