0.3.4 • Published 2 years ago

shx v0.3.4

Weekly downloads
171,976
License
MIT
Repository
github
Last release
2 years ago

Shx

Build Status Codecov npm version npm downloads

shx is a wrapper around ShellJS Unix commands, providing an easy solution for simple Unix-like, cross-platform commands in npm package scripts.

shx is proudly tested on every node release since v6!

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 using shx in your package.json scripts.

Usage

Command Line

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

$ shx pwd                       # ShellJS commands are supported automatically
/home/username/path/to/dir

$ shx ls                        # files are outputted one per line
file.txt
file2.txt

$ shx rm *.txt                  # a cross-platform way to delete files!

$ shx ls

$ shx echo "Hi there!"
Hi there!

$ shx touch helloworld.txt

$ shx cp helloworld.txt foobar.txt

$ shx mkdir sub

$ shx ls
foobar.txt
helloworld.txt
sub

$ shx rm -r sub                 # options work as well

$ shx --silent ls fakeFileName  # silence error output

All commands internally call the ShellJS corresponding function, guaranteeing cross-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 inside package.json:

{
  "scripts": {
    "clean": "shx rm -rf build dist && shx echo Done"
  }
}

Tip: because Windows treats single quotes (ex. 'some string') differently than double quotes, we recommend wrapping your arguments in double quotes for cross platform compatibility (ex. "some string").

Command reference

Shx exposes most ShellJS commands. If a command is not listed here, assume it's supported!

sed

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

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

would turn into the following Shx command:

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

Note: like unix sed, shx sed treats / as a special character, and this must be escaped (as \/ in the shell, or \\/ in package.json) if you intend to use this character in either the regex or replacement string. Do not escape / characters in the file path.

Unsupported Commands

As mentioned above, most ShellJS commands are supported in ShellJS. Due to the differences in execution environments between ShellJS and shx (JS vs CLI) the following commands are not supported:

Unsupported commandRecommend workaround
shx cdJust use plain old cd (it's the same on windows too)
shx pushdJust use plain old pushd. Use forward slashes and double-quote the path. (e.g. pushd "../docs". This would fail on Windows without the quotes)
shx popdJust use plain old popd
shx dirsNo workaround
shx setSee below
shx exitJust use plain old exit
shx execInstead of shx exec cmd, just use plain old cmd
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 of supported 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

Team

Nate FischerAri PoradLevi Thomason
Nate FischerAri PoradLevi Thomason
lozita@powerkernel/power-sls-libs@citeright/mixpanel@actum-digital/aria-hookscreate-io-scriptcreate-ali-feastrajs-transactions@gotabit/giojs@gotabit/provider@gotabit/transactionsdev-tavaasa-address-converterasa-astrajsasa-eip712asa-protoasa-providerasa-transactionshyas-modulemasternpm-test@infinitebrahmanuniverse/nolb-shx@vestify/dev-kitportalizeprojen@everything-registry/sub-chunk-2756vue-vault-provider-v2@api-helper/cli@aegis.inc/pont-engine-plus@afraj-attar/vite-react-three.js-typescript-mui-template-generator@althea-net/address-converter@althea-net/eip712@althea-net/proto@althea-net/provider@althea-net/transactions@ably/zeromq@cuyo-proyects/shared-assets@d-vc/mailer@dojotrading/sdk-ts@deltalabs/eos-transit-ledger-provider@dbmx/auth@dbmx/mom@dbmx/rpc@dbmx/sdmx@dbmx/semver@dbmx/spaces@dbmx/users@davidterpay/canto-proto-update@davidterpay/canto-transaction-update@davidterpay/canto-transactions-update@e2y/bdd-initializer@elrondnetwork/dapp-core@evmos/eip712@evmos/proto@evmos/provider@evmos/transactions@evmos/address-converter@00a/cli@deepgram/cli@coolcore/development@hyas/cliesfc@expertbridge-custom-plugins/plugin-chart-column-chart@generic-mmo/buildtools@koorya/babel-plugin-add-jsx-attribute-width-ideth_bsc_api@kinvolk/headlamp-plugin@liriliri/lslafatbackevmosjsextensive-react-server@labelu/annotation@labelu/components@labelu/utils@lared/lared-node-bootstrapfluentui-svg-icon-sprites@h4v0c/renderer2dzane-mvcyourdltzeromq@astradefi/address-converter@astradefi/astrajs@astradefi/eip712@astradefi/proto@astradefi/provider@astradefi/utils@astradefi/transactions@bcc-code/arango-migrate@beisen/Beaute@beisen/ChaosUI@andieis/test-package@blueking/lesscode-cli@creatajs/address-converter@creatajs/etheros-proto@creatajs/proto@creatajs/transactions@cdk8s-extensions/argo-rollout@bud-tools/kit@buidly/sdk-dapp-with-lythra@brevity-builder/cli@canlooks/adms@canlooks/stage
0.3.4

2 years ago

0.3.3

3 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago