Skip to content

Commit 858251b

Browse files
author
Simon he
committed
chore: add writeFile
1 parent d94a56e commit 858251b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/writeFile.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import fs from 'fs'
2+
import { isStr } from './isStr'
3+
4+
export function writeFile(paths: string[] | string, callback: (content: string, index: number) => string) {
5+
if (!callback)
6+
throw new Error('callback is required')
7+
if (isStr(paths))
8+
paths = [paths as string];
9+
(paths as string[]).forEach(async (relativepath, i) => {
10+
const result = callback(await fs.readFileSync('./dist/index.js', 'utf-8'), i)
11+
if (!result)
12+
throw new Error('callback needs to return string content')
13+
fs.writeFile(relativepath, result, (err) => {
14+
if (err)
15+
throw err
16+
})
17+
})
18+
}

0 commit comments

Comments
 (0)