initial commit, build is not working
This commit is contained in:
42
src/app.ts
Normal file
42
src/app.ts
Normal 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('Something’s wrong try again'));
|
||||
})
|
||||
.catch(err => console.error(err));
|
||||
}
|
||||
|
||||
}
|
37
src/bin.ts
Normal file
37
src/bin.ts
Normal 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
21
src/config.d.ts
vendored
Normal 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
21
src/modules/svgo.clean.ts
Normal 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');
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
}
|
27
src/modules/svgo.config.ts
Normal file
27
src/modules/svgo.config.ts
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user