done with MVP version

This commit is contained in:
Konstantin Vulsonov
2018-09-19 00:11:51 +03:00
parent 20b6eacc96
commit 103da83d24
3 changed files with 42 additions and 11 deletions

View File

@@ -1,7 +1,7 @@
import { createWriteStream, WriteStream } from 'fs'; import { createWriteStream, WriteStream } from 'fs';
import { join } from 'path'; import { join } from 'path';
import { MainConfig, SVGOConfig } from './config'; import { MainConfig, SVGOConfig, OptimizedResponse } from './config';
import { svgClean } from './modules/svgo.clean'; import { svgClean } from './modules/svgo.clean';
import { makeConfig } from './modules/svgo.config'; import { makeConfig } from './modules/svgo.config';
@@ -28,14 +28,26 @@ export class SvgIconset {
} }
svgClean(this.config.source, this.svgoPlugin) svgClean(this.config.source, this.svgoPlugin)
.then(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`)) createWriteStream(join(process.cwd(), this.config.source, `${this.config.result}-iconset.svg`))
.once('open', function (this: WriteStream) { .once('open', function (this: WriteStream) {
this.write(`<svg>${files}</svg>`); try {
const data = resFiles.map(res => res.data.replace(/<svg /, `<svg id="${ids[idx]}" `));
this.write(`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">${data.join('')}</svg>`);
idx++;
} catch (err) {
console.log('ERROR', err);
}
}) })
.on('close', () => console.log(`Succesfully written file ${this.config.result}-iconset.svg`)) .on('close', () => console.log(`Succesfully written file ${this.config.result}-iconset.svg`))
.on('error', () => console.error('Somethings wrong try again')); .on('error', () => console.error('Somethings wrong try again'));
}) })
.catch(console.error);
})
.catch(err => console.error(err)); .catch(err => console.error(err));
} }

10
src/config.d.ts vendored
View File

@@ -14,6 +14,16 @@ export interface SvgOptimizeConfig {
removeViewBox?: boolean removeViewBox?: boolean
} }
export interface SVGOResponse {
data: string;
info: { [propname: string]: string }
}
export interface OptimizedResponse {
id: string;
optimized: SVGOResponse
}
export interface MainConfig { export interface MainConfig {
source: string; source: string;
result: string; result: string;

View File

@@ -1,7 +1,10 @@
const SVGO = require('svgo'); import { readdir, readFileSync } from 'fs';
import { readdir } from 'fs'; import { join } from 'path';
export function svgClean(path: string, svgo: typeof SVGO): Promise<SVGElement[]> { import { OptimizedResponse } from '../config';
const SVGO = require('svgo');
export function svgClean(path: string, svgo: typeof SVGO): Promise<OptimizedResponse[]> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@@ -11,7 +14,13 @@ export function svgClean(path: string, svgo: typeof SVGO): Promise<SVGElement[]>
} }
const files = svgFiles.filter(filename => filename.indexOf('iconset') === -1 && filename.indexOf('.svg') > -1) 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'); return files.length > 0 ? resolve(files) : reject('There are no SVG files to optimize');