collect files during packaging

This commit is contained in:
Joao Moreno 2015-09-18 08:53:57 +02:00
parent 06e5703e7d
commit 7af4f47d9c

View file

@ -1,17 +1,18 @@
import { readFile, createWriteStream } from 'fs';
import * as fs from 'fs';
import * as path from 'path';
import * as _ from 'lodash';
import * as yazl from 'yazl';
import { Manifest } from './manifest';
import { nfcall, Promise, reject, resolve } from 'q';
import { nfcall, Promise, reject, resolve, all } from 'q';
import * as glob from 'glob';
const resourcesPath = path.join(path.dirname(__dirname), 'resources');
const vsixManifestTemplatePath = path.join(resourcesPath, 'extension.vsixmanifest');
function readManifest(root: string): Promise<Manifest> {
const manifestPath = path.join(root, 'package.json');
function readManifest(cwd: string): Promise<Manifest> {
const manifestPath = path.join(cwd, 'package.json');
return nfcall<string>(readFile, manifestPath, 'utf8')
return nfcall<string>(fs.readFile, manifestPath, 'utf8')
.catch(() => reject<string>(`Extension manifest not found: ${ manifestPath }`))
.then<Manifest>(manifestStr => {
try {
@ -47,7 +48,7 @@ function validateManifest(manifest: Manifest): Promise<Manifest> {
}
function toVsixManifest(manifest: Manifest): Promise<string> {
return nfcall<string>(readFile, vsixManifestTemplatePath, 'utf8')
return nfcall<string>(fs.readFile, vsixManifestTemplatePath, 'utf8')
.then(vsixManifestTemplateStr => _.template(vsixManifestTemplateStr))
.then(vsixManifestTemplate => vsixManifestTemplate({
id: manifest.name,
@ -59,33 +60,39 @@ function toVsixManifest(manifest: Manifest): Promise<string> {
}));
}
function writeVsix(packagePath: string, vsixManifest: string): Promise<void> {
return Promise<void>((c, e) => {
function collectFiles(cwd: string): Promise<string[]> {
return nfcall<string[]>(glob, '**', { cwd, nodir: true });
}
function writeVsix(cwd: string, manifest: Manifest, packagePath: string): Promise<string> {
packagePath = packagePath || defaultPackagePath(cwd, manifest);
return nfcall(fs.unlink, packagePath)
.catch(err => err.code !== 'ENOENT' ? reject(err) : resolve(null))
.then(() => {
return all<any>([toVsixManifest(manifest), collectFiles(cwd)])
.spread((vsixManifest: string, files: string[]) => Promise<string>((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');
files.forEach(file => zip.addFile(path.join(cwd, file), 'extension/' + file));
zip.end();
const zipStream = createWriteStream(packagePath);
const zipStream = fs.createWriteStream(packagePath);
zip.outputStream.pipe(zipStream);
zip.outputStream.once('error', e);
zip.outputStream.once('end', c);
zip.outputStream.once('end', () => c(packagePath));
}));
});
}
function defaultPackagePath(root: string, manifest: Manifest) {
return path.join(root, `${ manifest.name }-${ manifest.version }.vsix`);
function defaultPackagePath(cwd: string, manifest: Manifest): string {
return path.join(cwd, `${ manifest.name }-${ manifest.version }.vsix`);
}
export = function (packagePath?: string, root = process.cwd()): Promise<any> {
return readManifest(root)
export = function (packagePath?: string, cwd = process.cwd()): Promise<any> {
return readManifest(cwd)
.then(validateManifest)
.then(manifest => {
packagePath = packagePath || defaultPackagePath(root, manifest);
return toVsixManifest(manifest)
.then(vsixManifest => writeVsix(packagePath, vsixManifest))
.then(() => console.log(`Package created: ${ path.resolve(packagePath) }`));
});
.then(manifest => writeVsix(cwd, manifest, packagePath))
.then(packagePath => console.log(`Package created: ${ packagePath }`));
};