require-json5
Advanced tools
Comparing version 1.2.0 to 1.3.0
52
index.js
@@ -8,15 +8,11 @@ /** | ||
*/ | ||
const VERSION = '1.2.0'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const JSON5 = require('json5'); | ||
/// Require a JSON file with comments | ||
function requireJSON5(filename) { | ||
if ( path.extname(filename) == '' ) { | ||
if (path.extname(filename) == '') { | ||
const extensions = ['.json5', '.json']; | ||
for (let i=0, l = extensions.length, ext; i<l; ++i) { | ||
for (let i = 0, l = extensions.length, ext; i < l; ++i) { | ||
ext = extensions[i]; | ||
@@ -29,3 +25,2 @@ const fn = filename + ext; | ||
} | ||
// ES5 alternative | ||
@@ -39,7 +34,6 @@ // ['.json5', '.json'].some((ext) => { | ||
} | ||
try { | ||
return JSON5.parse(stripBOM(fs.readFileSync(filename, 'utf8'))); | ||
} | ||
catch(error) { | ||
catch (error) { | ||
error.message = filename + ": " + error.message; | ||
@@ -49,30 +43,24 @@ throw error; | ||
} | ||
function require_hook(module, filename) { | ||
module.exports = requireJSON5(filename); | ||
} | ||
const _backup_require_hooks = {}; | ||
/// Override require for .json extension | ||
function replace_require(ext) { | ||
if (ext == undefined) ext = '.json'; | ||
if (ext == undefined) | ||
ext = '.json'; | ||
const bak = require.extensions[ext]; | ||
if (bak === require_hook) return; | ||
if (bak === require_hook) | ||
return; | ||
_backup_require_hooks[ext] = bak; | ||
require.extensions[ext] = require_hook; | ||
} | ||
/// Restore the original require for .json extension | ||
function restore_require(ext) { | ||
if (ext == undefined) ext = '.json'; | ||
if (ext == undefined) | ||
ext = '.json'; | ||
const bak = _backup_require_hooks[ext]; | ||
if (!(bak && ext in _backup_require_hooks)) return false; | ||
if (!(bak && ext in _backup_require_hooks)) | ||
return false; | ||
delete _backup_require_hooks[ext]; | ||
if (bak) { | ||
@@ -82,22 +70,14 @@ require.extensions[ext] = bak; | ||
} | ||
return delete require.extensions[ext]; | ||
} | ||
/// Register .json5 extension for require | ||
replace_require('.json5'); | ||
/// Exports: | ||
requireJSON5.parse = JSON5.parse.bind(JSON5); | ||
requireJSON5.stringify = JSON5.stringify.bind(JSON5); | ||
requireJSON5.replace = replace_require; | ||
requireJSON5.restore = restore_require; | ||
requireJSON5.VERSION = VERSION; | ||
requireJSON5.parse = JSON5.parse.bind(JSON5); | ||
requireJSON5.stringify = JSON5.stringify.bind(JSON5); | ||
requireJSON5.replace = replace_require; | ||
requireJSON5.restore = restore_require; | ||
requireJSON5.VERSION = VERSION; | ||
module.exports = requireJSON5; | ||
/// Helpers: | ||
function stripBOM(content) { | ||
@@ -104,0 +84,0 @@ // Remove byte order marker. This catches EF BB BF (the UTF-8 BOM) |
{ | ||
"name": "require-json5", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Require JSON5 files in node - a better JSON for ES5 era", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"scripts": { | ||
"build": "npx tsc --build", | ||
"test": "node test/", | ||
@@ -43,2 +45,4 @@ "verup": "npx verup" | ||
"devDependencies": { | ||
"@types/node": "^17.0.42", | ||
"typescript": "^4.7.3", | ||
"verup": "^1.x" | ||
@@ -49,3 +53,3 @@ }, | ||
"files": [ | ||
"index.js" | ||
"index.ts" | ||
] | ||
@@ -52,0 +56,0 @@ } |
14245
13
269
3