diff --git a/cmd.js b/cmd.js index 80f1f9f..d3ccbc8 100755 --- a/cmd.js +++ b/cmd.js @@ -10,6 +10,7 @@ var deepEqual = require('deep-equal') var find = require('findit') var minimist = require('minimist') var parallel = require('run-parallel') +var yarnlock = require('@yarnpkg/lockfile') var allShims = require('./shims') var coreList = require('./coreList') var browser = require('./browser') @@ -21,11 +22,12 @@ var argv = minimist(process.argv.slice(2), { h: 'help', i: 'install', e: 'hack', - o: 'overwrite' + o: 'overwrite', + y: 'yarn' } }) -var BASE_INSTALL_LINE = 'npm install --save' +var BASE_INSTALL_LINE = argv.yarn ? 'yarn add' : 'npm install --save' if (argv.help) { runHelp() @@ -114,6 +116,25 @@ function installShims ({ modules, overwrite }, done) { return finish() } + // Load the exact package versions from the lockfile + var lockfile + if (argv.yarn) { + if (fs.existsSync('yarn.lock')) { + let result = yarnlock.parse(fs.readFileSync('yarn.lock', 'utf8')) + if (result.type == 'success') { + lockfile = result.object + } + } + } else { + var lockpath = path.join(process.cwd(), 'package-lock.json') + if (fs.existsSync(lockpath)) { + let result = require(lockpath) + if (result && result.dependencies) { + lockfile = result.dependencies + } + } + } + parallel(shimPkgNames.map(function (name) { var modPath = path.resolve('./node_modules/' + name) return function (cb) { @@ -121,17 +142,44 @@ function installShims ({ modules, overwrite }, done) { if (!exists) return cb() var install = true - var pkgJson = require(modPath + '/package.json') - if (/^git\:\/\//.test(pkgJson._resolved)) { - var hash = allShims[name].split('#')[1] - if (hash && pkgJson.gitHead.indexOf(hash) === 0) { - install = false + if (lockfile) { + // Use the lockfile to resolve installed version of package + if (argv.yarn) { + if (`${name}@${allShims[name]}` in lockfile) { + install = false + } + } else { + var lockfileVer = (lockfile[name] || {}).version + var targetVer = allShims[name] + if (semver.valid(lockfileVer)) { + if (semver.satisfies(lockfileVer, targetVer)) { + install = false + } + } else if (lockfileVer) { + // To be considered up-to-date, we need an exact match, + // after doing some normalization of github url's + if (lockfileVer.startsWith('github:')) { + lockfileVer = lockfileVer.slice(7) + } + if (lockfileVer.indexOf(targetVer) == 0) { + install = false + } + } } } else { - var existingVerNpm5 = (/\-([^\-]+)\.tgz/.exec(pkgJson.version) || [null, null])[1] - var existingVer = existingVerNpm5 || pkgJson.version - if (semver.satisfies(existingVer, allShims[name])) { - install = false + // Fallback to using the version from the dependency's package.json + var pkgJson = require(modPath + '/package.json') + if (/^git\:\/\//.test(pkgJson._resolved)) { + var hash = allShims[name].split('#')[1] + if (hash && pkgJson.gitHead.indexOf(hash) === 0) { + install = false + } + } else { + var existingVerNpm5 = (/\-([^\-]+)\.tgz/.exec(pkgJson.version) || [null, null])[1] + var existingVer = existingVerNpm5 || pkgJson.version + if (semver.satisfies(existingVer, allShims[name])) { + install = false + } } } @@ -155,7 +203,11 @@ function installShims ({ modules, overwrite }, done) { let version = allShims[name] if (!version) return if (version.indexOf('/') === -1) { - log('installing from npm', name) + if (argv.yarn) { + log('installing from yarn', name) + } else { + log('installing from npm', name) + } installLine += name + '@' + version } else { // github url @@ -309,6 +361,8 @@ function runHelp () { -h --help show usage -e, --hack run package-specific hacks (list or leave blank to run all) -i, --install install shims (list or leave blank to install all) + -o, --overwrite updates installed packages if a newer version is available + -y, --yarn use yarn to install packages instead of npm (experimental) Please report bugs! https://github.com/mvayngrib/rn-nodeify/issues */ diff --git a/package.json b/package.json index 484e810..e02ff90 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "bin": "./cmd.js", "license": "MIT", "dependencies": { + "@yarnpkg/lockfile": "^1.0.0", "deep-equal": "^1.0.0", "findit": "^2.0.0", "fs-extra": "^0.22.1",