-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcd.ts
More file actions
68 lines (57 loc) · 2.34 KB
/
Copy pathcd.ts
File metadata and controls
68 lines (57 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import Command from '../command';
import type { CommandFunction, NamedArgumentOptions } from '../types';
const help = `cd: cd [-L|[-P [-e]] [-@]] [dir]
Change the shell working directory.
Change the current directory to DIR. The default DIR is the value of the
HOME shell variable.
The variable CDPATH defines the search path for the directory containing
DIR. Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory. If DIR begins
with a slash (/), then CDPATH is not used.
If the directory is not found, and the shell option \`cdable_vars' is set,
the word is assumed to be a variable name. If that variable has a value,
its value is used for DIR.
Options:
-L force symbolic links to be followed: resolve symbolic
links in DIR after processing instances of \`..'
-P use the physical directory structure without following
symbolic links: resolve symbolic links in DIR before
processing instances of \`..'
-e if the -P option is supplied, and the current working
directory cannot be determined successfully, exit with
a non-zero status.
-@ on systems that support it, present a file with extended
attributes as a directory containing the file attributes
The default is to follow symbolic links, as if \`-L' were specified.
\`..' is processed by removing the immediately previous pathname component
back to a slash or the beginning of DIR.
Exit Status:
Returns 0 if the directory is changed, and if $PWD is set successfully when
-P is used; non-zero otherwise.`;
const cd: CommandFunction = ({ command: { positionalArguments: positional, namedArguments: named }, dir, env }) => {
if (named.help) return help;
const requestedPath = positional[0]?.replace('~', env.get('HOME') ?? '/');
if (!requestedPath) {
dir.cwd = env.get('HOME') ?? '/';
return;
}
const absolute = dir.getAbsolutePath(requestedPath);
// Check if path exists
const resolved = dir._locate(absolute);
if (resolved === null || typeof resolved !== 'object')
return `cd: ${requestedPath}: No such file or directory`;
// Navigate to path
dir.cwd = requestedPath;
};
const description = 'Changes current working directory';
const namedArguments: NamedArgumentOptions = [
{
name: 'help',
choices: ['h', 'help']
}
];
export default new Command({
command: cd,
description,
namedArguments
});