Skip to content

Commit

Permalink
Merge branch 'dev'; tag v0.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Harvey committed Aug 23, 2018
2 parents d5b44d5 + 489d937 commit 8f7e09f
Show file tree
Hide file tree
Showing 24 changed files with 982 additions and 818 deletions.
19 changes: 9 additions & 10 deletions lib/requireOther.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
const fs = require('fs')
const path = require('path')
const util = require('util')

/**
* @summary Like node.js `require()`, but can be used on files other than `.js` or `.json`.
* @description Example: `requireOther('my-data.jsonld')`.
* @param {string} path the path of the file to read
* @param {string} filepath the path of the file to read
* @returns {(string|number|boolean|?Object|Array|null)} the text of the file, parsed by JSON
*/
function requireOther(path) {
function requireOther(filepath) {
let object;
try {
object = require(path)
object = require(filepath)
} catch (e) {
let data = fs.readFileSync(path, 'utf8')
let data = fs.readFileSync(filepath, 'utf8')
try {
object = JSON.parse(data)
} catch (e) {
e.filename = path
e.filename = filepath
console.error(e)
throw e
}
Expand All @@ -27,16 +26,16 @@ function requireOther(path) {

/**
* @summary asynchronous version of `requireOther`.
* @param {string} path the path of the file to read
* @param {string} filepath the path of the file to read
* @returns {(string|number|boolean|?Object|Array|null)} the text of the file, parsed by JSON
*/
async function requireOtherAsync(path) {
let data = await util.promisify(fs.readFile)(path, 'utf8')
async function requireOtherAsync(filepath) {
let data = await util.promisify(fs.readFile)(filepath, 'utf8')
let object;
try {
object = JSON.parse(data)
} catch (e) {
e.filename = path
e.filename = filepath
console.error(e)
throw e
}
Expand Down
Loading

0 comments on commit 8f7e09f

Please sign in to comment.