create-yo: Use any Yeoman generator. Don't install stuff.
I wanted to introduce a little tool I made called create-yo.
Beginning in npm v6.1.0, npm init <something>
lets you magically invoke a scaffolding tool (think create-react-app) using npx.
If you’re not familiar withnpx
, it’s a tool installed alongsidenpm
which allows execution of command-line Node.js packages without first having tonpm install --global <package-name>
.
For example, running npm init react-app
will use npx
to grab create-react-app
, then execute its script specified in its bin
property of its package.json
. In other words, npm init react-app
is the same as invoking npx create-react-app
.
Since the scaffolding tool Yeoman has a large ecosystem already, I thought it might be cool to piggyback on the new(-ish) functionality of npm init
.
Example
To run any Yeoman generator, all you need to do (given npm
v6.1.0+ and Node.js v8.0.0+) is execute:
$ npm init yo foo
…where foo
refers to package generator-foo
. A more concrete example—to run Yeoman’s own generator-generator, would be this:
$ npm init yo generator
After npx
does its thing, you’ll be prompted to to complete the wizard.
It also supports subgenerators, e.g.:
$ npm init yo generator:subgenerator
Invokes the subgenerator
subgenerator of the generator
generator. Yep.
For The Curious
npx
calls create-yo
’s executable, which in turn invokes npx
(via libnpx) to run yo
’s executable. It uses npx
’s --package
option to grab your generator. It is not fancy.
I'm happy to make this work with Yarn if possible (and would accept a PR to that effect), but don't use Yarn, so I won't be implementing it myself.
Links
Here’s create-yo on GitHub, and its package on npmjs.com.
TL;DR
Try using npm init yo <generator>
instead of npm install -g yo generator-<generator>; yo <generator>
.