mirror of
https://github.com/shelljs/shelljs.git
synced 2026-01-25 16:07:37 +00:00
Added -q (quiet) option to push, popd, dirs functions. (#777)
* Added `-q` (quiet) option to `push`, `popd`, `dirs` functions. * Added unit tests for pushd/popd quiet mode. * Added tests for `pushd` and `popd` with quiet mode off. * Updated docs for `pushd` and `popd` functions. * Moved preliminary `pushd` commands for `popd` tests before disabling of silent flag.
This commit is contained in:
parent
c889075f78
commit
cea0e58250
@ -201,6 +201,7 @@ Copies files.
|
||||
Available options:
|
||||
|
||||
+ `-n`: Suppresses the normal change of directory when adding directories to the stack, so that only the stack is manipulated.
|
||||
+ `-q`: Supresses output to the console.
|
||||
|
||||
Arguments:
|
||||
|
||||
@ -223,6 +224,7 @@ Save the current directory on the top of the directory stack and then cd to `dir
|
||||
Available options:
|
||||
|
||||
+ `-n`: Suppresses the normal change of directory when removing directories from the stack, so that only the stack is manipulated.
|
||||
+ `-q`: Supresses output to the console.
|
||||
|
||||
Arguments:
|
||||
|
||||
@ -246,6 +248,7 @@ When no arguments are given, popd removes the top directory from the stack and p
|
||||
Available options:
|
||||
|
||||
+ `-c`: Clears the directory stack by deleting all of the elements.
|
||||
+ `-q`: Supresses output to the console.
|
||||
|
||||
Arguments:
|
||||
|
||||
|
||||
18
src/dirs.js
18
src/dirs.js
@ -40,6 +40,7 @@ function _actualDirStack() {
|
||||
//@ Available options:
|
||||
//@
|
||||
//@ + `-n`: Suppresses the normal change of directory when adding directories to the stack, so that only the stack is manipulated.
|
||||
//@ + `-q`: Supresses output to the console.
|
||||
//@
|
||||
//@ Arguments:
|
||||
//@
|
||||
@ -64,6 +65,7 @@ function _pushd(options, dir) {
|
||||
|
||||
options = common.parseOptions(options, {
|
||||
'n': 'no-cd',
|
||||
'q': 'quiet',
|
||||
});
|
||||
|
||||
var dirs = _actualDirStack();
|
||||
@ -95,7 +97,7 @@ function _pushd(options, dir) {
|
||||
}
|
||||
|
||||
_dirStack = dirs;
|
||||
return _dirs('');
|
||||
return _dirs(options.quiet ? '-q' : '');
|
||||
}
|
||||
exports.pushd = _pushd;
|
||||
|
||||
@ -105,6 +107,7 @@ exports.pushd = _pushd;
|
||||
//@ Available options:
|
||||
//@
|
||||
//@ + `-n`: Suppresses the normal change of directory when removing directories from the stack, so that only the stack is manipulated.
|
||||
//@ + `-q`: Supresses output to the console.
|
||||
//@
|
||||
//@ Arguments:
|
||||
//@
|
||||
@ -130,6 +133,7 @@ function _popd(options, index) {
|
||||
|
||||
options = common.parseOptions(options, {
|
||||
'n': 'no-cd',
|
||||
'q': 'quiet',
|
||||
});
|
||||
|
||||
if (!_dirStack.length) {
|
||||
@ -146,7 +150,7 @@ function _popd(options, index) {
|
||||
_cd('', dir);
|
||||
}
|
||||
|
||||
return _dirs('');
|
||||
return _dirs(options.quiet ? '-q' : '');
|
||||
}
|
||||
exports.popd = _popd;
|
||||
|
||||
@ -156,6 +160,7 @@ exports.popd = _popd;
|
||||
//@ Available options:
|
||||
//@
|
||||
//@ + `-c`: Clears the directory stack by deleting all of the elements.
|
||||
//@ + `-q`: Supresses output to the console.
|
||||
//@
|
||||
//@ Arguments:
|
||||
//@
|
||||
@ -173,6 +178,7 @@ function _dirs(options, index) {
|
||||
|
||||
options = common.parseOptions(options, {
|
||||
'c': 'clear',
|
||||
'q': 'quiet',
|
||||
});
|
||||
|
||||
if (options.clear) {
|
||||
@ -189,11 +195,15 @@ function _dirs(options, index) {
|
||||
index = stack.length + index;
|
||||
}
|
||||
|
||||
common.log(stack[index]);
|
||||
if (!options.quiet) {
|
||||
common.log(stack[index]);
|
||||
}
|
||||
return stack[index];
|
||||
}
|
||||
|
||||
common.log(stack.join(' '));
|
||||
if (!options.quiet) {
|
||||
common.log(stack.join(' '));
|
||||
}
|
||||
|
||||
return stack;
|
||||
}
|
||||
|
||||
39
test/popd.js
39
test/popd.js
@ -3,6 +3,7 @@ import path from 'path';
|
||||
import test from 'ava';
|
||||
|
||||
import shell from '..';
|
||||
import mocks from './utils/mocks';
|
||||
|
||||
const rootDir = path.resolve();
|
||||
|
||||
@ -114,3 +115,41 @@ test('Test that rootDir is not stored', t => {
|
||||
shell.popd(); // no more in the stack
|
||||
t.truthy(shell.error());
|
||||
});
|
||||
|
||||
test('quiet mode off', t => {
|
||||
try {
|
||||
shell.pushd('test/resources/pushd');
|
||||
shell.config.silent = false;
|
||||
mocks.init();
|
||||
const trail = shell.popd();
|
||||
const stdout = mocks.stdout();
|
||||
const stderr = mocks.stderr();
|
||||
t.falsy(shell.error());
|
||||
t.is(stdout, '');
|
||||
t.is(stderr, `${rootDir}\n`);
|
||||
t.is(process.cwd(), trail[0]);
|
||||
t.deepEqual(trail, [rootDir]);
|
||||
} finally {
|
||||
shell.config.silent = true;
|
||||
mocks.restore();
|
||||
}
|
||||
});
|
||||
|
||||
test('quiet mode on', t => {
|
||||
try {
|
||||
shell.pushd('test/resources/pushd');
|
||||
shell.config.silent = false;
|
||||
mocks.init();
|
||||
const trail = shell.popd('-q');
|
||||
const stdout = mocks.stdout();
|
||||
const stderr = mocks.stderr();
|
||||
t.falsy(shell.error());
|
||||
t.is(stdout, '');
|
||||
t.is(stderr, '');
|
||||
t.is(process.cwd(), trail[0]);
|
||||
t.deepEqual(trail, [rootDir]);
|
||||
} finally {
|
||||
shell.config.silent = true;
|
||||
mocks.restore();
|
||||
}
|
||||
});
|
||||
|
||||
@ -3,6 +3,7 @@ import path from 'path';
|
||||
import test from 'ava';
|
||||
|
||||
import shell from '..';
|
||||
import mocks from './utils/mocks';
|
||||
|
||||
const rootDir = path.resolve();
|
||||
|
||||
@ -328,3 +329,45 @@ test('Push without arguments invalid when stack is empty', t => {
|
||||
shell.pushd();
|
||||
t.is(shell.error(), 'pushd: no other directory');
|
||||
});
|
||||
|
||||
test('quiet mode off', t => {
|
||||
try {
|
||||
shell.config.silent = false;
|
||||
mocks.init();
|
||||
const trail = shell.pushd('test/resources/pushd');
|
||||
const stdout = mocks.stdout();
|
||||
const stderr = mocks.stderr();
|
||||
t.falsy(shell.error());
|
||||
t.is(stdout, '');
|
||||
t.is(stderr, `${path.resolve(rootDir, 'test/resources/pushd')} ${rootDir}\n`);
|
||||
t.is(process.cwd(), trail[0]);
|
||||
t.deepEqual(trail, [
|
||||
path.resolve(rootDir, 'test/resources/pushd'),
|
||||
rootDir,
|
||||
]);
|
||||
} finally {
|
||||
shell.config.silent = true;
|
||||
mocks.restore();
|
||||
}
|
||||
});
|
||||
|
||||
test('quiet mode on', t => {
|
||||
try {
|
||||
shell.config.silent = false;
|
||||
mocks.init();
|
||||
const trail = shell.pushd('-q', 'test/resources/pushd');
|
||||
const stdout = mocks.stdout();
|
||||
const stderr = mocks.stderr();
|
||||
t.falsy(shell.error());
|
||||
t.is(stdout, '');
|
||||
t.is(stderr, '');
|
||||
t.is(process.cwd(), trail[0]);
|
||||
t.deepEqual(trail, [
|
||||
path.resolve(rootDir, 'test/resources/pushd'),
|
||||
rootDir,
|
||||
]);
|
||||
} finally {
|
||||
shell.config.silent = true;
|
||||
mocks.restore();
|
||||
}
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user