jbenet / transformer

transformer - multiformat data conversion
transform.datadex.io
130 stars 7 forks source link

transformer module install paths #15

Closed jbenet closed 10 years ago

jbenet commented 10 years ago

transformer module install paths

Usage - Use Cases

Here are the use cases that transformer should support. Useful to list these out to ensure solutions satisfy these. (It may be the case that the cli should be its own module).

Things to keep in mind:

> tree
.
├── index.js
└── node_modules
    ├── dat-transformer
    ├── transformer.us-city
    └── transformer.us-zipcode
> cat index.js
var transformer = require('dat-transformer');
var zip2city = transformer('us-zipcode', 'us-city');
console.log(zip2city(94123))
  1. loads transformer modules using require('transformer.<id>').
  2. module deps listed in user's package.json
  3. modules must be in node_modules/ (npm root)
  4. throws exception if module is not found (guiding user to install it locally)

    As a lib, from js in the web.

Same as above, using browserify.

  1. modules normally in the bundle.js
  2. can work with browserify-cdn
  3. http://transform.datadex.io/browser/ should work like http://requirebin.com

    From cli, globally, with globally installed modules

# works anywhere in the fs
> tree
.
> echo '94123' | transform us-zipcode us-city
San Francisco, CA
  1. modules installed somewhere globally accessible.
  2. probably best in global node_modules/ (npm -g root).
  3. Graceful error if module not found, guiding user to install, either globally or locally. Example:
# without modules installed
> echo '94123' | transform us-zipcode us-city  
Error: transformer needs the following modules to perform this conversion:

  - us-zipcode 
  - us-city
  - us-zipcode-to-us-city

To install them, run:

  # locally, to be used within this directory
  npm install transformer.us-zipcode transformer.us-city transformer.us-zipcode-to-us-city

  # globally, to be used everywhere in your system (you may need to sudo)
  npm install -g transformer.us-zipcode transformer.us-city transformer.us-zipcode-to-us-city

From cli, globally, with locally installed modules

> tree
.
└── node_modules
    └── transformer.us-zipcode
> echo '94123' | transform us-zipcode us-city
San Francisco, CA
  1. as above, but prioritizes us-zipcode installed locally
  2. falls back to use us-city from global

    From cli, locally, with locally installed modules

> tree
.
└── node_modules
    ├── dat-transformer
    ├── transformer.us-city
    └── transformer.us-zipcode
> echo '94123' | node_modules/.bin/transform us-zipcode us-city
San Francisco, CA
  1. modules installed locally, inside node_modules/
  2. module deps listed in user's package.json
  3. does not use globally installed modules
jbenet commented 10 years ago
  1. possible simple solution is:
    • lib should just require(module).
    • cli separately does try { regular transformer require } catch (e) { try global require } (doesn't satisfy "local cli not using global modules")
jbenet commented 10 years ago

This is pretty much solved. See how transformer-loader does it. Turns out the simple solution works great. I added a -g, --global flag to transform and transformer clis to denote the same semantics that npm uses.