Skip to content

Commit 1a3ed1a

Browse files
committed
fix: require ~ in untildifyUser
1 parent c85b694 commit 1a3ed1a

File tree

13 files changed

+82
-49
lines changed

13 files changed

+82
-49
lines changed

cspell.config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ words:
3030
- dearmor
3131
- CPPFLAGS
3232
- cpprc
33+
- untildified
3334
- Cpython
3435
- DCMAKE
3536
- deps

dist/actions/setup-cpp.js

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/actions/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/modern/setup-cpp.js

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/modern/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/untildify-user/README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,23 @@ npm install --save untildify-user
1818

1919
<!-- INSERT GENERATED DOCS START -->
2020

21+
### `userHomeDir` (function)
22+
23+
**returns:** string
24+
2125
### `untildifyUser` (function)
2226

27+
Replaces a tilde with the user's home directory
28+
2329
**Parameters:**
2430

25-
- path (`string`)
31+
- path (`string`) - The path to untildify
2632

27-
**returns:** any
33+
**returns:** string
34+
35+
```tsx
36+
UntildifyUser("~/foo") // /home/user/foo
37+
```
2838

2939
<!-- INSERT GENERATED DOCS END -->
3040

packages/untildify-user/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
"build": "tsc"
1212
},
1313
"dependencies": {
14-
"admina": "1.0.1",
15-
"untildify": "^5.0.0"
14+
"admina": "1.0.1"
1615
},
1716
"keywords": [
1817
"tilde",

packages/untildify-user/src/index.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
11
import { join } from "path"
2-
import untildify from "untildify"
32
import { isSudo } from "admina"
3+
import { homedir } from "os"
44

5-
export function untildifyUser(path: string) {
6-
if (isSudo() && typeof process.env.SUDO_USER === "string") {
5+
export function userHomeDir() {
6+
if (isSudo() && typeof process.env.SUDO_USER === "string" && process.env.SUDO_USER !== "") {
77
// use the user profile even if root
88
if (process.platform === "darwin") {
9-
return join("/Users/", process.env.SUDO_USER, path)
9+
return join("/Users/", process.env.SUDO_USER)
1010
} else {
11-
return join("/home/", process.env.SUDO_USER, path)
11+
return join("/home/", process.env.SUDO_USER)
1212
}
1313
} else {
14-
return untildify(`~/${path}`)
14+
const maybeHomeDir = homedir()
15+
if (maybeHomeDir === "") {
16+
return undefined
17+
}
18+
return maybeHomeDir
19+
}
20+
}
21+
22+
const tildeRegex = /^~(?=$|\/|\\)/
23+
24+
/**
25+
* Replaces a tilde with the user's home directory
26+
*
27+
* @example UntildifyUser("~/foo") // /home/user/foo
28+
*
29+
* @param path The path to untildify
30+
* @returns The untildified path
31+
*/
32+
export function untildifyUser(path: string) {
33+
const maybeHomeDir = userHomeDir()
34+
if (maybeHomeDir === undefined) {
35+
return path
1536
}
37+
38+
return path.replace(tildeRegex, maybeHomeDir)
1639
}

0 commit comments

Comments
 (0)