Skip to content

Commit

Permalink
Fix npm#472 Adds support for os/cpu fields in package.json
Browse files Browse the repository at this point in the history
See docs/cli/json.md for details.
  • Loading branch information
regality authored and isaacs committed Mar 13, 2012
1 parent 7c36d0e commit af50a3c
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 1 deletion.
32 changes: 31 additions & 1 deletion doc/cli/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ node that your stuff works on:
{ "engines" : { "node" : ">=0.1.27 <0.1.30" } }

And, like with dependencies, if you don't specify the version (or if you
specify "*" as the version), then any version of node will do.
specify "\*" as the version), then any version of node will do.

If you specify an "engines" field, then npm will require that "node" be
somewhere on that list. If "engines" is omitted, then npm will just assume
Expand All @@ -439,6 +439,36 @@ are capable of properly installing your program. For example:

{ "engines" : { "npm" : "~1.0.20" } }

## os

You can specify which operating systems your
module will run on:

"os" : [ "darwin", "linux" ]

You can also blacklist instead of whitelist operating systems,
just prepend the blacklisted os with a '!':

"os" : [ "!win32" ]

The host operating system is determined by `process.platform`

It is allowed to both blacklist, and whitelist, although there isn't any
good reason to do this.

## cpu

If your code only runs on certain cpu architectures,
you can specify which ones.

"cpu" : [ "x64", "ia32" ]

Like the `os` option, you can also blacklist architectures:

"cpu" : [ "!arm", "!mips" ]

The host architecture is determined by `process.arch`

## preferGlobal

If your package is primarily a command-line application that should be
Expand Down
53 changes: 53 additions & 0 deletions lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ function installOne_ (target, where, context, cb) {

chain
( [ [checkEngine, target]
, [checkPlatform, target]
, [checkCycle, target, context.ancestors]
, [checkGit, targetFolder]
, [write, target, targetFolder, context] ]
Expand Down Expand Up @@ -648,6 +649,58 @@ function checkEngine (target, cb) {
return cb()
}

function checkPlatform (target, cb) {
var platform = process.platform
, arch = process.arch
, osOk = true
, cpuOk = true
, force = npm.config.get("force")

if (force) {
return cb()
}

if (target.os) {
osOk = checkList(platform, target.os)
}
if (target.cpu) {
cpuOk = checkList(arch, target.cpu)
}
if (!osOk || !cpuOk) {
var er = new Error("Unsupported")
er.errno = npm.EBADPLATFORM
er.os = target.os || ['any']
er.cpu = target.cpu || ['any']
er.pkgid = target._id
return cb(er)
}
return cb()
}

function checkList (value, list) {
var tmp
, match = false
, blc = 0
if (typeof list === "string") {
list = [list]
}
if (list.length === 1 && list[0] === "any") {
return true;
}
for (var i = 0; i < list.length; ++i) {
tmp = list[i]
if (tmp[0] === '!') {
tmp = tmp.slice(1)
if (tmp === value) {
return false;
}
++blc
} else {
match = match || tmp === value
}
}
return match || blc === list.length
}

function checkCycle (target, ancestors, cb) {
// there are some very rare and pathological edge-cases where
Expand Down
1 change: 1 addition & 0 deletions lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ npm.EJSONPARSE = {}
npm.EISGIT = {}
npm.ECYCLE = {}
npm.ENOTSUP = {}
npm.EBADPLATFORM = {}

// HACK for windows
if (process.platform === "win32") {
Expand Down
11 changes: 11 additions & 0 deletions lib/utils/error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ function errorHandler (er) {
].join("\n"))
break

case npm.EBADPLATFORM:
er.code = "EBADPLATFORM"
log.error([er.message
,"Not compatible with your operating system or architecture: "+er.pkgid
,"Valid OS: "+er.os.join(",")
,"Valid Arch: "+er.cpu.join(",")
,"Actual OS: "+process.platform
,"Actual Arch: "+process.arch
].join("\n"))
break

case "EEXIST":
case constants.EEXIST:
log.error([er.message
Expand Down
5 changes: 5 additions & 0 deletions test/packages/npm-test-platform-all/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{"name":"npm-test-platform"
,"version":"9.9.9-9"
,"homepage":"http://www.zombo.com/",
,"os":["darwin","linux","win32","solaris","haiku","sunos","freebsd","openbsd","netbsd"]
,"cpu":["arm","mips","ia32","x64","sparc"]}
5 changes: 5 additions & 0 deletions test/packages/npm-test-platform/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{"name":"npm-test-platform"
,"version":"9.9.9-9"
,"homepage":"http://www.youtube.com/watch?v=dQw4w9WgXcQ"
,"os":["!this_is_not_a_real_os", "!neither_is_this"]
,"cpu":["!this_is_not_a_real_cpu","!this_isnt_either"]}

0 comments on commit af50a3c

Please sign in to comment.