-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathgit-credentials.ts
More file actions
36 lines (30 loc) · 992 Bytes
/
git-credentials.ts
File metadata and controls
36 lines (30 loc) · 992 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import process from 'process'
import type BaseCommand from '../base-command.js'
const readStdin = (): Promise<string> =>
new Promise((resolve) => {
let data = ''
process.stdin.setEncoding('utf8')
process.stdin.on('data', (chunk: string) => {
data += chunk
})
process.stdin.on('end', () => {
resolve(data)
})
// If stdin isn't being piped, resolve after a short timeout
if (process.stdin.isTTY) {
resolve(data)
}
})
export const gitCredentials = async (command: BaseCommand) => {
const input = await readStdin()
// Only respond to "get" requests from the git credential protocol
if (!input.includes('protocol=') && !input.startsWith('get')) {
return
}
const token = command.netlify.api.accessToken
if (!token) {
throw new Error('No access token found. Please run `netlify login` first.')
}
// Output in git credential helper format
process.stdout.write(`username=x-access-token\npassword=${token}\n`)
}