This repository has been archived by the owner on Aug 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lifecycle): run scripts in dep order
Fixes: #16
- Loading branch information
Showing
4 changed files
with
328 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
'use strict' | ||
|
||
class LogicalTree { | ||
constructor (name, lockNode, address) { | ||
this.name = name | ||
this.version = lockNode.version | ||
this.address = address | ||
this.optional = !!lockNode.optional | ||
this.dev = !!lockNode.dev | ||
this.bundled = !!lockNode.bundled | ||
this.resolved = lockNode.resolved | ||
this.integrity = lockNode.integrity | ||
this.dependencies = new Map() | ||
this.pending = null | ||
} | ||
|
||
addDep (dep) { | ||
this.dependencies.set(dep.name, dep) | ||
} | ||
|
||
getDep (name) { | ||
return this.dependencies.get(name) | ||
} | ||
} | ||
|
||
module.exports = logicalTree | ||
function logicalTree (pkg, pkgLock, opts) { | ||
const tree = new LogicalTree(pkg.name, pkg, null) | ||
const allDeps = new Map() | ||
Array.from( | ||
new Set(Object.keys(pkg.devDependencies || {}) | ||
.concat(Object.keys(pkg.optionalDependencies || {})) | ||
.concat(Object.keys(pkg.dependencies || {}))) | ||
).forEach(name => { | ||
let dep = allDeps.get(name) | ||
if (!dep) { | ||
dep = new LogicalTree(name, (pkgLock.dependencies || {})[name] || {}, name) | ||
} | ||
addChild(dep, tree, allDeps, pkgLock) | ||
}) | ||
return tree | ||
} | ||
|
||
function addChild (dep, tree, allDeps, pkgLock) { | ||
tree.addDep(dep) | ||
allDeps.set(dep.address, dep) | ||
const addr = dep.address | ||
const lockNode = atAddr(pkgLock, addr) | ||
Object.keys(lockNode.requires || {}).forEach(name => { | ||
const tdepAddr = reqAddr(pkgLock, name, lockNode.requires[name], addr) | ||
let tdep = allDeps.get(tdepAddr) | ||
if (!tdep) { | ||
tdep = new LogicalTree(name, atAddr(pkgLock, tdepAddr), tdepAddr) | ||
addChild(tdep, dep, allDeps, pkgLock) | ||
} else { | ||
dep.addDep(tdep) | ||
} | ||
}) | ||
} | ||
|
||
module.exports._reqAddr = reqAddr | ||
function reqAddr (pkgLock, name, version, fromAddr) { | ||
const lockNode = atAddr(pkgLock, fromAddr) | ||
const child = (lockNode.dependencies || {})[name] | ||
if (child && child.version === version) { | ||
return `${fromAddr}:${name}` | ||
} else { | ||
const parts = fromAddr.split(':') | ||
while (parts.length) { | ||
parts.pop() | ||
const parent = atAddr(pkgLock, parts.join(':')) | ||
if (parent) { | ||
const child = (parent.dependencies || {})[name] | ||
if (child && child.version === version) { | ||
return `${parts.join(':')}${parts.length ? ':' : ''}${name}` | ||
} | ||
} | ||
} | ||
throw new Error('req not found') | ||
} | ||
} | ||
|
||
module.exports._atAddr = atAddr | ||
function atAddr (pkgLock, addr) { | ||
if (!addr.length) { return pkgLock } | ||
const parts = addr.split(':') | ||
return parts.reduce((acc, next) => { | ||
return acc && (acc.dependencies || {})[next] | ||
}, pkgLock) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.