345 Commits

Author SHA1 Message Date
Nate Fischer
964fb48e9e test: check for README changes in the unit tests
No change to logic. This checks for stale README in the unit tests
instead of by calling 'gendocs' directly.

The main goal is for GitHub CI to stop getting tripped up by
package-lock.json formatting changes. `npm install` sometimes overwrites
the package-lock file with a new format, and this breaks the
after-travis step.

Test: node_modules/.bin/ava test/docs.js
2025-11-18 22:25:05 -08:00
Nate Fischer
9a06336bc1 Deprecate the shjs binary
This removes the shjs binary. This feature was not widely used and it
was buggy. The recommended workaround is to invoke scripts directly with
'node' (you may also need to set your NODE_PATH environmental variable).

Dropping this script lets us trim down the dependency tree.

For those who still need the original script, the source code has moved
to https://github.com/shelljs/shjs.
2025-05-09 00:55:23 -07:00
Nate Fischer
67a26e2ccc refactor: change how internal errors are handled
This is an internal refactor to change how we handle execa errors inside
of shell.cmd().

This also adds a test case for a command which fails silently to make
sure that we handle this case correctly.
2025-04-24 19:48:23 -07:00
Alana Gilston
bfd06fa096
Add -B, -A, and -C options to grep (#1206)
Adds the -B (before context), -A (after context), and -C (before+after context) options to grep. Example usage:

```
grep -B <num> [args...]
grep -A <num> [args...]
```
2025-04-19 08:26:51 -07:00
Nate Fischer
3149e09d5f refactor: explicit handling for execa errors
This adds a dedicated if-condition to handle internal errors from execa
(timeout, maxBuffer, etc.) and to distinguish these from "regular"
errors (the command executed, but it failed with non-zero status).
2025-04-17 13:52:12 -07:00
Nate Fischer
8bc5833828 chore: update ava
This updates ava to v6. This resolves some `npm audit` warnings in our
project's devDependencies. This adds `workerThreads: false` because
operations like `process.chdir()` don't work in workers (but are
required for shelljs).

This is a partial fix #1180, although I found more errors when I ran the
tests locally on v22.10.0.

Test: npm audit
2025-04-10 01:11:39 -07:00
Nate Fischer
29eba085f7 refactor: prefer promises over test.cb
This is a followup to PR #1211 to use promises everywhere and migrate
away from `test.cb()`. The `test.cb()` function is removed in a future
version of ava.

I made an attempt to convert the skipped shell.cmd() async test cases,
but async functionality isn't implemented so it's possible the test
conversion is not totally correct.
2025-04-07 23:52:01 -07:00
Nate Fischer
ffdd60b3b6 refactor: use expectations for t.throws
No change to logic. This is a small refactor to use stricter
"expectation" arguments for calls to t.throws().
2025-04-07 23:29:18 -07:00
Nate Fischer
dbac0a724d refactor: use promises for utils.runScript
No change to logic. This swaps out utils.runScript() to return a Promise
instead of taking a callback. This also moves some of `test.cb()` cases
over to async functions instead.
2025-04-07 22:44:41 -07:00
Nate Fischer
1a31356343 refactor: use require instead of import
No change to logic. This swaps over tests to use require() since
everything is currently designed for the commonjs module system.
2025-04-07 22:44:24 -07:00
Nate Fischer
4fe8897b39 test: add tests for shelljs glob expansion
No change to logic. This adds unit tests for glob expansion.

This includes one test case for behavior which was broken by v0.9.0
(probably due to the switch to fastglob). This behavior hasn't been
fixed yet, so the test is marked as skipped.

Issue #1197
2025-03-08 17:08:14 -08:00
Nate Fischer
ea57f4ee78 refactor: prefer includes() over indexOf()
No change to logic. This swaps out Array.prototype.indexOf in favor for
Array.prototype.includes() to simplify logic. This does the same for the
equivalent String.prototype functions.

Test: npm test
2025-03-08 13:48:44 -08:00
Nate Fischer
f29a7a85af test: document test for newline in cmd() function
No change to logic. This adds documentation for the test case for
handling newlines.

This also removes a nearby eslint suppression which is no longer
necessary after PR #1184.

Fixes #175
2025-02-22 00:48:06 -08:00
Nate Fischer
519308489c refactor: move exec-child logic into main function
No change to logic. This refactors the exec-child.js script to move all
of its main logic into a main() function. This function is only invoked
if the script is executed, not when it is imported.

Importing the script is now a NOOP instead of throwing an exception.
It's still not advisable to import the script, however this can be done
if absolutely necessary to trick JavaScript bundlers which try to prune
non-imported code files.

Partially related to issue #1160 and #1172.
2025-02-20 00:32:19 -08:00
Nate Fischer
65baedbf52 feat: expose new shell.cmd()
shell.cmd() was originally implemented in #866, however was not yet
exposed. This command should be ready for people to try out, so this
exposes the command by default.

This is still not a full implementation of everything in the
https://shelljs.page.link/cmd-design design doc, however this completes
the initial phase and is likely good enough to replace most use cases of
synchronous shell.exec().

Fixes #495
2025-02-20 00:26:35 -08:00
Nate Fischer
03d2f20e27 refactor: unescape quotes
This swaps out quote characters to limit how often we need to escape
strings. This is just to improve code readability.

Almost all of the changes could be done without changes to .eslintrc,
however this amends the lint rule to also permit backtick strings which
can be useful to eliminate a few extra instances of quote escaping.

Fixes issue #788
Test: npm run lint
2025-02-19 22:35:39 -08:00
Keith Mashinter
d9d7f4d211
Add the exit code to the fatal error thrown from common.error(). (#1179)
Merged-by: Nate Fischer <ntfschr@google.com>
2025-02-19 21:41:54 -08:00
Nate Fischer
7e71b2686d chore: drop support for old node versions
Dropping support for everything before node v18, which is the current
maintenance LTS and also the version in Ubuntu 24.04 LTS.

This also updates all devDependencies while trying to minimize the
required source code changes. Some devDependencies can go higher still
(ava, chalk), but this is going to require extensive refactoring.

Test: npm run test-with-coverage
Test: npm run lint
Test: npm run check-node-support
2025-02-17 22:31:10 -08:00
Keith Mashinter
edac78e407
Fix Windows test errors in src/ls.js and test/cp.js. (#1166) 2024-06-22 17:05:47 -07:00
Nate Fischer
c3797a9258 test: add more coverage for globOptions
No change to logic. This adds tests for more `config.globOptions`
values. This aims to cover the options which appear to be supported by
both node-glob and fast-glob, however the options have different names
in each module. This is a followup to PR #1163.

See https://github.com/mrmlnc/fast-glob?tab=readme-ov-file#compatible-with-node-glob

Issue #828
2024-06-14 12:03:33 -07:00
Nate Fischer
d14782914f test: add test coverage for some globOptions
No change to logic. This adds test coverage support for all of the
globOptions which share the same name between node-glob and fast-glob.

See https://github.com/mrmlnc/fast-glob?tab=readme-ov-file#compatible-with-node-glob

Issue #828
2024-06-14 10:55:43 -07:00
Nate Fischer
2ff87eff00 test: create test files inside temp directory
No change to logic. This updates some test cases to create the files
they need inside the temp directory instead of in the repo itself. This
is helpful in case the test case fails early, that way we don't leave
this file behind.

This contributes toward #828, since the change to fast-glob made it
clear that this test was mishandling link files and leaving side effects
in the git repo. However this change is desirably independent of
fast-glob.
2024-02-17 18:07:35 -08:00
Murph Murphy
85dd4729dd
test: make a test more forgiving for systems with non-standard bash and sh paths (#1144)
Made a test
more forgiving so it passes on systems that have non-standard `bash` and
`sh` binary paths.
2023-12-06 22:48:30 -08:00
Andreas Deininger
626973367f
Fixing typos (#1137)
This PR fixes a few typos that I spotted in the project.
2023-11-12 21:06:23 -08:00
Nate Fischer
39ebb71846 test: add coverage for exit function
This adds test coverage for the shell.exit() function. This also
refactors how we mock stdout/stderr and adds support for mocking
process.exit() (which was needed for this change).

While I was writing these tests, I realized there was an edge case I
missed in PR #1122. This change fixes that edge case.

Issue #1013
2023-11-11 18:27:54 -08:00
Marco Perrando
8c926df43a
Added -L to find to visit symlinked folders too. (#1080) 2022-02-24 19:18:29 -08:00
Nate Fischer
4b6b17a635 docs(touch): clarify docs for touch() command
No change to logic. This expands on the `touch()` command documentation
to make it more obvious how to use multiple options, long options, etc.
This also renames a variable in a test case to make the usage more
obvious.
2022-02-13 17:38:11 -08:00
Nate Fischer
9a0e5f6694
Add preserve option to cp (#869)
Co-authored-by: dwi2 <dwi2@dwi2.com>
2022-01-26 22:41:40 -08:00
Nate Fischer
a329b49780 test(cp): fix cp -Ru test cases
This fixes several issues with the cp -Ru test cases:

* Originally, I noticed the touch syntax looked wrong
* After fixing that, I was trying to figure out how the test case was
  even passing before. I realized one of the assertions was wrong (it
  was asserting sourceFile contents, but should have asserted destFile
  contents)
* I realized the test should have created a nested destination
  directory; without proper nesting, the sourceFile never actually
  collided with destFile during the copy
* Lastly, I swapped touch() usage to use hardcoded timestamps for test
  consistency
2022-01-26 20:00:00 -08:00
Andrea Canepa
42754c14ec
Added support for -n +NUM in tail.js (with sign) (#1027) 2022-01-15 13:44:54 -08:00
fhanrath
5af16d5c38
Feature/grep n (#1057) 2022-01-15 13:39:41 -08:00
JessieFrance
403a57cb9f
Feature: Expose Error Code (#1036) 2022-01-15 13:19:47 -08:00
Shubham Joshi
b473f2eab9
sed with -i option now runs silently (#959) 2022-01-15 13:09:02 -08:00
JessieFrance
79ae14d30d
remove file extension (#1033) 2021-06-02 16:23:58 -07:00
JessieFrance
124d3349af
Added mkdir -p tests for subdirectories (#1026) 2021-03-31 17:44:20 -07:00
Nate Fischer
cb64b92439
test(cmd): add test for caret char (#1017)
No change to logic. This adds a test case to make sure the caret
character ('^') is passed through to the command. This is to make sure
the character is not treated specially on Windows, where it's an escape
character in the shell.

Fixes #1015
2021-02-08 09:37:50 -08:00
Nate Fischer
a421b9e3a5
feat: add shell.cmd to replace exec (#866)
This adds an initial implementation of shell.cmd(), which is intended as
the eventual replacement for shell.exec(). This PR does not fully
implement the API, but demonstrates a simple and secure alternative, and
will allow further iteration to cover other use cases in follow-up PRs.

Design doc: https://shelljs.page.link/cmd-design

Issue #495
Test: automated test suite
2019-10-29 18:35:14 -07:00
Nate Fischer
37b42fef25
test: misc test changes (#970)
No change to production logic.

This renames test cases with duplicate names, deletes a redundant test,
and uses the public `t.title` API in favor of `t._test.title`.

This is a prerequisite for updating ava.

Issue #969
2019-10-08 22:41:16 -07:00
Wes Cossick
4e38240eda Add boolean fatal option to exec() function (#961)
This PR introduces a new boolean fatal option for the exec() function. Like the existing silent option, this new option allows you to override the global fatal configuration parameter on a per-command basis.
2019-10-01 23:43:54 -07:00
Nate Fischer
57df38c6ea
chore(lint): update lint dependencies (#948)
This updates `eslint-config-airbnb-base` and `eslint-plugin-import`.
This also addresses lint errors these updates raise, and excludes
several rules.

This also adds a minor simplification to the gendocs script.

Test: npm run lint
Test: npm run gendocs
2019-07-12 12:36:55 -07:00
Nate Fischer
a8e543a8d0
chore(lint): upgrade eslint (#947)
This upgrades eslint to a new version to resolve a security warning.
This fixes a couple new lint errors, and disables some lint checks.

The current version of the airbnb lint config triggers
DeprecationWarnings in the new version of eslint. These warnings will be
resolved in a follow-up.

Test: npm run lint
2019-06-20 23:34:41 -07:00
Nate Fischer
f7dbc61d41
chore(lint): alphabetize lint rules (#946)
No change to the rules, this just reorders the eslintrc file so there's
a sane way to add new rules.

Test: npm run lint
2019-06-20 22:52:47 -07:00
Nate Fischer
1560ffdc5f
feat(options): initial support for long options (#926)
* feat(options): initial support for long options

This adds initial support for long options. This integration tests them
with `shell.touch()` and unit tests them in `test/common.js`. This
documents the new syntax.

This also refactors some of the common internals:

 * Creates a new CommandError type to replace the `'earlyExit'` hack
 * Clearer Error types for `parseOptions()` exceptions
 * Removes side effects from a test which modifies `common.config`
 * Fixes appveyor and travis config to run lint (regressed in #920)

Issue #924
Test: touch.js, common.js

* small refactor for errorOptions
2019-01-05 00:54:30 -08:00
Nate Fischer
5371e1629f
test(touch): add coverage for -d option (#925)
No change to production logic.

We never had coverage for `touch({'-d': ...})`, so this adds test
coverage. This also updates documentation to clarify the parameter
should be an instance of the `Date` type.

Test: `handles date argument` case
2019-01-04 02:02:04 -08:00
Nate Fischer
e606706855
fix(exec): consistent error message for maxBuffer (#919)
* fix(exec): consistent error message for maxBuffer

This explicitly checks for maxBuffer errors in exec-child.js and
provides a consistent error message. This modifies the test to verify
this.

This also supports the change in Node v10, which emits this as a
RangeError instead of a regular Error.

Although the error message is now explicit, this is not part of our API,
since it's just tacked onto the end of exec's stderr.

Fixes #915

* Remove debugging log, ignore uncovered lines
2018-12-02 21:52:54 -08:00
Shubham Joshi
18d8bbf290 feat(cp): support update flag when recursing (#889)
Fixed `cp -Ru` ignoring `-u`, and added a test.

Fixes #808
2018-11-12 21:35:06 -08:00
Will Yardley
5da1dda414 fix: Exit 1 with no output if no match (#900) (#901)
When we can read all files, but none match, exit 1 and return no output. This
matches the behavior of grep more closely.

Fixes #900
2018-11-12 21:34:13 -08:00
Will Yardley
db317bf092 Add test case for sed on empty file (#904)
As discussed as an aside in #900, add test case with an empty file.
2018-11-12 20:46:10 -08:00
Nate Fischer
6b3c7b1e44
refactor: don't expose tempdir in common.state (#903)
Previously, the cached `tempdir` value was stored in `common.state`.
Unlike the other `common.state` values, this isn't immediately useful to
other commands (they can just call the tempdir API). So, this moves the
cached value into `tempdir.js`.

This also adds a unit test for the caching behavior, and exposes
test-only helpers to verify this behavior.

Finally, this adds a note to `common.state` that values should generally
be considered read-only, since this can be important for customized
behavior. Although, I recognize our code base has one exception to this
rule (`echo()`), we should strive to maintain this.

Fixes #902
Test: Added a unit test.
2018-11-08 23:00:22 -08:00
Nate Fischer
2b3b781bbc
fix: silent exec (#892)
Unconditionally apply `silent: true` when calling `common.error()` from
`exec()`. This is because errors are already printed to stderr, or are
intentionally silenced by `shell.config.silent`.

Based on #861 

Fixes #851
2018-10-27 13:51:36 -07:00