initial commit, build is not working

This commit is contained in:
Konstantin Vulsonov
2018-09-18 23:16:10 +03:00
commit 024d0e3125
12 changed files with 1395 additions and 0 deletions

42
src/app.ts Normal file
View File

@@ -0,0 +1,42 @@
import { createWriteStream, WriteStream } from 'fs';
import { join } from 'path';
import { MainConfig, SVGOConfig } from './config';
import { svgClean } from './modules/svgo.clean';
import { makeConfig } from './modules/svgo.config';
const SVGO = require('svgo');
export class SvgIconset {
private optimizeConfig: SVGOConfig;
private isValid: boolean = true;
private svgoPlugin: typeof SVGO;
constructor(private config: MainConfig) {
if (config.result === undefined || config.source === undefined) {
this.isValid = false;
}
this.optimizeConfig = config.optimize !== undefined ? makeConfig(config.optimize) : makeConfig();
this.svgoPlugin = new SVGO(this.optimizeConfig);
}
public createSet() {
if (!this.isValid) {
return console.error('Configuration is not valid, check source or result are available');
}
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>`);
})
.on('close', () => console.log(`Succesfully written file ${this.config.result}-iconset.svg`))
.on('error', () => console.error('Somethings wrong try again'));
})
.catch(err => console.error(err));
}
}

37
src/bin.ts Normal file
View File

@@ -0,0 +1,37 @@
#!/usr/bin/env node
import { exit } from 'process';
import { SvgIconset } from './app';
const argv = require('yargs').argv;
const exampleString = 'Example: svg-iconset --source=assets/images/icons --name=icons';
const {
source,
name,
attrs,
removeViewBox
} = argv;
if (source === undefined) {
console.error('Source dir is not set');
console.error(exampleString);
exit();
}
if (name === undefined) {
console.error('Result filename is not set');
console.error(exampleString);
exit();
}
new SvgIconset({
source,
result: name,
optimize: {
attrs,
removeViewBox
}
}).createSet();

21
src/config.d.ts vendored Normal file
View File

@@ -0,0 +1,21 @@
declare type SVGOConfig = [
{
removeAttrs: {
attrs: string
}
},
{
removeViewBox: boolean
}
]
export interface SvgOptimizeConfig {
attrs?: string;
removeViewBox?: boolean
}
export interface MainConfig {
source: string;
result: string;
optimize?: SvgOptimizeConfig
}

21
src/modules/svgo.clean.ts Normal file
View File

@@ -0,0 +1,21 @@
const SVGO = require('svgo');
import { readdir } from 'fs';
export function svgClean(path: string, svgo: typeof SVGO): Promise<SVGElement[]> {
return new Promise((resolve, reject) => {
readdir(path, (err, svgFiles) => {
if (err) {
return reject(err);
}
const files = svgFiles.filter(filename => filename.indexOf('iconset') === -1 && filename.indexOf('.svg') > -1)
.map(dirtyFile => svgo.optimize(dirtyFile));
return files.length > 0 ? resolve(files) : reject('There are no SVG files to optimize');
});
});
}

View File

@@ -0,0 +1,27 @@
import { SVGOConfig, SvgOptimizeConfig } from '../config';
const defaultConfig: SVGOConfig = [
{
removeAttrs: {
attrs: '(width|height)'
},
},
{ removeViewBox: false }
];
export function makeConfig(config?: SvgOptimizeConfig): SVGOConfig {
if (config !== undefined) {
const { attrs, removeViewBox } = config;
if (attrs !== undefined) {
defaultConfig[0].removeAttrs.attrs = `(${attrs.replace(/,\s/g, '|')})`;
}
if (removeViewBox) {
defaultConfig[1].removeViewBox = removeViewBox;
}
}
return defaultConfig;
}