vsc-material-theme/extensions/helpers/fs.ts

78 lines
2.2 KiB
TypeScript
Raw Normal View History

import * as fs from 'fs';
import * as path from 'path';
2018-04-20 20:07:36 +02:00
import {IPackageJSON, IPackageJSONThemeIcons} from './../interfaces/ipackage.json';
2018-04-20 20:07:36 +02:00
import {CHARSET} from './../consts/files';
import {IDefaults} from '../interfaces/idefaults';
import {IThemeIcons} from '../interfaces/itheme-icons';
import {PATHS} from '../consts/paths';
2018-04-20 20:07:36 +02:00
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'));
2018-04-20 20:07:36 +02:00
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);
2018-04-20 20:07:36 +02:00
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);
2018-04-20 20:07:36 +02:00
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;
}
2018-04-20 20:07:36 +02:00
/**
* Gets package JSON
*/
export function getPackageJSON(): IPackageJSON {
return require(path.join(PATHS.VSIX_DIR, './package.json'));
2018-04-20 20:07:36 +02:00
}
/**
* 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});
}