1+ import * as fs from 'fs' ;
12import * as merge from 'lodash.merge' ;
23import * as path from 'path' ;
3- import * as fs from 'fs ' ;
4+ import { getFileConfigString } from '../../helpers/fileConfig ' ;
45import { IconConfiguration , IconJsonOptions } from '../../models/index' ;
56import { fileIcons } from '../fileIcons' ;
67import { folderIcons } from '../folderIcons' ;
@@ -49,17 +50,6 @@ export const createIconFile = (updatedConfigs?: IconJsonOptions, updatedJSONConf
4950 }
5051 }
5152
52- try {
53- let iconsPath = __dirname ;
54- if ( path . basename ( __dirname ) !== 'dist' ) {
55- // executed via script
56- iconsPath = path . join ( __dirname , '..' , '..' , '..' , 'dist' ) ;
57- }
58- fs . writeFileSync ( path . join ( iconsPath , iconJsonName ) , JSON . stringify ( json , undefined , 2 ) , 'utf-8' ) ;
59- } catch ( error ) {
60- throw Error ( error ) ;
61- }
62-
6353 try {
6454 if ( ! updatedConfigs || ( updatedConfigs . folders || { } ) . color ) {
6555 // if updatedConfigs do not exist (because of initial setup)
@@ -73,6 +63,23 @@ export const createIconFile = (updatedConfigs?: IconJsonOptions, updatedJSONConf
7363 if ( ! updatedConfigs || updatedConfigs . saturation !== undefined ) {
7464 setIconSaturation ( options . saturation ) ;
7565 }
66+ let iconJsonPath = __dirname ;
67+ // if executed via script
68+ if ( path . basename ( __dirname ) !== 'dist' ) {
69+ iconJsonPath = path . join ( __dirname , '..' , '..' , '..' , 'dist' ) ;
70+ }
71+ renameIconFiles ( iconJsonPath , options ) ;
72+ } catch ( error ) {
73+ throw Error ( error ) ;
74+ }
75+
76+ try {
77+ let iconJsonPath = __dirname ;
78+ // if executed via script
79+ if ( path . basename ( __dirname ) !== 'dist' ) {
80+ iconJsonPath = path . join ( __dirname , '..' , '..' , '..' , 'dist' ) ;
81+ }
82+ fs . writeFileSync ( path . join ( iconJsonPath , iconJsonName ) , JSON . stringify ( json , undefined , 2 ) , 'utf-8' ) ;
7683 } catch ( error ) {
7784 throw Error ( error ) ;
7885 }
@@ -96,3 +103,21 @@ export const getDefaultIconOptions = (): IconJsonOptions => ({
96103 files : { associations : { } } ,
97104 languages : { associations : { } } ,
98105} ) ;
106+
107+ /**
108+ * Rename all icon files according their respective config
109+ * @param iconJsonPath Path of icon json folder
110+ * @param options Icon Json Options
111+ */
112+ const renameIconFiles = ( iconJsonPath : string , options : IconJsonOptions ) => {
113+ fs . readdirSync ( path . join ( iconJsonPath , '..' , 'icons' ) )
114+ . filter ( f => f . match ( / \. s v g / gi) )
115+ . forEach ( f => {
116+ const filePath = path . join ( iconJsonPath , '..' , 'icons' , f ) ;
117+ const fileConfig = getFileConfigString ( options ) ;
118+
119+ // append file config to file name
120+ const newFilePath = path . join ( iconJsonPath , '..' , 'icons' , f . replace ( / ( ^ [ ^ \. ~ ] + ) ( .* ) \. s v g / , `$1${ fileConfig } .svg` ) ) ;
121+ fs . renameSync ( filePath , newFilePath ) ;
122+ } ) ;
123+ } ;
0 commit comments