Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for package.json5 #266

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 @@ -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
Expand All @@ -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<object>} 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)
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
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("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"))
)
})