package command: first steps

This commit is contained in:
Joao Moreno 2015-09-17 23:30:16 +02:00
parent 9fa7f619ff
commit 34d0a91383
3 changed files with 121 additions and 17 deletions

48
src/manifest.ts Normal file
View file

@ -0,0 +1,48 @@
export interface Person {
name: string;
url?: string;
email?: string;
}
export interface Manifest {
// mandatory (npm)
name: string;
version: string;
engines: { vscode: string; [name: string]: string; };
// mandatory (vscode)
publisher: string;
// optional (npm)
author?: string | Person;
description?: string;
keywords?: string[];
homepage?: string;
bugs?: string | { url?: string; email?: string };
license?: string;
contributors?: string | Person[];
main?: string;
repository?: string | { type: string; url: string; };
scripts?: { [name: string]: string; };
dependencies?: { [name: string]: string; };
devDependencies?: { [name: string]: string; };
private?: boolean;
// not supported (npm)
// files?: string[];
// bin
// man
// directories
// config
// peerDependencies
// bundledDependencies
// optionalDependencies
// os?: string[];
// cpu?: string[];
// preferGlobal
// publishConfig
}
export interface VsixManifest {
}

View file

@ -1,36 +1,88 @@
import * as fs from 'fs'; import { existsSync, readFileSync, createWriteStream } from 'fs';
import { dirname, join, resolve } from 'path'; import { dirname, join, resolve } from 'path';
import * as _ from 'lodash'; import * as _ from 'lodash';
import * as yazl from 'yazl'; import * as yazl from 'yazl';
import { Manifest, VsixManifest } from './manifest';
import { fatal } from './util';
export = function (path?: string) {
const resourcesPath = join(dirname(__dirname), 'resources'); const resourcesPath = join(dirname(__dirname), 'resources');
const manifestTemplatePath = join(resourcesPath, 'extension.vsixmanifest'); const vsixManifestTemplatePath = join(resourcesPath, 'extension.vsixmanifest');
const manifestTemplateStr = fs.readFileSync(manifestTemplatePath, 'utf8'); const vsixManifestTemplateStr = readFileSync(vsixManifestTemplatePath, 'utf8');
const manifestTemplate = _.template(manifestTemplateStr); const vsixManifestTemplate = _.template(vsixManifestTemplateStr);
const manifest = { function validate(manifest: any): string {
id: 'uuid', if (!manifest.name) {
displayName: 'UUID', return 'Manifest missing field: name';
version: '0.2.0', }
publisher: 'joaomoreno2',
description: 'This is a UUID extension', if (!manifest.version) {
tags: 'VSCode' return 'Manifest missing field: version';
}
if (!manifest.publisher) {
return 'Manifest missing field: publisher';
}
if (!manifest.engines) {
return 'Manifest missing field: engines';
}
if (!manifest.engines.vscode) {
return 'Manifest missing field: engines.vscode';
}
return null;
}
function toVsixManifest(manifest: Manifest): VsixManifest {
return {
id: manifest.name,
displayName: manifest.name,
version: manifest.version,
publisher: manifest.publisher,
description: manifest.description || '',
tags: (manifest.keywords || []).concat('vscode').join(';')
}; };
}
const manifestStr = manifestTemplate(manifest); export = function (path?: string): void {
const manifestPath = join(process.cwd(), 'package.json');
let manifestStr: string;
try {
manifestStr = readFileSync(manifestPath, 'utf8');
} catch (e) {
return fatal(`Extension manifest not found: ${ manifestPath }`);
}
let manifest: any;
try {
manifest = JSON.parse(manifestStr);
} catch (e) {
return fatal(`Error parsing JSON: ${ manifestPath }`);
}
const validation = validate(manifest);
if (validation) {
return fatal(validation);
}
const vsixManifest = toVsixManifest(manifest);
const vsixManifestStr = vsixManifestTemplate(vsixManifest);
const zip = new yazl.ZipFile(); const zip = new yazl.ZipFile();
zip.addBuffer(new Buffer(manifestStr, 'utf8'), 'extension.vsixmanifest'); zip.addBuffer(new Buffer(vsixManifestStr, 'utf8'), 'extension.vsixmanifest');
zip.addFile(join(resourcesPath, '[Content_Types].xml'), '[Content_Types].xml'); zip.addFile(join(resourcesPath, '[Content_Types].xml'), '[Content_Types].xml');
zip.addBuffer(new Buffer('hello world', 'utf8'), 'hello.txt'); zip.addBuffer(new Buffer('hello world', 'utf8'), 'hello.txt');
zip.end(); zip.end();
if (!path) { if (!path) {
path = join(process.cwd(), `${ manifest.id }-${ manifest.version }.vsix`); path = join(process.cwd(), `${ manifest.name }-${ manifest.version }.vsix`);
} }
const zipStream = fs.createWriteStream(path); const zipStream = createWriteStream(path);
zip.outputStream.pipe(zipStream); zip.outputStream.pipe(zipStream);
console.log(`Package created: ${ resolve(path) }`); console.log(`Package created: ${ resolve(path) }`);

4
src/util.ts Normal file
View file

@ -0,0 +1,4 @@
export function fatal(message: string, ...args: any[]) {
console.error(message, ...args);
process.exit(1);
}