CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
README.md74 linesDownload Raw Back to pirates
1# Pirates [![Coverage][codecov-badge]][codecov-link]2 3### Properly hijack require4 5This library allows to add custom require hooks, which do not interfere with other require hooks.6 7This library only works with commonJS.8 9[codecov-badge]: https://img.shields.io/codecov/c/github/danez/pirates/master.svg?style=flat "codecov"10[codecov-link]: https://codecov.io/gh/danez/pirates "codecov"11 12## Why?13 14Two reasons:151. Babel and istanbul were breaking each other.162. Everyone seemed to re-invent the wheel on this, and everyone wanted a solution that was DRY, simple, easy to use,17and made everything Just Work™, while allowing multiple require hooks, in a fashion similar to calling `super`.18 19For some context, see [the Babel issue thread][] which started this all, then [the nyc issue thread][], where20discussion was moved (as we began to discuss just using the code nyc had developed), and finally to [#1][issue-1]21where discussion was finally moved.22 23[the Babel issue thread]: https://github.com/babel/babel/pull/3062 "Babel Issue Thread"24[the nyc issue thread]: https://github.com/bcoe/nyc/issues/70 "NYC Issue Thread"25[issue-1]: https://github.com/danez/pirates/issues/1 "Issue #1"26 27## Installation28 29    npm install --save pirates30 31## Usage32 33Using pirates is really easy:34```javascript35// my-module/register.js36const addHook = require('pirates').addHook;37// Or if you use ES modules38// import { addHook } from 'pirates';39 40function matcher(filename) {41  // Here, you can inspect the filename to determine if it should be hooked or42  // not. Just return a truthy/falsey. Files in node_modules are automatically ignored,43  // unless otherwise specified in options (see below).44 45  // TODO: Implement your logic here46  return true;47}48 49const revert = addHook(50  (code, filename) => code.replace('@@foo', 'console.log(\'foo\');'),51  { exts: ['.js'], matcher }52);53 54// And later, if you want to un-hook require, you can just do:55revert();56```57 58## API59 60### pirates.addHook(hook, [opts={ [matcher: true], [exts: ['.js']], [ignoreNodeModules: true] }]);61Add a require hook. `hook` must be a function that takes `(code, filename)`, and returns the modified code. `opts` is62an optional options object. Available options are: `matcher`, which is a function that accepts a filename, and63returns a truthy value if the file should be hooked (defaults to a function that always returns true), falsey if64otherwise; `exts`, which is an array of extensions to hook, they should begin with `.` (defaults to `['.js']`);65`ignoreNodeModules`, if true, any file in a `node_modules` folder wont be hooked (the matcher also wont be called),66if false, then the matcher will be called for any files in `node_modules` (defaults to true).67 68 69## Projects that use Pirates70 71See the [wiki page](https://github.com/danez/pirates/wiki/Projects-using-Pirates). If you add Pirates to your project,72(And you should! It works best if everyone uses it. Then we can have a happy world full of happy require hooks!), please73add yourself to the wiki.74 
basant307/AI_Governance_Project · CoolFace