fix: require ~ in untildifyUser

This commit is contained in:
Amin Yahyaabadi 2024-01-22 16:23:22 -08:00
parent c85b694b1c
commit 1a3ed1a856
No known key found for this signature in database
GPG Key ID: F52AF77F636088F0
13 changed files with 82 additions and 49 deletions

View File

@ -30,6 +30,7 @@ words:
- dearmor
- CPPFLAGS
- cpprc
- untildified
- Cpython
- DCMAKE
- deps

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -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 -->

View File

@ -11,8 +11,7 @@
"build": "tsc"
},
"dependencies": {
"admina": "1.0.1",
"untildify": "^5.0.0"
"admina": "1.0.1"
},
"keywords": [
"tilde",

View File

@ -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)
}

View File

@ -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
}

View File

@ -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[] = []

View File

@ -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 ?? ""
@ -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) {