Skip to content

Commit a679fd9

Browse files
committed
Support an extensionDataDirectory option
1 parent 51714b4 commit a679fd9

File tree

3 files changed

+52
-25
lines changed

3 files changed

+52
-25
lines changed

src/downloadExtension.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ async function mergeCache(cache, key, value) {
3535
/**
3636
* @param {import('.').ExtensionDemand} extensionDemand
3737
* @param {*} cache
38+
* @param {string} extensionDir
3839
*/
39-
async function syncExtensionData({ identifier }, cache) {
40-
const packageJsonPath = path.join(getExtensionPath(identifier), 'package.json');
40+
async function syncExtensionData({ identifier }, cache, extensionDir) {
41+
const packageJsonPath = path.join(getExtensionPath(identifier, extensionDir), 'package.json');
4142
const { grammars, themes } = await processExtension(packageJsonPath);
4243
Object.keys(grammars).forEach(scopeName => (grammars[scopeName].languageId = languageId++));
4344
await mergeCache(cache, 'grammars', grammars);
@@ -47,8 +48,9 @@ async function syncExtensionData({ identifier }, cache) {
4748
/**
4849
* @param {import('.').ExtensionDemand} extensionDemand
4950
* @param {*} cache
51+
* @param {string} extensionDir
5052
*/
51-
async function downloadExtension(extensionDemand, cache) {
53+
async function downloadExtension(extensionDemand, cache, extensionDir) {
5254
const { identifier, version } = extensionDemand;
5355
const { publisher, name } = parseExtensionIdentifier(identifier);
5456
const url = `https://marketplace.visualstudio.com/_apis/public/gallery/publishers/${publisher}/vsextensions/${name}/${version}/vspackage`;
@@ -73,37 +75,43 @@ async function downloadExtension(extensionDemand, cache) {
7375
});
7476
});
7577

76-
const extensionPath = getExtensionBasePath(identifier);
78+
const extensionPath = getExtensionBasePath(identifier, extensionDir);
7779
await decompress(archive, extensionPath);
78-
await syncExtensionData(extensionDemand, cache);
80+
await syncExtensionData(extensionDemand, cache, extensionDir);
7981
return extensionPath;
8082
}
8183

8284
/**
83-
* @param {'grammar' | 'theme'} type
84-
* @param {string} name
85-
* @param {import('.').ExtensionDemand[]} extensions
86-
* @param {*} cache
87-
* @param {Record<string, string>} languageAliases
85+
* @typedef {object} DownloadExtensionOptions
86+
* @property {'grammar' | 'theme'} type
87+
* @property {string} name
88+
* @property {import('.').ExtensionDemand[]} extensions
89+
* @property {*} cache
90+
* @property {Record<string, string>} languageAliases
91+
* @property {string} extensionDir
92+
*/
93+
94+
/**
95+
* @param {DownloadExtensionOptions} options
8896
*/
89-
async function downloadExtensionIfNeeded(type, name, extensions, cache, languageAliases) {
97+
async function downloadExtensionIfNeeded({ type, name, extensions, cache, languageAliases, extensionDir }) {
9098
extensions = extensions.slice();
9199
const extensionExists = type === 'grammar' ? grammarExists : themeExists;
92100
while (extensions.length && !(await extensionExists(name))) {
93101
const extensionDemand = extensions.shift();
94102
const { identifier, version } = extensionDemand;
95-
const extensionPath = getExtensionBasePath(identifier);
103+
const extensionPath = getExtensionBasePath(identifier, extensionDir);
96104
if (!fs.existsSync(extensionPath)) {
97-
await downloadExtension(extensionDemand, cache);
105+
await downloadExtension(extensionDemand, cache, extensionDir);
98106
continue;
99107
}
100-
const packageJson = getExtensionPackageJson(identifier);
108+
const packageJson = getExtensionPackageJson(identifier, extensionDir);
101109
if (packageJson.version !== version) {
102-
await downloadExtension(extensionDemand, cache);
110+
await downloadExtension(extensionDemand, cache, extensionDir);
103111
continue;
104112
}
105113

106-
await syncExtensionData(extensionDemand, cache);
114+
await syncExtensionData(extensionDemand, cache, extensionDir);
107115
}
108116

109117
/** @param {string} languageName */

src/index.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ function getStylesFromSettings(settings) {
119119
* @property {(line: LineData) => string=} getLineClassName
120120
* @property {boolean=} injectStyles
121121
* @property {(colorValue: string, theme: string) => string=} replaceColor
122+
* @property {string=} extensionDataDirectory
122123
*/
123124

124125
function createPlugin() {
@@ -138,7 +139,8 @@ function createPlugin() {
138139
extensions = [],
139140
getLineClassName = () => '',
140141
injectStyles = true,
141-
replaceColor = x => x
142+
replaceColor = x => x,
143+
extensionDataDirectory = path.resolve(__dirname, '../lib/extensions')
142144
} = {}
143145
) {
144146
/** @type {Record<string, string>} */
@@ -153,7 +155,14 @@ function createPlugin() {
153155
const text = node.value || (node.children && node.children[0] && node.children[0].value);
154156
if (!text) continue;
155157
const { languageName, options } = parseCodeFenceHeader(node.lang ? node.lang.toLowerCase() : '');
156-
await downloadExtensionIfNeeded('grammar', languageName, extensions, cache, languageAliases);
158+
await downloadExtensionIfNeeded({
159+
type: 'grammar',
160+
name: languageName,
161+
extensions,
162+
cache,
163+
languageAliases,
164+
extensionDir: extensionDataDirectory
165+
});
157166

158167
const grammarCache = await cache.get('grammars');
159168
const scope = getScope(languageName, grammarCache, languageAliases);
@@ -180,7 +189,14 @@ function createPlugin() {
180189
for (const setting in colorThemeSettings) {
181190
const colorThemeIdentifier = colorThemeSettings[setting];
182191
if (!colorThemeIdentifier) continue;
183-
await downloadExtensionIfNeeded('theme', colorThemeIdentifier, extensions, cache, languageAliases);
192+
await downloadExtensionIfNeeded({
193+
type: 'theme',
194+
name: colorThemeIdentifier,
195+
extensions,
196+
cache,
197+
languageAliases,
198+
extensionDir: extensionDataDirectory
199+
});
184200

185201
const themeClassName = themeClassNames[setting];
186202
const themeCache = await cache.get('themes');

src/utils.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,29 @@ function parseExtensionIdentifier(identifier) {
2222
/**
2323
* Gets the absolute path to the download path of a downloaded extension.
2424
* @param {string} identifier
25+
* @param {string} extensionDir
2526
*/
26-
function getExtensionBasePath(identifier) {
27-
return path.resolve(__dirname, '../lib/extensions', identifier);
27+
function getExtensionBasePath(identifier, extensionDir) {
28+
return path.join(extensionDir, identifier);
2829
}
2930

3031
/**
3132
* Gets the absolute path to the data directory of a downloaded extension.
3233
* @param {string} identifier
34+
* @param {string} extensionDir
3335
*/
34-
function getExtensionPath(identifier) {
35-
return path.resolve(getExtensionBasePath(identifier), 'extension');
36+
function getExtensionPath(identifier, extensionDir) {
37+
return path.join(getExtensionBasePath(identifier, extensionDir), 'extension');
3638
}
3739

3840
/**
3941
* Gets the package.json of an extension as a JavaScript object.
4042
* @param {string} identifier
43+
* @param {string} extensionDir
4144
* @returns {object}
4245
*/
43-
function getExtensionPackageJson(identifier) {
44-
return require(path.join(getExtensionPath(identifier), 'package.json'));
46+
function getExtensionPackageJson(identifier, extensionDir) {
47+
return require(path.join(getExtensionPath(identifier, extensionDir), 'package.json'));
4548
}
4649

4750
/**

0 commit comments

Comments
 (0)