Skip to content

Commit a4e6fc4

Browse files
committed
fix: update bashrc even if cpprc exists already
1 parent f1bbf95 commit a4e6fc4

File tree

7 files changed

+45
-35
lines changed

7 files changed

+45
-35
lines changed

dist/actions/setup-cpp.js

Lines changed: 4 additions & 4 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: 4 additions & 4 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: 11 additions & 11 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.

src/utils/env/addEnv.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import { error, warning } from "ci-log"
55
import escapeSpace from "escape-path-with-spaces"
66
import escapeQuote from "escape-quotes"
77
import { execPowershell } from "exec-powershell"
8-
import { appendFileSync, readFileSync, writeFileSync } from "fs"
8+
import { appendFileSync, readFile, readFileSync, writeFileSync } from "fs"
99
import { delimiter } from "path"
1010
import { pathExists } from "path-exists"
1111
import { untildifyUser } from "untildify-user"
1212

1313
type AddEnvOptions = {
1414
/** If true, the value will be escaped with quotes and spaces will be escaped with backslash */
15-
shouldEscapeSpace?: boolean
15+
shouldEscapeSpace: boolean
1616
/** If true, the variable will be only added if it is not defined */
17-
shouldAddOnlyIfNotDefined?: boolean
17+
shouldAddOnlyIfNotDefined: boolean
1818
}
1919

2020
const defaultAddEnvOptions: AddEnvOptions = {
@@ -30,8 +30,10 @@ const defaultAddEnvOptions: AddEnvOptions = {
3030
export async function addEnv(
3131
name: string,
3232
valGiven: string | undefined,
33-
options: AddEnvOptions = defaultAddEnvOptions,
33+
givenOptions: Partial<AddEnvOptions> = defaultAddEnvOptions,
3434
) {
35+
const options = { ...defaultAddEnvOptions, ...givenOptions }
36+
3537
const val = escapeString(valGiven ?? "", options.shouldEscapeSpace)
3638
try {
3739
if (GITHUB_ACTIONS) {
@@ -173,15 +175,13 @@ export async function setupCppInProfile() {
173175

174176
if (await pathExists(cpprc_path)) {
175177
const cpprc_content = readFileSync(cpprc_path, "utf8")
176-
if (cpprc_content.includes(source_cpprc_str)) {
178+
if (!cpprc_content.includes(source_cpprc_str)) {
177179
// already executed setupCppInProfile
178-
return
180+
appendFileSync(cpprc_path, `\n${source_cpprc_str}\n`)
181+
info(`Added ${source_cpprc_str} to ${cpprc_path}`)
179182
}
180183
}
181184

182-
appendFileSync(cpprc_path, `\n${source_cpprc_str}\n`)
183-
info(`Added ${source_cpprc_str} to ${cpprc_path}`)
184-
185185
// source cpprc in bashrc/profile
186186

187187
const source_cpprc_string =
@@ -190,13 +190,23 @@ export async function setupCppInProfile() {
190190
try {
191191
// source cpprc in .profile
192192
const profile_path = untildifyUser("~/.profile")
193-
appendFileSync(profile_path, source_cpprc_string)
194-
info(`${source_cpprc_string} was added to ${profile_path}`)
193+
if (await pathExists(profile_path)) {
194+
const profileContent = readFileSync(profile_path, "utf-8")
195+
if (!profileContent.includes(source_cpprc_string)) {
196+
appendFileSync(profile_path, source_cpprc_string)
197+
info(`${source_cpprc_string} was added to ${profile_path}`)
198+
}
199+
}
195200

196201
// source cpprc in .bashrc too
197202
const bashrc_path = untildifyUser("~/.bashrc")
198-
appendFileSync(bashrc_path, source_cpprc_string)
199-
info(`${source_cpprc_string} was added to ${bashrc_path}`)
203+
if (await pathExists(bashrc_path)) {
204+
const bashrcContent = readFileSync(bashrc_path, "utf-8")
205+
if (!bashrcContent.includes(source_cpprc_string)) {
206+
appendFileSync(bashrc_path, source_cpprc_string)
207+
info(`${source_cpprc_string} was added to ${bashrc_path}`)
208+
}
209+
}
200210
} catch (err) {
201211
warning(`Failed to add ${source_cpprc_string} to .profile or .bashrc. You should add it manually: ${err}`)
202212
}

0 commit comments

Comments
 (0)