Refactor isAvailable()

This commit is contained in:
Rico Sta. Cruz
2016-02-03 16:16:28 +08:00
parent 16592a9e0e
commit 0afe27aed0
2 changed files with 50 additions and 39 deletions

View File

@@ -1,23 +1,27 @@
var Promise = require('./promise')
var semver = require('semver')
var debug = require('debug')('pnpm:install')
var npa = require('npm-package-arg')
var getUuid = require('node-uuid')
var fs = require('mz/fs')
var join = require('path').join
var dirname = require('path').dirname
var basename = require('path').basename
var abspath = require('path').resolve
var mkdirp = require('./fs/mkdirp')
var fetch = require('./fetch')
var resolve = require('./resolve')
var getUuid = require('node-uuid')
var mkdirp = require('./fs/mkdirp')
var symlink = require('./fs/force_symlink')
var relSymlink = require('./fs/rel_symlink')
var linkBins = require('./install/link_bins')
var linkBundledDeps = require('./install/link_bundled_deps')
var postInstall = require('./install/post_install')
var fs = require('mz/fs')
var obliterate = require('./fs/obliterate')
var requireJson = require('./fs/require_json')
var relSymlink = require('./fs/rel_symlink')
var linkBins = require('./install/link_bins')
var linkBundledDeps = require('./install/link_bundled_deps')
var isAvailable = require('./install/is_available')
var postInstall = require('./install/post_install')
/*
* Installs a package.
@@ -86,7 +90,7 @@ module.exports = function install (ctx, pkgSpec, modules, options) {
var log = ctx.log(pkg.spec) // function
// it might be a bundleDependency, in which case, don't bother
return isBundled(pkg.spec, modules)
return isAvailable(pkg.spec, modules)
.then(_ => _
? saveCachedResolution()
.then(data => log('package.json', data))
@@ -141,36 +145,6 @@ module.exports = function install (ctx, pkgSpec, modules, options) {
}
}
/*
* Check if a module exists (eg, `node_modules/node-pre-gyp`). This is the case when
* it's part of bundleDependencies.
*
* This check is also responsible for stopping `pnpm i lodash` from doing anything when
* 'node_modules/lodash' already exists.
*/
function isBundled (spec, modules) {
var name = spec && spec.name
if (!name) return Promise.resolve(false)
var packageJsonPath = join(modules, name, 'package.json')
return Promise.resolve()
.then(_ => fs.readFile(packageJsonPath))
.then(_ => JSON.parse(_))
.then(_ => verify(spec, _))
.catch(err => {
if (err.code !== 'ENOENT') throw err
return false
})
function verify (spec, packageJson) {
return packageJson.name === spec.name &&
((spec.type !== 'range' && spec.type !== 'version') ||
semver.satisfies(packageJson.version, spec.spec))
}
}
/*
* Builds to `.store/lodash@4.0.0` (paths.target)
* If an ongoing build is already working, use it. Also, if that ongoing build

View File

@@ -0,0 +1,37 @@
var Promise = require('./promise')
var semver = require('semver')
var join = require('path').join
var fs = require('mz/fs')
/*
* Check if a module exists (eg, `node_modules/node-pre-gyp`). This is the case when
* it's part of bundleDependencies.
*
* This check is also responsible for stopping `pnpm i lodash` from doing anything when
* 'node_modules/lodash' already exists.
*
* spec = { name: 'lodash', spec: '^3.0.2' }
* isAvailable(spec, 'path/to/node_modules')
*/
module.exports = function isAvailable (spec, modules) {
var name = spec && spec.name
if (!name) return Promise.resolve(false)
var packageJsonPath = join(modules, name, 'package.json')
return Promise.resolve()
.then(_ => fs.readFile(packageJsonPath))
.then(_ => JSON.parse(_))
.then(_ => verify(spec, _))
.catch(err => {
if (err.code !== 'ENOENT') throw err
return false
})
function verify (spec, packageJson) {
return packageJson.name === spec.name &&
((spec.type !== 'range' && spec.type !== 'version') ||
semver.satisfies(packageJson.version, spec.spec))
}
}