From 92ae0ff29cf5473a033f2571649684e8dac984fc Mon Sep 17 00:00:00 2001 From: Alexander Gugel Date: Wed, 7 Sep 2016 02:01:52 +0100 Subject: [PATCH] WIP --- src/cache.js | 13 ++++++----- src/cmd.js | 45 ++++++++++++++++++++++---------------- src/fetch.js | 44 ++++++++++++++++--------------------- test/mocha.opts | 1 + test/spec/registry.spec.js | 2 +- 5 files changed, 55 insertions(+), 50 deletions(-) diff --git a/src/cache.js b/src/cache.js index 2759fd5..edbfd69 100644 --- a/src/cache.js +++ b/src/cache.js @@ -42,6 +42,10 @@ export const write = () => export const read = id => fs.createReadStream(path.join(config.cacheDir, id)) +const extractOptions = { + strip: 1 +} + /** * extract a dependency from the cache. * @param {String} dest - pathname into which the cached dependency should be @@ -50,10 +54,10 @@ export const read = id => * @return {Observable} - observable sequence that will be completed once * the cached dependency has been fetched. */ -export function extract (dest, id) { - return Observable.create((observer) => { +export const extract = (dest, id) => + Observable.create(observer => { const tmpDest = getTmp() - const untar = tar.extract(tmpDest, {strip: 1}) + const untar = tar.extract(tmpDest, extractOptions) const completeHandler = () => { observer.next(tmpDest) @@ -61,7 +65,7 @@ export function extract (dest, id) { } const errorHandler = err => observer.error(err) - this.read(id).on('error', errorHandler) + read(id).on('error', errorHandler) .pipe(gunzip()).on('error', errorHandler) .pipe(untar).on('error', errorHandler) .on('finish', completeHandler) @@ -74,4 +78,3 @@ export function extract (dest, id) { } })) ) -} diff --git a/src/cmd.js b/src/cmd.js index 80a58c7..d35d73a 100755 --- a/src/cmd.js +++ b/src/cmd.js @@ -3,10 +3,6 @@ import minimist from 'minimist' import * as config from './config' -if (['development', 'test'].indexOf(process.env.NODE_ENV) !== -1) { - require('source-map-support').install() -} - const alias = { h: 'help', v: 'version', @@ -33,7 +29,12 @@ const boolean = [ ] const cwd = process.cwd() -const argv = minimist(process.argv.slice(2), {alias, string, boolean}) + +const argv = minimist(process.argv.slice(2), { + alias, + string, + boolean +}) if (argv.registry) { config.registry = argv.registry @@ -51,19 +52,13 @@ let helpCmd let versionCmd let cacheCmd -(() => { - if (argv.help) { - helpCmd = require('./help_cmd').default - helpCmd().subscribe() - return - } - - if (argv.version) { - versionCmd = require('./version_cmd').default - versionCmd().subscribe() - return - } - +if (argv.help) { + helpCmd = require('./help_cmd').default + helpCmd().subscribe() +} else if (argv.version) { + versionCmd = require('./version_cmd').default + versionCmd().subscribe() +} else { const [subCommand] = argv._ switch (subCommand) { @@ -72,17 +67,20 @@ let cacheCmd installCmd = require('./install_cmd').default(config) installCmd(cwd, argv).subscribe() break + case 'sh': case 'shell': shellCmd = require('./shell_cmd').default(config) shellCmd(cwd).subscribe() break + case 'r': case 'run': case 'run-script': runCmd = require('./run_cmd').default runCmd(cwd, argv).subscribe() break + case 't': case 'test': case 'start': @@ -91,37 +89,46 @@ let cacheCmd runCmd = require('./run_cmd').default runCmd(cwd, {...argv, _: ['run'].concat(argv._)}).subscribe() break + case 'ping': pingCmd = require('./ping_cmd').default(config) pingCmd().subscribe() break + case 'conf': case 'config': configCmd = require('./config_cmd').default(config) configCmd() break + case 'init': initCmd = require('./init_cmd').default(config) initCmd(cwd, argv).subscribe() break + case 'link': linkCmd = require('./link_cmd').default(config) linkCmd(cwd, argv).subscribe() break + case 'unlink': unlinkCmd = require('./unlink_cmd').default unlinkCmd(cwd, argv).subscribe() break + case 'cache': cacheCmd = require('./cache_cmd').default(config) cacheCmd(cwd, argv) break + case 'version': versionCmd = require('./version_cmd').default versionCmd().subscribe() break + default: helpCmd = require('./help_cmd').default helpCmd().subscribe() } -})() +} + diff --git a/src/fetch.js b/src/fetch.js index a65c572..6380285 100644 --- a/src/fetch.js +++ b/src/fetch.js @@ -1,12 +1,12 @@ +import assert from 'assert' import crypto from 'crypto' +import needle from 'needle' import path from 'path' import {Observable} from 'rxjs/Observable' -import {concatStatic} from 'rxjs/operator/concat' import {_catch} from 'rxjs/operator/catch' +import {concatStatic} from 'rxjs/operator/concat' +import {ignoreElements} from 'rxjs/operator/ignoreElements' import {mergeMap} from 'rxjs/operator/mergeMap' -import {skip} from 'rxjs/operator/skip' -import needle from 'needle' -import assert from 'assert' import * as cache from './cache' import * as config from './config' @@ -18,13 +18,13 @@ export const checkShasum = (shasum, expected, tarball) => `shasum mismatch for ${tarball}: ${shasum} <-> ${expected}`) const download = (tarball, expected, type) => - Observable.create((observer) => { + Observable.create(observer => { const shasum = crypto.createHash('sha1') const response = needle.get(tarball, config.httpOptions) const cached = response.pipe(cache.write()) - const errorHandler = (error) => observer.error(error) - const dataHandler = (chunk) => shasum.update(chunk) + const errorHandler = error => observer.error(error) + const dataHandler = chunk => shasum.update(chunk) const finishHandler = () => { const actualShasum = shasum.digest('hex') // only actually check shasum integrity for npm tarballs @@ -41,9 +41,7 @@ const download = (tarball, expected, type) => cached.on('finish', finishHandler) }) ::mergeMap(({tmpPath, shasum}) => { - if (expected) { - checkShasum(shasum, expected, tarball) - } + if (expected) checkShasum(shasum, expected, tarball) const newPath = path.join(config.cacheDir, shasum) return util.rename(tmpPath, newPath) @@ -51,24 +49,20 @@ const download = (tarball, expected, type) => export default function fetch (nodeModules) { const {target, type, pkgJson: {name, bin, dist: {tarball, shasum}}} = this - const where = path.join(nodeModules, target, 'package') + const packageDir = path.join(nodeModules, target, 'package') - return util.stat(where)::skip(1)::_catch(err => { - if (err.code !== 'ENOENT') { - throw err - } - const extracted = cache.extract(where, shasum)::_catch(err2 => { - if (err2.code !== 'ENOENT') { - throw err2 - } + return util.stat(packageDir) + ::_catch(err => { + if (err.code !== 'ENOENT') throw err + return cache.extract(packageDir, shasum) + }) + ::_catch(err => { + if (err.code !== 'ENOENT') throw err return concatStatic( download(tarball, shasum, type), - cache.extract(where, shasum) + cache.extract(packageDir, shasum), + util.fixPermissions(packageDir, normalizeBin({name, bin})) ) }) - return concatStatic( - extracted, - util.fixPermissions(where, normalizeBin({name, bin})) - ) - }) + ::ignoreElements() } diff --git a/test/mocha.opts b/test/mocha.opts index 017a516..9522037 100644 --- a/test/mocha.opts +++ b/test/mocha.opts @@ -1,3 +1,4 @@ --ui bdd --compilers js:babel-register --recursive +--require source-map-support/register diff --git a/test/spec/registry.spec.js b/test/spec/registry.spec.js index 00be783..3ffa816 100644 --- a/test/spec/registry.spec.js +++ b/test/spec/registry.spec.js @@ -2,7 +2,7 @@ import * as registry from '../../src/registry' import assert from 'assert' import sinon from 'sinon' -describe.only('registry', () => { +describe('registry', () => { const sandbox = sinon.sandbox.create() afterEach(() => sandbox.restore())