forked from intlify/bundle-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
26 lines (24 loc) · 652 Bytes
/
utils.ts
File metadata and controls
26 lines (24 loc) · 652 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
import { promises as fs } from 'fs'
import { parseSync } from 'oxc-parser'
import path from 'path'
export async function readFile(
filepath: string,
encoding: BufferEncoding = 'utf-8'
): Promise<{ filename: string; source: string }> {
const filename = path.resolve(__dirname, filepath)
const source = await fs.readFile(filename, { encoding })
return { filename, source }
}
export function validateSyntax(code: string): boolean {
let ret = false
try {
parseSync('test.js', code, {
sourceType: 'module'
})
ret = true
} catch (e) {
console.log(`invalid syntax on \n${code}`)
console.error(e)
}
return ret
}