vsc-material-theme/extensions/helpers/fs.ts
Alessio Occhipinti 82cfb4587b Small refactor + UX improvements [WIP] (#190)
* chore: small cleanup helper

* chore: adding interfaces for icon variants

* feat: You can now switch between Material Theme icons

* chore: helpers small fix

* chore(helpers): small fix helpers

* refactor: refactoring fixIcons command, ready for new prop autoFix

* refactor: accentsSetter command, ready for autoFix prop

* chore: added new exports file for all commands

* refactor: main theme file, ready for autoFix prop

* chore: added listener also when changing iconTheme

* feat: autoApplyIcons added as option and toggleApplyIcons as command

* chore: added check for autoApplyIcons flag. Removed listen icon change

* feat: Notification shows up when the window needs reload

* chore: Update CTA's

* chore: Make consistent indent guides and add support to editorIndentGuide.activeBackground

Close #188

* chore: fix check on CTA ok

* chore: split up configurationChange and changelog  (added to commands)

* chore: small change settings method

* feat: Now the theme will auto fix if needed on change icons theme
2018-05-18 13:47:22 +02:00

77 lines
2.2 KiB
TypeScript

import * as fs from 'fs';
import * as path from 'path';
import {IPackageJSON, IPackageJSONThemeIcons} from './../interfaces/ipackage.json';
import {CHARSET} from './../consts/files';
import {IDefaults} from '../interfaces/idefaults';
import {IThemeIcons} from '../interfaces/itheme-icons';
import {PATHS} from '../consts/paths';
export function ensureDir(dirname: string): void {
if (!fs.existsSync(dirname)) {
fs.mkdirSync(dirname);
}
}
export function getDefaultValues(): IDefaults {
const defaults: IDefaults = require(path.join(PATHS.VSIX_DIR, './extensions/defaults.json'));
if (defaults === undefined || defaults === null) {
throw new Error('Cannot find defaults params');
}
return defaults;
}
export function getAbsolutePath(input: string): string {
return path.join(PATHS.VSIX_DIR, input);
}
export function getAccentableIcons(): string[] {
return getDefaultValues().accentableIcons;
}
export function getVariantIcons(): string[] {
return getDefaultValues().variantsIcons;
}
/**
* Gets a theme content by a given contribute ID
*/
export function getThemeIconsByContributeID(ID: string): IThemeIcons | null {
const contribute: IPackageJSONThemeIcons = getThemeIconsContribute(ID);
return contribute !== null ? require(path.join(PATHS.VSIX_DIR, contribute.path)) : null;
}
/**
* Gets a theme by name
*/
export function getThemeIconsContribute(ID: string): IPackageJSONThemeIcons {
const contributes = getPackageJSON().contributes.iconThemes.filter(contribute => contribute.id === ID);
return contributes[0] !== undefined ? contributes[0] : null;
}
/**
* Icon variant name from theme name
*/
export function getIconVariantFromTheme(theme: string): string {
const {themeIconVariants} = getDefaultValues();
const found = Object.keys(themeIconVariants).find(variant => theme.includes(variant));
return found ? found.toLowerCase() : null;
}
/**
* Gets package JSON
*/
export function getPackageJSON(): IPackageJSON {
return require(path.join(PATHS.VSIX_DIR, './package.json'));
}
/**
* Writes a file inside the vsix directory
*/
export function writeFile(filename: string, filecontent: string): void {
const filePath = path.join(PATHS.VSIX_DIR, filename);
fs.writeFileSync(filePath, filecontent, {encoding: CHARSET});
}