Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
dedupe: Handle scoped packages
Browse files Browse the repository at this point in the history
  • Loading branch information
KidkArolis authored and othiym23 committed Mar 5, 2015
1 parent 6823807 commit c56cfcd
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 5 deletions.
27 changes: 22 additions & 5 deletions lib/dedupe.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,28 @@ function readInstalled (dir, counter, parent, cb) {
})

fs.readdir(path.resolve(dir, "node_modules"), function (er, c) {
children = c || [] // error is ok, just means no children.
children = children.filter(function (p) {
return !p.match(/^[\._-]/)
})
next()
children = children || [] // error is ok, just means no children.
// check if there are scoped packages.
asyncMap(c || [], function (child, cb) {
if (child.indexOf('@') === 0) {
fs.readdir(path.resolve(dir, "node_modules", child), function (er, scopedChildren) {
// error is ok, just means no children.
;(scopedChildren || []).forEach(function (sc) {
children.push(child + "/" + sc)
})
cb()
})
} else {
children.push(child)
cb()
}
}, function (er) {
if (er) return cb(er)
children = children.filter(function (p) {
return !p.match(/^[\._-]/)
})
next();
});
})

function next () {
Expand Down
71 changes: 71 additions & 0 deletions test/tap/dedupe-scoped.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
var test = require("tap").test
, fs = require("fs")
, path = require("path")
, existsSync = fs.existsSync || path.existsSync
, rimraf = require("rimraf")
, mr = require("npm-registry-mock")
, common = require("../common-tap.js")

var EXEC_OPTS = {}

test("dedupe finds the common scoped modules and moves it up one level", function (t) {
setup(function (s) {
common.npm(
[
"install", ".",
"--registry", common.registry
],
EXEC_OPTS,
function (err, code) {
scopePackages(function () {
t.ifError(err, "successfully installed directory")
t.equal(code, 0, "npm install exited with code")
common.npm(["dedupe"], {}, function (err, code) {
t.ifError(err, "successfully deduped against previous install")
t.notOk(code, "npm dedupe exited with code")
t.ok(existsSync(path.join(__dirname, "dedupe-scoped", "node_modules", "minimist")))
t.ok(!existsSync(path.join(__dirname, "dedupe-scoped", "node_modules", "checker")))
s.close() // shutdown mock registry.
t.end()
})
})
})
})
})

function setup (cb) {
process.chdir(path.join(__dirname, "dedupe-scoped"))
mr({port : common.port}, function (er, s) { // create mock registry.
rimraf.sync(path.join(__dirname, "dedupe-scoped", "node_modules"))
fs.mkdirSync(path.join(__dirname, "dedupe-scoped", "node_modules"))
cb(s)
})
}

function scopePackages (cb) {
scopeAt("minimist", path.join(__dirname, "dedupe-scoped", "node_modules", "optimist"));
scopeAt("minimist", path.join(__dirname, "dedupe-scoped", "node_modules", "clean"));
cb();
}

function scopeAt(pkgName, targetPath) {
var pkg, p, p2;

p = path.join(targetPath, "package.json")
pkg = JSON.parse(fs.readFileSync(p).toString())
pkg.dependencies["@scoped/" + pkgName] = pkg.dependencies[pkgName]
delete pkg.dependencies[pkgName]
fs.writeFileSync(p, JSON.stringify(pkg, null, 2))

p = path.join(targetPath, "node_modules", pkgName, "package.json")
pkg = JSON.parse(fs.readFileSync(p).toString())
pkg.name = "@scoped/" + pkgName
fs.writeFileSync(p, JSON.stringify(pkg, null, 2))

p = path.join(targetPath, "node_modules", "@scoped")
fs.mkdirSync(p)

p = path.join(targetPath, "node_modules", pkgName)
p2 = path.join(targetPath, "node_modules", "@scoped", pkgName)
fs.renameSync(p, p2)
}
9 changes: 9 additions & 0 deletions test/tap/dedupe-scoped/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"author": "Dedupe tester",
"name": "dedupe",
"version": "0.0.0",
"dependencies": {
"optimist": "0.6.0",
"clean": "2.1.6"
}
}

0 comments on commit c56cfcd

Please sign in to comment.