├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── bin └── engine-deps.js ├── index.js ├── package.json └── test ├── index.js └── mocks ├── empty.json └── simple.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "node" 4 | - "5" 5 | - "4" 6 | - "0.12" 7 | - "0.10" 8 | 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2015 Sam Saccone 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 |

Engine Deps

3 |

4 |
5 |

6 | 7 |

8 |

9 | 10 |

11 |
12 | npm i --save-dev engine-deps
13 | 
14 |

15 | 16 | #### What: 17 | 18 | Manage Engine Specific dependencies with ease. 19 | 20 | #### How: 21 | 22 | In package.json add a new `engine-deps` section: 23 | 24 | ```json 25 | { 26 | "engine-deps": { 27 | "0.12.x": { 28 | "backbone": "1.0.x" 29 | }, 30 | "0.10.x": { 31 | "backbone": "1.1.x" 32 | }, 33 | "^4": { 34 | "backbone": "1.2.x" 35 | } 36 | } 37 | } 38 | ``` 39 | 40 | Then add a new install hook 41 | 42 | ```json 43 | { 44 | "scripts": { 45 | "install": "engine-deps" 46 | } 47 | } 48 | ``` 49 | 50 | ##### IRL Examples 51 | 52 | * [https://github.com/mounirlamouri/manifest-validator](https://github.com/mounirlamouri/manifest-validator/blob/0df7775d79b59232800656db69128f34bfdd21e7/package.json#L8-L26) 53 | -------------------------------------------------------------------------------- /bin/engine-deps.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var path = require('path'); 4 | 5 | require('../').installFromJson(path.join(process.cwd(), 'package.json'), process.version); 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var semver = require('semver'); 3 | var npm = require('npm'); 4 | 5 | /** 6 | * Reads json from a file path and returns the 7 | * engine-deps key from the json or if not present an empty object 8 | * @param {string} filePath 9 | * @returns {!object} engine-deps object 10 | */ 11 | function getEngineDeps(filePath) { 12 | return JSON.parse(fs.readFileSync(filePath, 'utf8'))['engine-deps'] || {}; 13 | } 14 | 15 | /** 16 | * 17 | * @param {string} tipVersion -- eg v0.1.1 18 | * @param {Array} allRanges 19 | * @return {Array} all valid ranges inside allRanges given tip 20 | */ 21 | function getValidEngineRanges(tipVersion, allRanges) { 22 | return allRanges.reduce(function(prev, curr) { 23 | if (semver.satisfies(tipVersion, curr)) { 24 | prev.push(curr); 25 | } 26 | 27 | return prev; 28 | }, []); 29 | } 30 | 31 | /** 32 | * @param {Array}} packages 33 | * @return {Promise} when the install is done or rejected 34 | */ 35 | function installPackages(packages) { 36 | npm.load({}, function (er) { 37 | if (er) { 38 | throw err; 39 | } 40 | 41 | npm.commands.install(packages, function (er, data) { 42 | if (er) { 43 | throw err; 44 | } 45 | }); 46 | }); 47 | } 48 | 49 | /** 50 | * Converts node style deps into a list of dependencies with versions 51 | * eg: 52 | * 53 | * {'lodash': '0.1.x', {'underscore': '0.2.x'} => ['lodash@0.1.x', 'underscore@0.2.x'] 54 | * 55 | * @param {!Object} deps 56 | * @return {Array} 57 | */ 58 | function convertDependencyHash(deps) { 59 | return Object.keys(deps).reduce(function(prev, curr) { 60 | prev.push(curr + '@' + deps[curr]); 61 | return prev; 62 | }, []); 63 | } 64 | 65 | 66 | /** 67 | * @param {string} jsonPath 68 | * @param {string} version 69 | */ 70 | function installFromJson(jsonPath, version) { 71 | var engineDeps = getEngineDeps(jsonPath); 72 | 73 | installPackages(getValidEngineRanges(process.version, Object.keys(engineDeps)) 74 | .reduce(function(prev, curr) { 75 | return prev.concat(convertDependencyHash(engineDeps[curr])); 76 | }, [])); 77 | } 78 | 79 | module.exports = { 80 | installFromJson: installFromJson, 81 | __convertDependencyHash: convertDependencyHash, 82 | __getValidEngineRanges: getValidEngineRanges, 83 | __getEngineDeps: getEngineDeps, 84 | }; 85 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "scripts": { 3 | "test": "mocha" 4 | }, 5 | "bin": { 6 | "engine-deps": "bin/engine-deps.js" 7 | }, 8 | "dependencies": { 9 | "npm": "^3.5.2", 10 | "semver": "^5.1.0" 11 | }, 12 | "devDependencies": { 13 | "mocha": "^2.3.4" 14 | }, 15 | "name": "engine-deps", 16 | "description": "[![Build Status](https://travis-ci.org/samccone/engine-deps.svg?branch=master)](https://travis-ci.org/samccone/engine-deps)", 17 | "version": "0.1.0", 18 | "main": "index.js", 19 | "directories": { 20 | "test": "test" 21 | }, 22 | "repository": { 23 | "type": "git", 24 | "url": "git+https://github.com/samccone/engine-deps.git" 25 | }, 26 | "keywords": [ 27 | "engine", 28 | "dependencies", 29 | "install", 30 | "npm" 31 | ], 32 | "author": "Sam Saccone", 33 | "license": "MIT", 34 | "bugs": { 35 | "url": "https://github.com/samccone/engine-deps/issues" 36 | }, 37 | "homepage": "https://github.com/samccone/engine-deps#readme" 38 | } 39 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var path = require('path'); 3 | var engineDeps = require('../'); 4 | 5 | describe('engine dependency reading', function() { 6 | beforeEach(function() { 7 | this.fn = engineDeps.__getEngineDeps; 8 | }); 9 | 10 | it('returns an empty object when no key present', function() { 11 | assert.deepEqual({}, 12 | this.fn(path.join(__dirname, 'mocks/empty.json'))); 13 | }); 14 | 15 | it('can read engine deps', function() { 16 | assert.deepEqual({ 17 | "0.10.x": {"backbone": "latest"}, 18 | "0.12.x": {"lodash": "latest"}, 19 | "^0.12.x": { 20 | "underscore": "latest", 21 | "backbone": "latest" 22 | } 23 | }, 24 | this.fn(path.join(__dirname, 'mocks/simple.json'))); 25 | }); 26 | }); 27 | 28 | describe('getting valid ranges', function() { 29 | beforeEach(function() { 30 | this.fn = engineDeps.__getValidEngineRanges; 31 | }); 32 | 33 | it('returns an empty set when no matching', function() { 34 | assert.deepEqual([], this.fn('0.1.0', ['0.12.x'])); 35 | }); 36 | 37 | it('returns duplicates if defined', function() { 38 | assert.deepEqual(['0.1.x', '0.1.0'], 39 | this.fn('0.1.0', ['0.1.x', '0.1.0'])); 40 | }); 41 | }); 42 | 43 | describe('converts dependency hash', function() { 44 | beforeEach(function() { 45 | this.fn = engineDeps.__convertDependencyHash; 46 | }); 47 | 48 | it('handles an empty hash', function() { 49 | assert.deepEqual(this.fn({}), []); 50 | }); 51 | 52 | it('converts single key hash', function() { 53 | assert.deepEqual(this.fn({ 54 | foo: '0.1.x' 55 | }), ['foo@0.1.x']); 56 | }); 57 | 58 | it('converts multiple key hash', function() { 59 | assert.deepEqual(this.fn({ 60 | foo: '0.1.x', 61 | zap: '0.1.1', 62 | }), ['foo@0.1.x', 'zap@0.1.1']); 63 | }); 64 | }); 65 | 66 | -------------------------------------------------------------------------------- /test/mocks/empty.json: -------------------------------------------------------------------------------- 1 | {} 2 | -------------------------------------------------------------------------------- /test/mocks/simple.json: -------------------------------------------------------------------------------- 1 | { 2 | "engine-deps": { 3 | "0.10.x": { 4 | "backbone": "latest" 5 | }, 6 | "0.12.x": { 7 | "lodash": "latest" 8 | }, 9 | "^0.12.x": { 10 | "underscore": "latest", 11 | "backbone": "latest" 12 | } 13 | } 14 | } 15 | --------------------------------------------------------------------------------