UNPKG

code-theme-converter

Version:

Convert any vscode theme with ease!

93 lines (92 loc) 5.45 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const chalk_1 = __importDefault(require("chalk")); const rimraf_1 = __importDefault(require("rimraf")); const fetchRepo_1 = require("../fetchRepo"); const colorScheme_1 = require("./colorScheme"); const vscode_1 = require("../util/vscode"); const editorTheme_1 = require("./editorTheme"); const plugin_1 = require("./plugin"); function convertToIdea(vscodeThemeRepoUrl, options) { var _a; return __awaiter(this, void 0, void 0, function* () { try { console.log(chalk_1.default.cyan('Fetching repo')); const { repo } = yield fetchRepo_1.fetchRepo(vscodeThemeRepoUrl); const themeDir = path_1.default.join(repo, (_a = options.directory) !== null && _a !== void 0 ? _a : ''); const themes = yield fs_extra_1.default.readdir(themeDir); const themePackage = yield vscode_1.readCodeThemePackage(repo); const themesCount = themes.length; console.log(chalk_1.default.cyan(`Converting ${themesCount.toString()} theme${themesCount > 1 ? 's' : ''}`)); const promises = []; themes.forEach((themeName) => { vscode_1.readCodeTheme(themeDir, themeName) .then(vscodeTheme => { console.log(chalk_1.default.yellow(`Converting ${vscodeTheme.name}`)); const ideaThemeDir = path_1.default.join(process.cwd(), vscodeTheme.name); if (fs_extra_1.default.existsSync(ideaThemeDir)) { rimraf_1.default.sync(ideaThemeDir); } fs_extra_1.default.mkdir(ideaThemeDir) .then(() => __awaiter(this, void 0, void 0, function* () { var _a, _b; const colorScheme = colorScheme_1.convertToIdea(vscodeTheme, themePackage.version); const themeName = vscodeTheme.name .replace(/\s+/g, '') .toLowerCase(); const resourcesDir = path_1.default.join(ideaThemeDir + '/resources'); const metaInfDir = path_1.default.join(resourcesDir + '/META-INF'); const themesDir = path_1.default.join(resourcesDir + '/themes'); const srcDir = path_1.default.join(ideaThemeDir + '/src'); yield fs_extra_1.default.mkdir(resourcesDir); yield fs_extra_1.default.mkdir(metaInfDir); yield fs_extra_1.default.mkdir(themesDir); yield fs_extra_1.default.mkdir(srcDir); yield fs_extra_1.default.writeFile(path_1.default.join(srcDir, themeName + '.theme.json'), JSON.stringify(editorTheme_1.createEditorTheme(vscodeTheme, themeName), null, 2)); yield fs_extra_1.default .writeFile(path_1.default.join(metaInfDir, 'plugin.xml'), plugin_1.createPluginXml({ plugin: { id: (_a = themePackage.__metadata.id) !== null && _a !== void 0 ? _a : '', name: themePackage.displayName, description: themePackage.description, version: themePackage.version, changelog: '' }, theme: { name: vscodeTheme.name }, vendor: { name: (_b = vscodeTheme.name) !== null && _b !== void 0 ? _b : themePackage.author, email: '', url: '' } })) .catch(error => console.trace(chalk_1.default.red(error))); yield fs_extra_1.default.writeFile(path_1.default.join(themesDir, themeName + '.xml'), colorScheme); })) .catch(error => console.trace(chalk_1.default.red(error))); }) .catch(error => console.trace(chalk_1.default.red(error))); }); return Promise.all(promises); } catch (error) { console.trace(chalk_1.default.red(error)); } }); } exports.convertToIdea = convertToIdea;