2018-04-20 20:07:36 +02:00
|
|
|
import * as fs from 'fs';
|
|
|
|
|
2018-05-03 11:39:51 +02:00
|
|
|
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-05-03 11:39:51 +02:00
|
|
|
};
|
2018-04-20 20:07:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces icon path with the accented one.
|
|
|
|
*/
|
2018-05-03 11:39:51 +02:00
|
|
|
const replaceIconPathWithAccent = (iconPath: string, accentName: string): string => {
|
2018-04-20 20:07:36 +02:00
|
|
|
return iconPath.replace('.svg', `.accent.${ accentName }.svg`);
|
2018-05-03 11:39:51 +02:00
|
|
|
};
|
2018-04-20 20:07:36 +02:00
|
|
|
|
2018-05-05 11:55:58 +02:00
|
|
|
const getVariantFromColor = (color: string): string => {
|
|
|
|
switch (true) {
|
|
|
|
case color === undefined || color === 'Material Theme':
|
2018-04-21 13:57:08 +02:00
|
|
|
return 'Default';
|
2018-05-05 11:55:58 +02:00
|
|
|
case color === 'Material Theme High Contrast':
|
2018-04-21 13:57:08 +02:00
|
|
|
return 'Default High Contrast';
|
2018-05-05 11:55:58 +02:00
|
|
|
case color.includes('Material Theme Lighter'):
|
|
|
|
return 'Light';
|
2018-04-21 13:57:08 +02:00
|
|
|
default:
|
|
|
|
return color.replace(/Material Theme /gi, '');
|
|
|
|
}
|
2018-05-05 11:55:58 +02:00
|
|
|
};
|
2018-04-21 13:57:08 +02:00
|
|
|
|
2018-04-20 20:07:36 +02:00
|
|
|
export const THEME_ICONS = () => {
|
2018-05-03 11:39:51 +02:00
|
|
|
const deferred: any = {};
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
2018-04-20 20:07:36 +02:00
|
|
|
deferred.resolve = resolve;
|
|
|
|
deferred.reject = reject;
|
|
|
|
});
|
2018-05-03 11:39:51 +02:00
|
|
|
const themeIconsID: string = getCurrentThemeIconsID();
|
2018-04-20 20:07:36 +02:00
|
|
|
|
|
|
|
if (isMaterialThemeIcons(themeIconsID)) {
|
2018-05-03 11:39:51 +02:00
|
|
|
const themeID = getCurrentThemeID();
|
|
|
|
const customSettings = getCustomSettings();
|
|
|
|
const defaults = getDefaultValues();
|
|
|
|
const accentName = customSettings.accent;
|
|
|
|
const variantName: string = getVariantFromColor(themeID);
|
2018-04-21 13:57:08 +02:00
|
|
|
|
2018-05-03 11:39:51 +02:00
|
|
|
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)) {
|
2018-05-03 11:39:51 +02:00
|
|
|
const realAccentName = accentName.replace(/\s+/, '-');
|
2018-04-20 20:07:36 +02:00
|
|
|
getAccentableIcons().forEach(iconname => {
|
2018-05-03 11:39:51 +02:00
|
|
|
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') {
|
2018-05-03 11:39:51 +02:00
|
|
|
distIcon.iconPath = replaceIconPathWithAccent(outIcon.iconPath, realAccentName);
|
2018-04-20 20:07:36 +02:00
|
|
|
}
|
2018-05-03 11:39:51 +02:00
|
|
|
});
|
2018-04-20 20:07:36 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
getAccentableIcons().forEach(iconname => {
|
2018-05-03 11:39:51 +02:00
|
|
|
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 => {
|
2018-05-03 11:39:51 +02:00
|
|
|
const distIcon = getIconDefinition(theme.iconDefinitions, iconname);
|
|
|
|
const outIcon = getIconDefinition(defaults.icons.theme.iconDefinitions, iconname);
|
2018-04-20 20:07:36 +02:00
|
|
|
|
2018-05-03 11:39:51 +02:00
|
|
|
if (distIcon && outIcon) {
|
2018-04-20 20:07:36 +02:00
|
|
|
distIcon.iconPath = outIcon.iconPath.replace('.svg', `${ variantName }.svg`);
|
|
|
|
}
|
2018-05-03 11:39:51 +02:00
|
|
|
});
|
2018-04-20 20:07:36 +02:00
|
|
|
|
2018-05-03 11:39:51 +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;
|
2018-05-03 11:39:51 +02:00
|
|
|
};
|