|
| 1 | +/** |
| 2 | + Copyright (c) 2015, 2017, Oracle and/or its affiliates. |
| 3 | + The Universal Permissive License (UPL), Version 1.0 |
| 4 | +*/ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +const fs = require('fs-extra'); |
| 8 | +const util = require('./util'); |
| 9 | +const path = require('path'); |
| 10 | +const SVGO = require('svgo'); |
| 11 | +const glob = require('glob'); |
| 12 | +const config = require('./config'); |
| 13 | +const CONSTANTS = require('./constants'); |
| 14 | +const SVGSpriter = require('svg-sprite'); |
| 15 | + |
| 16 | +/** |
| 17 | + * Return the promise to optimize user provided svg files, remove redundant content |
| 18 | + * @param {Object} Running context that contains all options |
| 19 | + * @returns {Promise} return the promise |
| 20 | + */ |
| 21 | + |
| 22 | +function _svgMin(context) { |
| 23 | + const opts = context.opts.svgMin; |
| 24 | + const fileResult = util.getFileList(context.buildType, opts.fileList); |
| 25 | + const svgo = new SVGO(opts.options); |
| 26 | + return new Promise((resolve, reject) => { |
| 27 | + try { |
| 28 | + fileResult.forEach((file) => { |
| 29 | + const src = fs.readFileSync(file.src); |
| 30 | + svgo.optimize(src, (result) => { |
| 31 | + if (result.error) reject(result.error); |
| 32 | + fs.outputFileSync(path.resolve(file.dest), result.data); |
| 33 | + }); |
| 34 | + }); |
| 35 | + resolve(context); |
| 36 | + } catch (error) { |
| 37 | + reject(error); |
| 38 | + } |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Return the promise to combine user provided svg files into a sprite.svg |
| 44 | + * and update the _oj.common.sprite.scss |
| 45 | + * @param {Object} Running context that contains all options |
| 46 | + * @returns {Promise} return the promise |
| 47 | + */ |
| 48 | +function _svgSprite(context) { |
| 49 | + const opts = context.opts.svgSprite; |
| 50 | + const padding = opts.options.shape.spacing.padding || 2; |
| 51 | + const themePath = path.join(`${config('paths').staging.themes}`, CONSTANTS.DEFAULT_THEME); |
| 52 | + return new Promise((resolve, reject) => { |
| 53 | + try { |
| 54 | + fs.readdirSync(themePath).forEach((dir) => { |
| 55 | + if (CONSTANTS.SUPPORTED_THEME_PLATFORMS.indexOf(dir) !== -1) { |
| 56 | + opts.options.mode = _svgSpriteMode(_mapToSourceSkinName(dir)); |
| 57 | + const fileList = glob.sync('**/*.svg', { cwd: path.join(themePath, dir) }); |
| 58 | + if (opts.options.shape.spacing.padding) opts.options.shape.spacing.padding = padding; |
| 59 | + const spriter = new SVGSpriter(opts.options); |
| 60 | + fileList.forEach((file) => { |
| 61 | + if (path.basename(file) !== 'sprite.svg') { |
| 62 | + const fileBase = path.join(themePath, dir); |
| 63 | + spriter.add(path.resolve(file), path.basename(file), fs.readFileSync(path.resolve(fileBase, file), { encoding: 'utf-8' })); |
| 64 | + } |
| 65 | + }); |
| 66 | + spriter.compile((error, result) => { |
| 67 | + Object.keys(result).forEach((mode) => { |
| 68 | + Object.keys(result[mode]).forEach((resource) => { |
| 69 | + const output = result[mode][resource]; |
| 70 | + const svgDestSuffix = 'images/sprites/sprite.svg'; |
| 71 | + let dest = path.join(config('paths').staging.themes, CONSTANTS.DEFAULT_THEME, dir, svgDestSuffix); |
| 72 | + dest = path.extname(output.path) === '.svg' ? dest : output.path; |
| 73 | + const filePrefix = _mapToSourceSkinName(dir).replace('-', '.'); |
| 74 | + fs.outputFileSync(dest, output.contents); |
| 75 | + const content = fs.readFileSync(dest, 'utf-8').replace(/_(?!.+\.svg)/g, '-'); |
| 76 | + if (filePrefix !== 'common') { |
| 77 | + fs.outputFileSync(dest, content.replace(/common\.sprite/g, `${filePrefix}.sprite`)); |
| 78 | + } else { |
| 79 | + fs.outputFileSync(dest, content.replace(/oj-image-url/g, 'oj-common-image-url')); |
| 80 | + } |
| 81 | + }); |
| 82 | + }); |
| 83 | + }); |
| 84 | + } |
| 85 | + }); |
| 86 | + resolve(context); |
| 87 | + } catch (error) { |
| 88 | + reject(error); |
| 89 | + } |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Return the mode configuration for svg spriter |
| 95 | + * @param {String} Skin name |
| 96 | + * @returns {Object} return the object configuration |
| 97 | + */ |
| 98 | +function _svgSpriteMode(skin) { |
| 99 | + const filePrefix = skin.replace('-', '.'); |
| 100 | + return { |
| 101 | + css: { |
| 102 | + render: { |
| 103 | + scss: { |
| 104 | + template: `${CONSTANTS.PATH_TO_ORACLEJET}/scss/templates/svg-sprite-template.scss.txt`, |
| 105 | + dest: `${CONSTANTS.PATH_TO_ORACLEJET}/scss/${skin}/widgets/_oj.${filePrefix}.sprite.scss` |
| 106 | + } |
| 107 | + }, |
| 108 | + layout: 'horizontal', |
| 109 | + dest: '.', |
| 110 | + sprite: 'sprite.svg', |
| 111 | + bust: false, |
| 112 | + example: false |
| 113 | + } |
| 114 | + }; |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Map the tooling theme skin to JET distribution |
| 119 | + * @param {String} skin name |
| 120 | + * @returns {String} skin name |
| 121 | + */ |
| 122 | +function _mapToSourceSkinName(skin) { |
| 123 | + switch (skin) { |
| 124 | + case 'web': |
| 125 | + return 'alta'; |
| 126 | + case 'ios': |
| 127 | + return 'alta-ios'; |
| 128 | + case 'android': |
| 129 | + return 'alta-android'; |
| 130 | + case 'windows': |
| 131 | + return 'alta-windows'; |
| 132 | + case 'common': |
| 133 | + return 'common'; |
| 134 | + default: |
| 135 | + return skin; |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +module.exports = { |
| 140 | + spriteSvg: function _spriteSvg(context) { |
| 141 | + return new Promise((resolve, reject) => { |
| 142 | + try { |
| 143 | + _svgMin(context) |
| 144 | + .then(_svgSprite) |
| 145 | + .then(() => { |
| 146 | + resolve(context); |
| 147 | + }); |
| 148 | + } catch (error) { |
| 149 | + reject(error); |
| 150 | + } |
| 151 | + }); |
| 152 | + } |
| 153 | +}; |
0 commit comments