Skip to content

Commit 33a238a

Browse files
committed
Append hash value to icon file name if content changes
1 parent c803b43 commit 33a238a

5 files changed

Lines changed: 92 additions & 27 deletions

File tree

src/helpers/fileConfig.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { getDefaultIconOptions } from '../icons';
2+
import { IconJsonOptions } from '../models';
3+
4+
/**
5+
* Generate a config string that is appended to each icon file name.
6+
* @param config Icon Configuration object
7+
*/
8+
export const getFileConfigString = (options: IconJsonOptions) => {
9+
try {
10+
const defaults = getDefaultIconOptions();
11+
let fileConfigString = '';
12+
if (options.saturation !== defaults.saturation ||
13+
options.opacity !== defaults.opacity ||
14+
options.folders.color !== defaults.folders.color
15+
) {
16+
fileConfigString += `~${getHash(JSON.stringify(options))}`;
17+
}
18+
return fileConfigString;
19+
20+
} catch (error) {
21+
console.error(error);
22+
}
23+
};
24+
25+
const getHash = (value: string) => {
26+
let hash = 0, i, chr;
27+
if (value.length === 0) return hash;
28+
for (i = 0; i < value.length; i++) {
29+
chr = value.charCodeAt(i);
30+
hash = ((hash << 5) - hash) + chr;
31+
hash |= 0; // Convert to 32bit integer
32+
}
33+
return hash;
34+
};

src/icons/generator/fileGenerator.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as merge from 'lodash.merge';
2+
import { getFileConfigString } from '../../helpers/fileConfig';
23
import { FileIcon, FileIcons, IconAssociations, IconConfiguration, IconJsonOptions } from '../../models/index';
34
import { highContrastVersion, iconFolderPath, lightVersion, wildcardPattern } from './constants';
45

@@ -13,13 +14,13 @@ export const getFileIconDefinitions = (fileIcons: FileIcons, config: IconConfigu
1314

1415
allFileIcons.forEach(icon => {
1516
if (icon.disabled) return;
16-
config = merge({}, config, setIconDefinition(icon.name));
17+
config = merge({}, config, setIconDefinition(config, icon.name));
1718

1819
if (icon.light) {
19-
config = merge({}, config, setIconDefinition(icon.name, lightVersion));
20+
config = merge({}, config, setIconDefinition(config, icon.name, lightVersion));
2021
}
2122
if (icon.highContrast) {
22-
config = merge({}, config, setIconDefinition(icon.name, highContrastVersion));
23+
config = merge({}, config, setIconDefinition(config, icon.name, highContrastVersion));
2324
}
2425

2526
if (icon.fileExtensions) {
@@ -31,16 +32,16 @@ export const getFileIconDefinitions = (fileIcons: FileIcons, config: IconConfigu
3132
});
3233

3334
// set default file icon
34-
config = merge({}, config, setIconDefinition(fileIcons.defaultIcon.name));
35+
config = merge({}, config, setIconDefinition(config, fileIcons.defaultIcon.name));
3536
config.file = fileIcons.defaultIcon.name;
3637

3738
if (fileIcons.defaultIcon.light) {
38-
config = merge({}, config, setIconDefinition(fileIcons.defaultIcon.name, lightVersion));
39+
config = merge({}, config, setIconDefinition(config, fileIcons.defaultIcon.name, lightVersion));
3940
config.light.file = fileIcons.defaultIcon.name + lightVersion;
4041
}
4142

4243
if (fileIcons.defaultIcon.highContrast) {
43-
config = merge({}, config, setIconDefinition(fileIcons.defaultIcon.name, highContrastVersion));
44+
config = merge({}, config, setIconDefinition(config, fileIcons.defaultIcon.name, highContrastVersion));
4445
config.highContrast.file = fileIcons.defaultIcon.name + highContrastVersion;
4546
}
4647

@@ -87,10 +88,11 @@ const disableIconsByPack = (fileIcons: FileIcons, activatedIconPack: string): Fi
8788
});
8889
};
8990

90-
const setIconDefinition = (iconName: string, appendix: string = '') => {
91+
const setIconDefinition = (config: IconConfiguration, iconName: string, appendix: string = '') => {
9192
const obj = { iconDefinitions: {} };
93+
const fileConfig = getFileConfigString(config.options);
9294
obj.iconDefinitions[`${iconName}${appendix}`] = {
93-
iconPath: `${iconFolderPath}${iconName}${appendix}.svg`
95+
iconPath: `${iconFolderPath}${iconName}${appendix}${fileConfig}.svg`
9496
};
9597
return obj;
9698
};

src/icons/generator/folderGenerator.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as fs from 'fs';
22
import * as merge from 'lodash.merge';
33
import * as path from 'path';
4+
import { getFileConfigString } from '../../helpers/fileConfig';
45
import { DefaultIcon, FolderIcon, FolderTheme, IconAssociations, IconConfiguration, IconJsonOptions } from '../../models/index';
56
import { highContrastVersion, iconFolderPath, lightVersion, openedFolder } from './constants';
67

@@ -87,11 +88,12 @@ const setIconDefinitions = (config: IconConfiguration, icon: FolderIcon | Defaul
8788

8889
const createIconDefinitions = (config: IconConfiguration, iconName: string, appendix: string = '') => {
8990
config = merge({}, config);
91+
const fileConfig = getFileConfigString(config.options);
9092
config.iconDefinitions[iconName + appendix] = {
91-
iconPath: `${iconFolderPath}${iconName}${appendix}.svg`
93+
iconPath: `${iconFolderPath}${iconName}${appendix}${fileConfig}.svg`
9294
};
9395
config.iconDefinitions[`${iconName}${openedFolder}${appendix}`] = {
94-
iconPath: `${iconFolderPath}${iconName}${openedFolder}${appendix}.svg`
96+
iconPath: `${iconFolderPath}${iconName}${openedFolder}${appendix}${fileConfig}.svg`
9597
};
9698
return config;
9799
};

src/icons/generator/jsonGenerator.ts

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import * as fs from 'fs';
12
import * as merge from 'lodash.merge';
23
import * as path from 'path';
3-
import * as fs from 'fs';
4+
import { getFileConfigString } from '../../helpers/fileConfig';
45
import { IconConfiguration, IconJsonOptions } from '../../models/index';
56
import { fileIcons } from '../fileIcons';
67
import { 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(/\.svg/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(/(^[^\.~]+)(.*)\.svg/, `$1${fileConfig}.svg`));
121+
fs.renameSync(filePath, newFilePath);
122+
});
123+
};

src/icons/generator/languageGenerator.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as merge from 'lodash.merge';
2+
import { getFileConfigString } from '../../helpers/fileConfig';
23
import { DefaultIcon, IconAssociations, IconConfiguration, IconJsonOptions, LanguageIcon } from '../../models/index';
34
import { highContrastVersion, iconFolderPath, lightVersion } from './constants';
45

@@ -32,8 +33,9 @@ const setIconDefinitions = (config: IconConfiguration, icon: DefaultIcon) => {
3233

3334
const createIconDefinitions = (config: IconConfiguration, iconName: string) => {
3435
config = merge({}, config);
36+
const fileConfig = getFileConfigString(config.options);
3537
config.iconDefinitions[iconName] = {
36-
iconPath: `${iconFolderPath}${iconName}.svg`
38+
iconPath: `${iconFolderPath}${iconName}${fileConfig}.svg`
3739
};
3840
return config;
3941
};
@@ -46,11 +48,11 @@ const setLanguageIdentifiers = (iconName: string, languageIds: string[]) => {
4648
return obj;
4749
};
4850

49-
const getCustomIcons = (languageAssocitations: IconAssociations) => {
50-
if (!languageAssocitations) return [];
51+
const getCustomIcons = (languageAssociations: IconAssociations) => {
52+
if (!languageAssociations) return [];
5153

52-
const icons: LanguageIcon[] = Object.keys(languageAssocitations).map(fa => ({
53-
icon: { name: languageAssocitations[fa].toLowerCase() },
54+
const icons: LanguageIcon[] = Object.keys(languageAssociations).map(fa => ({
55+
icon: { name: languageAssociations[fa].toLowerCase() },
5456
ids: [fa.toLowerCase()]
5557
}));
5658

0 commit comments

Comments
 (0)