-
Notifications
You must be signed in to change notification settings - Fork 393
/
Copy pathblobs-get.ts
43 lines (34 loc) · 1.09 KB
/
blobs-get.ts
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
37
38
39
40
41
42
43
import { promises as fs } from 'fs'
import { resolve } from 'path'
import { getStore } from '@netlify/blobs'
import { OptionValues } from 'commander'
import { chalk, logAndThrowError } from '../../utils/command-helpers.js'
import BaseCommand from '../base-command.js'
interface Options extends OptionValues {
output?: string
}
export const blobsGet = async (storeName: string, key: string, options: Options, command: BaseCommand) => {
const { api, siteInfo } = command.netlify
const { output } = options
const store = getStore({
apiURL: `${api.scheme}://${api.host}`,
name: storeName,
siteID: siteInfo?.id ?? '',
token: api.accessToken ?? '',
})
let blob
try {
blob = await store.get(key)
} catch {
return logAndThrowError(`Could not retrieve blob ${chalk.yellow(key)} from store ${chalk.yellow(storeName)}`)
}
if (blob === null) {
return logAndThrowError(`Blob ${chalk.yellow(key)} does not exist in store ${chalk.yellow(storeName)}`)
}
if (output) {
const path = resolve(output)
await fs.writeFile(path, blob)
} else {
console.log(blob)
}
}