-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (62 loc) · 2.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const fs = require('fs');
let componentString = `import { Component, OnInit } from '@angular/core'; \n`;
const componentsNames = [];
/**
*
* @param {String} pathname Svg Files Directory
* @param {*} fileOptions
* @param {*} options
*/
function ConvertIconComponentSvgFiles(pathname, fileOptions = { componentFileToWriteName, moduleFileToWriteName }, options = { selectorPrefix: 'icon' }) {
const files = fs.readdirSync(pathname, 'utf-8');
files.forEach(file => {
const fileContent = fs.readFileSync(`${pathname}/${file}`, 'utf-8');
const spritedName = file.substring(0, file.length - 4);
const filenameArr = spritedName.split('-');
let componentName = '';
filenameArr.forEach(e => componentName += e.charAt(0).toLocaleUpperCase() + e.substring(1));
componentsNames.push(componentName);
componentString += `
@Component({
selector: '${options.selectorPrefix}-${spritedName.toLocaleLowerCase()}',
template: \x60
${fileContent.replace('<?xml version="1.0" encoding="UTF-8"?>', '')}
\x60
})
export class Icon${componentName}Component implements OnInit {
constructor() { }
ngOnInit() { }
}
`;
})
fs.writeFile(fileOptions.componentFileToWriteName, componentString, (err) => {
if (err) { console.log(err); return; }
});
const moduleString = `
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ${componentsNames.map(name => `Icon${name}Component`)} } from './${fileOptions.componentFileToWriteName}';
@NgModule({
imports: [
CommonModule
],
exports: [
${componentsNames.map(name => `Icon${name}Component`)}
],
declarations: [
${componentsNames.map(name => `Icon${name}Component`)}
],
providers: [],
})
export class IconsModule { }
`;
fs.writeFile(fileOptions.moduleFileToWriteName, moduleString, (err) => {
if (err) { console.log(err); return; }
});
}
ConvertIconComponentSvgFiles('iconsfolder', {
componentFileToWriteName: 'icons.component.ts',
moduleFileToWriteName: 'icons.module.ts'
}, {
selectorPrefix: 'icon',
})