Skip to content

Commit

Permalink
Fix #827
Browse files Browse the repository at this point in the history
  • Loading branch information
PKief committed Sep 6, 2020
1 parent cd46788 commit 5be67ba
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions src/icons/generator/jsonGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as fs from 'fs';
import * as merge from 'lodash.merge';
import * as path from 'path';
import { getFileConfigString } from '../../helpers/fileConfig';
import { getCustomIconPaths } from '../../helpers/customIcons';
import { getFileConfigHash } from '../../helpers/fileConfig';
import { IconConfiguration, IconJsonOptions } from '../../models/index';
import { fileIcons } from '../fileIcons';
import { folderIcons } from '../folderIcons';
Expand Down Expand Up @@ -43,6 +44,11 @@ export const createIconFile = (updatedConfigs?: IconJsonOptions, updatedJSONConf
}

try {
let iconJsonPath = __dirname;
// if executed via script
if (path.basename(__dirname) !== 'dist') {
iconJsonPath = path.join(__dirname, '..', '..', '..', 'dist');
}
if (!updatedConfigs || (updatedConfigs.folders || {}).color) {
// if updatedConfigs do not exist (because of initial setup)
// or new config value was detected by the change detection
Expand All @@ -55,11 +61,6 @@ export const createIconFile = (updatedConfigs?: IconJsonOptions, updatedJSONConf
if (!updatedConfigs || updatedConfigs.saturation !== undefined) {
setIconSaturation(options.saturation);
}
let iconJsonPath = __dirname;
// if executed via script
if (path.basename(__dirname) !== 'dist') {
iconJsonPath = path.join(__dirname, '..', '..', '..', 'dist');
}
renameIconFiles(iconJsonPath, options);
} catch (error) {
throw Error(error);
Expand Down Expand Up @@ -102,20 +103,26 @@ export const getDefaultIconOptions = (): IconJsonOptions => ({
* @param options Icon Json Options
*/
const renameIconFiles = (iconJsonPath: string, options: IconJsonOptions) => {
fs.readdirSync(path.join(iconJsonPath, '..', 'icons'))
.filter(f => f.match(/\.svg/gi))
.forEach(f => {
const filePath = path.join(iconJsonPath, '..', 'icons', f);
const fileConfig = getFileConfigString(options);
const customPaths = getCustomIconPaths(options);
const defaultIconPath = path.join(iconJsonPath, '..', 'icons');
const iconPaths = [defaultIconPath, ...customPaths];

iconPaths.forEach(iconPath => {
fs.readdirSync(iconPath)
.filter(f => f.match(/\.svg/gi))
.forEach(f => {
const filePath = path.join(iconPath, f);
const fileConfigHash = getFileConfigHash(options);

// append file config to file name
const newFilePath = path.join(iconJsonPath, '..', 'icons', f.replace(/(^[^\.~]+)(.*)\.svg/, `$1${fileConfig}.svg`));
// append file config to file name
const newFilePath = path.join(iconPath, f.replace(/(^[^\.~]+)(.*)\.svg/, `$1${fileConfigHash}.svg`));

// if generated files are already in place, do not overwrite them
if (filePath !== newFilePath && fs.existsSync(newFilePath)) {
fs.unlinkSync(filePath);
} else {
fs.renameSync(filePath, newFilePath);
}
});
// if generated files are already in place, do not overwrite them
if (filePath !== newFilePath && fs.existsSync(newFilePath)) {
fs.unlinkSync(filePath);
} else {
fs.renameSync(filePath, newFilePath);
}
});
});
};

0 comments on commit 5be67ba

Please sign in to comment.