on *nix check profile files in case shell PATH is outdated

This commit is contained in:
dmc
2024-01-15 19:40:53 +01:00
parent ef7832de1f
commit d0ed30d7f8

View File

@@ -29,14 +29,10 @@ customElements.define('system-status', class extends HTMLElement {
super()
this.zsh = false
this.bash = false
this.paths = process.env.PATH.split(path.delimiter)
this.installed = this.paths.some((bin) => {
if (isWin === false) {
if (this.zsh === false && fs.existsSync(path.join(bin, 'zsh'))) this.zsh = true
if (this.bash === false && fs.existsSync(path.join(bin, 'bash'))) this.bash = true
}
return bin === BIN && fs.existsSync(path.join(BIN, isWin ? 'pear.cmd' : 'pear'))
})
this.statement = `export PATH="${BIN}":$PATH`
this.stmtrx = new RegExp(`^export PATH="${BIN}":\\$PATH$`, 'm')
this.shellProfiles = null
this.installed = this.#installed()
this.template = document.createElement('template')
this.template.innerHTML = `
<div id="panel">
@@ -82,7 +78,7 @@ customElements.define('system-status', class extends HTMLElement {
if (this.button) {
const listener = () => {
this.button.removeEventListener('click', listener)
this.#installPear()
this.#install()
.then(() => {
this.replaceWith(new this.constructor())
console.log('now show version info, and a gif showing pear command line help output run through')
@@ -97,7 +93,31 @@ customElements.define('system-status', class extends HTMLElement {
console.error(err)
}
#installPear () {
#installed () {
if (isWin === false) {
let hasPear = false
this.shellProfiles = {}
for (const file of ['.zshrc', '.zshenv', '.zshprofile', '.zlogin', '.profile', '.bashrc']) {
const filepath = path.join(os.homedir(), file)
let contents = null
try { contents = fs.readFileSync(filepath, { encoding: 'utf-8' }) } catch {}
if (contents !== null) {
this.shellProfiles[file] = { filepath, hasPear: this.stmtrx.test(contents) }
hasPear = hasPear || this.shellProfiles[file].hasPear
}
console.log('hi', contents, hasPear, this.stmtrx, this.shellProfiles[file])
}
if (hasPear) process.env.PATH = `${BIN}:${process.env.PATH}`
}
this.paths = process.env.PATH.split(path.delimiter)
return this.paths.some((bin) => {
return bin === BIN && fs.existsSync(path.join(BIN, isWin ? 'pear.cmd' : 'pear'))
})
}
#install () {
const runtime = path.join(config.pearDir, 'current', 'by-arch', process.platform + '-' + process.arch, 'bin', 'bare', 'pear')
fs.mkdirSync(BIN, { recursive: true })
if (isWin) {
@@ -127,25 +147,25 @@ customElements.define('system-status', class extends HTMLElement {
})
})
}
const profiles = Object.values(this.shellProfiles)
if (profiles.length > 0) {
for (const { filepath, hasPear } of profiles) {
if (hasPear === false) fs.writeFileSync(filepath, this.statement, { flags: 'a' })
}
} else {
const bash = this.paths.some((bin) => fs.existsSync(path.join(bin, 'bash')))
const zsh = this.paths.some((bin) => fs.existsSync(path.join(bin, 'zsh')))
fs.writeFileSync(path.join(os.homedir(), bash ? '.bashrc' : '.profile'), this.statement + '\n')
if (zsh) fs.writeFileSync(path.join(os.homedir(), '.zshrc'), this.statement + '\n')
}
const pear = path.join(BIN, 'pear')
const tmp = path.join(BIN, Math.floor(Math.random() * 1000) + '.pear')
fs.symlinkSync(runtime, tmp)
fs.renameSync(tmp, pear)
fs.chmodSync(pear, 0o755)
let shellProfileExists = false
const statement = `\nexport PATH="${BIN}":$PATH\n`
for (const file of ['.zshrc', '.zshenv', '.zshprofile', '.zlogin', '.profile', '.bashrc']) {
const filepath = path.join(os.homedir(), file)
if (fs.existsSync(filepath)) {
fs.writeFileSync(filepath, statement, { flag: 'a' })
shellProfileExists = true
}
}
if (shellProfileExists === false) {
fs.writeFileSync(path.join(os.homedir(), this.bash ? '.bashrc' : '.profile'), statement, { flag: 'a' })
if (this.zsh) fs.writeFileSync(path.join(os.homedir(), '.zshrc'), statement, { flag: 'a' })
}
process.env.PATH = `${BIN}:${process.env.PATH}`
return Promise.resolve()
}