Skip to content

Commit

Permalink
Add back buildAll
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderGugel committed Sep 8, 2016
1 parent 9186736 commit a840839
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 46 deletions.
21 changes: 9 additions & 12 deletions src/todo/build.js → src/build_all.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export const LIFECYCLE_SCRIPTS = [
'postinstall'
]


// error class used for representing an error that occurs due to a lifecycle
// script that exits with a non-zero status code.
inherits(FailedBuildError, Error)
Expand All @@ -39,12 +38,10 @@ export function FailedBuildError () {
* @param {String} dep.script - script to be executed (usually using `sh`).
* @return {Observable} - observable sequence of the returned exit code.
*/
export const build = nodeModules => dep => {
const {target, script} = dep

return Observable.create((observer) => {
// some packages do expect a defined `npm_execpath` env
// eg. https://github.com/chrisa/node-dtrace-provider/blob/v0.6.0/scripts/install.js#L19
export const build = nodeModules => ({target, script}) =>
Observable.create(observer => {
// some packages do expect a defined `npm_execpath` env, e.g.
// https://github.com/chrisa/node-dtrace-provider/blob/v0.6.0/scripts/install.js#L19
const env = {npm_execpath: '', ...process.env}

env.PATH = [
Expand All @@ -57,7 +54,6 @@ export const build = nodeModules => dep => {
cwd: path.join(nodeModules, target, 'package'),
env,
stdio: 'inherit'
// shell: true // does break `dtrace-provider@0.6.0` build
})
childProcess.on('error', (error) => {
observer.error(error)
Expand All @@ -67,14 +63,13 @@ export const build = nodeModules => dep => {
observer.complete()
})
})
}

/**
* extract lifecycle scripts from supplied dependency.
* @param {Dep} dep - dependency to be parsed.
* @return {Array.<Object>} - array of script targets to be executed.
*/
export function parseLifecycleScripts ({target, pkgJson: {scripts = {}}}) {
export const parseLifecycleScripts = ({target, pkgJson: {scripts = {}}}) => {
const results = []
for (let i = 0; i < LIFECYCLE_SCRIPTS.length; i++) {
const name = LIFECYCLE_SCRIPTS[i]
Expand All @@ -92,13 +87,15 @@ export function parseLifecycleScripts ({target, pkgJson: {scripts = {}}}) {
* @return {Observable} - empty observable sequence that will be completed once
* all lifecycle scripts have been executed.
*/
export const buildAll = nodeModules => o =>
o
export default function buildAll (nodeModules) {
return this
::map(parseLifecycleScripts)
::mergeMap(scripts => ArrayObservable.create(scripts))
// build dependencies sequentially.
::concatMap(build(nodeModules))
::every(code => code === 0)
::filter(ok => !ok)
::_do(() => {
throw new FailedBuildError()
})
}
21 changes: 15 additions & 6 deletions src/install_cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@ import {mergeStatic} from 'rxjs/operator/merge'
import {publishReplay} from 'rxjs/operator/publishReplay'
import {skip} from 'rxjs/operator/skip'

import buildAll from './build_all'
import fetchAll from './fetch_all'
import linkAll from './link_all'
import resolveAll from './resolve_all'
import {fromArgv, fromFs, save} from './pkg_json'
import {init as initCache} from './cache'

function installAll (dir) {
return mergeStatic(
this::linkAll(),
this::fetchAll(dir)
function installAll (dir, shouldBuild) {
return concatStatic(
mergeStatic(
this::linkAll(),
this::fetchAll(dir)
),
shouldBuild
? this::buildAll(dir)
: EmptyObservable.create()
)
}

Expand All @@ -26,7 +32,10 @@ const parseArgv = ({_, production}) => ({
})

const shouldSave = argv =>
!!(argv.save || argv['save-dev'] || argv['save-optional'])
argv.save || argv['save-dev'] || argv['save-optional']

const shouldBuild = argv =>
argv.build

export default config => (cwd, argv) => {
const {isExplicit, isProd} = parseArgv(argv)
Expand All @@ -53,7 +62,7 @@ export default config => (cwd, argv) => {
::resolveAll(dir, config)
::skip(1)
::publishReplay().refCount()
::installAll(dir)
::installAll(dir, shouldBuild(argv))

return concatStatic(
initCache(),
Expand Down
2 changes: 1 addition & 1 deletion src/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const isScoped = name =>

// escape the given package name, which can then be used as part of the package
// root URL.
const escapeName = name => (
export const escapeName = name => (
isScoped(name)
? `@${encodeURIComponent(name.substr(1))}`
: encodeURIComponent(name)
Expand Down
21 changes: 0 additions & 21 deletions src:_install_cmd.js

This file was deleted.

8 changes: 4 additions & 4 deletions test/spec/cache.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ describe('cache', () => {
context('when cache.read read stream emits an error', () => {
it('should throw an error', () => {
const readStream = new stream.Readable()
sandbox.stub(cache, 'read').returns(readStream)
sandbox.stub(fs, 'createReadStream').returns(readStream)
const expectedError = new Error()

const next = sandbox.stub()
const error = sandbox.stub()
const complete = sandbox.stub()

cache.extract().subscribe(next, error, complete)
cache.extract('/dest', 'id').subscribe(next, error, complete)
readStream.emit('error', expectedError)

sinon.assert.notCalled(next)
Expand All @@ -92,7 +92,7 @@ describe('cache', () => {
context('when tar.extract read stream emits an error', () => {
it('should thorw an error', () => {
const readStream = new stream.Readable()
sandbox.stub(cache, 'read').returns(new stream.Readable())
sandbox.stub(fs, 'createReadStream').returns(readStream)
sandbox.stub(tar, 'extract').returns(readStream)

const expectedError = new Error()
Expand All @@ -101,7 +101,7 @@ describe('cache', () => {
const error = sandbox.stub()
const complete = sandbox.stub()

cache.extract().subscribe(next, error, complete)
cache.extract('/dest', 'id').subscribe(next, error, complete)
readStream.emit('error', expectedError)

sinon.assert.notCalled(next)
Expand Down
4 changes: 2 additions & 2 deletions test/spec/registry.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ describe('registry', () => {
it('should throw an error', () => {
const response = {statusCode: 400, body: {error: 'Some error'}}
assert.throws(() => {
registry.checkStatus('http://example.com', response)
registry.checkStatus('name', 'version')(response)
})
})
})
context('when statusCode is 200', () => {
it('should not throw an error', () => {
const response = {statusCode: 200, body: {}}
registry.checkStatus('http://example.com', response)
registry.checkStatus('name', 'version')(response)
})
})
})
Expand Down

0 comments on commit a840839

Please sign in to comment.