@gilbarbara/node-helpers
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

Node Helpers

Collection of useful functions for node.

Usage

$ npm install @gilbarbara/node-helpers

API

replaceContent(options: ReplaceContentOptions): Promise<string[]>
Replace content in files.

Type Definition
type ReplaceActionResult = string | false | undefined;

interface ReplaceContentOptions {
callback(content: string): Promise<ReplaceActionResult> | ReplaceActionResult;
directory?: string;
globOptions?: fg.Options;
name?: string;
pattern: fg.Pattern | fg.Pattern[];
silent?: boolean;
}
Example
async function fixCjsDts() {
const results = await replaceContent({
    directory: 'dist',
    name: 'fix-cjs-dts',
    pattern: '**/*.d.ts',
    callback: content => {
      const { exp, named } =
        /(?<exp>export (?<named>.+) from 'some-packages';)/u.exec(content)?.groups ?? {};
      const { exportDefault } =
        /(?<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);
}

Readme

Keywords

Package Sidebar

Install

npm i @gilbarbara/node-helpers

Weekly Downloads

6

Version

0.1.0

License

MIT

Unpacked Size

16.9 kB

Total Files

11

Last publish

Collaborators

  • gilbarbara