An open API service indexing awesome lists of open source software.

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

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);
}
```