Create Ember Addon with command line support

$ ember unused:components
More than 20% of components is unused in this project

Create an Addon

Take a deep breath and run:

$ ember addon ember-awesome-addon-name

Plan the structure

There is no any strong preference towards how you will organize “command line addon” but I found the following pretty nice:

ember-awesome-addon-name/
├── addon/
├── app/
├── ...
├── lib/
│ ├── commands/
│ └── unused.js
│ └── components.js

├── test/
└── ...

The meat: add the command

Go to index.js and add following:

'use strict';module.exports = {
name: 'ember-awesome-addon-name',
includedCommands() {
return {
'unused:components': require('./lib/commands/unused')
};
}

};

Define the command

Go to lib/commands/unused.js and define the following:

'use strict';const components = require('../components');module.exports = {
name: 'unused:components',
aliases: ['uc'],
availableOptions: [
{ name: 'pods', type: Boolean, default: false, aliases: ['p'] },
{ name: 'pods-dir', type: String, aliases: ['pd'] }
],
description: 'Search for unused components in your Ember project',
run(commandOptions) {
components.searchForUnusedComponents(commandOptions);
}
};
  • name — it will be used as a command you put after ember, e.g. ember nice if name: 'nice'
  • aliases — just a list of aliases for your long command
  • availableOptions — these are the arguments you will allow the developer to pass with the command like ember nice --what=everything. Like you see we set the type, can set the default value and even aliases
  • description — keep it simple. It will be shown when someone uses ember help command:
Addon description via ember help command
  • run — this function is fired when the command was used. I recommend to again use a different file for that and keep commands directory tidy.

Run

Last thing worth mentioning is that run function receives commandOptions argument which is an object with arguments passed with the command (remember availableOptions?).

--

--

I connect humans and machines. Usually write about interfaces, digital products, and UX on tomekdev.com. Founder of checknlearn.com. A bit nerdy 🤓

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tomek Niezurawski

Tomek Niezurawski

30 Followers

I connect humans and machines. Usually write about interfaces, digital products, and UX on tomekdev.com. Founder of checknlearn.com. A bit nerdy 🤓