@rexlabs/plz-cli :pray:
CLI toolkit for frontend web dev at rexlabs, enabling you to:
The latest version of plz comes with webpack 4, babel 7, jest 24 and storybook 5, as well as everything needed to support React 16.9 š
Why? ā Rationale
"Oh geez, another front dev tool" ... wrong!
This is a kit of tools - it says so in the first sentence. It is šÆ gud.
Frontend development is filled to the brim with open source tools like webpack, babel, jest and storybook. As great as they are, they're difficult to configure, don't play nice together, and each new tool introduces learning curves. Boo.
Rather than we all struggling through the pains of configuration setup and maintenance, in each app and package we build @ rexlabs, we invest all that tooling focus into plz
. It can sit in front of these tools, with a bunch of sane default configurations and hacks, to then be consistently employed in our projects. Devs are given the opportunity to forget about tooling woes, freeing their minds for better tasks. What a pleasant experience!
Why not just use create-react-app
?
CRA, or more specifically react-scripts, is an amazing toolkit and one of the starters of this trend we're now seeing. However, it is (for very good reasons) very opinionated when it comes to the ability to customise it. We needed something a little bit more flexible, since every app after all is different and will have different requirements to certain extends.
Also, we wanted a toolkit that would allow us to build all kinds of projects, not just apps. Since we heavily invest in building centralised components and solutions in form of npm libraries, we were looking for a toolkit that could sit on top of any form of project. This is exactly what plz
does š
Shoutout š
Even though he's not working at rexlabs anymore, Lochlan Bunn still deserves a massive shout out here. Not only did he create plz
in the first place, he also drove its adaption, educating everyone about the concepts, motivations and benefits. Without his work this library and the work that we do thanks to it would not be the same š
Getting started
npm i -g @rexlabs/plz-cli
plz <command> [options]
npx @rexlabs/plz-cli <command> [options]
Usage
$ plz --help
Commands:
plz build Bundles a project for distribution.
plz clean Removes previously built files.
plz config [key] [value] Sets (or reads) config of the plz instance.
Can be used to change defaults.
plz create <name> [root-dir] Generates a new project called <name>
plz serve Starts an dev server for the project.
plz stories Starts a storybook for UI components.
plz test [options] Starts a jest test runner in current
directory.
plz use <package-name> Sets the given package as template source.
See --help for more details.
Options:
-h, --help Show help [boolean]
--no-emoji Strip all emojis from the cli output in the terminal. [boolean]
--report Add memory reporting at the end of each command execution.
[boolean]
--version Show version number [boolean]
Run `plz --help` or `plz <command> --help` to get more info.
Run `DEBUG=plz plz <command> [options]` to get more detailed output for
troubleshooting.
CLI Commands and options
create
Creates a new project.
$ plz create <name> [root-dir]
Generates a new project called <name>
Positionals:
name Name of the project, will be used by default for the name of the
generated directory. [string] [required]
root-dir Optional root directory for new project [string]
Options:
-h, --help Show help [boolean]
--no-emoji Strip all emojis from the cli output in the terminal.
[boolean]
--report Add memory reporting at the end of each command execution.
[boolean]
--version Show version number [boolean]
--project-type, -t Project type, one of: react-app, react-component,
module.The tooling configs (webpack, babel, etc.) will be
determined from the project type. [default: "react-app"]
--template, --tpl Optional custom URL of template repo to be used for the
project. Templates use handlebars to create directories
and files dynamically.
name
Name of the project, will be used by default for the name of the generated directory. It will also be passed down to the template to be used for the package name, core class names, etc.
project-type
The type of project you want to create. This determines the default template, as well as the default configs and setup for the other commands.
template
By default plz will pick the template, according to the project type, from the src/templates/[type]
folder. Optionally you can pass in your own template, either via path (relative to the current working directory), or via git repo. You can also define a sub directory of a repo, by just extending the repo URL with the dir path, e.g.:
plz create new-project --template ./my/relative/sub-folder
plz create new-project --template git@github.com:smith/my-monorepo.git
plz create new-project --template git@github.com:smith/my-monorepo.git/some/sub-folder
The above will clone git@github.com:smith/my-monorepo.git
and then use the sub directory some/sub-folder
(relative to the repo's root folder) as the template directory.
You can also override the default template via plz config
, so you don't have to always manually specify them when using plz.
plz config templates.react-app ./my/relative/sub-folder
If you want to use an npm package for your templates, you can use the plz use
shortcut.
npm i -g my-template-package
plz use my-template-package
Templates are parsed using copy-template-dir, which allows injecting variables via mustache syntax. See below for more details and available variables. For more details on how templates work see the advanced docs.
serve
Exclusive to project type react-app
!
Starts webpack dev server for a dev enviroment with good developer experience.
$ plz serve
Options:
-h, --help Show help [boolean]
--version Show version number [boolean]
--port Port for the dev server. [default: 3000]
--host Host for the dev server. [default: "localhost"]
--open-browser Open the server automatically in the default browser once it
is started. [default: true]
port / host
Port and host to be used for the dev server. By default, plz will start the server on http://localhost:3000. If the port is already used, plz will prompt you with the option to use the next available port instead.
open-browser
By default plz will automatically open the dev environment in your default browser once the server is successfully started. If you don't want that, you can disable it via plz serve --open-browser=false
.
build
Bundles a project for distribution.
$ plz build
Bundles a project for distribution.
Options:
-h, --help Show help [boolean]
--no-emoji Strip all emojis from the cli output in the terminal. [boolean]
--report Add memory reporting at the end of each command execution.
[boolean]
--version Show version number [boolean]
--stats Use this option to generate webpack stats during the build and
automatically start webpack-bundle-analyzer after successful
build.
--serve Starts express server to serve the production bundle after
successful build.
The output depends on the project type. Modules and React components will be bundled optimised for releasing them as npm libraries, while React apps will be bundled into a public
directory optimised for deployment. Plz will default the NODE_ENV
to production, unless manually specified otherwise.
stats
For react apps you can run plz build --stats
. This will run the normal build, and then automatically run webpack-bundle-analyser
, giving you a nice visual representation of your bundles and which dependencies they consist of. This can be really useful when trying to improve your bundle size š
serve
Also for react apps, you can run plz build --serve
. Again, this will build your app normally, but will then start up a local node server to serve that build. This can be helpful if you want to test your application in production mode.
stories
Opens development environment, starting a Storybook dev server. Plz will pick up stories from src/.stories.js
by default, you can override this via the storybook.storiesPattern
config.
$ plz stories
Options:
-h, --help Show help [boolean]
--version Show version number [boolean]
--port Port for the storybook server. [default: 3001]
--root-dir Directory to search in, for multiple package stories.
[string] [default: "components"]
--output-dir Directory to build & output a static storybook app. [string]
port
Same as with serve
, the port for the dev server. Again, plz will prompt you if the port is already in use. Defaults to http://localhost:3001.
root-dir
This option is for monorepo support. If you're maintaining a monorepo with multiple repos that all contain stories (ideally bootstrapped via plz to ensure proper folder structure), you can run plz serve
from the root specifying the root dir for all the repos you want plz to look in for stories.
output-dir
If you specify this, instead of booting up the dev server, plz will run build-stories
and put the output into the specified output directory. This can be useful e.g. for CI, if you want to create a static build of storybook for you master or PR branches.
test
Runs jest tests on the project.
$ plz test
Options:
-h, --help Show help [boolean]
--version Show version number [boolean]
--watch Rerun tests when files change. [boolean] [default: false]
Any options will be passed through to jest
itself, so you can use everything you already know, e.g. --watch
, --coverage
, etc.
config
Set instance config key value pair, or read current config. This can be useful for globally installed plz
instances, e.g. to override the templates used for the create
command.
$ plz config [key] [value]
Sets (or reads) config of the plz instance. Can be used to change defaults.
Positionals:
key Config key [string]
value Config value for given key, if a relative path is given it will be
resolved based on the current working directory. [string]
Options:
-h, --help Show help [boolean]
--no-emoji Strip all emojis from the cli output in the terminal. [boolean]
--report Add memory reporting at the end of each command execution.
[boolean]
--version Show version number [boolean]
--get Read config value(s).
Example for overriding the default template for react apps:
plz config templates.react-app ./my/custom/path
use
Abstraction for config
, using the given npm package for template paths in the cli default config.
$ plz use <package-name>
Sets the given package as template source. See --help for more details.
Positionals:
package-name Name of globally installed npm package. It needs to be already
installed, plz will use "npm root -g" to find the given package.
[string] [required]
Options:
-h, --help Show help [boolean]
--no-emoji Strip all emojis from the cli output in the terminal. [boolean]
--report Add memory reporting at the end of each command execution.
[boolean]
--version Show version number [boolean]
Debugging
If you need to troubleshoot issues, you might find the more verbose debug output helpful. To show these, simply run:
DEBUG=plz plz <command> [options]
You can find more info here: https://github.com/visionmedia/debug
Config
Config for the plz
š cli is resolved from established configuration files like other cli tools.
- the
plz
property in package.json
file - the
.plzrc
file (optionally with .json
, .yaml
, .js
extensions) - the
.plz.config.js
file
The Javascript versions are especially helpful when overriding the tool configs, as you will want to define a function that receives the proposed config and returns your adjusted version.
Available options
{
"projectType": "react-component",
"buildDir": "public",
"cssModules": false,
"runtimeCompilation": false,
"proxy": {},
"storybook": {},
"corejs": 3,
"babel": (config) => config,
"webpack": (config) => config
}
For more details about the tool configs see the futher documentation.
Contributing
Structure of this repo
.
āāā CHANGELOG.md
āāā __tests__
āāā package.json
āāā README.md
āāā src
āĀ Ā āāā bin.js
āĀ Ā āāā commands
āĀ Ā āĀ Ā āāā build.js
āĀ Ā āĀ Ā āāā clean.js
āĀ Ā āĀ Ā āāā create.js
āĀ Ā āĀ Ā āāā serve.js
āĀ Ā āĀ Ā āāā stories.js
āĀ Ā āĀ Ā āāā test.js
āĀ Ā āāā configs
āĀ Ā āĀ Ā āāā babel
āĀ Ā āĀ Ā āāā jest
āĀ Ā āĀ Ā āāā project
āĀ Ā āĀ Ā āāā storybook
āĀ Ā āĀ Ā āāā webpack
āĀ Ā āāā index.js
ā āāā templates
ā ā āāā module
ā ā āāā react-app
ā ā āāā react-component
āĀ Ā āāā utils
āāā support
All commands are automatically detected by yargs through the commandDir
method, which is set to /commands
. Files in there need to export all the command relevant information themselves.
Running tests
yarn test:watch
yarn test
yarn test --coverage
yarn test:integration
Further documentation
Resources
Roadmap