package cleanup

This commit is contained in:
Joao Moreno 2015-09-18 08:19:10 +02:00
parent c540fb7278
commit 06e5703e7d
3 changed files with 77 additions and 76 deletions

View file

@ -41,8 +41,4 @@ export interface Manifest {
// cpu?: string[];
// preferGlobal
// publishConfig
}
export interface VsixManifest {
}

View file

@ -1,86 +1,91 @@
import { readFile, createWriteStream } from 'fs';
import { dirname, join, resolve } from 'path';
import * as path from 'path';
import * as _ from 'lodash';
import * as yazl from 'yazl';
import { Manifest, VsixManifest } from './manifest';
import { nfcall, Promise, reject } from 'q';
import { Manifest } from './manifest';
import { nfcall, Promise, reject, resolve } from 'q';
function validate(manifest: Manifest): string {
if (!manifest.name) {
return 'Manifest missing field: name';
}
if (!manifest.version) {
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;
}
const resourcesPath = path.join(path.dirname(__dirname), 'resources');
const vsixManifestTemplatePath = path.join(resourcesPath, 'extension.vsixmanifest');
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(';')
};
}
export = function (path?: string): Promise<any> {
const manifestPath = join(process.cwd(), 'package.json');
function readManifest(root: string): Promise<Manifest> {
const manifestPath = path.join(root, 'package.json');
return nfcall<string>(readFile, manifestPath, 'utf8')
.catch(() => reject<string>(`Extension manifest not found: ${ manifestPath }`))
.then<Manifest>(manifestStr => {
try {
return JSON.parse(manifestStr);
return resolve(JSON.parse(manifestStr));
} catch (e) {
return reject<Manifest>(`Error parsing manifest file: not a valid JSON file.`);
}
})
.then(manifest => {
const validation = validate(manifest);
if (validation) {
return reject<void>(validation);
return reject(`Error parsing manifest file: not a valid JSON file.`);
}
});
}
const resourcesPath = join(dirname(__dirname), 'resources');
const vsixManifestTemplatePath = join(resourcesPath, 'extension.vsixmanifest');
function validateManifest(manifest: Manifest): Promise<Manifest> {
if (!manifest.name) {
return reject<Manifest>('Manifest missing field: name');
}
if (!manifest.version) {
return reject<Manifest>('Manifest missing field: version');
}
if (!manifest.publisher) {
return reject<Manifest>('Manifest missing field: publisher');
}
if (!manifest.engines) {
return reject<Manifest>('Manifest missing field: engines');
}
if (!manifest.engines.vscode) {
return reject<Manifest>('Manifest missing field: engines.vscode');
}
return resolve(manifest);
}
function toVsixManifest(manifest: Manifest): Promise<string> {
return nfcall<string>(readFile, vsixManifestTemplatePath, 'utf8')
.then(vsixManifestTemplateStr => _.template(vsixManifestTemplateStr))
.then(vsixManifestTemplate => vsixManifestTemplate({
id: manifest.name,
displayName: manifest.name,
version: manifest.version,
publisher: manifest.publisher,
description: manifest.description || '',
tags: (manifest.keywords || []).concat('vscode').join(';')
}));
}
function writeVsix(packagePath: string, vsixManifest: string): Promise<void> {
return Promise<void>((c, e) => {
const zip = new yazl.ZipFile();
zip.addBuffer(new Buffer(vsixManifest, 'utf8'), 'extension.vsixmanifest');
zip.addFile(path.join(resourcesPath, '[Content_Types].xml'), '[Content_Types].xml');
zip.addBuffer(new Buffer('hello world', 'utf8'), 'hello.txt');
zip.end();
const zipStream = createWriteStream(packagePath);
zip.outputStream.pipe(zipStream);
zip.outputStream.once('error', e);
zip.outputStream.once('end', c);
});
}
function defaultPackagePath(root: string, manifest: Manifest) {
return path.join(root, `${ manifest.name }-${ manifest.version }.vsix`);
}
export = function (packagePath?: string, root = process.cwd()): Promise<any> {
return readManifest(root)
.then(validateManifest)
.then(manifest => {
packagePath = packagePath || defaultPackagePath(root, manifest);
return nfcall<string>(readFile, vsixManifestTemplatePath, 'utf8')
.then(vsixManifestTemplateStr => _.template(vsixManifestTemplateStr))
.then(vsixManifestTemplate => vsixManifestTemplate(toVsixManifest(manifest)))
.then(vsixManifestStr => Promise<void>((c, e) => {
const zip = new yazl.ZipFile();
zip.addBuffer(new Buffer(vsixManifestStr, 'utf8'), 'extension.vsixmanifest');
zip.addFile(join(resourcesPath, '[Content_Types].xml'), '[Content_Types].xml');
zip.addBuffer(new Buffer('hello world', 'utf8'), 'hello.txt');
zip.end();
if (!path) {
path = join(process.cwd(), `${ manifest.name }-${ manifest.version }.vsix`);
}
const zipStream = createWriteStream(path);
zip.outputStream.pipe(zipStream);
zip.outputStream.once('error', e);
zip.outputStream.once('end', c);
}));
})
.then(() => console.log(`Package created: ${ resolve(path) }`));
return toVsixManifest(manifest)
.then(vsixManifest => writeVsix(packagePath, vsixManifest))
.then(() => console.log(`Package created: ${ path.resolve(packagePath) }`));
});
};

View file

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