forked from sindresorhus/realpath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·57 lines (49 loc) · 1.08 KB
/
cli.js
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
#!/usr/bin/env node
import process from 'node:process';
import path from 'node:path';
import realpath from 'fs.realpath';
import meow from 'meow';
const cli = meow(`
Usage
$ realpath <filepath>
Options
--relative-to=DIR Print the resolved path relative to DIR
--no-symlinks, -s Don't expand symlinks
Example
$ realpath ../unicorn
$ realpath --no-symlinks /tmp/link
$ realpath --relative-to /Users/sindresorhus/dev ../unicorn
`, {
importMeta: import.meta,
flags: {
relativeTo: {
type: 'string',
},
symlinks: {
type: 'boolean',
default: true,
shortFlag: 's',
aliases: ['strip'],
},
},
});
let filePath = cli.input[0];
if (!filePath) {
console.error('Please specify a file path');
process.exit(1);
}
if (cli.flags.symlinks) {
try {
filePath = realpath.realpathSync(filePath);
} catch (error) {
console.error(error.message);
process.exit(1);
}
} else {
filePath = path.resolve(filePath);
}
if (cli.flags.relativeTo) {
const base = path.resolve(cli.flags.relativeTo);
filePath = path.relative(base, filePath);
}
console.log(filePath);