Skip to content

Commit

Permalink
Basic yarn support (tradle#51)
Browse files Browse the repository at this point in the history
* Add yarn argument to control install command

* Use lockfile to resolve installed pkg version when doing semver check
  • Loading branch information
jd20 authored and mvayngrib committed Sep 20, 2017
1 parent 91913e1 commit 55575c0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 12 deletions.
78 changes: 66 additions & 12 deletions cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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()
Expand Down Expand Up @@ -114,24 +116,70 @@ 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) {
fs.exists(modPath, function (exists) {
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
}
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 55575c0

Please sign in to comment.