Skip to content

Commit 249376e

Browse files
authored
Merge pull request #508 from FabianHenneke/master
Ensure package.json is writable in *.vsix
2 parents f8869d7 + 9863503 commit 249376e

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/package.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ const MinimatchOptions: minimatch.IOptions = { dot: true };
3737

3838
export interface IInMemoryFile {
3939
path: string;
40+
mode?: number;
4041
readonly contents: Buffer | string;
4142
}
4243

4344
export interface ILocalFile {
4445
path: string;
46+
mode?: number;
4547
readonly localPath: string;
4648
}
4749

@@ -208,7 +210,7 @@ function isHostTrusted(url: url.UrlWithStringQuery): boolean {
208210
return TrustedSVGSources.indexOf(url.host.toLowerCase()) > -1 || isGitHubBadge(url.href);
209211
}
210212

211-
class ManifestProcessor extends BaseProcessor {
213+
export class ManifestProcessor extends BaseProcessor {
212214
constructor(manifest: Manifest) {
213215
super(manifest);
214216

@@ -274,6 +276,18 @@ class ManifestProcessor extends BaseProcessor {
274276
}
275277
}
276278

279+
async onFile(file: IFile): Promise<IFile> {
280+
const path = util.normalize(file.path);
281+
282+
if (!/^extension\/package.json$/i.test(path)) {
283+
return Promise.resolve(file);
284+
}
285+
286+
// Ensure that package.json is writable as VS Code needs to
287+
// store metadata in the extracted file.
288+
return { ...file, mode: 0o100644 };
289+
}
290+
277291
async onEnd(): Promise<void> {
278292
if (typeof this.manifest.extensionKind === 'string') {
279293
util.log.warn(
@@ -1104,8 +1118,10 @@ function writeVsix(files: IFile[], packagePath: string): Promise<void> {
11041118
const zip = new yazl.ZipFile();
11051119
files.forEach(f =>
11061120
isInMemoryFile(f)
1107-
? zip.addBuffer(typeof f.contents === 'string' ? Buffer.from(f.contents, 'utf8') : f.contents, f.path)
1108-
: zip.addFile(f.localPath, f.path)
1121+
? zip.addBuffer(typeof f.contents === 'string' ? Buffer.from(f.contents, 'utf8') : f.contents, f.path, {
1122+
mode: f.mode,
1123+
})
1124+
: zip.addFile(f.localPath, f.path, { mode: f.mode })
11091125
);
11101126
zip.end();
11111127

src/test/package.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
WebExtensionProcessor,
1414
IAsset,
1515
IPackageOptions,
16+
ManifestProcessor,
1617
} from '../package';
1718
import { Manifest } from '../manifest';
1819
import * as path from 'path';
@@ -1684,6 +1685,21 @@ describe('toContentTypes', () => {
16841685
});
16851686
});
16861687

1688+
describe('ManifestProcessor', () => {
1689+
it('should ensure that package.json is writable', async () => {
1690+
const root = fixture('uuid');
1691+
const manifest = JSON.parse(await readFile(path.join(root, 'package.json'), 'utf8'));
1692+
const processor = new ManifestProcessor(manifest);
1693+
const packageJson = {
1694+
path: 'extension/package.json',
1695+
localPath: path.join(root, 'package.json'),
1696+
};
1697+
1698+
const outPackageJson = await processor.onFile(packageJson);
1699+
assert.ok(outPackageJson.mode & 0o200);
1700+
});
1701+
});
1702+
16871703
describe('MarkdownProcessor', () => {
16881704
it('should throw when no baseContentUrl is provided', async () => {
16891705
const manifest = {

0 commit comments

Comments
 (0)