Skip to content

Commit

Permalink
fix: require ~ in untildifyUser
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Jan 23, 2024
1 parent c85b694 commit 1a3ed1a
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 49 deletions.
1 change: 1 addition & 0 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ words:
- dearmor
- CPPFLAGS
- cpprc
- untildified
- Cpython
- DCMAKE
- deps
Expand Down
20 changes: 10 additions & 10 deletions dist/actions/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/actions/setup-cpp.js.map

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions dist/legacy/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/legacy/setup-cpp.js.map

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions dist/modern/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/modern/setup-cpp.js.map

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions packages/untildify-user/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,23 @@ npm install --save untildify-user

<!-- INSERT GENERATED DOCS START -->

### `userHomeDir` (function)

**returns:** string

### `untildifyUser` (function)

Replaces a tilde with the user's home directory

**Parameters:**

- path (`string`)
- path (`string`) - The path to untildify

**returns:** any
**returns:** string

```tsx
UntildifyUser("~/foo") // /home/user/foo
```

<!-- INSERT GENERATED DOCS END -->

Expand Down
3 changes: 1 addition & 2 deletions packages/untildify-user/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"build": "tsc"
},
"dependencies": {
"admina": "1.0.1",
"untildify": "^5.0.0"
"admina": "1.0.1"
},
"keywords": [
"tilde",
Expand Down
35 changes: 29 additions & 6 deletions packages/untildify-user/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
import { join } from "path"
import untildify from "untildify"
import { isSudo } from "admina"
import { homedir } from "os"

export function untildifyUser(path: string) {
if (isSudo() && typeof process.env.SUDO_USER === "string") {
export function userHomeDir() {
if (isSudo() && typeof process.env.SUDO_USER === "string" && process.env.SUDO_USER !== "") {
// use the user profile even if root
if (process.platform === "darwin") {
return join("/Users/", process.env.SUDO_USER, path)
return join("/Users/", process.env.SUDO_USER)
} else {
return join("/home/", process.env.SUDO_USER, path)
return join("/home/", process.env.SUDO_USER)
}
} else {
return untildify(`~/${path}`)
const maybeHomeDir = homedir()
if (maybeHomeDir === "") {
return undefined
}
return maybeHomeDir
}
}

const tildeRegex = /^~(?=$|\/|\\)/

/**
* Replaces a tilde with the user's home directory
*
* @example UntildifyUser("~/foo") // /home/user/foo
*
* @param path The path to untildify
* @returns The untildified path
*/
export function untildifyUser(path: string) {
const maybeHomeDir = userHomeDir()
if (maybeHomeDir === undefined) {
return path
}

return path.replace(tildeRegex, maybeHomeDir)
}
4 changes: 2 additions & 2 deletions src/kcov/kcov.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ async function getCmake() {
if (cmake === null) {
const { binDir } = await setupCmake(
getVersion("cmake", undefined, await ubuntuVersion()),
join(untildifyUser(""), "cmake"),
join(untildifyUser("~"), "cmake"),
"",
)
cmake = join(binDir, "cmake")
}
const ninja = which.sync("ninja", { nothrow: true })
if (ninja === null) {
await setupNinja(getVersion("ninja", undefined, await ubuntuVersion()), join(untildifyUser(""), "ninja"), "")
await setupNinja(getVersion("ninja", undefined, await ubuntuVersion()), join(untildifyUser("~"), "ninja"), "")
}
return cmake
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function main(args: string[]): Promise<number> {
const arch = opts.architecture ?? process.arch

// the installation dir for the tools that are downloaded directly
const setupCppDir = process.env.SETUP_CPP_DIR ?? untildifyUser("")
const setupCppDir = process.env.SETUP_CPP_DIR ?? untildifyUser("~")

// report messages
const successMessages: string[] = []
Expand Down
6 changes: 3 additions & 3 deletions src/utils/env/addEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export async function addPath(path: string) {
}
}

export const cpprc_path = untildifyUser(".cpprc")
export const cpprc_path = untildifyUser("~/.cpprc")

async function addEnvSystem(name: string, valGiven: string | undefined, options: AddEnvOptions) {
const val = valGiven ?? ""
Expand Down Expand Up @@ -188,12 +188,12 @@ export async function setupCppInProfile() {

try {
// source cpprc in .profile
const profile_path = untildifyUser(".profile")
const profile_path = untildifyUser("~/.profile")
appendFileSync(profile_path, source_cpprc_string)
info(`${source_cpprc_string} was added to ${profile_path}`)

// source cpprc in .bashrc too
const bashrc_path = untildifyUser(".bashrc")
const bashrc_path = untildifyUser("~/.bashrc")
appendFileSync(bashrc_path, source_cpprc_string)
info(`${source_cpprc_string} was added to ${bashrc_path}`)
} catch (err) {
Expand Down

0 comments on commit 1a3ed1a

Please sign in to comment.