Skip to content

Commit

Permalink
ImageHome addBuiltInImages
Browse files Browse the repository at this point in the history
  • Loading branch information
cancerberoSgx committed Nov 15, 2018
1 parent 3ff1e01 commit 4ca1718
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
3 changes: 1 addition & 2 deletions spec/executionContextSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ export default describe('executionContext', () => {
it('should addBuiltInImages()', async done => {
let all = await context.getAllFiles()
expect(all.find(i => i.name === 'rose:')).toBeUndefined()
const builtIn = await context.addBuiltInImages()
expect(builtIn.find(i => i.name === 'rose:')).toBeDefined()
await context.addBuiltInImages()
all = await context.getAllFiles()
expect(all.find(i => i.name === 'rose:')).toBeDefined()
done()
Expand Down
14 changes: 14 additions & 0 deletions spec/imageHomeSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,18 @@ export default describe('imageHome', () => {
expect(all.find(f => f.name === 'holocaust.jpg')).toBeTruthy()
done()
})

it('should addBuiltInImages()', async done => {

const imageHome = createImageHome()
let all = await imageHome.getAll()
expect(all.find(i => i.name === 'rose:')).toBeUndefined()
// const builtIn = await context.addBuiltInImages()
// expect(builtIn.find(i => i.name === 'rose:')).toBeDefined()
await imageHome.addBuiltInImages()
all = await imageHome.getAll()// .getAllFiles()
expect(all.find(i => i.name === 'rose:')).toBeDefined()
done()
})

})
33 changes: 11 additions & 22 deletions src/executionContext.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { ImageHome, ExecuteConfig, ExecuteResult, execute, ExecuteCommand, createImageHome, MagickInputFile, MagickFile, extractInfo } from '.'
import pMap from 'p-map'
import { asOutputFile, asInputFile, getBuiltInImages } from './util'
import { createImageHome, execute, ExecuteCommand, ExecuteConfig, ExecuteResult, ImageHome, MagickFile, MagickInputFile } from '.'
import { getBuiltInImages } from './util'

/**
* Allow multiple execute() calls remembering previus execute() generated output files and previous given input files that can be used as input files in next calls.
* Allow multiple execute() calls remembering previus execute() generated output files and previous given input files that
* can be used as input files in next calls.
*/
export interface ExecutionContext {
execute(configOrCommands: ExecuteConfig|ExecuteCommand|string): Promise<ExecuteResult>
/** programmatically add new files so they are available if following execute() calls */
addFiles(files: MagickFile[]): void
/** get all the files currently available in this context */
getAllFiles(): Promise<MagickInputFile[]>
addBuiltInImages(): Promise<MagickFile[]>
// /** add built-in images like `rose:` to this execution context so they are present in getAllFiles() */
addBuiltInImages(): Promise<void>
getFile(name: string): Promise<MagickInputFile>
}

class ExecutionContextImpl implements ExecutionContext {

private builtInImages: MagickInputFile[] = []

constructor(private imageHome: ImageHome = createImageHome()) {
}

Expand All @@ -39,26 +40,14 @@ class ExecutionContextImpl implements ExecutionContext {

async getAllFiles(): Promise<MagickInputFile[]> {
const all = await this.imageHome.getAll()
return all.concat(this.builtInImages)
return all// .concat(this.builtInImages)
}

async getFile(name: string): Promise<MagickInputFile> {
return (await this.imageHome.get(name)) || this.builtInImages.find(i => i.name === name)
return (await this.imageHome.get(name))// || this.builtInImages.find(i => i.name === name)
}

async addBuiltInImages() {
if (!this.builtInImages.length) {
this.builtInImages = await getBuiltInImages()
// const builtInImages = ['rose:', 'logo:', 'wizard:', 'granite:', 'netscape:']
// this.builtInImages = await pMap(builtInImages, async name=>{
// const info = await extractInfo(name)
// // const outputName = `output1.${info[0].image.format.toLowerCase()}`
// const {outputFiles} = await execute({commands:`convert ${name} ${`output1.${info[0].image.format.toLowerCase()}`}`} )
// outputFiles[0].name = name
// return await asInputFile(outputFiles[0])
// })
}
return this.builtInImages
return this.imageHome.addBuiltInImages()
}

static create(inheritFrom?: ExecutionContext) {
Expand Down
14 changes: 12 additions & 2 deletions src/imageHome.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { MagickInputFile, MagickFile } from '.'
import { asInputFile } from './util/file'
import { MagickInputFile, MagickFile, asInputFile, getBuiltInImages } from '.'
import pMap from 'p-map'

export interface ImageHome {
get(name: string): Promise<MagickInputFile>
register(file: MagickFile, name?: string): void
isRegistered(name: string): boolean
getAll(): Promise<MagickInputFile[]>
addBuiltInImages(): Promise<void>
}

type MagickInputFilePromise = Promise<MagickInputFile> & { resolved: true }

class ImageHomeImpl implements ImageHome {

private images: { [name: string]: MagickInputFilePromise } = {}
private builtInImagesAdded: boolean = false

get(name: string): Promise<MagickInputFile> {
return this.images[name]
Expand All @@ -34,5 +36,13 @@ class ImageHomeImpl implements ImageHome {
isRegistered(name: string, andReady: boolean= true): boolean {
return this.images[name] && (andReady && this.images[name].resolved)
}

async addBuiltInImages() {
if (!this.builtInImagesAdded) {
await pMap(await getBuiltInImages(), img => this.register(img))
this.builtInImagesAdded = true
}
}

}
export function createImageHome() {return new ImageHomeImpl()}

0 comments on commit 4ca1718

Please sign in to comment.