auvred / prettier-pnp

Run Prettier with plugins without installing them manually
MIT License
4 stars 1 forks source link

prettier-pnp

Prettier-pnp is utility for running Prettier with plugins, without installing them manually.

Why?

Sometimes you need to format some files, but you don't want to create a package.json, install Prettier and plugins for it. Also Prettier can't load plugins installed globally (it tries to import() them, but Node.js doesn't support import from NODE_PATH).

This may be useful for CI environments, where you need to quickly check the formatting of files.

How?

Prettier-pnp works like a proxy for prettier cli. All arguments passed to prettier-pnp will be forwarded directly to prettier. Prettier-pnp adds only three new args:

Prettier-pnp includes Prettier as dependency. Also, all requested plugins are stored in the internal storage (plugin-store folder inside the installed prettier-pnp). So, when you run:

npx prettier-pnp --pn my-plugin index.js

Prettier-pnp tries to install prettier-plugin-my-plugin as a regular NPM package. Then it resolves the absolute path to the plugin and passes it to the Prettier.

After you uninstall prettier-pnp, all plugins will also be uninstalled, because they are stored inside the plugin-store

Let's assume that you have installed prettier-pnp globally to /home/user/node_modules. After

npx prettier-pnp --pn my-plugin --single-quote index.js

The folder structure will look like this:

/home/user/node_modules
 📂 prettier-pnp
 ├─   package.json
 ├─ 📁 dist
 └─ 📂 plugin-store
    ├─   package.json
    └─ 📂 node_modules
       └─ 📂 prettier-plugin-my-plugin
          ├─   index.js
          └─   package.json

And the Prettier will be executed with the following arguments:

prettier --plugin /home/user/node_modules/prettier-pnp/plugin-store/node_modules/prettier-plugin-my-plugin/index.js --single-quote index.js

License

MIT License © 2023 auvred