Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Commit

Permalink
feat(directory): implement local dir packing
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Apr 14, 2017
1 parent 5825d33 commit 017d989
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 60 deletions.
8 changes: 4 additions & 4 deletions lib/handlers/directory/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ function manifest (spec, opts) {
).then(pkg => {
if (!pkg.bin && pkg.directories && pkg.directories.bin) {
const dirBin = pkg.directories.bin
return glob(path.join(spec.spec, dirBin, '/**')).then(matches => {
return glob(path.join(spec.spec, dirBin, '/**'), {nodir: true}).then(matches => {
matches.forEach(filePath => {
const relative = path.relative(dirBin, filePath)
const relative = path.relative(spec.spec, filePath)
if (relative && relative[0] !== '.') {
if (!pkg.bin) { pkg.bin = {} }
pkg.bin[path.basename(relative)] = path.join(dirBin, relative)
pkg.bin[path.basename(relative)] = relative
}
})
})
}).then(() => pkg)
} else {
return pkg
}
Expand Down
26 changes: 22 additions & 4 deletions lib/handlers/directory/tarball.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
'use strict'

module.exports = tarballNope
module.exports.fromManifest = tarballNope
const BB = require('bluebird')

function tarballNope () {
return null
const manifest = require('./manifest')
const packDir = require('../../util/pack-dir')
const through = require('mississippi').through
const pipe = BB.promisify(require('mississippi').pipe)

module.exports = tarball
function tarball (spec, opts) {
const stream = through()
manifest(spec, opts).then(mani => {
return pipe(fromManifest(mani, spec, opts), stream)
}).catch(err => stream.emit('error', err))
return stream
}

module.exports.fromManifest = fromManifest
function fromManifest (manifest, spec, opts) {
const stream = through()
packDir(manifest, manifest._resolved, manifest._resolved, stream, opts).catch(err => {
stream.emit('error', err)
})
return stream
}
32 changes: 2 additions & 30 deletions lib/handlers/git/tarball.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const git = require('../../util/git')
const mkdirp = BB.promisify(require('mkdirp'))
const optCheck = require('../../util/opt-check')
const osenv = require('osenv')
const packDir = require('../../util/pack-dir')
const PassThrough = require('stream').PassThrough
const path = require('path')
const pipe = BB.promisify(require('mississippi').pipe)
const rimraf = BB.promisify(require('rimraf'))
const tar = require('tar-fs')
const uniqueFilename = require('unique-filename')

const gitManifest = require('./manifest')
Expand All @@ -35,7 +35,7 @@ function fromManifest (manifest, spec, opts) {
const cacheStream = (
opts.cache &&
cache.get.stream(
opts.cache, cache.key('git-clone', manifest._resolved), opts
opts.cache, cache.key('packed-dir', manifest._resolved), opts
)
)
cacheStream.pipe(stream)
Expand Down Expand Up @@ -77,31 +77,3 @@ function cloneRepo (repo, resolvedRef, rawRef, tmp, opts) {
return git.clone(repo, rawRef, tmp, opts)
}
}

function packDir (manifest, label, tmp, target, opts) {
opts = optCheck(opts)

const packer = opts.dirPacker
? opts.dirPacker(manifest, tmp)
: tar.pack(tmp, {
map: header => {
header.name = header.name.replace(tmp, 'package')
header.mtime = 0 // make tarballs idempotent
},
ignore: (name) => {
return name.match(/\.git/)
}
})

if (!opts.cache) {
return pipe(packer, target)
} else {
const cacher = cache.put.stream(
opts.cache, cache.key('git-clone', label), opts
)
return Promise.all([
pipe(packer, cacher),
pipe(packer, target)
])
}
}
41 changes: 41 additions & 0 deletions lib/util/pack-dir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

const BB = require('bluebird')

const cache = require('../cache')
const optCheck = require('./opt-check')
const pipe = BB.promisify(require('mississippi').pipe)
const tar = require('tar-fs')

module.exports = packDir
function packDir (manifest, label, dir, target, opts) {
opts = optCheck(opts)

const packer = opts.dirPacker
? opts.dirPacker(manifest, dir)
: tar.pack(dir, {
map: header => {
header.name = header.name.replace(dir, 'package')
header.mtime = 0 // make tarballs idempotent
},
ignore: (name) => {
return name.match(/\.git/)
}
})

if (!opts.cache) {
console.log('piping!')
return pipe(packer, target).catch(err => {
console.log('got an error:', err)
throw err
})
} else {
const cacher = cache.put.stream(
opts.cache, cache.key('packed-dir', label), opts
)
return BB.all([
pipe(packer, cacher),
pipe(packer, target)
])
}
}
2 changes: 1 addition & 1 deletion prefetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function prefetchByManifest (start, spec, opts) {
return {
manifest,
spec,
integrity: integrity || manifest._integrity,
integrity: integrity || (manifest && manifest._integrity),
byDigest: false
}
})
Expand Down
66 changes: 45 additions & 21 deletions test/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const mkdirp = BB.promisify(require('mkdirp'))
const path = require('path')
const test = require('tap').test

const dirManifest = require('../lib/handlers/directory/manifest')
const dirTarball = require('../lib/handlers/directory/tarball')
// const extract = require('../extract')
const manifest = require('../manifest')

const CACHE = require('./util/test-dir')(__filename)

test('supports directory dep manifests', t => {
test('supports directory deps', t => {
const pkg = {
name: 'foo',
version: '1.2.3',
Expand All @@ -23,35 +23,59 @@ test('supports directory dep manifests', t => {
const sr = {
name: 'foo',
version: '1.2.3',
isShrinkwrap: true,
dependencies: { bar: '3.2.1' }
}
return mkdirp(path.join(CACHE, 'x')).then(() => {
const PKG = path.join(CACHE, 'pkg')
// const EXT = path.join(CACHE, 'extracted')
return mkdirp(path.join(PKG, 'x')).then(() => {
return BB.join(
fs.writeFileAsync(
path.join(CACHE, 'package.json'), JSON.stringify(pkg)
path.join(PKG, 'package.json'), JSON.stringify(pkg)
),
fs.writeFileAsync(
path.join(CACHE, 'npm-shrinkwrap.json'), JSON.stringify(sr)
path.join(PKG, 'npm-shrinkwrap.json'), JSON.stringify(sr)
),
fs.writeFileAsync(
path.join(CACHE, 'x', 'mybin'), 'console.log("hi there")'
path.join(PKG, 'x', 'mybin'), 'console.log("hi there")'
)
)
}).then(() => {
return dirManifest({
type: 'directory',
spec: CACHE
})
return manifest(PKG)
}).then(manifest => {
t.deepEqual(manifest, null, 'got a filled-out manifest')
})
})

test('returns null instead of a stream for tarballs', t => {
return mkdirp(CACHE).then(() => {
return dirTarball({type: 'directory', spec: CACHE})
}).then(tarball => {
t.deepEqual(tarball, null, 'got null back')
t.deepEqual(manifest, {
name: pkg.name,
version: pkg.version,
dependencies: pkg.dependencies,
optionalDependencies: {},
devDependencies: {},
bundleDependencies: false,
peerDependencies: {},
deprecated: false,
_resolved: path.resolve(PKG),
_integrity: null,
_shrinkwrap: sr,
bin: { mybin: path.join('x', 'mybin') },
_id: `${pkg.name}@${pkg.version}`
}, 'got a filled-out manifest')
// TODO - this is spitting out a seriously bizarre error?
// }).then(() => {
// return extract(PKG, EXT)
// }).then(() => {
// return BB.join(
// fs.readFileAsync(
// path.join(EXT, 'package.json'), 'utf8'
// ),
// fs.readFileAsync(
// path.join(EXT, 'npm-shrinkwrap.json'), 'utf8'
// ),
// fs.readFileAsync(
// path.join(EXT, 'x', 'mybin'), 'utf8'
// ),
// (xpkg, xsr, xbin) => {
// t.deepEqual(JSON.parse(xpkg), pkg, 'extracted package.json')
// t.deepEqual(JSON.parse(xsr), sr, 'extracted npm-shrinkwrap.json')
// t.deepEqual(xbin, 'console.log("hi there")', 'extracted binary')
// }
// )
})
})

0 comments on commit 017d989

Please sign in to comment.