Skip to content

Commit

Permalink
feat(version): respect CLI version like have lodash@3.10.1 close #10
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Feb 24, 2017
1 parent 6c8ff4b commit dbc9064
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ module loader.
save installed name version in `package.json`
* [ ] [#9](https://github.com/bahmutov/have-it/issues/9)
respect `package.json` versions when installing
* [ ] [#10](https://github.com/bahmutov/have-it/issues/10)
* [x] [#10](https://github.com/bahmutov/have-it/issues/10)
allow installing specific version from CLI `have lodash@3.0.0`

## Related projects
Expand Down
4 changes: 3 additions & 1 deletion __snapshots__/utils-spec.js.snap-shot
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
exports['finds missing deps 1'] = [
"bar"
{
"name": "bar"
}
]

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
],
"bin": {
"have-it": "dist/have-it.js",
"have": "dist/have-it.js"
"have": "dist/have-it.js",
"_have": "bin/_have-it.js"
},
"homepage": "https://github.com/bahmutov/have-it#readme",
"keywords": [
Expand Down Expand Up @@ -98,5 +99,8 @@
},
"release": {
"analyzeCommits": "simple-commit-message"
},
"dependencies": {
"parse-package-name": "0.1.0"
}
}
54 changes: 45 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const la = require('lazy-ass')
const is = require('check-more-types')
const glob = require('glob-all')
const execa = require('execa')
const parse = require('parse-package-name')

const {mkdir, saveJSON, findMissing, saveVersions} = require('./utils')

Expand Down Expand Up @@ -67,21 +68,52 @@ const latestVersion = R.pipe(
R.last
)

// TODO return found / not found modules
function findModules (names) {
la(is.strings(names), 'expected names', names)
const pickFoundVersion = target => found => {
la(is.array(found), 'expected list of found items', found)

if (is.not.semver(target)) {
return latestVersion(found)
}
debug('need to pick version %s', target)
debug('from list with %d found items', found.length)
const sameVersion = R.propEq('version', target)
return R.find(sameVersion, found)
}

// TODO unit test this
const pickFoundVersions = targets => found => {
debug('only need the following targets', targets)

const pickInstall = (found, name) => {
const target = R.find(R.propEq('name', name), targets)
const picked = pickFoundVersion(target.version)(found)
return picked
}

return R.mapObjIndexed(pickInstall, found)
}

function findModules (searchNames) {
la(is.strings(searchNames), 'expected names to find', searchNames)

// names could potentially have version part
const parsedNames = searchNames.map(parse)
const names = R.pluck('name', parsedNames)
debug('just names', names)

const searches = names.map(name => `${rootFolder}/*/node_modules/${name}`)
const folders = glob.sync(searches)
return Promise.all(folders.map(getVersionSafe))
.then(R.filter(R.is(Object)))
.then(R.groupBy(R.prop('name')))
.then(R.mapObjIndexed(latestVersion))
.then(found => {
.then(pickFoundVersions(parsedNames))
.then(results => {
const found = R.pickBy(is.object, results)
const foundNames = R.keys(found)
const missing = findMissing(names, foundNames)
const missing = findMissing(parsedNames, foundNames)
if (is.not.empty(missing)) {
console.log('You do not have %d module(s): %s',
missing.length, missing.join(', '))
missing.length, missing.map(R.prop('name')).join(', '))
debug('all names to find', names)
debug('found names', foundNames)
debug('missing names', missing)
Expand Down Expand Up @@ -150,20 +182,24 @@ function haveModules (list, options) {
})
}

const fullInstallName = parsed =>
parsed.version ? `${parsed.name}@${parsed.version}` : parsed.name

function npmInstall (list, options) {
if (is.empty(list)) {
return Promise.resolve()
}
const flags = options.join(' ')
const names = list.join(' ')
const names = list.map(fullInstallName).join(' ')
const cmd = `npm install ${flags} ${names}`
console.log(cmd)
return execa.shell(cmd)
}

const installModules = options => ({found, missing}) => {
la(is.object(found), 'expected found modules object', found)
la(is.strings(missing), 'expected list of missing names', missing)
la(is.array(missing), 'expected list of missing names', missing)

const list = R.values(found)

return haveModules(list, options)
Expand Down
6 changes: 5 additions & 1 deletion src/utils-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ describe.only('findMissing', () => {
})

it('finds missing deps', () => {
const names = ['foo', 'bar']
const names = [{
name: 'foo'
}, {
name: 'bar'
}]
const found = ['foo']
const missing = findMissing(names, found)
snapshot(missing)
Expand Down
13 changes: 11 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'use strict'

const mkdirp = require('mkdirp')
const fs = require('fs')
const path = require('path')
const {concat, difference} = require('ramda')
const la = require('lazy-ass')
const is = require('check-more-types')
const R = require('ramda')

function mkdir (name) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -55,10 +58,16 @@ function toInstall () {
})
}

// returns just the list of missing objects
function findMissing (names, found) {
la(is.strings(names), 'wrong names', names)
la(is.array(names), 'wrong names', names)
la(is.strings(found), 'wrong installed', found)
return difference(names, found)

// each object in "names" is parsed object
// {name, version}

const missingNames = difference(R.pluck('name', names), found)
return missingNames.map(name => R.find(R.propEq('name', name), names))
}

function saveVersions (list, dev) {
Expand Down
5 changes: 5 additions & 0 deletions test/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ DEBUG=have-it have lodash --save
echo "Trying to use the program"
node index.js

echo "Installing specific version of lodash"
DEBUG=have-it HAVE=$HAVE have lodash@3.10.0 --save
echo "Trying to use the program"
node index.js

echo "All done testing have-it in $folder"

0 comments on commit dbc9064

Please sign in to comment.