From 103da83d24e9f451b14c8105d289d64b4f969622 Mon Sep 17 00:00:00 2001 From: Konstantin Vulsonov Date: Wed, 19 Sep 2018 00:11:51 +0300 Subject: [PATCH] done with MVP version --- src/app.ts | 26 +++++++++++++++++++------- src/config.d.ts | 10 ++++++++++ src/modules/svgo.clean.ts | 17 +++++++++++++---- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/app.ts b/src/app.ts index 7a7be19..86d8956 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,7 +1,7 @@ import { createWriteStream, WriteStream } from 'fs'; import { join } from 'path'; -import { MainConfig, SVGOConfig } from './config'; +import { MainConfig, SVGOConfig, OptimizedResponse } from './config'; import { svgClean } from './modules/svgo.clean'; import { makeConfig } from './modules/svgo.config'; @@ -28,13 +28,25 @@ export class SvgIconset { } svgClean(this.config.source, this.svgoPlugin) - .then(files => { - createWriteStream(join(process.cwd(), this.config.source, `${this.config.result}-iconset.svg`)) - .once('open', function (this: WriteStream) { - this.write(`${files}`); + .then(optimizedResponse => { + let idx = 0; + const ids = optimizedResponse.map(el => el.id); + Promise.all(optimizedResponse.map(el => el.optimized)) + .then(resFiles => { + createWriteStream(join(process.cwd(), this.config.source, `${this.config.result}-iconset.svg`)) + .once('open', function (this: WriteStream) { + try { + const data = resFiles.map(res => res.data.replace(/${data.join('')}`); + idx++; + } catch (err) { + console.log('ERROR', err); + } + }) + .on('close', () => console.log(`Succesfully written file ${this.config.result}-iconset.svg`)) + .on('error', () => console.error('Something’s wrong try again')); }) - .on('close', () => console.log(`Succesfully written file ${this.config.result}-iconset.svg`)) - .on('error', () => console.error('Something’s wrong try again')); + .catch(console.error); }) .catch(err => console.error(err)); } diff --git a/src/config.d.ts b/src/config.d.ts index 585617b..7217262 100644 --- a/src/config.d.ts +++ b/src/config.d.ts @@ -14,6 +14,16 @@ export interface SvgOptimizeConfig { removeViewBox?: boolean } +export interface SVGOResponse { + data: string; + info: { [propname: string]: string } +} + +export interface OptimizedResponse { + id: string; + optimized: SVGOResponse +} + export interface MainConfig { source: string; result: string; diff --git a/src/modules/svgo.clean.ts b/src/modules/svgo.clean.ts index b73dd59..09aff2b 100644 --- a/src/modules/svgo.clean.ts +++ b/src/modules/svgo.clean.ts @@ -1,7 +1,10 @@ -const SVGO = require('svgo'); -import { readdir } from 'fs'; +import { readdir, readFileSync } from 'fs'; +import { join } from 'path'; -export function svgClean(path: string, svgo: typeof SVGO): Promise { +import { OptimizedResponse } from '../config'; + +const SVGO = require('svgo'); +export function svgClean(path: string, svgo: typeof SVGO): Promise { return new Promise((resolve, reject) => { @@ -11,7 +14,13 @@ export function svgClean(path: string, svgo: typeof SVGO): Promise } const files = svgFiles.filter(filename => filename.indexOf('iconset') === -1 && filename.indexOf('.svg') > -1) - .map(dirtyFile => svgo.optimize(dirtyFile)); + .map(dirtyFile => { + const filePath = join(path, dirtyFile); + return { + id: dirtyFile.replace(/.svg/, ''), + optimized: svgo.optimize(readFileSync(filePath)) + }; + }); return files.length > 0 ? resolve(files) : reject('There are no SVG files to optimize');