#!/usr/bin/env node
'use strict';
// Builds the extension's .vsix without any packaging tool. A .vsix is a zip that holds
// `[Content_Types].xml`, `extension.vsixmanifest` and the extension itself under `extension/`.
// The version comes from the repository's VERSION file and is written into the packaged
// manifest; nothing in the source tree repeats it.
//
// node vscode/tools/vsix.js [output directory] (default: dist/)
const fs = require('node:fs');
const path = require('node:path');
const { zip } = require('./zip');
const ROOT = path.resolve(__dirname, '..');
const REPO = path.resolve(ROOT, '..');
// What ships: the code, the view's files and the read-me. Tests and tools stay behind.
const SHIPPED = ['extension.js', 'README.md', 'src', 'media'];
const SEMVER = /^\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?$/;
function xml(text) {
return String(text).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"');
}
function walk(dir, base, out) {
for (const entry of fs.readdirSync(dir, { withFileTypes: true }).sort((a, b) => (a.name < b.name ? -1 : 1))) {
const full = path.join(dir, entry.name);
const rel = base ? base + '/' + entry.name : entry.name;
if (entry.isDirectory()) walk(full, rel, out);
else if (entry.isFile()) out.push({ name: rel, data: fs.readFileSync(full) });
}
return out;
}
function manifestXml(pkg) {
const kinds = Array.isArray(pkg.extensionKind) ? pkg.extensionKind.join(',') : 'workspace';
return `
${xml(pkg.displayName || pkg.name)}
${xml(pkg.description || '')}
${xml((pkg.keywords || []).join(','))}
${xml((pkg.categories || []).join(','))}
Public
extension/LICENSE.txt${pkg.icon ? `\n extension/${xml(pkg.icon)}` : ''}
${pkg.icon ? ` \n` : ''}
`;
}
const TYPES = {
'.json': 'application/json',
'.vsixmanifest': 'text/xml',
'.js': 'application/javascript',
'.css': 'text/css',
'.md': 'text/markdown',
'.txt': 'text/plain',
'.svg': 'image/svg+xml',
'.png': 'image/png',
};
function contentTypesXml(names) {
const used = new Set(['.vsixmanifest']);
for (const n of names) used.add(path.extname(n).toLowerCase());
const lines = [];
for (const ext of [...used].sort()) {
if (!TYPES[ext]) throw new Error(`a file of a kind the package does not know: ${ext || '(no ending)'}`);
lines.push(``);
}
return `\n${lines.join('')}\n`;
}
/** The files of the archive, in order, and the manifest they were made from. */
function build(options) {
const root = (options && options.root) || ROOT;
const repo = (options && options.repo) || REPO;
const version = fs.readFileSync(path.join(repo, 'VERSION'), 'utf8').trim();
if (!SEMVER.test(version)) throw new Error(`VERSION is not a version: ${version}`);
const pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8'));
pkg.version = version;
const files = [{ name: 'package.json', data: Buffer.from(JSON.stringify(pkg, null, 2) + '\n') }];
for (const item of SHIPPED) {
const full = path.join(root, item);
if (fs.statSync(full).isDirectory()) walk(full, item, files);
else files.push({ name: item, data: fs.readFileSync(full) });
}
files.push({ name: 'LICENSE.txt', data: fs.readFileSync(path.join(repo, 'LICENSE')) });
// Everything the manifest points at has to be in the package.
const have = new Set(files.map((f) => f.name));
const main = String(pkg.main || '').replace(/^\.\//, '');
for (const need of [main, 'README.md', 'LICENSE.txt']) if (!have.has(need)) throw new Error('missing from the package: ' + need);
const entries = [
{ name: 'extension.vsixmanifest', data: Buffer.from(manifestXml(pkg)) },
{ name: '[Content_Types].xml', data: Buffer.from(contentTypesXml(files.map((f) => f.name))) },
...files.map((f) => ({ name: 'extension/' + f.name, data: f.data })),
];
return { pkg, version, entries, fileName: `${pkg.name}-${version}.vsix` };
}
function main() {
const outDir = path.resolve(process.argv[2] || path.join(REPO, 'dist'));
const built = build();
fs.mkdirSync(outDir, { recursive: true });
const out = path.join(outDir, built.fileName);
const archive = zip(built.entries);
fs.writeFileSync(out, archive);
process.stdout.write(`${out}\n${built.entries.length} files, ${archive.length} bytes, version ${built.version}\n`);
}
if (require.main === module) {
try {
main();
} catch (e) {
process.stderr.write(`package-vscode: ${e.message}\n`);
process.exit(1);
}
}
module.exports = { build, manifestXml, contentTypesXml };