Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Using ShellJS Plugins

Alex Rattray edited this pageMay 9, 2021 ·13 revisions

Using an existing plugin (list)

Using ShellJS plugins is very easy! To useshelljs-plugin-foo, follow these steps:

Save the package

$ npm install --save shelljs$ npm install --save shelljs-plugin-foo

Load the plugin

// index.js// Require all ShellJS plugins firstrequire('shelljs-plugin-foo');// Require ShellJS itselfvarshell=require('shelljs');// 'shelljs/global' is allowed instead// Require any other packages you likevarret=shell.foo();// this function works!shell.echo(ret.stdout);// most plugins return a ShellString, just like ShellJS commands!shell.foo().grep('o').sed(/o/g,'a');// most plugins will be fully pipe-able

Writing a plugin

Disclaimer: this API is not yet stable, and it's likely to change a bit

Naming

We recommend the following format:shelljs-plugin-<your command name>. If you're writing agit plugin, you'd name itshelljs-plugin-git.

package.json

The most important thing is to require the most recent version of ShellJS as apeer-dependency. If you want to add unit tests for your plugin as well, you'll probably want it as adev-dependency too:

{"name":"shelljs-plugin-foo","version":"0.1.0","description":"A foo plugin for ShellJS","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"keywords":["shelljs","plugin","foo"],"author":"Joe Smith","license":"MIT","devDependencies":{"shelljs":"^0.7.3"},"peerDependencies":{"shelljs":"^0.7.3"}}

Boilerplate

Create a file namedindex.js and start hacking away! You'll need to implement your command as a JavaScript function, and then register that command with ShellJS. Here's an example:

// This exposes the plugin utilitiesvarplugin=require('shelljs/plugin');// Require whatever modules you need for your project here// Implement your command in a function, which accepts `options` as the// first parameter, and other arguments after thatfunctionfooImplementation(options,fileName){return'Hello world';}// Register the new plugin as a ShellJS commandplugin.register('foo',fooImplementation,{cmdOptions:{},// There are no supported options for this command});// Optionally, you can export the implementation of the command like so:exports.foo=fooImplementation;

plugin.register() options

plugin.register() can take a few different options. In the example above, we use thecmdOptions attribute. For the list of available options, check outthe source code (note: this is subject to change until the API is officially released).

Look atthis file to see some options in action.

TODO: once the API stabilizes, we'll add an actual table of options, defaults, and their meanings.

OptionTypeDefaultMeaning
allowGlobbingBooleantrueShould arguments be automatically glob-expanded?
canReceivePipeBooleanfalseCan this command handle piped input?
cmdOptionsObject/nullnullAn object specifying the command's allowed options. Ifnull, no option parsing will happen (the options string will be passed bare to your command). If{}, then no options will be allowed (and the first parameter your command receives will always be{}). Otherwise, the options-string will be parsed such thatcmdOptions: { '-f': 'foo' } will result in your function receiving{ foo: true } or{ foo: false }, depending on if-f was specified.
globStartNumber (int)1The first argument to glob-expand (all arguments following this will also be glob-expanded)
pipeOnlyBooleanfalseShould this commandonly be allowed on the right-hand-side of pipes (e.g.tr)?
wrapOutputBooleantrueShould output be wrapped as aShellString() object?

README badge

You should now have a functioning shelljs-plugin! Feel free to use the following README badge:

shelljs-plugin

[![shelljs-plugin](https://img.shields.io/badge/shelljs-plugin-brightgreen.svg?style=flat-square)](https://github.com/shelljs/shelljs/wiki/Using-ShellJS-Plugins)

Also, feel free to add your plugin to thelist of available plugins.

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp