├── .editorconfig ├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | [*] 3 | end_of_line = lf 4 | insert_final_newline = true 5 | 6 | [*.js] 7 | indent_style = space 8 | indent_size = 2 9 | 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .vscode 3 | build/ 4 | dist/ 5 | npm-debug.log 6 | 7 | *.DS_Store 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Alberto Fernandez 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NPM Platform Dependencies 2 | Small script that allows you to specify OS-specific dependencies in your package.json and install them only in that OS. 3 | 4 | Install via: 5 | 6 | `npm install npm-platform-dependencies` 7 | 8 | ## Specify your packages inside package.json 9 | 10 | Specify the specific dependencies for each platform, like: 11 | 12 | ``` 13 | "darwinDependencies": { 14 | "nodobjc": "latest" 15 | }, 16 | "win32Dependencies": { 17 | "edge": "latest" 18 | } 19 | ``` 20 | 21 | Then run: 22 | 23 | ``` 24 | npmpd 25 | ``` 26 | 27 | And that's it! Depending the platform where you run the command, it will install one or the other (or nothing if linux). 28 | 29 | ## Use with npm install 30 | To use it just after npm install, put this script on the scripts section of package.json 31 | 32 | ``` 33 | "postinstall": "npmpd" 34 | ``` 35 | 36 | ## Append your own arguments to the install process 37 | Simply add any arguments to the npmpd call to have them appended to the `npm install` process. 38 | The example below shows how you could append the `--ignore-scripts` argument to the install process. 39 | ``` 40 | npmpd --ignore-scripts 41 | ``` 42 | 43 | Or append the arguments at the scripts section of package.json 44 | ``` 45 | "postinstall": "npmpd --ignore-scripts" 46 | ``` 47 | 48 | ## Inside the code 49 | Be sure to make your code detect the platform you are running on, or you will end up requiring things you don't have installed! 50 | 51 | ## Motivation 52 | I came across a problem where it would be useful to specify platform specific dependencies. For instance, when trying to deal with addons that connect to specific 53 | os frameworks (Cocoa, .NET...). This is an easy solution if you don't want to deal with building a native addon with [node-gyp](https://github.com/nodejs/node-gyp), if 54 | the functionalities you need are already in different os specific packages. 55 | 56 | Another use case would be when dealing with wrappers of os specific programs, and you want to link to one or the other depending the platform you run your program with. 57 | 58 | ## License 59 | 60 | MIT License Copyright (c) 2016 Alberto Fernandez 61 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | var path = require('path') 3 | var pkg = require(path.join(process.cwd(), 'package.json')) 4 | var os = require('os') 5 | var platform = os.platform() 6 | var log = console.log 7 | 8 | var spawn = require('cross-spawn') 9 | 10 | // Build arguments for npm 11 | var dependencies = platform + 'Dependencies' 12 | var dependenciesObj = pkg[dependencies] 13 | 14 | if (dependenciesObj && Object.keys(dependenciesObj).length) { 15 | log('Installing dependencies for ' + platform) 16 | var npmArgs = ['install'] 17 | // Append any arguments from commandline 18 | npmArgs = npmArgs.concat(process.argv.slice(2)); 19 | for (var dep in dependenciesObj) { 20 | if (dependenciesObj.hasOwnProperty(dep)) { 21 | npmArgs.push(dep.concat('@').concat(dependenciesObj[dep])) 22 | } 23 | } 24 | var options = { 25 | stdio: 'inherit' // feed all child process logging into parent process 26 | } 27 | 28 | spawn('npm', npmArgs, options) 29 | } else { 30 | log('No specific dependencies on this platform: ' + platform) 31 | } 32 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "npm-platform-dependencies", 3 | "version": "0.1.0", 4 | "description": "Module to specify dependencies for different operating systems.", 5 | "main": "index.js", 6 | "bin": { 7 | "npmpd": "index.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/bertofer/npm-platform-dependencies" 12 | }, 13 | "keywords": [ 14 | "os", 15 | "dependency", 16 | "install", 17 | "dependencies", 18 | "operating", 19 | "system" 20 | ], 21 | "author": "Alberto Fernandez", 22 | "license": "MIT", 23 | "dependencies": { 24 | "cross-spawn": "^4.0.0" 25 | } 26 | } 27 | --------------------------------------------------------------------------------