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

Portable Shell Commands for Node

License

NotificationsYou must be signed in to change notification settings

shelljs/shx

Repository files navigation

GitHub ActionsCodecovnpm versionnpm downloads

shx is a wrapper aroundShellJS Unixcommands, providing an easy solution for simple Unix-like, cross-platformcommands in npm package scripts.

shx is proudly tested on every LTS node release sincev20!

Difference Between ShellJS and shx

  • ShellJS: Good for writing long scripts, all in JS, running via NodeJS (e.g.node myScript.js).
  • shx: Good for writing one-off commands in npm package scripts (e.g."clean": "shx rm -rf out/").

Install

npm install shx --save-dev

This will allow usingshx in yourpackage.json scripts.

Usage

Command Line

If you'd like to useshx on the command line, install it globally with the-g flag.The following code can be runeither a Unix or Windows command line:

$ shxpwd# ShellJS commands are supported automatically/home/username/path/to/dir$ shx ls# files are outputted one per linefile.txtfile2.txt$ shx rm*.txt# a cross-platform way to delete files!$ shx ls$ shxecho"Hi there!"Hi there!$ shx touch helloworld.txt$ shx cp helloworld.txt foobar.txt$ shx mkdir sub$ shx lsfoobar.txthelloworld.txtsub$ shx rm -r sub# options work as well$ shx --silent ls fakeFileName# silence error output$ shx --negatetest -d dir# Negate status code output (e.g., failed commands will now have status 0)

All commands internally call the ShellJS corresponding function, guaranteeingcross-platform compatibility.

package.json

ShellJS is good for writing long scripts. If you want to write bash-like,platform-independent scripts, we recommend you go with that.

However,shx is ideal for one-liners insidepackage.json:

{"scripts": {"clean":"shx rm -rf\"build/**/*.js\"\"build/output\" && shx echo\"Done cleaning\""  }}

It's safe to use&& and|| operators in npm package scripts. These will beinterpreted by the operating system's shell (sh on Unix,cmd.exe onWindows). If you're using glob operators like* or**, then we recommend toput these in double quotes, which ensures thatshx will expand the globrather than the operating system shell.

Important

Windows treats single quotes (ex.'some string') differently than doublequotes.We recommendwrapping your arguments inescaped double quotes so that your code iscompatible cross platform (ex."clean": "shx echo \"some string\"").

Command reference

Shx exposesmost ShellJScommands. If a command isnot listed here, assume it's supported!

sed

Shx provides unix-like syntax on top ofshell.sed(). So ShellJS code like:

shell.sed('-i',/originalstring/g,'replacement','filename.txt');

would turn into the following Shx command:

shx sed -i"s/original string/replacement/g" filename.txt

Note: like unixsed,shx sed treats/ as a special character, andthis must beescaped (as\/ in the shell, or\\/ inpackage.json) if you intend to use thischaracter in either the regex or replacement string. Donot escape/characters in the file path.

Unsupported Commands

As mentioned above, most ShellJS commands are supported inshx. Due to thedifferences in execution environments between ShellJS andshx (JS vs CLI) thefollowing commands are not supported:

Unsupported commandRecommend workaround
shx cdJust use plain oldcd (it's the same on windows too)
shx pushdJust use plain oldpushd. Use forward slashes and double-quote the path. (e.g.pushd "../docs". This would fail on Windows without the quotes)
shx popdJust use plain oldpopd
shx dirsNo workaround
shx setSee below
shx exitJust use plain oldexit
shx execInstead ofshx exec cmd, just use plain oldcmd
shx ShellStringNo workaround (but why would you want this?)

Shx options

Shx allows you to modify its behavior by passing arguments. Here's a list ofsupported options:

set flagshell.config settingshx commandEffect
-econfig.fatal = trueNot supportedExit upon first error.
-vconfig.verbose = trueshx --verbose cd fooLog the command as it's run.
-fconfig.noglob = trueshx --noglob cat '*.txt'Don't expand wildcards.
N/Aconfig.silent = trueshx --silent cd noexistDon't show error output.
N/AN/Ashx --negate test -d dirRuns the specified command but negates the exit status. Failed command = status 0, successful command = status 1.
N/AN/Ashx --helpShow help text.
N/AN/Ashx --versionPrint the shx version.

Plugin support

Did you know thatshx can also take advantage of all the greatShellJS plugins?SeePLUGINS.md for instructions.

Team

Nate FischerAri PoradLevi Thomason
Nate FischerAri PoradLevi Thomason

About

Portable Shell Commands for Node

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp