fix: update bashrc even if cpprc exists already

This commit is contained in:
Amin Yahyaabadi 2024-04-03 00:29:44 -07:00
parent f1bbf95a2d
commit a4e6fc40b5
7 changed files with 45 additions and 35 deletions

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

@ -5,16 +5,16 @@ import { error, warning } from "ci-log"
import escapeSpace from "escape-path-with-spaces" import escapeSpace from "escape-path-with-spaces"
import escapeQuote from "escape-quotes" import escapeQuote from "escape-quotes"
import { execPowershell } from "exec-powershell" import { execPowershell } from "exec-powershell"
import { appendFileSync, readFileSync, writeFileSync } from "fs" import { appendFileSync, readFile, readFileSync, writeFileSync } from "fs"
import { delimiter } from "path" import { delimiter } from "path"
import { pathExists } from "path-exists" import { pathExists } from "path-exists"
import { untildifyUser } from "untildify-user" import { untildifyUser } from "untildify-user"
type AddEnvOptions = { type AddEnvOptions = {
/** If true, the value will be escaped with quotes and spaces will be escaped with backslash */ /** If true, the value will be escaped with quotes and spaces will be escaped with backslash */
shouldEscapeSpace?: boolean shouldEscapeSpace: boolean
/** If true, the variable will be only added if it is not defined */ /** If true, the variable will be only added if it is not defined */
shouldAddOnlyIfNotDefined?: boolean shouldAddOnlyIfNotDefined: boolean
} }
const defaultAddEnvOptions: AddEnvOptions = { const defaultAddEnvOptions: AddEnvOptions = {
@ -30,8 +30,10 @@ const defaultAddEnvOptions: AddEnvOptions = {
export async function addEnv( export async function addEnv(
name: string, name: string,
valGiven: string | undefined, valGiven: string | undefined,
options: AddEnvOptions = defaultAddEnvOptions, givenOptions: Partial<AddEnvOptions> = defaultAddEnvOptions,
) { ) {
const options = { ...defaultAddEnvOptions, ...givenOptions }
const val = escapeString(valGiven ?? "", options.shouldEscapeSpace) const val = escapeString(valGiven ?? "", options.shouldEscapeSpace)
try { try {
if (GITHUB_ACTIONS) { if (GITHUB_ACTIONS) {
@ -173,14 +175,12 @@ export async function setupCppInProfile() {
if (await pathExists(cpprc_path)) { if (await pathExists(cpprc_path)) {
const cpprc_content = readFileSync(cpprc_path, "utf8") const cpprc_content = readFileSync(cpprc_path, "utf8")
if (cpprc_content.includes(source_cpprc_str)) { if (!cpprc_content.includes(source_cpprc_str)) {
// already executed setupCppInProfile // already executed setupCppInProfile
return
}
}
appendFileSync(cpprc_path, `\n${source_cpprc_str}\n`) appendFileSync(cpprc_path, `\n${source_cpprc_str}\n`)
info(`Added ${source_cpprc_str} to ${cpprc_path}`) info(`Added ${source_cpprc_str} to ${cpprc_path}`)
}
}
// source cpprc in bashrc/profile // source cpprc in bashrc/profile
@ -190,13 +190,23 @@ export async function setupCppInProfile() {
try { try {
// source cpprc in .profile // source cpprc in .profile
const profile_path = untildifyUser("~/.profile") const profile_path = untildifyUser("~/.profile")
if (await pathExists(profile_path)) {
const profileContent = readFileSync(profile_path, "utf-8")
if (!profileContent.includes(source_cpprc_string)) {
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")
if (await pathExists(bashrc_path)) {
const bashrcContent = readFileSync(bashrc_path, "utf-8")
if (!bashrcContent.includes(source_cpprc_string)) {
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) {
warning(`Failed to add ${source_cpprc_string} to .profile or .bashrc. You should add it manually: ${err}`) warning(`Failed to add ${source_cpprc_string} to .profile or .bashrc. You should add it manually: ${err}`)
} }