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 - dearmor
- CPPFLAGS - CPPFLAGS
- cpprc - cpprc
- untildified
- Cpython - Cpython
- DCMAKE - DCMAKE
- deps - 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 --> <!-- INSERT GENERATED DOCS START -->
### `userHomeDir` (function)
**returns:** string
### `untildifyUser` (function) ### `untildifyUser` (function)
Replaces a tilde with the user's home directory
**Parameters:** **Parameters:**
- path (`string`) - path (`string`) - The path to untildify
**returns:** any **returns:** string
```tsx
UntildifyUser("~/foo") // /home/user/foo
```
<!-- INSERT GENERATED DOCS END --> <!-- INSERT GENERATED DOCS END -->

View File

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

View File

@ -1,16 +1,39 @@
import { join } from "path" import { join } from "path"
import untildify from "untildify"
import { isSudo } from "admina" import { isSudo } from "admina"
import { homedir } from "os"
export function untildifyUser(path: string) { export function userHomeDir() {
if (isSudo() && typeof process.env.SUDO_USER === "string") { if (isSudo() && typeof process.env.SUDO_USER === "string" && process.env.SUDO_USER !== "") {
// use the user profile even if root // use the user profile even if root
if (process.platform === "darwin") { if (process.platform === "darwin") {
return join("/Users/", process.env.SUDO_USER, path) return join("/Users/", process.env.SUDO_USER)
} else { } else {
return join("/home/", process.env.SUDO_USER, path) return join("/home/", process.env.SUDO_USER)
} }
} else { } 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) { if (cmake === null) {
const { binDir } = await setupCmake( const { binDir } = await setupCmake(
getVersion("cmake", undefined, await ubuntuVersion()), getVersion("cmake", undefined, await ubuntuVersion()),
join(untildifyUser(""), "cmake"), join(untildifyUser("~"), "cmake"),
"", "",
) )
cmake = join(binDir, "cmake") cmake = join(binDir, "cmake")
} }
const ninja = which.sync("ninja", { nothrow: true }) const ninja = which.sync("ninja", { nothrow: true })
if (ninja === null) { 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 return cmake
} }

View File

@ -39,7 +39,7 @@ async function main(args: string[]): Promise<number> {
const arch = opts.architecture ?? process.arch const arch = opts.architecture ?? process.arch
// the installation dir for the tools that are downloaded directly // 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 // report messages
const successMessages: string[] = [] 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) { async function addEnvSystem(name: string, valGiven: string | undefined, options: AddEnvOptions) {
const val = valGiven ?? "" const val = valGiven ?? ""
@ -188,12 +188,12 @@ export async function setupCppInProfile() {
try { try {
// source cpprc in .profile // source cpprc in .profile
const profile_path = untildifyUser(".profile") const profile_path = untildifyUser("~/.profile")
appendFileSync(profile_path, source_cpprc_string) appendFileSync(profile_path, source_cpprc_string)
info(`${source_cpprc_string} was added to ${profile_path}`) info(`${source_cpprc_string} was added to ${profile_path}`)
// source cpprc in .bashrc too // source cpprc in .bashrc too
const bashrc_path = untildifyUser(".bashrc") const bashrc_path = untildifyUser("~/.bashrc")
appendFileSync(bashrc_path, source_cpprc_string) appendFileSync(bashrc_path, source_cpprc_string)
info(`${source_cpprc_string} was added to ${bashrc_path}`) info(`${source_cpprc_string} was added to ${bashrc_path}`)
} catch (err) { } catch (err) {