Skip to content

Commit

Permalink
add requested changes to cache config, as well as namespace node incl…
Browse files Browse the repository at this point in the history
…udes and move dependancy
  • Loading branch information
pacotedev committed Jun 28, 2024
1 parent 5f58104 commit fcd6c9f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
47 changes: 25 additions & 22 deletions lib/opts.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
const fs = require('fs')
const os = require('os')
const path = require('path')
const fs = require('node:fs')
const os = require('node:os')
const path = require('node:path')
const ini = require('ini')

const gitConfigPath = path.join(os.homedir(), '.gitconfig')

// Function to check if sshCommand is set in the git config
const isGitSshCommandSetInConfig = () => {
try {
if (fs.existsSync(gitConfigPath)) {
const config = ini.parse(fs.readFileSync(gitConfigPath, 'utf-8'))
return config.core && config.core.sshCommand !== undefined
let cachedConfig = null

// Function to load and cache the git config
const loadGitConfig = () => {
if (cachedConfig === null) {
try {
if (fs.existsSync(gitConfigPath)) {
const configContent = fs.readFileSync(gitConfigPath, 'utf-8')
cachedConfig = ini.parse(configContent)
} else {
cachedConfig = {}
}
} catch (error) {
cachedConfig = {}
}
} catch (error) {
return false
}
return false
return cachedConfig
}

const isGitSshCommandSetInConfig = () => {
const config = loadGitConfig()
return config.core && config.core.sshCommand !== undefined
}

// Function to check if askpass is set in the git config
const isGitAskPassSetInConfig = () => {
try {
if (fs.existsSync(gitConfigPath)) {
const config = ini.parse(fs.readFileSync(gitConfigPath, 'utf-8'))
return config.core && config.core.askpass !== undefined
}
} catch (error) {
return false
}
return false
const config = loadGitConfig()
return config.core && config.core.askpass !== undefined
}

module.exports = (opts = {}) => {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"@npmcli/template-oss": "4.22.0",
"npm-package-arg": "^11.0.0",
"slash": "^3.0.0",
"tap": "^16.0.1"
"tap": "^16.0.1",
"proxyquire": "^2.1.3"
},
"dependencies": {
"@npmcli/promise-spawn": "^7.0.0",
Expand All @@ -44,7 +45,6 @@
"proc-log": "^4.0.0",
"promise-inflight": "^1.0.1",
"promise-retry": "^2.0.1",
"proxyquire": "^2.1.3",
"semver": "^7.3.5",
"which": "^4.0.0"
},
Expand Down
8 changes: 4 additions & 4 deletions test/opts.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const t = require('tap')
const proxyquire = require('proxyquire')
const gitOpts = require('../lib/opts.js')
const fs = require('fs')
const os = require('os')
const path = require('path')
const fs = require('node:fs')
const os = require('node:os')
const path = require('node:path')

const gitConfigPath = path.join(os.homedir(), '.gitconfig')

Expand Down Expand Up @@ -44,7 +44,7 @@ t.test('handle case when fs.existsSync throws an error', t => {

// Mocking fs.existsSync to throw an error
const gitOptsWithMockFs = proxyquire('../lib/opts.js', {
fs: {
'node:fs': {
...mockFs,
existsSync: () => {
throw new Error('Mocked error')
Expand Down

0 comments on commit fcd6c9f

Please sign in to comment.