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(``);
+ .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(/`);
+ 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');