Skip to content

Commit 8a0df30

Browse files
committed
Better error when theme isn’t found
1 parent 5dc1b2b commit 8a0df30

File tree

6 files changed

+26
-18
lines changed

6 files changed

+26
-18
lines changed

src/createGetRegistry.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// @ts-check
2-
const fs = require('fs');
32
const { getGrammarLocation, getGrammar, getAllGrammars } = require('./storeUtils');
4-
const { promisify } = require('util');
3+
const { readFile } = require('./utils');
54
const { Registry, parseRawGrammar } = require('vscode-textmate');
6-
const readFile = promisify(fs.readFile);
75

86
function createEmitter() {
97
/** @type {() => void} */

src/downloadExtension.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// @ts-check
22
const fs = require('fs');
33
const path = require('path');
4-
const zlib = require('zlib');
5-
const util = require('util');
64
const processExtension = require('./processExtension');
75
const { highestBuiltinLanguageId } = require('./storeUtils');
86
const {
@@ -11,7 +9,6 @@ const {
119
getExtensionBasePath,
1210
getExtensionPackageJson
1311
} = require('./utils');
14-
const gunzip = util.promisify(zlib.gunzip);
1512
let languageId = highestBuiltinLanguageId + 1;
1613

1714
/**

src/host.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const request = require('request');
22
const decompress = require('decompress');
3-
const zlib = require('zlib');
4-
const util = require('util');
5-
const gunzip = util.promisify(zlib.gunzip);
3+
const { gunzip } = require('./utils');
64

75
/**
86
* @typedef {object} Response

src/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const { sanitizeForClassName } = require('./utils');
1111
const { loadColorTheme } = require('../lib/vscode/colorThemeData');
1212
const { getClassNameFromMetadata } = require('../lib/vscode/modes');
1313
const { downloadExtensionIfNeeded: downloadExtensionsIfNeeded } = require('./downloadExtension');
14-
const { getGrammar, getScope, getThemeLocation } = require('./storeUtils');
14+
const { getGrammar, getScope, ensureThemeLocation } = require('./storeUtils');
1515
const { generateTokensCSSForColorMap } = require('../lib/vscode/tokenization');
1616
const {
1717
renderRule,
@@ -227,9 +227,7 @@ function createPlugin() {
227227

228228
const themeClassName = themeClassNames[setting];
229229
const themeCache = await cache.get('themes');
230-
const colorThemePath =
231-
getThemeLocation(colorThemeIdentifier, themeCache) ||
232-
path.resolve(markdownNode.fileAbsolutePath, colorThemeIdentifier);
230+
const colorThemePath = ensureThemeLocation(colorThemeIdentifier, themeCache, markdownNode.fileAbsolutePath);
233231

234232
const { resultRules: tokenColors, resultColors: settings } = loadColorTheme(colorThemePath);
235233
const defaultTokenColors = {

src/storeUtils.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @ts-check
22
const path = require('path');
3+
const { exists } = require('./utils');
34
// @ts-ignore
45
const grammarManifest = require('../lib/grammars/manifest.json');
56
// @ts-ignore
@@ -39,16 +40,25 @@ function getGrammarLocation(grammar) {
3940
/**
4041
*
4142
* @param {string} themeNameOrId
42-
* @param {*} themeCache
43+
* @param {object} themeCache
44+
* @param {string} markdownFilePath
4345
*/
44-
function getThemeLocation(themeNameOrId, themeCache) {
46+
async function ensureThemeLocation(themeNameOrId, themeCache, markdownFilePath) {
4547
const themes = { ...themeManifest, ...themeCache };
4648
for (const themeId in themes) {
4749
const theme = themes[themeId];
4850
if (themeNameOrId === themeId || themeNameOrId.toLowerCase() === theme.label.toLowerCase()) {
49-
return path.isAbsolute(theme.path) ? theme.path : path.resolve(__dirname, '../lib/themes', theme.path);
51+
const themePath = path.isAbsolute(theme.path) ? theme.path : path.resolve(__dirname, '../lib/themes', theme.path);
52+
if (!await exists(themePath)) {
53+
throw new Error(`Theme manifest lists '${themeNameOrId}' at '${themePath}, but no such file exists.'`);
54+
}
5055
}
5156
}
57+
58+
const locallyResolved = path.resolve(path.dirname(markdownFilePath), themeNameOrId);
59+
if (!await exists(locallyResolved)) {
60+
throw new Error(`Theme manifest does not contain theme '${themeNameOrId}', and no theme file exists at '${locallyResolved}'.`);
61+
}
5262
}
5363

5464
const highestBuiltinLanguageId = Object.keys(grammarManifest).reduce(
@@ -75,7 +85,7 @@ module.exports = {
7585
getScope,
7686
getGrammar,
7787
getGrammarLocation,
78-
getThemeLocation,
88+
ensureThemeLocation,
7989
highestBuiltinLanguageId,
8090
getAllGrammars
8191
};

src/utils.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
// @ts-check
22
const fs = require('fs');
33
const util = require('util');
4+
const zlib = require('zlib');
45
const path = require('path');
56
const JSON5 = require('json5');
67
const plist = require('plist');
78
const uniq = require('lodash.uniq');
89

10+
const readFile = util.promisify(fs.readFile);
11+
const exists = util.promisify(fs.exists);
12+
const gunzip = util.promisify(zlib.gunzip);
13+
914
/**
1015
* Splits a Visual Studio Marketplace extension identifier into publisher and extension name.
1116
* @param {string} identifier The unique identifier of a VS Code Marketplace extension in the format 'publisher.extension-name'.
@@ -70,12 +75,14 @@ function sanitizeForClassName(str) {
7075
.toLowerCase();
7176
}
7277

73-
const readFile = util.promisify(fs.readFile);
7478
const requireJson = /** @param {string} pathName */ pathName => JSON5.parse(fs.readFileSync(pathName, 'utf8'));
7579
const requirePlistOrJson = /** @param {string} pathName */ async pathName =>
7680
path.extname(pathName) === '.json' ? requireJson(pathName) : plist.parse(await readFile(pathName, 'utf8'));
7781

7882
module.exports = {
83+
readFile,
84+
exists,
85+
gunzip,
7986
parseExtensionIdentifier,
8087
getExtensionPath,
8188
getExtensionBasePath,

0 commit comments

Comments
 (0)