read-package-json
Advanced tools
Comparing version 0.4.1 to 1.0.0-pre
{ | ||
"name": "read-package-json", | ||
"version": "0.4.1", | ||
"version": "1.0.0-pre", | ||
"author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me/)", | ||
@@ -23,5 +23,4 @@ "description": "The thing npm uses to read package.json files with semantics and defaults and validation", | ||
"optionalDependencies": { | ||
"npmlog": "0", | ||
"graceful-fs": "~1.2" | ||
} | ||
} |
// vim: set softtabstop=16 shiftwidth=16: | ||
try { | ||
readJson.log = require("npmlog") | ||
} catch (er) { | ||
readJson.log = { | ||
info: function () {}, | ||
verbose: function () {}, | ||
warn: function () {} | ||
} | ||
} | ||
try { | ||
var fs = require("graceful-fs") | ||
@@ -43,10 +32,18 @@ } catch (er) { | ||
function readJson (file, cb) { | ||
function readJson (file, log, strict, cb) { | ||
if (typeof cb !== 'function') { | ||
cb = strict | ||
strict = false | ||
} | ||
if (typeof cb !== 'function') { | ||
cb = log | ||
log = function() {} | ||
} | ||
var c = readJson.cache.get(file) | ||
if (c) { | ||
readJson.log.verbose("from cache", file) | ||
cb = cb.bind(null, null, c) | ||
return process.nextTick(cb); | ||
} | ||
readJson.log.verbose("read json", file) | ||
cb = (function (orig) { return function (er, data) { | ||
@@ -56,9 +53,9 @@ if (data) readJson.cache.set(file, data); | ||
} })(cb) | ||
readJson_(file, cb) | ||
readJson_(file, log, strict, cb) | ||
} | ||
function readJson_ (file, cb) { | ||
function readJson_ (file, log, strict, cb) { | ||
fs.readFile(file, "utf8", function (er, d) { | ||
parseJson(file, er, d, cb) | ||
parseJson(file, er, d, log, strict, cb) | ||
}) | ||
@@ -79,5 +76,5 @@ } | ||
function parseJson (file, er, d, cb) { | ||
function parseJson (file, er, d, log, strict, cb) { | ||
if (er && er.code === "ENOENT") { | ||
indexjs(file, er, cb) | ||
indexjs(file, er, log, strict, cb) | ||
return | ||
@@ -92,7 +89,7 @@ } | ||
} | ||
extras(file, d, cb) | ||
extras(file, d, log, strict, cb) | ||
} | ||
function indexjs (file, er, cb) { | ||
function indexjs (file, er, log, strict, cb) { | ||
if (path.basename(file) === "index.js") { | ||
@@ -106,3 +103,3 @@ return cb(er); | ||
if (!d) return cb(er); | ||
extras(file, d, cb) | ||
extras(file, d, log, strict, cb) | ||
}) | ||
@@ -113,3 +110,3 @@ } | ||
readJson.extras = extras | ||
function extras (file, data, cb) { | ||
function extras (file, data, log, strict, cb) { | ||
var set = readJson.extraSet | ||
@@ -124,3 +121,4 @@ var n = set.length | ||
if (er) return cb(errState = er); | ||
if (--n === 0) final(file, data, cb); | ||
if (--n > 0) return; | ||
final(file, data, log, strict, cb); | ||
} | ||
@@ -304,10 +302,10 @@ } | ||
function final (file, data, cb) { | ||
function final (file, data, log, strict, cb) { | ||
var pId = makePackageId(data) | ||
function warn(msg) { | ||
if (typoWarned[pId]) return; | ||
readJson.log.warn("package.json", pId, msg) | ||
log("package.json", pId, msg) | ||
} | ||
try { | ||
normalizeData(data, warn) | ||
normalizeData(data, warn, strict) | ||
} | ||
@@ -314,0 +312,0 @@ catch (error) { |
@@ -20,3 +20,4 @@ # read-package-json | ||
readJson('/path/to/package.json', function (er, data) { | ||
// readJson(filename, [logFunction=noop], [strict=false], cb) | ||
readJson('/path/to/package.json', console.error, false, function (er, data) { | ||
if (er) { | ||
@@ -31,6 +32,9 @@ console.error("There was an error reading the file") | ||
## readJson(file, cb) | ||
## readJson(file, [logFn = noop], [strict = false], cb) | ||
* `file` {String} The path to the package.json file | ||
* `cb` {Function} | ||
* `logFn` {Function} Function to handle logging. Defaults to a noop. | ||
* `strict` {Boolean} True to enforce SemVer 2.0 version strings, and | ||
other strict requirements. | ||
* `cb` {Function} Gets called with `(er, data)`, as is The Node Way. | ||
@@ -37,0 +41,0 @@ Reads the JSON file and does the things. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25557
4
167
482