Commit 3189b21e authored by Dan Allen's avatar Dan Allen

consolidate file collections in content catalog

- generate string-based ID
- use string-based ID to index files
- store files in a single collection
parent 0892463b
'use strict'
const path = require('path')
const _ = require('lodash')
const $files = Symbol('$files')
const $filesIndex = Symbol('$filesIndex')
const $generateId = Symbol('$generateId')
// if we really expose addFile(), this class would need to be exposed
class FileCatalog {
constructor () {
this[$files] = []
this[$filesIndex] = {}
this[$files] = {}
}
getFiles () {
return this[$files]
return _.values(this[$files])
}
addFile (file) {
const id = [
file.src.component,
file.src.version,
file.src.module,
file.src.family,
file.src.subpath,
file.src.basename,
]
if (_.get(this[$filesIndex], id) != null) {
const id = this[$generateId](_.pick(file.src, 'component', 'version', 'module', 'family', 'subpath', 'basename'))
if (_.has(this[$files], id)) {
throw new Error('Duplicate file')
}
_.set(this[$filesIndex], id, file)
this[$files].push(file)
this[$files][id] = file
}
findBy (options) {
......@@ -40,8 +30,12 @@ class FileCatalog {
}
getById ({ component, version, module, family, subpath, basename }) {
const id = [component, version, module, family, subpath, basename]
return _.get(this[$filesIndex], id)
const id = this[$generateId]({ component, version, module, family, subpath, basename })
return this[$files][id]
}
[$generateId] ({ component, version, module, family, subpath, basename }) {
return `${family}/${version}@${component}:${module}:${subpath}${subpath ? '/' : ''}${basename}`
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment