UNPKG

1.04 kBJavaScriptView Raw
1const { readFile, readFileSync } = require('fs')
2
3function fromFileSync (parseString, filePath, sourcePaths) {
4 const string = readFileSync(filePath, 'utf8')
5 const parsed = parseString(string, filePath, sourcePaths)
6
7 return parsed
8}
9
10async function fromFile (parseString, filePath, sourcePaths) {
11 return new Promise((resolve, reject) => {
12 readFile(filePath, 'utf8', (err, string) => {
13 if (err) {
14 reject(err)
15 } else {
16 try {
17 const parsed = parseString(string, filePath, sourcePaths)
18
19 resolve(parsed)
20 } catch (err) {
21 reject(err)
22 }
23 }
24 })
25 })
26}
27
28async function fromString (parseString, string, sourcePaths) {
29 return new Promise((resolve, reject) => {
30 if (string.length === 0) {
31 resolve(undefined)
32 } else {
33 try {
34 const parsed = parseString(string, null, sourcePaths)
35
36 resolve(parsed)
37 } catch (err) {
38 reject(err)
39 }
40 }
41 })
42}
43
44module.exports = {
45 fromFileSync,
46 fromFile,
47 fromString
48}