vsc-material-theme/extensions/commands/theme-icons/index.ts

107 lines
3.3 KiB
TypeScript
Raw Normal View History

2018-04-20 20:07:36 +02:00
import * as fs from 'fs';
import {
getAccentableIcons,
getAbsolutePath,
getDefaultValues,
getThemeIconsByContributeID,
getThemeIconsContribute,
getVariantIcons
} from './../../helpers/fs';
import {
isAccent,
isMaterialThemeIcons,
getCustomSettings
} from './../../helpers/settings';
import {getCurrentThemeIconsID, getCurrentThemeID} from './../../helpers/vscode';
import {CHARSET} from './../../consts/files';
import {IPackageJSONThemeIcons} from './../../interfaces/ipackage.json';
import {IThemeIconsIconPath, IThemeIcons} from './../../interfaces/itheme-icons';
const getIconDefinition = (definitions: any, iconname: string): IThemeIconsIconPath => {
2018-04-20 20:07:36 +02:00
return (definitions as any)[iconname];
};
2018-04-20 20:07:36 +02:00
/**
* Replaces icon path with the accented one.
*/
const replaceIconPathWithAccent = (iconPath: string, accentName: string): string => {
2018-04-20 20:07:36 +02:00
return iconPath.replace('.svg', `.accent.${ accentName }.svg`);
};
2018-04-20 20:07:36 +02:00
const getVariantFromColor = (color: string): string => {
switch (color) {
case undefined || 'Material Theme':
return 'Default';
case 'Material Theme High Contrast':
return 'Default High Contrast';
default:
return color.replace(/Material Theme /gi, '');
}
};
2018-04-20 20:07:36 +02:00
export const THEME_ICONS = () => {
const deferred: any = {};
const promise = new Promise((resolve, reject) => {
2018-04-20 20:07:36 +02:00
deferred.resolve = resolve;
deferred.reject = reject;
});
const themeIconsID: string = getCurrentThemeIconsID();
2018-04-20 20:07:36 +02:00
if (isMaterialThemeIcons(themeIconsID)) {
const themeID = getCurrentThemeID();
const customSettings = getCustomSettings();
const defaults = getDefaultValues();
const accentName = customSettings.accent;
const variantName: string = getVariantFromColor(themeID);
const themeContribute: IPackageJSONThemeIcons = getThemeIconsContribute(themeIconsID);
const theme: IThemeIcons = getThemeIconsByContributeID(themeIconsID);
const themepath: string = getAbsolutePath(themeContribute.path);
2018-04-20 20:07:36 +02:00
if (isAccent(accentName, defaults)) {
const realAccentName = accentName.replace(/\s+/, '-');
2018-04-20 20:07:36 +02:00
getAccentableIcons().forEach(iconname => {
const distIcon = getIconDefinition(theme.iconDefinitions, iconname);
const outIcon = getIconDefinition(defaults.icons.theme.iconDefinitions, iconname);
2018-04-20 20:07:36 +02:00
if (typeof distIcon === 'object' && typeof outIcon === 'object') {
distIcon.iconPath = replaceIconPathWithAccent(outIcon.iconPath, realAccentName);
2018-04-20 20:07:36 +02:00
}
});
2018-04-20 20:07:36 +02:00
} else {
getAccentableIcons().forEach(iconname => {
const distIcon = getIconDefinition(theme.iconDefinitions, iconname);
const outIcon = getIconDefinition(defaults.icons.theme.iconDefinitions, iconname);
2018-04-20 20:07:36 +02:00
distIcon.iconPath = outIcon.iconPath;
});
}
getVariantIcons().forEach(iconname => {
const distIcon = getIconDefinition(theme.iconDefinitions, iconname);
const outIcon = getIconDefinition(defaults.icons.theme.iconDefinitions, iconname);
2018-04-20 20:07:36 +02:00
if (distIcon && outIcon) {
2018-04-20 20:07:36 +02:00
distIcon.iconPath = outIcon.iconPath.replace('.svg', `${ variantName }.svg`);
}
});
2018-04-20 20:07:36 +02:00
fs.writeFile(themepath, JSON.stringify(theme), {
encoding: CHARSET
}, err => {
if (err) {
deferred.reject(err);
2018-04-20 20:07:36 +02:00
return;
}
deferred.resolve();
});
} else {
deferred.resolve();
}
return promise;
};