chris48s / v8r

✔️ A command-line JSON, YAML and TOML validator that's on your wavelength
https://chris48s.github.io/v8r/
MIT License
29 stars 5 forks source link

Extend config file format to allow user to specify `userPlugins` #481

Closed chris48s closed 2 months ago

chris48s commented 2 months ago

https://github.com/chris48s/v8r/blob/900952a9939dba81a39e42c8c91569b9d1ba52e7/src/bootstrap.js#L192-L196

chris48s commented 2 months ago

Problem:

I would like to support both plugins installed from NPM and also local plugins.

i.e: It should be possible to load both

The problem with this is for the local ones, I'm going to need to path.resolve(process.cwd(), './subdir/my-local-plugin.mjs') to load it from the right place. Whereas for the remote ones, I just want to use the installed name. So I can see a few options here:

1. Use a prefix.

For example

plugins:
  - npm:v8r-plugin-foobar
  - local:./subdir/my-local-plugin.mjs

This is a bit "magical", but it is probably the simplest option.

2. Seperate local and npm plugins in the config file.

For example:

plugins:
  npm:
    - v8r-plugin-foobar
  local:
    - ./subdir/my-local-plugin.mjs

This is more explicit and less magical, but now ordering is hard (both for me and the user). Which of those two plugins should I run first? How does the user configure this?

3. Treat all plugins as local.

For example:

plugins:
  - ./node_modules/v8r-plugin-emoji-output/index.js
  - ./subdir/my-local-plugin.mjs

This is explicit but ugly and requires the user to dig about in node_modules to configure it correctly.

4. JS only config file

Bin off json and yaml config (breaking change), require javascript config file and have the user do the imports. For example:

import FooBarPlugin from 'v8r-plugin-foobar'
import BazQuxPlugin from './subdir/my-local-plugin.mjs'

const config = {
  plugins: [
    FooBarPlugin,
    BazQuxPlugin,
  ]
}

export default config

If I didn't already have json/yaml config files, I'd probably do this if I were starting from scratch today but I think I'd rather not make the breaking change.


Conclusion: Option 1 - use a prefix.

chris48s commented 2 months ago

I went with package: and file: rather than npm: and local: