UNPKG

ember-svg-jar

Version:

Best way to use SVG images in Ember applications

95 lines (75 loc) 1.88 kB
'use strict'; /** Merges two JSON files generated by ViewerAssetsBuilder into a single JSON file that will be used by the asset viewer. Required options: outputFile Optional options: annotation Examples of input and output: Input node: ├── inline.json └── symbol.json Output node: └── output.json inline.json can content: [ { "svg": { "content": "<path />", "attrs": { ... } }, "fileName": "alarm.svg", "strategy": "inline", ... }, { ... } ] symbol.json can content: [ { "svg": { "content": "<path />", "attrs": { ... } }, "fileName": "cat.svg", "strategy": "symbol", ... }, { ... } ] output.json can content: { "assets": [ { "svg": { "content": "<path />", "attrs": { ... } }, "fileName": "alarm.svg", "strategy": "inline", ... }, { "svg": { "content": "<path />", "attrs": { ... } }, "fileName": "cat.svg", "strategy": "symbol", ... }, ] } */ const flatMap = require('lodash/flatMap'); const path = require('path').posix; const CachingWriter = require('broccoli-caching-writer'); const { toPosixPath, readFile, saveToFile } = require('./utils'); class ViewerBuilder extends CachingWriter { constructor(inputNode, opts) { let options = opts || {}; super([inputNode], { name: 'ViewerBuilder', annotation: options.annotation, }); this.options = options; } build() { let outputPath = toPosixPath(this.outputPath); let outputFilePath = path.join(outputPath, this.options.outputFile); let assets = flatMap(this.listFiles(), filePath => JSON.parse(readFile(toPosixPath(filePath))) ); saveToFile(outputFilePath, JSON.stringify({ assets })); } } module.exports = ViewerBuilder;