Skip to content

Commit

Permalink
better naming
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Mar 12, 2016
1 parent 5868cd5 commit 1da1b98
Showing 1 changed file with 34 additions and 39 deletions.
73 changes: 34 additions & 39 deletions app/plugins/bundle-service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,60 +25,55 @@ exports.register = function (plugin, opts) {
}
}

function getPackageLoader (pkgMeta, matchingPkg, opts) {
if (!matchingPkg) {
function getPackageLoader (requestedPkgMeta, matchingPkgJSON, opts) {
if (!matchingPkgJSON) {
debug('No matching version found for ' +
chalk.blue(pkgMeta.name + '@' + pkgMeta.version))
return new Error('no matching version found for ' + pkgMeta.name + '@' +
pkgMeta.version)
chalk.blue(requestedPkgMeta.name + '@' + requestedPkgMeta.version))
return new Error('no matching version found for ' +
requestedPkgMeta.name + '@' + requestedPkgMeta.version)
}
if (matchingPkg.version !== pkgMeta.version) {
debug(chalk.blue(pkgMeta.name + '@' + pkgMeta.version) +
if (matchingPkgJSON.version !== requestedPkgMeta.version) {
debug(chalk.blue(requestedPkgMeta.name + '@' + requestedPkgMeta.version) +
' resolved to ' +
chalk.blue(pkgMeta.name + '@' + matchingPkg.version))
chalk.blue(requestedPkgMeta.name + '@' + matchingPkgJSON.version))
}
const isOverriden = !!overrides[pkgMeta.name]
const isOverriden = !!overrides[requestedPkgMeta.name]
if (isOverriden) {
debug('The requested package ' + chalk.blue(pkgMeta.name) +
debug('The requested package ' + chalk.blue(requestedPkgMeta.name) +
' is overriden locally with ' +
chalk.magenta(overrides[pkgMeta.name].path))
const pkg = localPackage(overrides[pkgMeta.name].path)
chalk.magenta(overrides[requestedPkgMeta.name].path))
const pkg = localPackage(overrides[requestedPkgMeta.name].path)
return {
pkg,
fs: pkg,
json: matchingPkgJSON,
isOverriden,
}
}
const pkg = createPackage(pkgMeta.name, matchingPkg.version, {
const pkg = createPackage(requestedPkgMeta.name, matchingPkgJSON.version, {
registry: opts.registry,
storagePath,
})
return {
pkg,
fs: pkg,
json: matchingPkgJSON,
isOverriden,
}
}

function getMatchingPkg (registryClient, pkgMeta) {
function getMatchingPkgJSON (registryClient, pkgMeta) {
if (overrides[pkgMeta.name]) {
return Promise.resolve(overrides[pkgMeta.name].pkg)
}
return registryClient.resolve(pkgMeta.name, pkgMeta.version)
}

function fetchResources (opts) {
function fetchPackage (opts) {
const reg = registry({
registry: opts.registry,
})
const matchingPkg$ = Rx.Observable
.fromPromise(getMatchingPkg(reg, opts.pkgMeta))

return Rx.Observable.combineLatest(
matchingPkg$,
matchingPkg$.map(matchingPkg =>
getPackageLoader(opts.pkgMeta, matchingPkg, opts)
),
(matchingPkg, packageLoader) => R.merge(packageLoader, {matchingPkg})
)
return Rx.Observable.fromPromise(getMatchingPkgJSON(reg, opts.pkgMeta))
.map(matchingPkgJSON =>
getPackageLoader(opts.pkgMeta, matchingPkgJSON, opts))
}

plugin.expose('get', function (packages, opts) {
Expand Down Expand Up @@ -107,32 +102,32 @@ exports.register = function (plugin, opts) {
]
}

return fetchResources({
return fetchPackage({
pkgMeta,
registry: opts.registry,
})
.flatMap(params => {
.flatMap(pkg => {
return Rx.Observable
.for(getFiles(params.matchingPkg), Rx.Observable.just)
.for(getFiles(pkg.json), Rx.Observable.just)
.flatMapWithMaxConcurrent(1, filePath =>
Rx.Observable.fromPromise(params.pkg.readFile(filePath))
Rx.Observable.fromPromise(pkg.fs.readFile(filePath))
.map(content => ({content, filePath}))
)
.map(file => opts.transformer({
content: file.content,
pkg: {
name: params.matchingPkg.name,
version: params.matchingPkg.version,
name: pkg.json.name,
version: pkg.json.version,
filePath: file.filePath,
},
}))
.pluck('content')
.reduce(R.concat, [])
.map(files => ({
name: params.matchingPkg.name,
version: params.matchingPkg.version,
name: pkg.json.name,
version: pkg.json.version,
files,
maxAge: params.isOverriden ?
maxAge: pkg.isOverriden ?
0 : plugin.plugins.fileMaxAge.getByExtension(opts.extension),
}))
})
Expand All @@ -147,14 +142,14 @@ exports.register = function (plugin, opts) {
throw new Error('opts.registry is required')
}

const fetchResources$ = fetchResources({
const fetchPackage$ = fetchPackage({
pkgMeta,
registry: opts.registry,
})

return Rx.Observable.combineLatest(
fetchResources$.pluck('isOverriden'),
fetchResources$.flatMap(params => params.pkg.streamFile(pkgMeta.file)),
fetchPackage$.pluck('isOverriden'),
fetchPackage$.flatMap(pkg => pkg.fs.streamFile(pkgMeta.file)),
(isOverriden, stream) => ({
stream,
maxAge: isOverriden ?
Expand Down

0 comments on commit 1da1b98

Please sign in to comment.