diff --git a/lib/read-package-json.js b/lib/read-package-json.js index a8ee072..e0ee9ab 100644 --- a/lib/read-package-json.js +++ b/lib/read-package-json.js @@ -6,13 +6,15 @@ */ 'use strict' -const readPackage = require('read-package-json-fast') - // ------------------------------------------------------------------------------ // Requirements // ------------------------------------------------------------------------------ +const fs = require('fs') const joinPath = require('path').join +const parseJson5 = require('json5').parse +const parseJson = require('json-parse-even-better-errors') +const normalizePackageData = require('npm-normalize-package-bin') // ------------------------------------------------------------------------------ // Public Interface @@ -24,9 +26,31 @@ const joinPath = require('path').join * @returns {object} package.json's information. */ module.exports = function readPackageJson () { - const path = joinPath(process.cwd(), 'package.json') - return readPackage(path).then(body => ({ - taskList: Object.keys(body.scripts || {}), - packageInfo: { path, body }, - })) + try { + let path = joinPath(process.cwd(), 'package.json5') + let isJson5 = true + + if (!fs.existsSync(path)) { + path = joinPath(process.cwd(), 'package.json') + isJson5 = false + } + + const rawPkg = fs.readFileSync(path, 'utf8') + let body = null + + if (isJson5) { + // Read package.json5 + body = parseJson5(rawPkg) + } else { + body = parseJson(rawPkg) + } + + normalizePackageData(body) + return Promise.resolve({ + taskList: Object.keys(body.scripts || {}), + packageInfo: { path, body }, + }) + } catch (err) { + return Promise.reject(err) + } } diff --git a/package.json b/package.json index 0f8eb90..0222e78 100644 --- a/package.json +++ b/package.json @@ -32,10 +32,12 @@ "dependencies": { "ansi-styles": "^6.2.1", "cross-spawn": "^7.0.3", + "json-parse-even-better-errors": "^4.0.0", + "json5": "^2.2.3", "memorystream": "^0.3.1", "minimatch": "^9.0.0", + "npm-normalize-package-bin": "^4.0.0", "pidtree": "^0.6.0", - "read-package-json-fast": "^4.0.0", "shell-quote": "^1.7.3", "which": "^5.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..4e017c1 --- /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('assert').strict +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') + )) +})