done with MVP version
This commit is contained in:
26
src/app.ts
26
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(`<svg>${files}</svg>`);
|
||||
.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(/<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('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));
|
||||
}
|
||||
|
10
src/config.d.ts
vendored
10
src/config.d.ts
vendored
@@ -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;
|
||||
|
@@ -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<SVGElement[]> {
|
||||
import { OptimizedResponse } from '../config';
|
||||
|
||||
const SVGO = require('svgo');
|
||||
export function svgClean(path: string, svgo: typeof SVGO): Promise<OptimizedResponse[]> {
|
||||
|
||||
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)
|
||||
.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');
|
||||
|
||||
|
Reference in New Issue
Block a user