Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderGugel committed Sep 7, 2016
1 parent f380a2c commit f9516c0
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 66 deletions.
34 changes: 19 additions & 15 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,28 +54,28 @@ 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 = () => {
const finishHandler = () => {
observer.next(tmpDest)
observer.complete()
}
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)
.on('finish', finishHandler)
})
::mergeMap(tmpDest => util.rename(tmpDest, dest)
::retryWhen(errors => errors::mergeMap(err => {
switch (err.code) {
case 'ENOENT': return util.mkdirp(path.dirname(dest))
default: throw err
}
}))
)
}
::mergeMap(tmpDest =>
util.rename(tmpDest, dest)
::retryWhen(errors => errors::mergeMap(err => {
switch (err.code) {
case 'ENOENT': return util.mkdirp(path.dirname(dest))
default: throw err
}
}))
)
45 changes: 26 additions & 19 deletions src/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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':
Expand All @@ -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()
}
})()
}

44 changes: 19 additions & 25 deletions src/fetch.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
Expand All @@ -41,34 +41,28 @@ 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)
})

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()
}
10 changes: 4 additions & 6 deletions src/link_all.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import path from 'path'
import {forceSymlink} from './util'
import {map} from 'rxjs/operator/map'
import {mergeMap} from 'rxjs/operator/mergeMap'
import {forceSymlink} from './util'
import {normalizeBin} from './pkg_json'

const resolveSymlink = (src, dst) =>
[path.relative(path.dirname(dst), src), dst]

const getBinLinks = dep => {
const {pkgJson, parentTarget, target} = dep
const getBinLinks = ({pkgJson, parentTarget, target}) => {
const binLinks = []
const bin = normalizeBin(pkgJson)
const names = Object.keys(bin)
Expand All @@ -21,16 +20,15 @@ const getBinLinks = dep => {
return binLinks
}

const getDirectLink = dep => {
const {parentTarget, target, name} = dep
const getDirectLink = ({parentTarget, target, name}) => {
const src = path.join('node_modules', target, 'package')
const dst = path.join('node_modules', parentTarget, 'node_modules', name)
return [src, dst]
}

export default function linkAll () {
return this
::mergeMap((dep) => [getDirectLink(dep), ...getBinLinks(dep)])
::mergeMap(dep => [getDirectLink(dep), ...getBinLinks(dep)])
::map(([src, dst]) => resolveSymlink(src, dst))
::mergeMap(([src, dst]) => forceSymlink(src, dst))
}
1 change: 1 addition & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
--ui bdd
--compilers js:babel-register
--recursive
--require source-map-support/register
2 changes: 1 addition & 1 deletion test/spec/registry.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit f9516c0

Please sign in to comment.