Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions lib/read-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
22 changes: 22 additions & 0 deletions test-workspace/json5/package.json5
Original file line number Diff line number Diff line change
@@ -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",
}
32 changes: 32 additions & 0 deletions test/json5.js
Original file line number Diff line number Diff line change
@@ -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')
))
})
Loading