Skip to content

Commit b2995f6

Browse files
committed
Add utility to change the working directory
1 parent 4d26162 commit b2995f6

13 files changed

Lines changed: 649 additions & 0 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# chdir
2+
3+
> Change the current working directory.
4+
5+
<section class="usage">
6+
7+
## Usage
8+
9+
```javascript
10+
var chdir = require( '@stdlib/process/chdir' );
11+
```
12+
13+
#### chdir( path )
14+
15+
Changes the current working directory to the specified `path`.
16+
17+
<!-- run-disable -->
18+
19+
```javascript
20+
var err = chdir( '/foo/bar' );
21+
```
22+
23+
If the function encounters an error when attempting to change the working directory, the function returns an `error`; otherwise, the function returns `null`.
24+
25+
</section>
26+
27+
<!-- /.usage -->
28+
29+
<section class="examples">
30+
31+
## Examples
32+
33+
<!-- eslint no-undef: "error" -->
34+
35+
```javascript
36+
var cwd = require( '@stdlib/process/cwd' );
37+
var chdir = require( '@stdlib/process/chdir' );
38+
39+
// Print the current working directory:
40+
var dir = cwd();
41+
console.log( dir );
42+
43+
// Change the current working directory to the directory of this file:
44+
chdir( __dirname );
45+
46+
// Print the current working directory:
47+
console.log( cwd() );
48+
49+
// Change the current working directory back to the original directory:
50+
chdir( dir );
51+
52+
// Print the current working directory:
53+
console.log( cwd() );
54+
```
55+
56+
</section>
57+
58+
<!-- /.examples -->
59+
60+
* * *
61+
62+
<section class="cli">
63+
64+
## CLI
65+
66+
<section class="usage">
67+
68+
### Usage
69+
70+
```text
71+
Usage: chdir [options] <path>
72+
73+
Options:
74+
75+
-h, --help Print this message.
76+
-V, --version Print the package version.
77+
```
78+
79+
</section>
80+
81+
<!-- /.usage -->
82+
83+
<section class="examples">
84+
85+
### Examples
86+
87+
<!-- TODO: consider enabling if have the ability to indicate whether a command will intentionally fail -->
88+
89+
<!-- run-disable -->
90+
91+
```bash
92+
$ chdir /foo/bar/beep/boop
93+
```
94+
95+
</section>
96+
97+
<!-- /.examples -->
98+
99+
</section>
100+
101+
<!-- /.cli -->
102+
103+
<section class="links">
104+
105+
</section>
106+
107+
<!-- /.links -->
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var bench = require( '@stdlib/bench' );
6+
var cwd = require( '@stdlib/process/cwd' );
7+
var pkg = require( './../package.json' ).name;
8+
var chdir = require( './../lib' );
9+
10+
11+
// VARIABLES //
12+
13+
var dir = cwd();
14+
15+
16+
// MAIN //
17+
18+
bench( pkg, function benchmark( b ) {
19+
var i;
20+
21+
b.tic();
22+
for ( i = 0; i < b.iterations; i++ ) {
23+
chdir( __dirname );
24+
if ( cwd() !== __dirname ) {
25+
b.fail( 'should set the working directory' );
26+
}
27+
// Reset the working directory to prevent side-effects:
28+
chdir( dir );
29+
}
30+
b.toc();
31+
if ( cwd() !== dir ) {
32+
b.fail( 'should reset the working directory' );
33+
}
34+
b.pass( 'benchmark finished' );
35+
b.end();
36+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env node
2+
'use strict';
3+
4+
// MODULES //
5+
6+
var resolve = require( 'path' ).resolve;
7+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
8+
var CLI = require( '@stdlib/tools/cli' );
9+
var chdir = require( './../lib' );
10+
11+
12+
// MAIN //
13+
14+
/**
15+
* Main execution sequence.
16+
*
17+
* @private
18+
* @returns {void}
19+
*/
20+
function main() {
21+
var args;
22+
var cli;
23+
var err;
24+
25+
// Create a command-line interface:
26+
cli = new CLI({
27+
'pkg': require( './../package.json' ),
28+
'options': require( './../etc/cli_opts.json' ),
29+
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
30+
'encoding': 'utf8'
31+
})
32+
});
33+
34+
// Get provided command-line arguments:
35+
args = cli.args();
36+
37+
err = chdir( args[ 0 ] );
38+
if ( err ) {
39+
process.exitCode = 1;
40+
return console.error( 'Error: %s', err.message ); // eslint-disable-line no-console
41+
}
42+
} // end FUNCTION main()
43+
44+
main();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
{{alias}}( path )
3+
Changes the current working directory.
4+
5+
If unable to set the current working directory (e.g., due to a non-existent
6+
path), the function returns an error; otherwise, the function returns
7+
`null`.
8+
9+
Parameters
10+
----------
11+
path: string
12+
Desired working directory.
13+
14+
Returns
15+
-------
16+
err: Error|null
17+
Error object or null.
18+
19+
Examples
20+
--------
21+
> var err = {{alias}}( '/path/to/current/working/directory' )
22+
23+
See Also
24+
--------
25+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
Usage: chdir [options] <path>
3+
4+
Options:
5+
6+
-h, --help Print this message.
7+
-V, --version Print the package version.
8+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"boolean": [
3+
"help",
4+
"version"
5+
],
6+
"alias": {
7+
"help": [
8+
"h"
9+
],
10+
"version": [
11+
"V"
12+
]
13+
}
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
var cwd = require( '@stdlib/process/cwd' );
4+
var chdir = require( './../lib' );
5+
6+
// Print the current working directory:
7+
var dir = cwd();
8+
console.log( dir );
9+
10+
// Change the current working directory to the directory of this file:
11+
chdir( __dirname );
12+
13+
// Print the current working directory:
14+
console.log( cwd() );
15+
16+
// Change the current working directory back to the original directory:
17+
chdir( dir );
18+
19+
// Print the current working directory:
20+
console.log( cwd() );
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
/**
4+
* Change the current working directory.
5+
*
6+
* @module @stdlib/process/chdir
7+
*
8+
* @example
9+
* var chdir = require( '@stdlib/process/chdir' );
10+
*
11+
* var err = chdir( __dirname );
12+
* if ( err ) {
13+
* throw err;
14+
* }
15+
*/
16+
17+
// MODULES //
18+
19+
var chdir = require( './main.js' );
20+
21+
22+
// EXPORTS //
23+
24+
module.exports = chdir;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
// MAIN //
4+
5+
/**
6+
* Changes the current working directory.
7+
*
8+
* @param {string} path - desired working directory
9+
* @returns {(Error|null)} error object
10+
*
11+
* @example
12+
* var err = chdir( __dirname );
13+
* if ( err ) {
14+
* throw err;
15+
* }
16+
*/
17+
function chdir( path ) {
18+
try {
19+
process.chdir( path );
20+
} catch ( err ) {
21+
return err;
22+
}
23+
return null;
24+
} // end FUNCTION chdir()
25+
26+
27+
// EXPORTS //
28+
29+
module.exports = chdir;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"name": "@stdlib/process/chdir",
3+
"version": "0.0.0",
4+
"description": "Change the current working directory.",
5+
"license": "Apache-2.0",
6+
"author": {
7+
"name": "The Stdlib Authors",
8+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9+
},
10+
"contributors": [
11+
{
12+
"name": "The Stdlib Authors",
13+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14+
}
15+
],
16+
"bin": {
17+
"chdir": "./bin/cli"
18+
},
19+
"main": "./lib",
20+
"directories": {
21+
"benchmark": "./benchmark",
22+
"bin": "./bin",
23+
"doc": "./docs",
24+
"example": "./examples",
25+
"lib": "./lib",
26+
"test": "./test"
27+
},
28+
"scripts": {},
29+
"homepage": "https://github.com/stdlib-js/stdlib",
30+
"repository": {
31+
"type": "git",
32+
"url": "git://github.com/stdlib-js/stdlib.git"
33+
},
34+
"bugs": {
35+
"url": "https://github.com/stdlib-js/stdlib/issues"
36+
},
37+
"dependencies": {},
38+
"devDependencies": {},
39+
"engines": {
40+
"node": ">=0.10.0",
41+
"npm": ">2.7.0"
42+
},
43+
"os": [
44+
"aix",
45+
"darwin",
46+
"freebsd",
47+
"linux",
48+
"macos",
49+
"openbsd",
50+
"sunos",
51+
"win32",
52+
"windows"
53+
],
54+
"keywords": [
55+
"stdlib",
56+
"stdutils",
57+
"stdutil",
58+
"utilities",
59+
"utility",
60+
"utils",
61+
"util",
62+
"working",
63+
"directory",
64+
"process",
65+
"dir",
66+
"cwd",
67+
"pwd",
68+
"path",
69+
"chdir"
70+
]
71+
}

0 commit comments

Comments
 (0)