-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support
output
when build env
- Loading branch information
1 parent
59c5812
commit 1e1c71e
Showing
4 changed files
with
180 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { createHash } from 'node:crypto' | ||
import fs, { promises as fsp } from 'node:fs' | ||
import path from 'node:path' | ||
import type { ImageType, OutputFile, OutputFilename } from './types' | ||
|
||
const RE_HTTP = /^https?:\/\// | ||
|
||
const isHTTP = (url: string): boolean => RE_HTTP.test(url) | ||
|
||
export async function bufferToFile( | ||
buffer: Buffer, | ||
type: ImageType, | ||
outDir: string, | ||
assetsDir: string, | ||
filename?: OutputFilename, | ||
) { | ||
const hash = createHash('sha256') | ||
hash.update(buffer) | ||
const hex = hash.digest('hex').slice(0, 12) | ||
|
||
const _filename = path.join(assetsDir, `${hex}.${type}`) | ||
const file: OutputFile = { basename: hex, assetsDir, ext: type } | ||
|
||
const pathname = filename ? filename(_filename, file) : _filename | ||
|
||
const _http = isHTTP(pathname) | ||
const output = path.join(outDir, _http ? _filename : pathname) | ||
|
||
const dirname = path.dirname(output) | ||
|
||
fs.mkdirSync(dirname, { recursive: true }) | ||
await fsp.writeFile(output, new Uint8Array(buffer)) | ||
|
||
return _http ? pathname : path.join('/', _filename) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters