@pzlr/build-core
Advanced tools
Comparing version 2.4.2 to 2.5.0
@@ -52,6 +52,8 @@ declare namespace PzlrBuildCore { | ||
type BlockMap = Map<string, Block>; | ||
class Block { | ||
static get(name: string): Promise<Block>; | ||
static getAll(names?: string[]): Promise<Map<string, Block>>; | ||
static getAll(names?: string[]): Promise<BlockMap>; | ||
@@ -73,6 +75,12 @@ readonly name: string; | ||
getParent(): Promise<Block | null>; | ||
getParent({cache}: {cache?: BlockMap}): Promise<Block | null>; | ||
getDependencies(onlyOwn?: boolean): Promise<Block[]>; | ||
getDependencies(): Promise<BlockMap>; | ||
getDependencies({onlyOwn, cache}: {onlyOwn?: boolean; cache?: BlockMap}): Promise<BlockMap>; | ||
getLibs(onlyOwn?: boolean): Promise<string[]>; | ||
getLibs(): Promise<Set<string>>; | ||
getLibs({onlyOwn, cache}: {onlyOwn?: boolean; cache?: BlockMap}): Promise<Set<string>>; | ||
getRuntimeDependencies(): Promise<{runtime: BlockMap, parents: BlockMap, libs: Set<string>}>; | ||
getRuntimeDependencies({cache}: {cache?: BlockMap}): Promise<{runtime: BlockMap, parents: BlockMap, libs: Set<string>}>; | ||
} | ||
@@ -79,0 +87,0 @@ } |
113
lib/block.js
@@ -44,2 +44,6 @@ 'use strict'; | ||
function blockNotFound(name) { | ||
throw new Error(`Block "${name}" is not defined`); | ||
} | ||
class Block { | ||
@@ -240,6 +244,19 @@ /** | ||
* Returns the block parent manifest | ||
* | ||
* @param {Map<string, !Block>=} [cache] - optional cache object with predefined blocks | ||
* @returns {!Promise<Block>} | ||
*/ | ||
async getParent() { | ||
return this.parent ? this.constructor.get(this.parent) : null; | ||
async getParent({cache} = {}) { | ||
if (!this.parent) { | ||
return null; | ||
} | ||
const | ||
block = cache ? cache.get(this.parent) : await this.constructor.get(this.parent); | ||
if (!block) { | ||
blockNotFound(this.parent); | ||
} | ||
return block; | ||
} | ||
@@ -251,6 +268,7 @@ | ||
* @param {boolean=} [onlyOwn] - if true parent dependencies also included | ||
* @returns {!Promise<!Array<!Block>>} | ||
* @param {Map<string, !Block>=} [cache] - optional cache object with predefined blocks | ||
* @returns {!Promise<!Map<string, !Block>>} | ||
*/ | ||
async getDependencies(onlyOwn = false) { | ||
const | ||
async getDependencies({onlyOwn, cache} = {}) { | ||
let | ||
names = this.dependencies.slice(); | ||
@@ -260,11 +278,24 @@ | ||
let | ||
parent = await this.getParent(); | ||
parent = await this.getParent({cache}); | ||
while (parent) { | ||
Sugar.Array.insert(names, parent.dependencies, 0); | ||
parent = await parent.getParent(); | ||
names = [...parent.dependencies, ...names]; | ||
parent = await parent.getParent({cache}); | ||
} | ||
} | ||
return this.constructor.getAll(Sugar.Array.unique(names)); | ||
if (cache) { | ||
return $C(names).reduce((map, name) => { | ||
const | ||
block = cache.get(name); | ||
if (!block) { | ||
blockNotFound(name); | ||
} | ||
return map.set(name, block); | ||
}, new Map()); | ||
} | ||
return this.constructor.getAll([...new Set(names)]); | ||
} | ||
@@ -276,6 +307,7 @@ | ||
* @param {boolean=} [onlyOwn] - if true parent libraries also included | ||
* @returns {!Promise<!Array<string>>} | ||
* @param {Map<string, !Block>=} [cache] - optional cache object with predefined blocks | ||
* @returns {!Promise<!Set<string>>} | ||
*/ | ||
async getLibs(onlyOwn = false) { | ||
const | ||
async getLibs({onlyOwn, cache} = {}) { | ||
let | ||
libs = this.libs.slice(); | ||
@@ -285,14 +317,63 @@ | ||
let | ||
parent = await this.getParent(); | ||
parent = await this.getParent({cache}); | ||
while (parent) { | ||
Sugar.Array.insert(libs, parent.libs, 0); | ||
parent = await parent.getParent(); | ||
libs = [...parent.libs, ...libs]; | ||
parent = await parent.getParent({cache}); | ||
} | ||
} | ||
return libs; | ||
return new Set(libs); | ||
} | ||
/** | ||
* Returns an object with block runtime dependencies | ||
* | ||
* @param {Map<string, !Block>=} [cache] - optional cache object with predefined blocks | ||
* @returns {Promise<{runtime: !Map<string, !Block>, parents: !Map<string, !Block>, libs: !Set<string>}>} | ||
*/ | ||
async getRuntimeDependencies({cache} = {}) { | ||
const get = async (name, isParent, runtime = new Map(), parents = new Map()) => { | ||
const | ||
block = name === this.name ? this : cache ? cache.get(name) : await this.constructor.get(name); | ||
if (!block) { | ||
blockNotFound(name); | ||
} | ||
runtime = new Map([[name, block], ...runtime.entries()]); | ||
if (!isParent && parents.has(name)) { | ||
parents.delete(name); | ||
} | ||
await $C(block.getDependencies({onlyOwn: true, cache})).async.forEach(async (block) => { | ||
runtime = new Map([[block.name, block], ...runtime.entries()]); | ||
await get(block.name, false, runtime, parents); | ||
}); | ||
const | ||
parentName = block.parent; | ||
if (parentName) { | ||
const | ||
parent = await block.getParent({cache}); | ||
if (!runtime.has(parentName)) { | ||
parents = new Map([[parentName, parent], ...parents.entries()]); | ||
runtime = new Map([[parentName, parent], ...runtime.entries()]); | ||
} | ||
await get(parentName, true, runtime, parents); | ||
} | ||
return { | ||
runtime, | ||
parents | ||
}; | ||
}; | ||
return Object.assign(await get(this.name), {libs: await this.getLibs()}); | ||
} | ||
} | ||
module.exports = Block; |
@@ -7,3 +7,3 @@ { | ||
"typings": "index.d.ts", | ||
"version": "2.4.2", | ||
"version": "2.5.0", | ||
"license": "MIT", | ||
@@ -10,0 +10,0 @@ "author": { |
60037
836