diff --git a/lib/read-package-json.js b/lib/read-package-json.js index 1497ebf..e367630 100644 --- a/lib/read-package-json.js +++ b/lib/read-package-json.js @@ -10,8 +10,11 @@ // Requirements //------------------------------------------------------------------------------ +const fs = require("fs") const joinPath = require("path").join -const readPkg = require("read-pkg") +const parseJson5 = require("json5").parse +const parseJson = require("load-json-file") +const normalizePackageData = require("normalize-package-data") //------------------------------------------------------------------------------ // Public Interface @@ -20,12 +23,33 @@ const readPkg = require("read-pkg") /** * Reads the package.json in the current directory. * - * @returns {object} package.json's information. + * @returns {Promise} package.json's information. */ module.exports = function readPackageJson() { - const path = joinPath(process.cwd(), "package.json") - return readPkg(path).then(body => ({ - taskList: Object.keys(body.scripts || {}), - packageInfo: { path, body }, - })) + try { + let path = joinPath(process.cwd(), "package.json5") + let bodyPromise = null + + if (fs.existsSync(path)) { + // Read package.json5 + const rawPkg = fs.readFileSync(path, "utf8") + bodyPromise = Promise.resolve(parseJson5(rawPkg)) + } + else { + // Read package.json + path = joinPath(process.cwd(), "package.json") + bodyPromise = parseJson(path) + } + + return bodyPromise.then(body => { + normalizePackageData(body) + return { + taskList: Object.keys(body.scripts || {}), + packageInfo: { path, body }, + } + }) + } + catch (err) { + return Promise.reject(err) + } } diff --git a/package.json b/package.json index f130b86..35a84a5 100644 --- a/package.json +++ b/package.json @@ -32,10 +32,12 @@ "ansi-styles": "^3.2.1", "chalk": "^2.4.1", "cross-spawn": "^6.0.5", + "json5": "^2.2.3", + "load-json-file": "^4.0.0", "memorystream": "^0.3.1", "minimatch": "^3.0.4", + "normalize-package-data": "^2.5.0", "pidtree": "^0.3.0", - "read-pkg": "^3.0.0", "shell-quote": "^1.6.1", "string.prototype.padend": "^3.0.0" }, diff --git a/test-workspace/json5/package.json5 b/test-workspace/json5/package.json5 new file mode 100644 index 0000000..ceabac2 --- /dev/null +++ b/test-workspace/json5/package.json5 @@ -0,0 +1,22 @@ +{ + /* + * Test comment + */ + + name: "npm-run-all-json5-test", + version: "0.0.0", + private: true, + description: "", + engines: { + node: ">=4", + }, + config: { + test: "OK", + }, + repository: { + type: "git", + url: "https://github.com/mysticatea/npm-run-all.git", + }, + author: "Toru Nagashima", + license: "MIT", +} diff --git a/test/json5.js b/test/json5.js new file mode 100644 index 0000000..7cb67b6 --- /dev/null +++ b/test/json5.js @@ -0,0 +1,32 @@ +/** + * @author Toru Nagashima + * @copyright 2016 Toru Nagashima. All rights reserved. + * See LICENSE file in root directory for full license. + */ +"use strict" + +//------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ + +const assert = require("power-assert") +const readPackageJson = require("../lib/read-package-json") + +//------------------------------------------------------------------------------ +// Test +//------------------------------------------------------------------------------ + +describe("[json5]", () => { + before(() => process.chdir("test-workspace/json5")) + after(() => process.chdir("../..")) + + it("should read package.json5 when available", () => + readPackageJson() + .then(result => assert(result.packageInfo.path.endsWith("/package.json5"))) + ) + + it("should parse package.json5", () => + readPackageJson() + .then(result => assert(result.packageInfo.body.name === "npm-run-all-json5-test")) + ) +})